混凝土()将渐变设置为0?

2024-06-09 15:00:23 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法用tf扫描?具体地说,我有这个代码用于Theano,我无法将此代码转换为Tensorflow。我尝试过使用标准的循环和张量切片和连接,但我认为它在优化中无法获得梯度。在

    stick_segment = theano.shared(value=np.zeros((batch_size,), dtype=theano.config.floatX), name='stick_segment')
    remaining_stick = theano.shared(value=np.ones((batch_size,), dtype=theano.config.floatX), name='remaining_stick')

    def compute_latent_vars(i, stick_segment, remaining_stick, v_samples):
        # compute stick segment                                                                                                     
        stick_segment = v_samples[:,i] * remaining_stick
        remaining_stick *= (1-v_samples[:,i])
        return (stick_segment, remaining_stick)

    (stick_segments, remaining_sticks), updates = theano.scan(fn=compute_latent_vars,
                                                              outputs_info=[stick_segment, remaining_stick],sequences=T.arange(latent_size-1),
                                                              non_sequences=[v_samples], strict=True)

我能够将Theano代码转换为Tensorflow,但是我无法得到梯度。也就是说,代码是:

^{pr2}$

但当我看到π或rem的梯度时,它们是非零的。但是当我看到z的梯度时,所有的都是0。是因为concat公司?有什么方法可以防止这个问题吗?在


Tags: 代码sizevaluetensorflownpsegmenttheanoshared