[resolved] AssertionError: Tensors not supported in scatter


I make a network which has the following structure, which I would like to input a testing image and a list of maps

class SimpleNet(nn.Module):

    def __init__(self, in_shape, num_classes):
        super(SimpleNet, self).__init__()

    def forward(self, x, maps):

        out = self.block0(x)
        assert out.size() == maps[4]
        out = torch.mul(out, maps[4])
        out = self.block1(out)
        out = torch.mul(out, maps[3])
#        print(out.size())
        out = self.block2(out)
        out = torch.mul(out, maps[2])
#        print(out.size())
        out = self.block3(out)
        out = torch.mul(out, maps[1])
#        print(out.size())
        out = self.block8(out)
        out = torch.mul(out, maps[0])
#        print(out.size())
        out = self.prob(out)
        out = torch.squeeze(out)
#        print(out.size())

        return out        

When I execute the model like this,

model2 = SimpleNet(img_shape, cls_num)
model2_output = model2(img_var, select_maps)

It comes the following error:

  File "/home/kang/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)

  File "/home/kang/anaconda3/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 57, in forward
    inputs, kwargs = self.scatter(inputs, kwargs, self.device_ids)

  File "/home/kang/anaconda3/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 68, in scatter
    return scatter_kwargs(inputs, kwargs, device_ids, dim=self.dim)

  File "/home/kang/anaconda3/lib/python3.6/site-packages/torch/nn/parallel/scatter_gather.py", line 30, in scatter_kwargs
    inputs = scatter(inputs, target_gpus, dim)

  File "/home/kang/anaconda3/lib/python3.6/site-packages/torch/nn/parallel/scatter_gather.py", line 25, in scatter
    return scatter_map(inputs)

  File "/home/kang/anaconda3/lib/python3.6/site-packages/torch/nn/parallel/scatter_gather.py", line 18, in scatter_map
    return tuple(zip(*map(scatter_map, obj)))

  File "/home/kang/anaconda3/lib/python3.6/site-packages/torch/nn/parallel/scatter_gather.py", line 20, in scatter_map
    return tuple(map(list, zip(*map(scatter_map, obj))))

  File "/home/kang/anaconda3/lib/python3.6/site-packages/torch/nn/parallel/scatter_gather.py", line 16, in scatter_map
    assert not torch.is_tensor(obj), "Tensors not supported in scatter."

AssertionError: Tensors not supported in scatter.

It seems that pytorch cannot support this.
How to solve this error?

Thank you.

I forgot to transfer maps into Variable. It works.

1 Like

I’m running int o a similar error as you upon trying to run training on a network I modified. I’m an absolute beginner at pytorch. Would you mind elaborating your solution further?

I remember the input of “maps” should also be transformed into variable.

1 Like

hey karl, could you give detail experience how you solve this one?

If you are using pytorch version before 0.4.0, you would have to use Variable from torch.autograd.

For example in his case, maps must have been a tensor. So to transform to a Variable,

from torch.autograd import Variable
maps = Variable(maps)

But I am not sure why this is done though.