ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more

Transforms a Tensor into a serialized TensorProto proto.

This operation transforms data in a tf.Tensor into a tf.Tensor of type tf.string containing the data in a binary string format. This operation can transform scalar data and linear arrays, but it is most useful in converting multidimensional arrays into a format accepted by binary storage formats such as a TFRecord or tf.train.Example.

See also:

Example of serializing scalar data:

t = tf.constant(1)
<tf.Tensor: shape=(), dtype=string, numpy=b'\x08...\x00'>

Example of storing non-scalar data into a tf.train.Example:

t1 = [[1, 2]]
t2 = [[7, 8]]
nonscalar = tf.concat([t1, t2], 0)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [7, 8]], dtype=int32)>

Serialize the data using

serialized_nonscalar =
<tf.Tensor: shape=(), dtype=string, numpy=b'\x08...\x00'>

Store the data in a tf.train.Feature.

feature_of_bytes = tf.train.Feature(
bytes_list {
  value: "\010...\000"

Put the tf.train.Feature message into a tf.train.Example.

features_for_example = {
  'feature0': feature_of_bytes
example_proto = tf.train.Example(
features {
  feature {
    key: "feature0"
    value {
      bytes_list {
        value: "\010...\000"

tensor A tf.Tensor.