input.size(-1) 必须等于 input_size。预期763,得到1
input.size(-1) must be equal to input_size. Expected 763, got 1
我正在尝试以 50 的批量大小训练我的模型。但是我收到错误消息:
input.size(-1) must be equal to input_size. Expected 763, got 1
我的代码是:
for epoch in range(1, n_epochs + 1):
for i, (x_batch, y_batch) in enumerate(trn_dl):
#model.to(device)
#model.train()
x_batch = x_batch.to(device)
y_batch = y_batch.to(device)
#sched.step()
print('shape of the input batch')
print(x_batch.shape)
opt.zero_grad()
x_batch=torch.unsqueeze(x_batch,2)
print(x_batch.shape)
print(x_batch)
out = model(x_batch) # here I am getting error
y_batch=torch.unsqueeze(y_batch,0)
print('NOW')
print(y_batch.dtype)
y_batch = y_batch.to(torch.float32)
out = out.to(torch.float32)
out=torch.transpose(out,1,0)
loss = loss_function(out, torch.max(y_batch, 1)[1])
#(out, y_batch)
#targets = targets.to(torch.float32)
loss.backward()
opt.step()
我的模型是:
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.hidden_size = hidden_size
self.lstm = nn.LSTM(input_size, hidden_size)
self.linear =nn.Linear(hidden_size, output_size)
self.hidden_cell = (torch.zeros(1,1,self.hidden_size),
torch.zeros(1,1,self.hidden_size))
def forward(self, input_seq):
h0 = torch.zeros(1, input_seq.size(0), self.hidden_size).to(device)
c0 = torch.zeros(1, input_seq.size(0), self.hidden_size).to(device)
lstm_out, _ = self.lstm(input_seq, (h0,c0))
lstm_out = self.fc(lstm_out[:, -1, :])
predictions = self.Linear(lstm_out.view(len(input_seq), -1))
print("predictions",predictions)
return predictions[-1]
任何人都可以查看并帮助我。
从外观上看,您正在尝试选择 LSTM 输出的最后一步:lstm_out[:, -1, :]
。但是,默认情况下 nn.RNN
s 批处理轴是第二个,而不是第一个:(sequence_length
、batch_size
、features
)。所以你最终选择了最后一个批次元素,而不是最后一个序列步骤。您可能希望在初始化 nn.LSTM
:
时使用 batch_first=True
类似于:
self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True)
我正在尝试以 50 的批量大小训练我的模型。但是我收到错误消息:
input.size(-1) must be equal to input_size. Expected 763, got 1
我的代码是:
for epoch in range(1, n_epochs + 1):
for i, (x_batch, y_batch) in enumerate(trn_dl):
#model.to(device)
#model.train()
x_batch = x_batch.to(device)
y_batch = y_batch.to(device)
#sched.step()
print('shape of the input batch')
print(x_batch.shape)
opt.zero_grad()
x_batch=torch.unsqueeze(x_batch,2)
print(x_batch.shape)
print(x_batch)
out = model(x_batch) # here I am getting error
y_batch=torch.unsqueeze(y_batch,0)
print('NOW')
print(y_batch.dtype)
y_batch = y_batch.to(torch.float32)
out = out.to(torch.float32)
out=torch.transpose(out,1,0)
loss = loss_function(out, torch.max(y_batch, 1)[1])
#(out, y_batch)
#targets = targets.to(torch.float32)
loss.backward()
opt.step()
我的模型是:
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.hidden_size = hidden_size
self.lstm = nn.LSTM(input_size, hidden_size)
self.linear =nn.Linear(hidden_size, output_size)
self.hidden_cell = (torch.zeros(1,1,self.hidden_size),
torch.zeros(1,1,self.hidden_size))
def forward(self, input_seq):
h0 = torch.zeros(1, input_seq.size(0), self.hidden_size).to(device)
c0 = torch.zeros(1, input_seq.size(0), self.hidden_size).to(device)
lstm_out, _ = self.lstm(input_seq, (h0,c0))
lstm_out = self.fc(lstm_out[:, -1, :])
predictions = self.Linear(lstm_out.view(len(input_seq), -1))
print("predictions",predictions)
return predictions[-1]
任何人都可以查看并帮助我。
从外观上看,您正在尝试选择 LSTM 输出的最后一步:lstm_out[:, -1, :]
。但是,默认情况下 nn.RNN
s 批处理轴是第二个,而不是第一个:(sequence_length
、batch_size
、features
)。所以你最终选择了最后一个批次元素,而不是最后一个序列步骤。您可能希望在初始化 nn.LSTM
:
batch_first=True
类似于:
self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True)