Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将形状(36,)的张量转换为(1,36)_Python_Numpy_Tensorflow - Fatal编程技术网

Python 如何将形状(36,)的张量转换为(1,36)

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列

我还不熟悉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列表中的一个列表,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, :]