Pytorch Neural Newtwork Sample 1
Full Script
import torch
import torch.optim as op
import torch.nn as nn
network = nn.Sequential(
nn.Linear(2, 5),
nn.Tanh(),
nn.Linear(5,2)
)
X = torch.tensor([[1.0,1.0],[2.0,1.0]])
y = torch.tensor([[1.0,1.0],[3.0,2.0]])
test = network(X)
print(test)
#args = torch.tensor([1.0,1.0], requires_grad=True)
opt = op.SGD(network.parameters(), lr=0.001)
mse = nn.MSELoss()
def train(model, opt, loss, X,y, epochs=1):
for epoch in range(epochs):
y2 = model(X)
loss1 = loss(y2, y)
loss1.backward()
opt.step()
opt.zero_grad()
pass
pass
train(network, opt, mse, X, y, 1000)
test2 = network(X)
print(test2)
loss_tensor = mse(network(X),y)
#loss_value is the total loss
loss_value = loss_tensor.item()
x = torch.randn(1, 2)
# Dictionary to store the intermediate outputs
layer_outputs = {}
current_input = x
for i, layer in enumerate(network):
# Pass the data sequentially through each layer
current_input = layer(current_input)
# Save the output of the current layer
layer_outputs[f"layer_{i}_{layer.__class__.__name__}"] = current_input
# Print results
for layer_name, output in layer_outputs.items():
print(f"{layer_name} output shape: {output.shape}")
#splitting the network into two models
# 1. Split into two sub-models
# Model 1 contains layers up to the Tanh activation (indices 0 and 1)
model_part1 = network[:2]
# Model 2 contains the final linear layer (index 2 onwards)
model_part2 = network[2:]
# 2. Test the split models with a dummy input
x = torch.randn(1, 2)
# Pass through Part 1
hidden_features = model_part1(x)
print("Part 1 Output Shape:", hidden_features.shape) # Expected: [1, 5]
# Pass the intermediate result through Part 2
final_output = model_part2(hidden_features)
print("Part 2 Output Shape:", final_output.shape)
# Extract weights as a list of nested lists
linear_weights = []
for layer in network:
if isinstance(layer, nn.Linear):
# .detach() removes it from the computational graph
# .tolist() automatically converts the PyTorch tensor into standard Python lists
layer_weight_matrix = layer.weight.detach().tolist()
linear_weights.append(layer_weight_matrix)
# Print the final result
print(linear_weights)
pass