Python 如何将形状(36,)的张量转换为(1,36)
我还不熟悉numpy,希望有人能帮我 代码是:Python 如何将形状(36,)的张量转换为(1,36),python,numpy,tensorflow,Python,Numpy,Tensorflow,我还不熟悉numpy,希望有人能帮我 代码是: x = tf.placeholder(tf.float32 , [1 , 36]) # L is a list from a list of lists. sess.run(rnn_model , {x : L}) 错误是: ValueError: Cannot feed value of shape (36,) for Tensor 'Placeholder:0', which has shape '(1, 36)' 我认为这是因为L是2D列
x = tf.placeholder(tf.float32 , [1 , 36])
# L is a list from a list of lists.
sess.run(rnn_model , {x : L})
错误是:
ValueError: Cannot feed value of shape (36,) for Tensor 'Placeholder:0', which has shape '(1, 36)'
我认为这是因为L是2D列表中的一个列表,python认为L必须是一个列列表
如何解决此错误?您可以使用
numpy
来重塑列表L
,然后使用tf.convert\u to\u tensor()
例子
输出:
1.15.0
1.17.3
Tensor("Const_7:0", shape=(1, 36), dtype=float32)
可以使用将列表转换为列向量,如下所示:
sess.run(rnn_model , {x : np.expand_dims(L, axis=0)})
通过在所需位置使用None,可以使用奇特的索引添加大小为1的额外维度
L[None, :]
或
如果L不是一个数组。使用
tf。重塑
有效,但我想重塑列表,但不是张量本身。那么,L
中有什么?我能看看它的形状吗?
L[None, :]
np.array(L)[None, :]