class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.linear_relu_stack = nn.Sequential(
# input of 28*28 and output of 512
nn.Linear(2,10),
nn.ReLU(),
nn.Linear(10,10),
nn.ReLU(),
# input of 512 and output of 10
nn.Linear(10,1)
)
def forward(self, x):
flatten = nn.Flatten()
x = flatten(x)
logits = self.linear_relu_stack(x)
return logits
loss_fn = nn.MSELoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.00001)
def train(dataloader, model, loss_fn, optimizer):
size = len(dataloader.dataset)
# model.train() Sets model into training mode i.e.
# BatchNorm layers use per-batch statistics
# Dropout layers are activated
model.train()
for batch, (x,y) in enumerate(dataloader):
# Compute prediction error
pred = model(x)
loss = loss_fn(pred, y)
# PyTorch's autograd simply accumulates the gradients for each model parameter.
# Thus for each epoch, one has to clear the existing gradients.
optimizer.zero_grad()
# compute gradient of loss w.r.t all parameters
loss.backward()
# backpropragation (i.e update weights and biases)
optimizer.step()
if batch % 100 == 0:
loss, current = loss.item(), batch * len(x)
print('Loss:', loss, '[', current, '/', size, ']')
def test(dataloader, model, loss_fn):
size = len(dataloader.dataset)
batch_size = len(dataloader)
# model.eval() Sets model into evaluation mode i.e.
# BatchNorm layers use running statistics
# Dropout layers are de-activated
model.eval()
test_loss, correct = 0,0
# operations within the block won't calculate a gradient
with torch.no_grad():
for x, y in dataloader:
pred = model(x)
test_loss += loss_fn(pred, y).item()
test_loss = test_loss / batch_size
print('\nTest Error:','Avg Loss:', test_loss, '\n')```
Epoch 1
Loss: 406.853515625 [ 0 / 3092 ]
Loss: 295.5381164550781 [ 1000 / 3092 ]
Loss: 307.9087219238281 [ 2000 / 3092 ]
Loss: 309.39630126953125 [ 3000 / 3092 ]
Test Error: Avg Loss: 297.10006644509053
Epoch 2
Loss: 406.853515625 [ 0 / 3092 ]
Loss: 295.5381164550781 [ 1000 / 3092 ]
Loss: 307.9087219238281 [ 2000 / 3092 ]
Loss: 309.39630126953125 [ 3000 / 3092 ]
Test Error: Avg Loss: 297.10006644509053
As stated, the error results for each epoch are the same. Did I implement something wrong? I’ve tried changing the learning rate and increasing the number of epochs.