pytorch icon indicating copy to clipboard operation
pytorch copied to clipboard

function expand_as() works incorrectly on latest Pytorch 0.2.0_1

Open Jiankai-Sun opened this issue 9 years ago • 6 comments

I think this is a bug for function expand_as() since expand_as() works correctly on pytorch 0.1.2 but incorrectly on pytorch '0.2.0_1'

System: Ubuntu 16.04 64-bit Python 2.7 pytorch '0.2.0_1

A toy example on latest pytorch 0.2.0_1

In [39]: import torch

In [40]: import torch.nn as nn

In [41]: linear_policy_1 = nn.Linear(256,6)

In [42]: weights = linear_policy_1.weight.data

In [43]: out = torch.randn(weights.size())

In [44]: out.pow(2).sum(1).expand_as(out)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-44-b84413eca516> in <module>()
----> 1 out.pow(2).sum(1).expand_as(out)

/home/jack/Applications/pythonEnv/python2/local/lib/python2.7/site-packages/torch/tensor.pyc in expand_as(self, tensor)
    234             self.expand(tensor.size())
    235         """
--> 236         return self.expand(tensor.size())
    237 
    238     def repeat(self, *sizes):

RuntimeError: The expanded size of the tensor (256) must match the existing size (6) at non-singleton dimension 1. at /pytorch/torch/lib/TH/generic/THTensor.c:308

In [45]: 


Jiankai-Sun avatar Aug 20 '17 10:08 Jiankai-Sun

I get the same issue. Any luck or suggestion?

rmaharajan avatar Aug 20 '17 17:08 rmaharajan

[2017-08-20 13:38:40,546] Making new env: Breakout-v0 Traceback (most recent call last):

File "", line 29, in shared_model = ActorCritic(env.observation_space.shape[0], env.action_space)

File "model.py", line 48, in init self.actor_linear.weight.data = normalized_columns_initializer(self.actor_linear.weight.data, 0.01)

File "model.py", line 12, in normalized_columns_initializer out *= std / torch.sqrt(out.pow(2).sum(1).expand_as(out))

File "/home/trphunt/anaconda3/envs/py27/lib/python2.7/site-packages/torch/tensor.py", line 236, in expand_as return self.expand(tensor.size())

RuntimeError: The expanded size of the tensor (256) must match the existing size (4) at non-singleton dimension 1. at /opt/conda/conda-bld/pytorch_1502001039157/work/torch/lib/TH/generic/THTensor.c:308

trphunt avatar Aug 20 '17 17:08 trphunt

Replace sum(1) with sum(1, keepdim=True)

This is caused by the change of sum in 0.2.0.

ruotianluo avatar Aug 20 '17 17:08 ruotianluo

Thank you!

sum(1, True) worked.

I'm not sure what version I'm using but apparently sum takes one of

  • no arguments
  • (int dim)
  • (int dim, bool keepdim)

P.S. That was an awesome response time, thanks again!

trphunt avatar Aug 20 '17 18:08 trphunt

I have face same problem and i am using torch 0.3.1 and cuda 9.1 ubuntu OS Traceback (most recent call last): File "main.py", line 204, in fixed_z = netE(fixed_img_v, encoder_h_1, encoder_h_2, encoder_h_3) File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 357, in call result = self.forward(*input, **kwargs) File "/home/mnit/Desktop/RNN_CAAE_Pytorch_10Label/models.py", line 62, in forward hidden1 = self.rnn1(x, hidden1) File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 357, in call result = self.forward(*input, **kwargs) File "/home/mnit/Desktop/RNN_CAAE_Pytorch_10Label/modules/conv_rnn.py", line 72, in forward gates = self.conv_ih(input) + self.conv_hh(hx) RuntimeError: The size of tensor a (32) must match the size of tensor b (20) at non-singleton dimension 0

Can any one provide solution

praveenkumarchandaliya avatar Jul 14 '18 18:07 praveenkumarchandaliya

Replace sum(1) with sum(1, keepdim=True)

This is caused by the change of sum in 0.2.0.

Im gettin a different error after doing this File "C:\Users\arjun\anaconda3\lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj)

TypeError: can't pickle module objects

Arjunsekar avatar Jul 07 '20 10:07 Arjunsekar