python - 如何将张量流张量转换为字节?
问题描述
在 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() 方法。
解决方案
tensorByteProto = tf.io.serialize_tensor(a)
要从 TensorProto 中提取字节,请使用tensorByteProto.tensor_content
推荐阅读
- javascript - 如何使整个表单只读/禁用,但是当单击按钮时应该启用它
- php - 如何在线显示 Woocommerce 结帐通知?
- kotlin - kotlin:辅助构造函数中的“未解析的引用”
- ada - 评估 Ada 的子程序声明部分顺序
- r - 为R中分类变量的连续观察添加计数器?
- javascript - react-navigation 如何在 createAppContainer 中嵌套 TapNavigator
- python - 如何使用flask和wtform执行python脚本或从pythons脚本调用函数
- javascript - Dropzone js在提交时验证
- python - unittest 类无法在 python 中导入源模块
- ajax - Ajax call with .net MVC