Version of Python¶
In [1]:
!python -V
Python 3.12.6
Import Required Packages¶
In [2]:
# Suppress warnings
import warnings
for warn in [UserWarning, FutureWarning]: warnings.filterwarnings("ignore", category = warn)
import os
import numpy as np
import torch
import torch.nn as nn
import pandas as pd
import jupyterlab as jlab
Versions of Required Libraries¶
In [3]:
packages = [
"Torch", "NumPy", "Pandas", "JupyterLab",
]
package_objects = [
torch, np, pd, jlab
]
versions = list(map(lambda obj: obj.__version__, package_objects))
pkgs = {"Package": packages, "Version": versions}
df_pkgs = pd.DataFrame(data = pkgs)
df_pkgs.index.name = "#"
df_pkgs.index += 1
display(df_pkgs)
path_to_reqs = "."
reqs_name = "requirements.txt"
def get_packages_and_versions():
"""Generate strings with libraries and their versions in the format: package==version"""
for package, version in zip(packages, versions):
yield f"{package.lower()}=={version}\n"
with open(os.path.join(path_to_reqs, reqs_name), "w", encoding = "utf-8") as f:
f.writelines(get_packages_and_versions())
Package | Version | |
---|---|---|
# | ||
1 | Torch | 2.2.2 |
2 | NumPy | 1.26.4 |
3 | Pandas | 2.2.3 |
4 | JupyterLab | 4.2.5 |
Example of a Dense Layer¶
In [4]:
# Determine the size of the input and output layers
input_size = 10
output_size = 5
# Create a random tensor
input = torch.randn(2, input_size)
# Create a Dense layer (fully connected)
dense = nn.Linear(in_features = input_size, out_features = output_size, bias = True)
# Apply a Dense layer to the input data
output = dense(input)
# Output of the resulting tensor after passing through the Dense layer
print("Resultant tensor after passing through the Dense layer:")
print(output)
# Output the size of the resulting tensor
print("Size of the resulting tensor:")
print(output.size())
Resultant tensor after passing through the Dense layer: tensor([[ 0.0159, -0.3479, 0.3961, 0.3081, -0.5841], [-0.8313, 0.1779, -0.0857, 0.6841, -0.2625]], grad_fn=<AddmmBackward0>) Size of the resulting tensor: torch.Size([2, 5])