김종현
김종현

Reputation: 151

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). 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 ?? 

Upvotes: 11

Views: 14665

Answers (1)

Daniel Trebbien
Daniel Trebbien

Reputation: 39208

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)

.. where the second argument ('my-checkpoints' in the above example) is the path to a directory in which the checkpoint binary files are stored.

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)

Upvotes: 3

Related Questions