2

I'm trying to work on lstm in pytorch. It takes only tensors as the input. The data that I have is in the form of a numpy.object_ and if I convert this to a numpy.float, then it can be converted to tensor.

I checked the data type using print(type(array)) it gives class 'numpy.ndarray' as output and print(arr.dtype.type) gives class 'numpy.object_' as output.

Or is there any way to convert tuple directly to torch.tensor?

4
  • docs.scipy.org/doc/numpy-1.13.0/reference/generated/… check this out if it helps Commented May 27, 2019 at 12:30
  • @mansisinha, can you please add some sample data ? Commented May 27, 2019 at 14:10
  • class fullstop(nn.Module): def __init__(self): super(fullstop, self).__init__() self.seq1=nn.LSTM(input_size=30,hidden_size=20) self.seq2=nn.LSTM(input_size=20,hidden_size=10) self.fc1=nn.Linear(20,5) self.fc2=nn.Linear(5,1) def forward(self,input1,input2): prefix1=self.seq1(input1) suffix1=self.seq1(input2) prefix2=self.seq2(prefix1) suffix2=self.seq2(suffix1) result=torch.cat(Variable(prefix1,suffix1),1) r1=F.sigmoid(self.fc1(result)) r2=self.fc2(r1) return r2 Commented May 28, 2019 at 5:25
  • @AnubhavSingh the forward function here takes only tensor as the input. So when I'm giving input1 and input2 as tensor, it works fine but then for seq2, output of seq1 is taken as input. This output is coming as tuple hence it is giving error. Commented May 28, 2019 at 5:31

1 Answer 1

1

The pytorch LSTM returns a tuple. So you get this error as your second LSTM layer self.seq2 can not handle this tuple. So, change

prefix1=self.seq1(input1) 
suffix1=self.seq1(input2)

to something like this:

prefix1_out, prefix1_states = self.seq1(input1) 
suffix1_out, suffix1_states = self.seq1(input2) 

and then pass prefix1_out and suffix1_out tensors to the next LSTM layers as

prefix2_out, prefix2_states = self.seq2(prefix1_out) 
suffix2_out, suffix2_states = self.seq2(suffix1_out)

And, concat prefix1_out and suffix1_out tensors like this

result = torch.cat([out1,out2],1) 

Also, change

r1=F.sigmoid(self.fc1(result)) 
r2=self.fc2(r1)

to something like this:

out_ll = self.fc1(result)
r1 = nn.Sigmoid() 
r2 = self.fc2(r1(out_ll))
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome. I am glad I could help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.