【发布时间】:2021-04-13 14:09:46
【问题描述】:
在 numpy 中,将 np 张量转换为字节可以如下完成:
import tensorflow as tf
import numpy as np
b = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.uint8)
bytesArr = b.tobytes()
print(bytesArr)
在 tensorflow 中,您可以这样做来创建张量,但是如何将结果转换为字节数组?
a = tf.cast(tf.constant([[1.0, 2.0], [3.0, 4.0]]), tf.uint8)
#Do conversion here
另外:我也在处理一个包裹在@tf.function 中的 tf 模型,所以没有启用急切执行,这意味着我不能在张量上使用 .numpy() 方法。
【问题讨论】:
标签: python numpy tensorflow