这是我的 lstm 网络代码,我将其实例化并传递给 Cuda 设备,但仍然收到隐藏和输入不在同一设备中的错误
class LSTM_net(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(LSTM_net, self).__init__()
self.hidden_size = hidden_size
self.lstm_cell = nn.LSTM(input_size, hidden_size)
self.h2o = nn.Linear(hidden_size, output_size)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, input, hidden_0=None, hidden_1=None, hidden_2=None):
input=resnet(input)
input=input.unsqueeze(0)
out_0, hidden_0 = self.lstm_cell(input, hidden_0)
out_1, hidden_1 = self.lstm_cell(out_0+input, hidden_1)
out_2, hidden_2 = self.lstm_cell(out_1+input, hidden_2)
output = self.h2o(hidden_2[0].view(-1, self.hidden_size))
output = self.softmax(output)
return output,hidden_0,hidden_1, hidden_2
def init_hidden(self, batch_size = 1):
return (torch.zeros(1, batch_size, self.hidden_size), torch.zeros(1, batch_size, self.hidden_size))
net1=LSTM_net(input_size=1000,hidden_size=1000, output_size=100)
net1=net1.to(device)