Adds type_spec_from_value, which can be used to get the TypeSpec for a Tensor or CompositeTensor value.

PiperOrigin-RevId: 314197441
Change-Id: I20c7034fcae6e70bc994297b84f4737d1794d859
This commit is contained in:
Edward Loper 2020-06-01 13:56:03 -07:00 committed by TensorFlower Gardener
parent a2ac929f45
commit 9985d45d95
3 changed files with 27 additions and 1 deletions

View File

@ -503,11 +503,29 @@ class BatchableTypeSpec(TypeSpec):
return tensor_list
@tf_export("type_spec_from_value")
def type_spec_from_value(value):
"""Returns a `TypeSpec` that represents the given `value`.
"""Returns a `tf.TypeSpec` that represents the given `value`.
Examples:
>>> tf.type_spec_from_value(tf.constant([1, 2, 3]))
TensorSpec(shape=(3,), dtype=tf.int32, name=None)
>>> tf.type_spec_from_value(np.array([4.0, 5.0], np.float64))
TensorSpec(shape=(2,), dtype=tf.float64, name=None)
>>> tf.type_spec_from_value(tf.ragged.constant([[1, 2], [3, 4, 5]]))
RaggedTensorSpec(TensorShape([2, None]), tf.int32, 1, tf.int64)
>>> example_input = tf.ragged.constant([[1, 2], [3]])
>>> @tf.function(input_signature=[tf.type_spec_from_value(example_input)])
... def f(x):
... return tf.reduce_sum(x, axis=1)
Args:
value: A value that can be accepted or returned by TensorFlow APIs.
Accepted types for `value` include `tf.Tensor`, any value that can be
converted to `tf.Tensor` using `tf.convert_to_tensor`, and any subclass
of `CompositeTensor` (such as `tf.RaggedTensor`).
Returns:
A `TypeSpec` that is compatible with `value`.

View File

@ -2432,6 +2432,10 @@ tf_module {
name: "tuple"
argspec: "args=[\'tensors\', \'name\', \'control_inputs\'], varargs=None, keywords=None, defaults=[\'None\', \'None\'], "
}
member_method {
name: "type_spec_from_value"
argspec: "args=[\'value\'], varargs=None, keywords=None, defaults=None"
}
member_method {
name: "unique"
argspec: "args=[\'x\', \'out_idx\', \'name\'], varargs=None, keywords=None, defaults=[\"<dtype: \'int32\'>\", \'None\'], "

View File

@ -1108,6 +1108,10 @@ tf_module {
name: "tuple"
argspec: "args=[\'tensors\', \'control_inputs\', \'name\'], varargs=None, keywords=None, defaults=[\'None\', \'None\'], "
}
member_method {
name: "type_spec_from_value"
argspec: "args=[\'value\'], varargs=None, keywords=None, defaults=None"
}
member_method {
name: "unique"
argspec: "args=[\'x\', \'out_idx\', \'name\'], varargs=None, keywords=None, defaults=[\"<dtype: \'int32\'>\", \'None\'], "