张量流如何重塑尺寸为None的张量,像这样[None,2,2,1]?

时间:2018-06-22 08:39:27

标签: docker tensorflow

错误是:

  

TypeError:无法将类型的对象转换为Tensor。   内容:[无,4]。   考虑将元素强制转换为受支持的类型。

import tensorflow as tf
xs=tf.placeholder(tf.float32,[None,2,2,1],name='x-input')
reshaped_xs=tf.reshape(xs,[None,4])

with tf.Session() as sess:
    print sess.run(reshaped_xs)

tensorflow版本是:1.4.0

docker run -d  --restart=always -p 8888:8888 tensorflow/tensorflow:1.4.0

那么如何解决这个问题? TKS!

3 个答案:

答案 0 :(得分:2)

要重塑占位符,只需对未知尺寸使用-1。使用sess.run时,还必须将一个值输入占位符。这将起作用:

import tensorflow as tf

xs = tf.placeholder(tf.float32, [None, 2, 2, 1], name='x-input')

reshaped_xs = tf.reshape(xs, [-1, 4])

with tf.Session() as sess:
    x = [[[[0.], [0.]], [[0.], [0.]]]]
    print(sess.run(reshaped_xs, feed_dict={xs: x}))

答案 1 :(得分:0)

您不能重塑占位符,它会为要在feed dict中传递的对象保留一个内存空间,“无”表示将动态分配内存,如果您将未分配的内存重塑为另一个固定大小内存,这可能是语法正确的,但是我怀疑是否可以在ipython中运行它。

答案 2 :(得分:0)

我认为这是问题和带答案的评论的意思。必须先为自己澄清。类似的东西。

import tensorflow as tf
import numpy as np

x = tf.placeholder(dtype=tf.float32, shape=(None))
x_reshaped = tf.reshape(x, shape=[tf.shape(x)[0], 2,2,1])

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    _, x_reshaped_result = sess.run([x, x_reshaped], feed_dict={x: np.random.random(16).reshape(4, 2, 2, 1)})
    print (x_reshaped_result.shape)