# tf.train.experimental.PythonState

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 2 version](https://www.tensorflow.org/api_docs/python/tf/train/experimental/PythonState) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/training/tracking/python_state.py#L31-L92) |

A mixin for putting Python state in an object-based checkpoint.

Inherits From: [`CheckpointableBase`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/contrib/checkpoint/CheckpointableBase)

#### View aliases


**Main aliases**

\`tf.contrib.checkpoint.PythonStateWrapper\`
**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.train.experimental.PythonState`](https://www.tensorflow.org/api_docs/python/tf/train/experimental/PythonState), \`tf.compat.v2.train.experimental.PythonState\`

<br />

This is an abstract class which allows extensions to TensorFlow's object-based
checkpointing (see [`tf.train.Checkpoint`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/train/Checkpoint)). For example a wrapper for NumPy
arrays:

    import io
    import numpy

    class NumpyWrapper(tf.train.experimental.PythonState):

      def __init__(self, array):
        self.array = array

      def serialize(self):
        string_file = io.BytesIO()
        try:
          numpy.save(string_file, self.array, allow_pickle=False)
          serialized = string_file.getvalue()
        finally:
          string_file.close()
        return serialized

      def deserialize(self, string_value):
        string_file = io.BytesIO(string_value)
        try:
          self.array = numpy.load(string_file, allow_pickle=False)
        finally:
          string_file.close()

Instances of `NumpyWrapper` are checkpointable objects, and will be saved and
restored from checkpoints along with TensorFlow state like variables.

    root = tf.train.Checkpoint(numpy=NumpyWrapper(numpy.array([1.])))
    save_path = root.save(prefix)
    root.numpy.array *= 2.
    assert [2.] == root.numpy.array
    root.restore(save_path)
    assert [1.] == root.numpy.array

## Methods

### `deserialize`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/training/tracking/python_state.py#L81-L83)

    @abc.abstractmethod
    deserialize(
        string_value
    )

Callback to deserialize the object.

### `serialize`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/training/tracking/python_state.py#L77-L79)

    @abc.abstractmethod
    serialize()

Callback to serialize the object. Returns a string.