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

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

我是神经网络领域的新手,我正在尝试从 this model and these equations (N=32) 中实现一个 CNN 生成器以生成运动。我编写了以下代码,其中 H_txt 是一个字典,其中包含作为键的剪辑名称和作为值的向量,表示剪辑中显示的动作,z 是维数为 (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)

当我 运行 我的代码时,我收到以下错误消息,来自我的 forward 函数:

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

我的代码不喜欢我尝试用字典对张量求和这一事实。我还没有在网上找到任何解决方案,所以我想知道是否可以用字典对我的张量求和?有没有可以将我的字典转换为张量的函数? 我尝试使用 np.asarray() 将字典转换为数组,但收到一条错误消息,提示我不能为此使用 numpy 函数。

非常感谢阅读我的消息

您的问题缺少有关您的变量的详细信息,但基于我给您的错误,我的 answer.You 正在添加字典和张量,它为您提供 error.If 您想要添加的字典的值到张量那么你必须将字典转换成 tensor.And 还有为什么你直接添加字典因为这里不需要 keys.If 你想在转换后连接字典中的值它沿所需轴转换为张量,然后您需要使用 torch.cat 函数。 将字典中的值添加到张量中,从字典中获取值并通过这样的方式转换为张量

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