neural_network #4
@ -22,29 +22,28 @@ if __name__ == '__main__':
|
||||
nn.Linear(input_dim, output_dim),
|
||||
nn.LogSoftmax()
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
def train(model, n_iter):
|
||||
criterion = nn.NLLLoss()
|
||||
optimizer = optim.SGD(model.parameters(), lr=0.001)
|
||||
|
||||
|
||||
for epoch in range(n_iter):
|
||||
for image, label in zip(train_images, train_labels):
|
||||
print(image.shape)
|
||||
optimizer.zero_grad()
|
||||
|
||||
|
||||
output = model(image)
|
||||
loss = criterion(output.unsqueeze(0), label.unsqueeze(0))
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
|
||||
|
||||
print(f'epoch: {epoch:03}')
|
||||
|
||||
|
||||
train(model, 100)
|
||||
|
||||
# def accuracy(expected, predicted):
|
||||
# return len([1 for e, p in zip(expected, predicted) if e == p]) / len(expected)
|
||||
# return len([_ for e, p in zip(expected, predicted) if e == p]) / len(expected)
|
||||
#
|
||||
#
|
||||
# predicted = [model(image).argmax() for image in train_images]
|
||||
|
Loading…
Reference in New Issue
Block a user