[英]What is the best way to save tensor value to file as binary format?
I'm trying to save tensor value to file as binary format. 我正在尝试将张量值保存为二进制格式的文件。 Especially I'm trying to save float32 tensor value as binary format(IEEE-754 format). 特别是我试图将float32张量值保存为二进制格式(IEEE-754格式)。 Could you please help me ?? 请你帮助我好吗 ??
import tensorflow as tf
x = tf.constant([[1.0, 2.0, 3.0], [5.5, 4.3, 2.5]])
# how to save tensor x as binary format ??
The recommended approach is to checkpoint your model. 建议的方法是检查您的模型。 As documented in the Saving and Restoring programmer's guide , you create a tf.train.Saver
object, optionally specifying which variables/saveable objects are to be saved. 如“ 保存和恢复”程序员指南中所述 ,您可以创建一个tf.train.Saver
对象,可以选择指定要保存哪些变量/可保存对象。 Then, whenever you want to save the values of the tensors, you invoke the save() method of the tf.train.Saver
object: 然后,只要您想保存张量的值,就可以调用tf.train.Saver
对象的save()方法:
saver = tf.train.Saver(...)
#...
saver.save(session, 'my-checkpoints', global_step = step)
.. where the second argument ( 'my-checkpoints'
in the above example) is the path to a directory in which the checkpoint binary files are stored. ..其中第二个参数(上例中的'my-checkpoints'
)是存储检查点二进制文件的目录的路径。
Another approach is to evaluate individual tensors (which will be NumPy ndarrays) and then save individual ndarrays to NPY files (via numpy.save()
) or multiple ndarrays to a single NPZ archive (via numpy.savez()
or numpy.savez_compressed()
): 另一种方法是评估单个张量(将是NumPy ndarrays),然后将单个ndarray保存到NPY文件(通过numpy.save()
)或将多个ndarray保存到单个NPZ存档(通过numpy.savez()
或numpy.savez_compressed()
):
np.save('x.npy', session.run(x), allow_pickle = False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.