Search code examples
pythondictionarypytorchmotionconv-neural-network

TypeError: unsupported operand type(s) for +: 'Tensor' and 'dict'


I am new to the world of neural networks and I am trying to implement a CNN generator from this model and these equations (N=32) in order to make motion generation. I wrote the following code, where H_txt is a dictionary containing, as keys, the name of my clips and as values, a vector representing the action shown in the clip, and z is a white gaussian noise of dimension (1, 256).

N=32  

class CNNGenerator(nn.Module):

    def __init__(self, htxt = H_txt):                                       
        super(CNNGenerator, self).__init__()

        self.htxt = htxt
        self.conv1 = nn.Conv1d(1, 1, 3)
        self.conv2 = nn.Conv1d(1, 1, 3)            
        self.conv3 = nn.Conv1d(1, 1, 3)            
        self.conv4 = nn.Conv1d(4, 4, 3)            
        self.conv5 = nn.Conv1d(2, 2, 3)
        self.conv6 = nn.Conv1d(8, 8, 3)
        self.conv7 = nn.Conv1d(4, 4, 3)
        self.conv8 = nn.Conv1d(16, 16, 3)        
        self.conv9 = nn.Conv1d(8, 8, 3)
        self.conv10 = nn.Conv1d(32, 32, 3)
        self.conv11 = nn.Conv1d(16, 16, 3)
        self.conv12 = nn.Conv1d(32, 32, 3)
        self.conv13 = nn.Conv1d(1, 1, 3)
        self.conv14 = nn.Conv1d(2, 2, 3)
        self.conv15 = nn.Conv1d(2, 2, 3)
        self.conv16 = nn.Conv1d(4, 4, 3)
        self.conv17 = nn.Conv1d(4, 4, 3)
        self.conv18 = nn.Conv1d(8, 8, 3)
        self.conv19 = nn.Conv1d(8, 8, 3)
        self.conv20 = nn.Conv1d(16, 16, 3)
        self.conv21 = nn.Conv1d(16, 16, 3)
        self.conv22 = nn.Conv1d(32, 32, 3)
        self.conv23 = nn.Conv1d(32, 32, 3)





    def forward(self, x):                              
        x[0] = self.conv1(F.relu(self.conv2(z) + self.htxt))  

        x[1] = self.conv3(F.relu(self.conv4(z) + self.htxt))
        x[2] = self.conv5(F.relu(self.conv6(z) + self.htxt))
        x[3] = self.conv7(F.relu(self.conv8(z) + self.htxt))
        x[4] = self.conv9(F.relu(self.conv10(z) + self.htxt))
        x[5] = self.conv11(F.relu(self.conv12(z) + self.htxt))
        h = np.zeros(np.log2(N))               

        h[0] = x[0]                                  

        h[1] = nn.AdaptiveAvgPool1d(2*h[0]) + self.conv13(F.relu(nn.AdaptiveAvgPool1d(2*(self.conv14(h[0])+x[1]))))   
        h[2] = nn.AdaptiveAvgPool1d(2*h[1]) + self.conv15(F.relu(nn.AdaptiveAvgPool1d(2*(self.conv16(h[1])+x[2]))))   
        h[3] = nn.AdaptiveAvgPool1d(2*h[2]) + self.conv17(F.relu(nn.AdaptiveAvgPool1d(2*(self.conv18(h[2])+x[3]))))   
        h[4] = nn.AdaptiveAvgPool1d(2*h[3]) + self.conv19(F.relu(nn.AdaptiveAvgPool1d(2*(self.conv20(h[3])+x[4]))))   
        h[5] = nn.AdaptiveAvgPool1d(2*h[4]) + self.conv21(F.relu(nn.AdaptiveAvgPool1d(2*(self.conv22(h[4])+x[5]))))
        A = self.conv23(h[np.log2(N)])
        return A




    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


    net = CNNGenerator()
    z= torch.randn(1, 1, 256)       #k=256
    out = net(z)
    print(out)

When I run my code, I get the following error message, coming from my forward function :

TypeError: unsupported operand type(s) for +: 'Tensor' and 'dict'

My code doesn't like the fact that I try to sum a tensor with a dictionary. I haven't found any solution online, so I was wondering if it was possible to sum my tensor with a dictionary? Is there a function that can convert my dictionary into a tensor? I tried to convert the dictionary into an array with np.asarray() but I got an error message saying I can't use a numpy function for this.

Thanks a lot for reading my message


Solution

  • Your question is missing details about your variables but based on the error i am giving you my answer.You are adding a dictionary and a tensor which is giving you the error.If you want to add the values of dictionary to the tensor then you must convert the dictionary into a tensor.And also why are you adding the dictionary directly because here there is no need for keys.If you want to concatenate the values in the dictionary after converting it into tensor along the desired axis then you need to use torch.cat function. To add the values in the dictionary to the tensor get the values from dict and convert to tensor by something like this

    torch.Tensor(list(htxt.values()))