Can not insert a Tensor that requires grad as a constant

Viewed 2525

I have made a model using pyro on top of pytorch. However, I get the following error:

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

TypeError: expand(): argument 'size' must be tuple of ints, but found element of type Tensor at pos 2

I thought it was because I was giving a tensor as an input. And then I explicitly gave it as an integer and then it gave me the following error:

RuntimeError: Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient

Tensor:

(1,.,.) = Columns 1 to 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

I have shown my code below in which I get the error (function guide). mini_batch, mini_batch_reversed, mini_batch_mask, and annealing factor, they all are tensors. I am using the code from the following:

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

I don't know what is causing this error. Insights would be appreciated.


    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

0 Answers
Related