An argument of forward function is a dictionary and causes type error

I’m trying to implement a variant of an algorithm (ArXiv: 1708.05344) that changes a part of its network for each backprop, and the following piece causes an error.

class SMASH(nn.Module):
    #................
    def forward(self, x, arch, W):
        num_output_units = arch['num_output_units']
        depth = arch['depth']
        #....(unpacking arch continues)....
        #....(then, network is built here according to variables in arch)....
        return F.log_softmax(x) #where x has passed through the network

This results in the following error message, i.e., using string as an index as in dictionary is forbidden in this case. However, since arch contains up to 20 variables, and since the set of variables in arch is different for different types of NN to consider, use of dictionary would be very helpful. Is there no way to avoid this error using dictionary?

File "/home/ubuntu/SMASH-master/SMASH.py", line 312, in forward
network_type = arch['network_type']
File "/usr/local/lib/python3.5/dist-packages/torch/autograd/variable.py", line 76, in __getitem__
return Index.apply(self, key)
File "/usr/local/lib/python3.5/dist-packages/torch/autograd/_functions/tensor.py", line 16, in forward
result = i.index(ctx.index)
TypeError: indexing a tensor with an object of type str. The only supported types are integers, slices, numpy scalars and torch.cuda.LongTensor or torch.cuda.ByteTensor as the only argument.

Are you using the DataParallel module or splitting things into a batch in any particular way? I originally passed all those arguments in as nested lists because it was the easiest way to get it all compatible with that, but I don’t see a reason why you shouldn’t be able to pass in a dictionary to a forward method (custom forward()'s don’t really care what you pass them). It looks like you’re actually passing a Variable in where you mean to be passing in that dictionary, seeing as the arch object is not a dict by the time it arrives at line 312.

Aside, wrapping all those things in a dict is something I’d been meaning to do but never got around to, good stuff.

1 Like

Thank you so much for your reply. It turned out to be exactly what you said.