Python 3.x 如何将具有object类型的numpy.ndarray转换为torch.tensor?

Python 3.x 如何将具有object类型的numpy.ndarray转换为torch.tensor?,python-3.x,numpy,pytorch,Python 3.x,Numpy,Pytorch,我想在pytorch做lstm。它只接受张量作为输入。我拥有的数据是以numpy.object.的形式存在的,如果我将其转换为numpy.float,那么它可以转换为张量 我使用print(type(array))检查数据类型,它将class'numpy.ndarray'作为输出,而print(arr.dtype.type)将class'numpy.object.作为输出 或者有没有办法将元组直接转换为torch.tensor?pytorch LSTM返回一个元组。因此,当您的第二个LSTM层s

我想在pytorch做lstm。它只接受张量作为输入。我拥有的数据是以
numpy.object.
的形式存在的,如果我将其转换为
numpy.float
,那么它可以转换为
张量

我使用
print(type(array))
检查数据类型,它将class
'numpy.ndarray'
作为输出,而
print(arr.dtype.type)
将class
'numpy.object.
作为输出


或者有没有办法将元组直接转换为torch.tensor?

pytorch LSTM返回一个元组。因此,当您的第二个LSTM层self.seq2无法处理此元组时,会出现此错误。所以 改变

对这样的事情:

prefix1_out, prefix1_states = self.seq1(input1) 
suffix1_out, suffix1_states = self.seq1(input2) 
out_ll = self.fc1(result)
r1 = nn.Sigmoid() 
r2 = self.fc2(r1(out_ll))
然后将prefix1_out和suffix1_out张量传递给下一个LSTM层,如下所示

prefix2_out, prefix2_states = self.seq2(prefix1_out) 
suffix2_out, suffix2_states = self.seq2(suffix1_out)
concat前缀为,后缀为,像这样的张量

result = torch.cat([out1,out2],1) 
还有,改变

r1=F.sigmoid(self.fc1(result)) 
r2=self.fc2(r1)
对这样的事情:

prefix1_out, prefix1_states = self.seq1(input1) 
suffix1_out, suffix1_states = self.seq1(input2) 
out_ll = self.fc1(result)
r1 = nn.Sigmoid() 
r2 = self.fc2(r1(out_ll))

看看这个,如果它helps@mansisinha,您可以添加一些示例数据吗?类fullstop(nn.Module):def uu init uu u(self):super(fullstop,self).u init uuuu()self.seq1=nn.LSTM(input u size=30,hidden u size=20)self.seq2=nn.LSTM(input u size=20,hidden u size=10)self.fc1=nn.Linear(5,1)def forward(self,input1,input2):prefix1=self.seq1(input1)suffix1=self.seq1(input2)prefix2=self.seq2(suffix1)result=torch.cat(变量(prefix1,suffix1),1)r1=F.sigmoid(self.fc1(result))r2=self.fc2(r1)返回r2@AnubhavSingh这里的正向函数只接受张量作为输入。所以当我把input1和input2作为张量时,效果很好,但对于seq2,seq1的输出作为输入。这个输出是以元组的形式出现的,因此它给出了error.Welcome。我很高兴能帮助你。