PyTorch: A User-Friendly Deep Learning Framework

in

First, you need to import the library:

# Import the torch library
import torch

# Create a tensor with values 1, 2, 3
tensor = torch.tensor([1, 2, 3])

# Print the tensor
print(tensor)

# Output: tensor([1, 2, 3])

# Create a tensor with values 4, 5, 6
tensor2 = torch.tensor([4, 5, 6])

# Add the two tensors together
result = tensor + tensor2

# Print the result
print(result)

# Output: tensor([5, 7, 9])

# Get the size of the tensor
size = result.size()

# Print the size
print(size)

# Output: torch.Size([3])

# Get the data type of the tensor
dtype = result.dtype

# Print the data type
print(dtype)

# Output: torch.int64

Next, create some data for your model to learn from (let’s keep it simple with just two inputs and one output):

# Create input and output data for model to learn from
# x1 and y1 are for feature X1, x2 and y2 are for feature X2
x1 = [2, 4] # input values for feature X1
y1 = [3, 5] # corresponding output values for Y1
x2 = [6, 8] # input values for feature X2
y2 = [9, 11] # corresponding output values for Y2

Now you can create a model that takes in two inputs (X1 and X2) and outputs one value:

# Define the structure of the neural network using PyTorch's built-in functions
model = torch.nn.Sequential(
    # Input layer with 2 neurons (one for each feature)
    torch.nn.Linear(2, 1), # Linear layer with 2 input features and 1 output neuron
    # Output layer with a single neuron and ReLU activation function
    torch.nn.ReLU() # Rectified Linear Unit (ReLU) activation function to introduce non-linearity
)

To train the model using PyTorch’s built-in optimizer, you can use the following code:


loss = 0.0 # initialize loss variable to 0
total_correct = 0 # initialize total_correct variable to 0

# loop through each input-output pair in your data set
for i, j in zip(x1, y1): # loop through each input-output pair in the data set using zip function
    # feed the current input into the model to get a prediction for that output value
    pred = model(torch.tensor([i])) # pass the current input into the model and store the prediction in pred variable
    # calculate the loss (difference between predicted and actual values) using PyTorch's built-in function
    loss += torch.nn.MSELoss()(pred, torch.tensor([j])).item() # calculate the loss using PyTorch's built-in function and add it to the loss variable
    
    # check if our prediction was correct or not
    if pred.item() == j: # compare the predicted value with the actual value
        total_correct += 1 # if they are equal, increase the total_correct variable by 1
        
# calculate the average loss and accuracy over all input-output pairs in your data set
avg_loss = loss / len(x1) # calculate the average loss by dividing the total loss by the number of input-output pairs
accuracy = (total_correct / len(y1)) * 100.0 # calculate the accuracy by dividing the total number of correct predictions by the number of input-output pairs and multiplying by 100 for percentage

And that’s it! You now have a basic understanding of how PyTorch works and can start experimenting with more complex models and data sets.

SICORPS