TypeError:描述符“ transpose”需要一个“ numpy.ndarray”对象,但收到一个“ Tensor”

时间:2019-06-13 13:01:06

标签: python numpy tensorflow tensor tensorflow2.0

我已经将Tensorflow 1.x代码升级到Tensorflow 2.0,但是我要解决一些问题。其中之一是关于代码的某些部分中的numpy数组与张量。我不知道该如何解决。

我在第29行的tf.transposetf.linalg.matrix_transposetf.linalg.transpose中,在外部函数和内部函数中都尝试过。另外,我在第30行中添加了.eval(),以获得np.array,如您在最后一次尝试中看到的:

TypeError                                 Traceback (most recent call last)

~/Documents/example.py in <module>
    202                       soft_gumbel_softmax=False,
    203                       hard_gumbel_softmax=False,
--> 204                       batch_discriminator=False)
    205 
    206 # optimizer

~/Documents/models/gan.py in __init__(self, vertexes, edges, nodes, embedding_dim, decoder_units, discriminator_units, decoder, discriminator, soft_gumbel_softmax, hard_gumbel_softmax, batch_discriminator)
     33         with tf.compat.v1.variable_scope('generator'):
     34             self.edges_logits, self.nodes_logits = self.decoder(self.embeddings, decoder_units, vertexes, edges, nodes,
---> 35                                                                 training=self.training, dropout_rate=self.dropout_rate)
     36 
     37         with tf.compat.v1.name_scope('outputs'):

~/Documents/models/__init__.py in decoder_adj(inputs, units, vertexes, edges, nodes, training, dropout_rate)
     28                                                   activation=None), (-1, edges, vertexes, vertexes))
     29         edges_logits = tf.transpose(a=(edges_logits + tf.linalg.matrix_transpose(edges_logits)) / 2, perm=(0, 2, 3, 1))
---> 30         edges_logits = tf.compat.v1.layers.dropout(edges_logits.eval(), dropout_rate, training=training)
     31 
     32     with tf.compat.v1.variable_scope('nodes_logits'):

TypeError: descriptor 'transpose' requires a 'numpy.ndarray' object but received a 'Tensor'

最后几行来自此函数:

def decoder_adj(inputs, units, vertexes, edges, nodes, training, dropout_rate=0.):
    output = multi_dense_layers(inputs, units, activation=tf.nn.tanh, dropout_rate=dropout_rate, training=training)

    with tf.compat.v1.variable_scope('edges_logits'):
        edges_logits = tf.reshape(tf.compat.v1.layers.dense(inputs=output, units=edges * vertexes * vertexes,
                                                  activation=None), (-1, edges, vertexes, vertexes))
        edges_logits = tf.transpose(a=(edges_logits + np.matrix.transpose(edges_logits)) / 2, perm=(0, 2, 3, 1))
        edges_logits = tf.compat.v1.layers.dropout(edges_logits, dropout_rate, training=training)

    with tf.compat.v1.variable_scope('nodes_logits'):
        nodes_logits = tf.compat.v1.layers.dense(inputs=output, units=vertexes * nodes, activation=None)
        nodes_logits = tf.reshape(nodes_logits, (-1, vertexes, nodes))
        nodes_logits = tf.compat.v1.layers.dropout(nodes_logits, dropout_rate, training=training)

    return edges_logits, nodes_logits

我想知道我应该在哪里,以哪种格式,为什么以及如何转换输入。

0 个答案:

没有答案