PyTorch - 计算和打印快速入门教程的训练准确性
PyTorch - Computing and printing the training accuracy of the QuickStart Tutorial
学习 PyTorch,我是从 Quickstart Tutorial 开始的。在 train() 方法中,我注意到 they don't print the training accuracy during the training session
。仅打印训练损失。
来自 Keras,这对我来说很不寻常,因为当您调用 fit() 时会自动打印训练准确率。
所以,我决定像下面这样修改教程代码来打印训练精度:
def train(dataloader, model, optimizer, loss_fn):
model.train()
size = len(dataloader.dataset)
num_batches = len(dataloader)
training_loss = 0.0
correct = 0.0
for batch, (imgs, labels) in enumerate(dataloader):
imgs = imgs.to(device=device)
labels = labels.to(device=device)
predictions = model(imgs)
loss = loss_fn(predictions, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# accumulate the training loss - each batch's loss will be added to trainin_loss
training_loss += loss.item()
# determines the number of correct predictions
correct += (predictions.argmax(1) == labels).type(torch.float).sum().item()
# end of for loop - all batches are processed
# after all batches are processed, determine the average training loss
training_loss = training_loss / num_batches
# this would be the training accuracy: number of correct predictions / number of samples in dataset
correct = correct / size
print(f"{datetime.datetime.now()} Training Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {training_loss:>8f} \n")
这样可以吗?作为 PyTorch 的初学者,我想在开始训练我的神经网络之前确保它是正确的。
看起来一切正常。这样做应该不会影响训练。 loss.backward()
计算你的梯度,任何不连接的东西都不能改变它们。顺便说一下,只是 运行 培训,你不能破坏任何东西:)(但是。等你开始制造自动驾驶汽车。)。
我想,在 Keras/TensorFlow fit()
中不会自动计算 accuracy,您仍然必须指定此指标,例如当 编译模型或作为fit()
的参数,例如:
model.compile(optimizer='sgd',
loss='mse',
metrics=[tf.keras.metrics.Accuracy()])
学习 PyTorch,我是从 Quickstart Tutorial 开始的。在 train() 方法中,我注意到 they don't print the training accuracy during the training session
。仅打印训练损失。
来自 Keras,这对我来说很不寻常,因为当您调用 fit() 时会自动打印训练准确率。
所以,我决定像下面这样修改教程代码来打印训练精度:
def train(dataloader, model, optimizer, loss_fn):
model.train()
size = len(dataloader.dataset)
num_batches = len(dataloader)
training_loss = 0.0
correct = 0.0
for batch, (imgs, labels) in enumerate(dataloader):
imgs = imgs.to(device=device)
labels = labels.to(device=device)
predictions = model(imgs)
loss = loss_fn(predictions, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# accumulate the training loss - each batch's loss will be added to trainin_loss
training_loss += loss.item()
# determines the number of correct predictions
correct += (predictions.argmax(1) == labels).type(torch.float).sum().item()
# end of for loop - all batches are processed
# after all batches are processed, determine the average training loss
training_loss = training_loss / num_batches
# this would be the training accuracy: number of correct predictions / number of samples in dataset
correct = correct / size
print(f"{datetime.datetime.now()} Training Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {training_loss:>8f} \n")
这样可以吗?作为 PyTorch 的初学者,我想在开始训练我的神经网络之前确保它是正确的。
看起来一切正常。这样做应该不会影响训练。 loss.backward()
计算你的梯度,任何不连接的东西都不能改变它们。顺便说一下,只是 运行 培训,你不能破坏任何东西:)(但是。等你开始制造自动驾驶汽车。)。
我想,在 Keras/TensorFlow fit()
中不会自动计算 accuracy,您仍然必须指定此指标,例如当 编译模型或作为fit()
的参数,例如:
model.compile(optimizer='sgd',
loss='mse',
metrics=[tf.keras.metrics.Accuracy()])