Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimension Error in Inference #10

Open
Mrs-Hudson opened this issue Nov 25, 2018 · 3 comments
Open

Dimension Error in Inference #10

Mrs-Hudson opened this issue Nov 25, 2018 · 3 comments

Comments

@Mrs-Hudson
Copy link

Hi

I was trying to run ' python a3c_main.py --evaluate 2 --load saved/pretrained_model' to run inference using the pre-trained model. However, I faced the following dimension error without changing the code:

File "/home/rparik/Downloads/Documents/CMU/Fall18/multi-modal/project/baselines/DeepRL-Grounding/models.py", line 88, in forward
  _, encoder_hidden = self.gru(word_embedding, encoder_hidden)
File "/home/rparik/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
  result = self.forward(*input, **kwargs)
File "/home/rparik/anaconda3/lib/python3.6/site-packages/torch/nn/modules/rnn.py", line 178, in forward
  self.check_forward_args(input, hx, batch_sizes)
File "/home/rparik/anaconda3/lib/python3.6/site-packages/torch/nn/modules/rnn.py", line 126, in check_forward_args
  expected_input_dim, input.dim()))
RuntimeError: input must have 3 dimensions, got 2

Any leads would be appreciated

@helenalee16888
Copy link

helenalee16888 commented Nov 29, 2018

Hi

I was trying to run ' python a3c_main.py --evaluate 2 --load saved/pretrained_model' to run inference using the pre-trained model. However, I faced the following dimension error without changing the code:

File "/home/rparik/Downloads/Documents/CMU/Fall18/multi-modal/project/baselines/DeepRL-Grounding/models.py", line 88, in forward
  _, encoder_hidden = self.gru(word_embedding, encoder_hidden)
File "/home/rparik/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
  result = self.forward(*input, **kwargs)
File "/home/rparik/anaconda3/lib/python3.6/site-packages/torch/nn/modules/rnn.py", line 178, in forward
  self.check_forward_args(input, hx, batch_sizes)
File "/home/rparik/anaconda3/lib/python3.6/site-packages/torch/nn/modules/rnn.py", line 126, in check_forward_args
  expected_input_dim, input.dim()))
RuntimeError: input must have 3 dimensions, got 2

Any leads would be appreciated

I got the same error as yours before, the error occurred just because the codes should run with older version of pytorch (at least before 0.4.0, maybe 0.3.1 I guess...You have to notice that the last modification of the project is 8 months ago ) , and then it fixed when I changed pytorch version.
Hope to help you!

@egg-west
Copy link

egg-west commented Mar 18, 2019

Yep this is relative to the version of Pytorch.
More specifically, you may modify some code of definition of hidden state and input about GRU. I post my modified forward function here, wish this helps

self.gru = nn.GRUCell(32, self.gru_hidden_size)
def forward(self, inputs):

        x, input_inst, (tx, hx, cx) = inputs

        # Get the image representation
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        x_image_rep = F.relu(self.conv3(x))

        # Get the instruction representation
        encoder_hidden = torch.zeros(1, self.gru_hidden_size)  # seq_len=1
        for i in range(input_inst.data.size(1)):
            word_embedding = self.embedding(input_inst[0, i]).unsqueeze(0)
            #print(word_embedding.shape)  # [1, 32]
            encoder_hidden = self.gru(word_embedding, encoder_hidden)
        x_instr_rep = encoder_hidden.view(-1, encoder_hidden.size(1))
        # print(x_instr_rep.shape)
        # Get the attention vector from the instruction representation
        x_attention = torch.sigmoid(self.attn_linear(x_instr_rep))

        # Gated-Attention
        x_attention = x_attention.unsqueeze(2).unsqueeze(3)
        x_attention = x_attention.expand(1, 64, 8, 17)
        assert x_image_rep.size() == x_attention.size()
        x = x_image_rep*x_attention
        x = x.view(x.size(0), -1)

        # A3C-LSTM
        x = F.relu(self.linear(x))
        hx, cx = self.lstm(x, (hx, cx))
        time_emb = self.time_emb_layer(tx)
        x = torch.cat((hx, time_emb.view(-1, self.time_emb_dim)), 1)

        return self.critic_linear(x), self.actor_linear(x), (hx, cx)

@YunlianMoon
Copy link

YunlianMoon commented Dec 18, 2019

@egg-west
when I modified the code accordding to your replay, I got another error:

  File "a3c_main.py", line 115, in <module>
    torch.load(args.load, map_location=lambda storage, loc: storage))
  File "/home/yunlian/virtualenvs/python3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 839, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for A3C_LSTM_GA:
        Missing key(s) in state_dict: "gru.weight_ih", "gru.weight_hh", "gru.bias_ih", "gru.bias_hh".
        Unexpected key(s) in state_dict: "gru.weight_ih_l0", "gru.weight_hh_l0", "gru.bias_ih_l0", "gru.bias_hh_l0".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants