首页 > 解决方案 > 无法插入需要 grad 作为常量的张量

问题描述

我在 pytorch 上使用 pyro 制作了一个模型。但是,我收到以下错误:

h_0_contig = self.h_0.expand(1,size,self.rnn.hidden_size)

类型错误:expand():参数“大小”必须是整数元组,但在位置 2 找到了张量类型的元素

我认为这是因为我将张量作为输入。然后我明确地将它作为一个整数给出,然后它给了我以下错误:

RuntimeError:无法插入需要 grad 作为常量的张量。考虑将其作为参数或输入,或分离梯度

张量:

(1,.,.) = 列 1 到 26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

我在下面显示了我的代码,其中出现了错误(功能指南)。mini_batch、mini_batch_reversed、mini_batch_mask 和退火因子,它们都是张量。我正在使用以下代码:

https://github.com/pyro-ppl/pyro/blob/dev/examples/dmm/dmm.py

我不知道是什么导致了这个错误。见解将不胜感激。


    def guide(self,mini_batch,mini_batch_reversed,mini_batch_mask,annealing_factor = torch.tensor(1.0)):

        #T_max = mini_batch.size(1)
        T_max = 247

        pyro.module('DMM',self)

        #size = mini_batch.size(0)
        size = 5
        print('type of size : ',type(size))

        print('rnn hidden size : ',self.rnn.hidden_size)

        h_0_contig = self.h_0.expand(1,size,self.rnn.hidden_size)

        rnn_output,_ = self.rnn(mini_batch_reversed,h_0_contig)

        rnn_output = dp.pad_and_reverse(rnn_output, mini_batch.size(1))

        z_prev = self.z_q_0.expand(mini_batch.size(0),self.z_q_0.size(0))

        with pyro.plate('z_minibatch',len(mini_batch)):
            for t in range(1,T_max+1):
                z_loc,z_scale = self.combiner(z_prev,rnn_output[:,t-1,:])

                z_dist = dist.Normal(z_loc,z_scale)

                with pyro.poutine.scale(None,annealing_factor):
                    z_t = pyro.sample("z_%d" % t,z_dist.mask(mini_batch_mask[:, t - 1:t]).to_event(1))

                z_prev = z_t

标签: pythonpython-3.xpytorch

解决方案


推荐阅读