Sample Code

import torch x = [[1.0],[1.0]] y = [[5.0],[2.0]] W = [[1.0,1.0],[1.0,2.0]] W2 = [[1.0,1.0],[1.0,2.0]] input = torch.tensor(x) weights = torch.tensor(W, requires_grad=True) weights2 = torch.tensor(W2, requires_grad=True) output = torch.tensor(y) learning_rate = 0.01 optimizer = torch.optim.SGD([weights, weights2], lr=learning_rate) # 3. Multi-step optimization loop epochs = 1000 for step in range(epochs): # Clear out old gradients from the previous step optimizer.zero_grad() # Forward pass: calculate prediction and loss result = weights @ input layer1_output = torch.sigmoid(result) layer2_output = weights2@layer1_output error1 = output - layer2_output loss = error1.T @ error1 #get total loss as a float numeric_loss = loss.item() # Backward pass: compute the gradients loss.backward() # Optimization step: update the weights using SGD math optimizer.step() print('final loss = '+str(numeric_loss)) print('final weights is '+str(weights)) print('final output is '+str(layer2_output))