Tensorflow虽然loop_vars和body参数相同,但是while_loop中两个结构的嵌套结构不同

时间:2019-04-17 17:16:38

标签: python tensorflow while-loop typeerror

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

这是我的代码:

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

对此有任何帮助吗?

谢谢!

0 个答案:

没有答案