'method' object does not support item assignment

I am trying to do something like this:

def forward(self, x):
x = self.conv1(x)
x=self.R1(x)
x=self.M1(x)

        layer1_mean=self.mean(x) ##calculate mean
        layer1_var=self.variance(x) ##calculate variance
        new_layer=((torch.zeros(2*batch, 36,1,1).cuda()).float) ##Create new tensor
        
        ## copy to new tensor
        new_layer[0:batch]=layer1_mean
        new_layer[batch:2*batch]=layer1_var

However, I am getting the error : ‘method’ object does not support item assignment

What’s going wrong and how can it be resolved?

You forgot parenthese for .float(), which means new_layer is a function in your code sample, and new_layer[0:batch] tries to index a function which is not possible.

Thanks @albanD. But its now throwing this error:
RuntimeError: copy from Variable to torch.cuda.FloatTensor isn’t implemented
Although, new_layer and layer1_mean are of same type.

What is given as the input of a module is a Variable, not a Tensor.
In your case, as the error message says, layer1_mean is a Variable while new_layer is a Tensor.
You should add a new_layer = Variable(new_layer).

1 Like

Thanks @albanD. Its working.