【发布时间】:2021-08-28 06:11:27
【问题描述】:
我已经在 pytorch 中创建并训练了一个非常简单的网络,如下所示:
self.task_layers[task][task_layer_key]; TaskLayerManager(
(taskLayers): ModuleList(
(0): lc_hidden(
(dropout_layer): Dropout(p=0.0, inplace=False)
(layer_norm): LayerNorm((768,), eps=1e-05, elementwise_affine=True)
)
(1): cnn(
(cnn_layer): CNN_Text(
(dropout): Dropout(p=0.1, inplace=False)
(fc1): Linear(in_features=300, out_features=2, bias=True)
(convs1): ModuleList(
(0): Conv2d(1, 300, kernel_size=(5, 768), stride=(1, 1), padding=(4, 0))
)
)
)
)
)
Layer descriptions:
taskLayers.0.linear_weights torch.Size([13])
taskLayers.0.layer_norm.weight torch.Size([768])
taskLayers.0.layer_norm.bias torch.Size([768])
taskLayers.1.cnn_layer.fc1.weight torch.Size([2, 300])
taskLayers.1.cnn_layer.fc1.bias torch.Size([2])
taskLayers.1.cnn_layer.convs1.0.weight torch.Size([300, 1, 5, 768])
taskLayers.1.cnn_layer.convs1.0.bias torch.Size([300])
这是一个二元分类网络,以 3d 张量作为输入 [N,K,768] 并给出输出 [N,2] 张量 我无法弄清楚“为什么每次运行都会给我不同的结果”? 请帮我解决这个问题 - 我是 pytorch 的新手。 如果需要任何其他信息,请告诉我。
【问题讨论】:
-
你是否先用
model.eval()将模型设置为推理模式? -
感谢@iacob,你救了我的命,这样做解决了问题。非常感谢您的帮助。
-
除了 model.eval(),如果你还没有这样做,在
with torch.no_grad():下运行推理或在推理函数上使用@torch.no_grad()装饰器也是一个好习惯。跨度>
标签: python deep-learning pytorch torch