首页 > 解决方案 > Tensorflow 虽然 loop_vars 和 body 参数相同,但两个结构在 while_loop 中没有相同的嵌套结构

问题描述

我正在尝试在 tensorflow 中实现对比发散,但我遇到了这个错误,我不知道如何解决它。

这是我的代码:

def gibbs_step(self, v_act, v_prob, v_mf, h_act, h_prob, h_mf):
    h_mf = tf.matmul(v_act, self.w) + self.hidden_bias
    h_prob = tf.nn.sigmoid(h_mf)
    h_act = self.hidden_activation(h_mf, h_prob)

    v_mf = tf.matmul(h_act, tf.transpose(self.w)) + self.visible_bias
    v_prob = tf.nn.sigmoid(v_mf)
    v_act = self.visible_activation(v_mf, v_prob)
    return [v_act, v_prob, v_mf, h_act, h_prob, h_mf]

def contrastive_divergence(self, x):
    v_act = tf.identity(x)#(tf.zeros([self.n_visibles]), dtype=tf.float32)
    v_prob = tf.zeros_like(x)
    v_mf = tf.zeros_like(x)
    h_act = tf.zeros_like(self.h_mf)
    h_prob = tf.zeros_like(self.h_mf)
    h_mf = tf.zeros_like(self.h_mf)

    def always_true(*kwargs):
        return True

    cond = always_true
    loop_vars = [v_act, v_prob, v_mf, h_act, h_prob, h_mf]

    return tf.while_loop(cond, self.gibbs_step, loop_vars, maximum_iterations = self.k)

从错误日志中我可以看到维度是正确的,但是,错误日志中所谓的“第二个结构”是一个元组而不是一个列表。我想这是导致错误的原因,但我不清楚它来自哪里,gibbs_step正在返回一个列表,loop_vars也是一个列表。

TypeError: The two structures don't have the same nested structure.

First structure: type=list str=[<tf.Tensor 'contrastive_divergence/while/Identity:0' shape=() dtype=int32>, [<tf.Tensor 'contrastive_divergence/while/Identity_1:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Identity_2:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Identity_3:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Identity_4:0' shape=(?, 150) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Identity_5:0' shape=(?, 150) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Identity_6:0' shape=(?, 150) dtype=float32>]]

Second structure: type=list str=[<tf.Tensor 'contrastive_divergence/while/add:0' shape=() dtype=int32>, (<tf.Tensor 'contrastive_divergence/while/random_normal_1:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Sigmoid_1:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/add_2:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Relu:0' shape=(?, 150) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Sigmoid:0' shape=(?, 150) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/add_1:0' shape=(?, 150) dtype=float32>)]

More specifically: The two namedtuples don't have the same sequence type. First structure type=list str=[<tf.Tensor 'contrastive_divergence/while/Identity_1:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Identity_2:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Identity_3:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Identity_4:0' shape=(?, 150) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Identity_5:0' shape=(?, 150) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Identity_6:0' shape=(?, 150) dtype=float32>] has type list, while second structure type=tuple str=(<tf.Tensor 'contrastive_divergence/while/random_normal_1:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Sigmoid_1:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/add_2:0' shape=(?, 400) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Relu:0' shape=(?, 150) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/Sigmoid:0' shape=(?, 150) dtype=float32>, <tf.Tensor 'contrastive_divergence/while/add_1:0' shape=(?, 150) dtype=float32>) has type tuple

对此有什么帮助吗?

谢谢!

标签: pythontensorflowwhile-looptypeerror

解决方案


推荐阅读