37 lines
876 B
Python
37 lines
876 B
Python
import torch
|
|
import torch.nn as nn
|
|
import torch.optim as optim
|
|
|
|
# Synthetic input and target data
|
|
inputs = torch.randn(100, 10) # 100 samples, 10 features each
|
|
targets = torch.randn(100, 1) # 100 target values
|
|
|
|
class TinyModel(nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.layer1 = nn.Linear(10, 5)
|
|
self.relu = nn.ReLU()
|
|
self.layer2 = nn.Linear(5, 1)
|
|
|
|
def forward(self, x):
|
|
x = self.layer1(x)
|
|
x = self.relu(x)
|
|
x = self.layer2(x)
|
|
return x
|
|
|
|
model = TinyModel()
|
|
loss_fn = nn.MSELoss()
|
|
optimizer = optim.SGD(model.parameters(), lr=0.01)
|
|
|
|
for epoch in range(200):
|
|
outputs = model(inputs)
|
|
loss = loss_fn(outputs, targets)
|
|
|
|
optimizer.zero_grad()
|
|
loss.backward()
|
|
optimizer.step()
|
|
|
|
if epoch % 20 == 0:
|
|
print(f"Epoch {epoch}: Loss = {loss.item():.4f}")
|
|
|