将张量列表更改为列表张量!

数据挖掘 Python 张量流 麻木的
2022-03-14 02:32:49

我有一个由创建的张量列表

[some_function(x) for x in something]

这个列表变成了这样

[<tf.Tensor: shape=(), dtype=string, numpy=b'I have gone and you will go'>, <tf.Tensor: shape=(), dtype=string, numpy=b'we will go'>]

但我想要一个这样的对象

<tf.Tensor: shape=(3,), dtype=string, numpy=array([b'I have gone and you will go', b'we will go'], dtype=object)>

这是字符串列表的张量(与前一个相反)。有什么办法可以解决这个问题,假设我可以访问所有这些张量,即some_function(x) for x in something单独访问。

1个回答

为了堆叠 tf 张量列表,您可以使用 tf 函数stack因此名称

对于你的情况,


tf.stack([some_function(x) for x in something],axis=0)


或者您可以将它们堆叠在 numpy 中,然后将数组转换为张量,使用 numpynp.stack输入列表和轴来执行此操作