tf.contrib.checkpoint.NumpyState
Stay organized with collections
Save and categorize content based on your preferences.
A trackable object whose NumPy array attributes are saved/restored.
Inherits From: CheckpointableBase
Example usage:
arrays = tf.contrib.checkpoint.NumpyState()
checkpoint = tf.train.Checkpoint(numpy_arrays=arrays)
arrays.x = numpy.zeros([3, 4])
save_path = checkpoint.save("/tmp/ckpt")
arrays.x[1, 1] = 4.
checkpoint.restore(save_path)
assert (arrays.x == numpy.zeros([3, 4])).all()
second_checkpoint = tf.train.Checkpoint(
numpy_arrays=tf.contrib.checkpoint.NumpyState())
# Attributes of NumpyState objects are created automatically by restore()
second_checkpoint.restore(save_path)
assert (second_checkpoint.numpy_arrays.x == numpy.zeros([3, 4])).all()
Note that NumpyState
objects re-create the attributes of the previously
saved object on restore()
. This is in contrast to TensorFlow variables, for
which a Variable
object must be created and assigned to an attribute.
This snippet works both when graph building and when executing eagerly. On
save, the NumPy array(s) are fed as strings to be saved in the checkpoint (via
a placeholder when graph building, or as a string constant when executing
eagerly). When restoring they skip the TensorFlow graph entirely, and so no
restore ops need be run. This means that restoration always happens eagerly,
rather than waiting for checkpoint.restore(...).run_restore_ops()
like
TensorFlow variables when graph building.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.contrib.checkpoint.NumpyState\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/checkpoint/python/python_state.py#L34-L126) |\n\nA trackable object whose NumPy array attributes are saved/restored.\n\nInherits From: [`CheckpointableBase`](../../../tf/contrib/checkpoint/CheckpointableBase)\n\n#### Example usage:\n\n arrays = tf.contrib.checkpoint.NumpyState()\n checkpoint = tf.train.Checkpoint(numpy_arrays=arrays)\n arrays.x = numpy.zeros([3, 4])\n save_path = checkpoint.save(\"/tmp/ckpt\")\n arrays.x[1, 1] = 4.\n checkpoint.restore(save_path)\n assert (arrays.x == numpy.zeros([3, 4])).all()\n\n second_checkpoint = tf.train.Checkpoint(\n numpy_arrays=tf.contrib.checkpoint.NumpyState())\n # Attributes of NumpyState objects are created automatically by restore()\n second_checkpoint.restore(save_path)\n assert (second_checkpoint.numpy_arrays.x == numpy.zeros([3, 4])).all()\n\nNote that `NumpyState` objects re-create the attributes of the previously\nsaved object on `restore()`. This is in contrast to TensorFlow variables, for\nwhich a `Variable` object must be created and assigned to an attribute.\n\nThis snippet works both when graph building and when executing eagerly. On\nsave, the NumPy array(s) are fed as strings to be saved in the checkpoint (via\na placeholder when graph building, or as a string constant when executing\neagerly). When restoring they skip the TensorFlow graph entirely, and so no\nrestore ops need be run. This means that restoration always happens eagerly,\nrather than waiting for `checkpoint.restore(...).run_restore_ops()` like\nTensorFlow variables when graph building."]]