GRU creation resulted in error

Pranavan_Theivendira: <p>Hi,</p>
<p>I try to create a GRU in pytorch as follows.</p>
<pre><code class="lang-auto">self.gru = nn.GRU(input_size=(4,128), hidden_size=128, num_layers=no_of_layers, batch_first=True)
</code></pre>
<p>Got the following error.</p>
<pre><code class="lang-auto">TypeError: new(): argument 'size' must be tuple of ints, but found element of type tuple at pos 2
</code></pre>
<p>Any idea what could be the mistake?</p>
<p>Thanks<br>
Regards<br>
Pranavan</p>

ptrblck: <p>The error message sounds a bit weird as apparently it’s being thrown while trying to create some internal tensors using the <code>hidden_size</code> and <code>input_size</code> for the shape definition.<br>
Anyway, try to pass the <code>input_size</code> as a single value and it should work.</p>

Pranavan_Theivendira: <p>Hi,</p>
<p>Thanks a lot for the input. I tried to pass single input, it works well. Does it mean that multi dimensional inputs are restricted in GRU?</p>
<p>Thanks</p>

ptrblck: <p>Could you explain your input a bit?<br>
<code>nn.GRU</code> expects the following input:</p>
<blockquote>
<p>Input1: (L,N,Hin​) tensor containing input features where Hin=input_size and L represents a sequence length.</p>
</blockquote>

Pranavan_Theivendira: <aside class="quote no-group quote-modified" data-username="ptrblck" data-post="4" data-topic="51550">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<blockquote>
<p>Input1: (L,N,Hin​) tensor containing input features where Hin=input_size and L represents a sequence length.</p>
</blockquote>
</blockquote>
</aside>
<p>Hi,</p>
<p>I have L, and N. But, in my case, the features(H_in) are two dimensional. Do I have to flatten the features to be 1 dimensional?</p>
<p>Thanks</p>

ptrblck: <p>You could flatten them or pass them through another linear layer to create a single feature.<br>
However, I’m not sure what the best approach would be so let’s wait for some experts on this topic. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

Navya: <p>I have similar error as well. Not sure how to solve it:</p>
<p>Traceback (most recent call last):<br>
File “train.py”, line 53, in <br>
model = Autoencoder( trainseq = X_train[-1,:,:].shape,testseq = tensor_y.shape,  hidden_dim = 1,  target_len= tensor_x.shape[1], batch_size = tensor_x.shape[1])<br>
File “/home/ubuntu/Desktop/Mol/models.py”, line 113, in <strong>init</strong><br>
self.encoder = Encoder(input_size = self.input_size, hidden_dim = self.hidden_dim,num_layers = self.num_layers,batch_size = self.batch_size)<br>
File “/home/ubuntu/Desktop/Mol/models.py”, line 44, in <strong>init</strong><br>
self.bi_lstm1 = nn.LSTM(self.input_size, self.hidden_dim, bidirectional=True)<span class="hashtag">#batch_first</span> = True)<br>
File “/home/ubuntu/.local/lib/python3.6/site-packages/torch/nn/modules/rnn.py”, line 590, in <strong>init</strong><br>
super(LSTM, self).<strong>init</strong>(‘LSTM’, *args, **kwargs)<br>
File “/home/ubuntu/.local/lib/python3.6/site-packages/torch/nn/modules/rnn.py”, line 87, in <strong>init</strong><br>
w_ih = Parameter(torch.Tensor(gate_size, layer_input_size))<br>
TypeError: new(): argument ‘size’ must be tuple of ints, but found element of type tuple at pos 2</p>

ptrblck: <p>It seems you are running into the same issue as the original author of this topic, so you should also pass the sizes as <code>ints</code>:</p>
<pre><code class="lang-python">input_size = (10, 1)
hidden_dim = 10
model = nn.LSTM(input_size, hidden_dim, bidirectional=True)
&gt; TypeError: empty(): argument 'size' must be tuple of ints, but found element of type tuple at pos 2

input_size = 10
model = nn.LSTM(input_size, hidden_dim, bidirectional=True) # works
</code></pre>