【发布时间】:2020-03-03 10:58:46
【问题描述】:
我有以下数组,
a = tf.random.uniform((5,2), 0, 10)
<tf.Tensor: shape=(5, 2), dtype=float32, numpy=
array([[3.8656425 , 6.7514324 ],
[0.49138665, 3.5968459 ],
[4.435692 , 4.7223845 ],
[7.3588967 , 0.31867146],
[1.6837907 , 3.2266355 ]], dtype=float32)>
我想要的是像下面这样的字符串化数组数组,这将返回一个 numpy 数组,但我想做 tensorflow ops 以返回一个张量:
list(map(str, a.numpy()))
['[3.8656425 6.7514324]',
'[0.49138665 3.5968459 ]',
'[4.435692 4.7223845]',
'[7.3588967 0.31867146]',
'[1.6837907 3.2266355]']
当我使用tf.as_string()时
tf.as_string(a)
<tf.Tensor: shape=(5, 2), dtype=string, numpy=
array([[b'3.865643', b'6.751432'],
[b'0.491387', b'3.596846'],
[b'4.435692', b'4.722384'],
[b'7.358897', b'0.318671'],
[b'1.683791', b'3.226635']], dtype=object)>
我也尝试过使用
tf.map_fn(tf.as_string, a, dtype=tf.string)
# Same output as above
tf.as_string() 将 float/int 张量转换为相同形状的字符串张量。
是否有任何 tensorflow 操作可以将张量作为一个整体进行字符串化?
【问题讨论】:
-
您使用的是什么版本的 TensorFlow?您是否需要将结果作为张量并仅使用 TensorFlow 操作,还是可以获取 NumPy 数组然后将其转换为字符串?
-
我正在使用 tensorflow 2;我需要一个张量结果,我不想使用问题中提到的 numpy 方式(或类似方法),然后将其转换为张量。我想要一个返回张量的操作,谢谢
标签: python arrays string tensorflow