tf.io.serialize_tensor
Stay organized with collections
Save and categorize content based on your preferences.
Transforms a Tensor into a serialized TensorProto proto.
tf.io.serialize_tensor(
tensor, name=None
)
This operation transforms data in a tf.Tensor
into a tf.Tensor
of type
tf.string
containing the data in a binary string in little-endian 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:
tf.io.parse_tensor
: inverse operation of tf.io.serialize_tensor
that
transforms a scalar string containing a serialized Tensor in little-endian
format into a Tensor of a specified type.
tf.ensure_shape
: parse_tensor
cannot statically determine the shape of
the parsed tensor. Use tf.ensure_shape
to set the static shape when running
under a tf.function
.SerializeToString
, serializes a proto to a binary-string
Example of serializing scalar data:
t = tf.constant(1)
tf.io.serialize_tensor(t)
<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)
nonscalar
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
[7, 8]], dtype=int32)>
Serialize the data using tf.io.serialize_tensor
.
serialized_nonscalar = tf.io.serialize_tensor(nonscalar)
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=tf.train.BytesList(value=[serialized_nonscalar.numpy()]))
feature_of_bytes
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=tf.train.Features(feature=features_for_example))
example_proto
features {
feature {
key: "feature0"
value {
bytes_list {
value: "\010...\000"
}
}
}
}
Args |
tensor
|
A tf.Tensor .
|
name
|
string. Optional name for the op.
|
Returns |
A Tensor of dtype string.
|
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. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.io.serialize_tensor\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/io_ops.py#L137-L214) |\n\nTransforms a Tensor into a serialized TensorProto proto.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.io.serialize_tensor`](https://www.tensorflow.org/api_docs/python/tf/io/serialize_tensor), [`tf.compat.v1.serialize_tensor`](https://www.tensorflow.org/api_docs/python/tf/io/serialize_tensor)\n\n\u003cbr /\u003e\n\n tf.io.serialize_tensor(\n tensor, name=None\n )\n\nThis operation transforms data in a [`tf.Tensor`](../../tf/Tensor) into a [`tf.Tensor`](../../tf/Tensor) of type\n[`tf.string`](../../tf#string) containing the data in a binary string in little-endian format.\nThis operation can transform scalar data and linear arrays, but it is most\nuseful in converting multidimensional arrays into a format accepted by binary\nstorage formats such as a `TFRecord` or [`tf.train.Example`](../../tf/train/Example).\n\n#### See also:\n\n- [`tf.io.parse_tensor`](../../tf/io/parse_tensor): inverse operation of [`tf.io.serialize_tensor`](../../tf/io/serialize_tensor) that transforms a scalar string containing a serialized Tensor in little-endian format into a Tensor of a specified type.\n- [`tf.ensure_shape`](../../tf/ensure_shape): `parse_tensor` cannot statically determine the shape of the parsed tensor. Use [`tf.ensure_shape`](../../tf/ensure_shape) to set the static shape when running under a [`tf.function`](../../tf/function)\n- `.SerializeToString`, serializes a proto to a binary-string\n\nExample of serializing scalar data: \n\n t = tf.constant(1)\n tf.io.serialize_tensor(t)\n \u003ctf.Tensor: shape=(), dtype=string, numpy=b'\\x08...\\x00'\u003e\n\nExample of storing non-scalar data into a [`tf.train.Example`](../../tf/train/Example): \n\n t1 = [[1, 2]]\n t2 = [[7, 8]]\n nonscalar = tf.concat([t1, t2], 0)\n nonscalar\n \u003ctf.Tensor: shape=(2, 2), dtype=int32, numpy=\n array([[1, 2],\n [7, 8]], dtype=int32)\u003e\n\nSerialize the data using [`tf.io.serialize_tensor`](../../tf/io/serialize_tensor). \n\n serialized_nonscalar = tf.io.serialize_tensor(nonscalar)\n serialized_nonscalar\n \u003ctf.Tensor: shape=(), dtype=string, numpy=b'\\x08...\\x00'\u003e\n\nStore the data in a [`tf.train.Feature`](../../tf/train/Feature). \n\n feature_of_bytes = tf.train.Feature(\n bytes_list=tf.train.BytesList(value=[serialized_nonscalar.numpy()]))\n feature_of_bytes\n bytes_list {\n value: \"\\010...\\000\"\n }\n\nPut the [`tf.train.Feature`](../../tf/train/Feature) message into a [`tf.train.Example`](../../tf/train/Example). \n\n features_for_example = {\n 'feature0': feature_of_bytes\n }\n example_proto = tf.train.Example(\n features=tf.train.Features(feature=features_for_example))\n example_proto\n features {\n feature {\n key: \"feature0\"\n value {\n bytes_list {\n value: \"\\010...\\000\"\n }\n }\n }\n }\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|-----------------------------------|\n| `tensor` | A [`tf.Tensor`](../../tf/Tensor). |\n| `name` | string. Optional name for the op. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A Tensor of dtype string. ||\n\n\u003cbr /\u003e"]]