Python Tensorflow:如何将行占位符重塑为列占位符

Python Tensorflow:如何将行占位符重塑为列占位符,python,multidimensional-array,tensorflow,Python,Multidimensional Array,Tensorflow,我想将行占位符(例如,[1,2])转换为列占位符(例如,[1,2]] y = tf.placeholder(tf.int32, shape=[None], name='target') y = tf.reshape(y, (y.shape[0], 1)) init = tf.global_variables_initializer() with tf.Session() as sess: init.run() print(sess.run(y, feed_dict={y:[1,

我想将行占位符(例如,
[1,2]
)转换为列占位符(例如,
[1,2]]

y = tf.placeholder(tf.int32, shape=[None], name='target')
y = tf.reshape(y, (y.shape[0], 1))

init = tf.global_variables_initializer()
with tf.Session() as sess:
    init.run()
    print(sess.run(y, feed_dict={y:[1,2]}))
但我有一个错误:

TypeError: Failed to convert object of type <class 'tuple'> to Tensor. Contents: (Dimension(None), 1). Consider casting elements to a supported type.
TypeError:无法将类型的对象转换为Tensor。内容:(维度(无),1)。将铸造元素考虑为支持类型。

问题在于
y.shape[0]
的用法。y的尺寸定义为
None
。我还尝试了
tf.shape(y)
,但效果并不理想

使用tf.expand_dims()将行转换为列


但是,在输入数据时还有另一个问题,因为简单地向张量添加维度并不能定义其形状,而只是添加一个额外的索引。这是另一个你可以独立发布的问题

我不确定您是不是有意的,但您不能在运行时更改tensorflow中节点的形状。因此,由于
目标
被定义为
[?]
占位符,它将保持不变

相反,您可以使用
tf.expand_dims
函数将其转换为一个新的张量(它不会是占位符!),如前所述。这意味着您仍然必须输入
[?]
数组,但您可以在计算中使用经过整形的张量

y=tf.placeholder(tf.int32,shape=[None],name='target')
z=tf.展开_dims(y,轴=1)#另一个张量
使用tf.Session()作为sess:
tf.global_variables_initializer().run()
打印(sess.run(z,feed_dict={y:[1,2]}))
y = tf.placeholder(tf.int32, shape=[None], name='target')
y = tf.expand_dims(y,axis=1)