简体   繁体   English

将张量值保存为二进制格式的文件的最佳方法是什么?

[英]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.

相关问题 将 Q 表保存到文件的最佳方法是什么? - What is the best way to save Q table to file? 将复杂字典保存到文件的最佳方法是什么? - What is the best way to save a complex dictionary to file? 将pdf文件转换为base64Binary的最佳方法是什么? - What is the best way to convert a pdf file into base64Binary? 搜索 pandas paquet 格式的列中的值是否唯一的最佳方法是什么? - What is the best way to search if a value is unique in a column in pandas paquet format? 将 numpy poly1d object 保存到文件的最佳方法是什么? - What is the best way to save a numpy poly1d object to a file? 读取具有 key=“value” 格式的文本数据文件的最佳方法? - Best way to read a text data file with key=“value” format? 将数据保存到文件的最佳方法? - Best way to save data to a file? 节省内存并将大型numpy数组保存到h5py文件的最佳方法是什么? - what is the best way to save the memory and save large numpy array to the h5py file? 用numpy保存要读取的二进制文件的最安全方法 - Safest way to save binary file with numpy to be read 读取具有定义格式的二进制文件的最快方法? - Fastest way to read a binary file with a defined format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM