F.nll_loss是什么
negative likelihood loss
2. nll_loss ,函数全称是negative log likelihood loss, 函数表达式为
f(x,class)=-x[class]
例如:假设x=[1,2,3], class=2, 则
f(x,class)=-x[2]=-3
pytorch中.item()的作用
pytorch中,.item()方法 是得到一个元素张量里面的元素值
具体就是 用于将一个零维张量转换成浮点数,比如计算loss,accuracy的值
就比如:
loss = (y_pred - y).pow(2).sum()
print(loss.item())
@torch.no_grad()和model.eval()的区别
被包裹 的部分会进行前向计算,但是不涉及梯度计算,加快计算速度,节省计算资源。
因为def test只进行前向的inference,不涉及反向传播,不需要计算梯度,故使用torch.no_grad()
model.eval()
主要作用在于保证dropout和batchnorm正常运行;
并且保证不进行反向传播;
虽然不进行反向传播;但是该操作不能阻止正向传播时梯度的计算;用@torch.no_grad()可以阻止正向计算时梯度计算导致的资源浪费
https://blog.csdn.net/weixin_46559271/article/details/10565865
parser = argparse.ArgumentParser使用
model.state_dict()作用
model.reset_parameters()
def test
这里的test函数挺有意思,不仅算了test,又把train、valid和test全都算了一遍,方便打印。