tf.train.experimental.PythonState
| Просмотреть исходный код на GitHub |
Mixin для хранения состояния Python в объектно-ориентированном контрольном пункте.
Это абстрактный класс, который позволяет расширять объектно-ориентированную систему контрольных пунктов TensorFlow (см. tf.train.Checkpoint). Например, оболочка для массивов NumPy:
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()
Экземпляры NumpyWrapper являются сохраняемыми объектами и будут сохраняться и восстанавливаться из контрольных пунктов вместе с состоянием TensorFlow, таким как переменные.
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
Методы
deserialize
@abc.abstractmethod
deserialize(
string_value
)
Обработчик для десериализации объекта.
serialize
@abc.abstractmethod serialize()
Обработчик для сериализации объекта. Возвращает строку.
© 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/train/experimental/PythonState