我尝试运行以下代码(翻译):
import torch
import torch.nn as nn
lstm = nn.LSTM(input_size=300, hidden_size=256, num_layers=2)
attn = nn.Linear(256, 10)
fc = nn.Linear(10 * 2, 300)
input_en = torch.tensor(inp_en_idx)
hidden = lstm(input_en)[0]
attn_weights = attn(hidden[-1])
context = attn_weights.softmax(dim=1).unsqueeze(1).expand_as(hidden)
context = context * hidden
context = context.sum(dim=1)
out = fc(context)
但出现此错误:
NameError Traceback (most recent call last)
<ipython-input-2-d484f124dd49> in <cell line: 7>()
5 fc = nn.Linear(10 * 2, 300)
6
----> 7 input_en = torch.tensor(inp_en_idx)
8 hidden = lstm(input_en)[0]
9 attn_weights = attn(hidden[-1])
NameError: name 'inp_en_idx' is not defined
我该如何解决这个错误?
什么是
inp_en_idx
你之前定义过 is 吗?使用前必须声明