将张量值保存为二进制格式的文件的最佳方法是什么? [英] What is the best way to save tensor value to file as binary format?
问题描述
我正在尝试将张量值保存为二进制格式的文件. 特别是我正在尝试将float32张量值保存为二进制格式(IEEE-754格式).你能帮我吗??
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). 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. Then, whenever you want to save the values of the tensors, you invoke the save() method of the tf.train.Saver
object:
saver = tf.train.Saver(...)
#...
saver.save(session, 'my-checkpoints', global_step = step)
..其中第二个参数(在上例中为'my-checkpoints'
)是存储检查点二进制文件的目录的路径.
.. where the second argument ('my-checkpoints'
in the above example) is the path to a directory in which the checkpoint binary files are stored.
另一种方法是评估单个张量(将为NumPy ndarrays),然后将单个ndarray保存到NPY文件中(通过 numpy.savez()
或
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()
):
np.save('x.npy', session.run(x), allow_pickle = False)
这篇关于将张量值保存为二进制格式的文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!