Describes a tf.Tensor. Metadata for describing the `tf.Tensor` objects accepted or returned by some TensorFlow APIs.
| 16 | |
| 17 | |
| 18 | class TensorSpec(object): |
| 19 | """Describes a tf.Tensor. |
| 20 | |
| 21 | Metadata for describing the `tf.Tensor` objects accepted or returned |
| 22 | by some TensorFlow APIs. |
| 23 | """ |
| 24 | |
| 25 | __slots__ = ["_shape", "_shape_tuple", "_dtype", "_name"] |
| 26 | |
| 27 | def __init__(self, shape, dtype=dtypes.float32, name=None): |
| 28 | """Creates a TensorSpec. |
| 29 | |
| 30 | Args: |
| 31 | shape: Value convertible to `tf.TensorShape`. The shape of the tensor. |
| 32 | dtype: Value convertible to `tf.DType`. The type of the tensor values. |
| 33 | name: Optional name for the Tensor. |
| 34 | |
| 35 | Raises: |
| 36 | TypeError: If shape is not convertible to a `tf.TensorShape`, or dtype is |
| 37 | not convertible to a `tf.DType`. |
| 38 | """ |
| 39 | self._shape = tensor_shape.TensorShape(shape) |
| 40 | try: |
| 41 | self._shape_tuple = tuple(self.shape.as_list()) |
| 42 | except ValueError: |
| 43 | self._shape_tuple = None |
| 44 | self._dtype = dtypes.as_dtype(dtype) |
| 45 | self._name = name |
| 46 | |
| 47 | @classmethod |
| 48 | def from_spec(cls, spec, name=None): |
| 49 | return cls(spec.shape, spec.dtype, name or spec.name) |
| 50 | |
| 51 | @classmethod |
| 52 | def from_tensor(cls, tensor, name=None): |
| 53 | if isinstance(tensor, ops.EagerTensor): |
| 54 | return TensorSpec(tensor.shape, tensor.dtype, name) |
| 55 | elif isinstance(tensor, ops.Tensor): |
| 56 | return TensorSpec(tensor.shape, tensor.dtype, name or tensor.op.name) |
| 57 | else: |
| 58 | raise ValueError("`tensor` should be a tf.Tensor") |
| 59 | |
| 60 | @property |
| 61 | def shape(self): |
| 62 | """Returns the `TensorShape` that represents the shape of the tensor.""" |
| 63 | return self._shape |
| 64 | |
| 65 | @property |
| 66 | def dtype(self): |
| 67 | """Returns the `dtype` of elements in the tensor.""" |
| 68 | return self._dtype |
| 69 | |
| 70 | @property |
| 71 | def name(self): |
| 72 | """Returns the (optionally provided) name of the described tensor.""" |
| 73 | return self._name |
| 74 | |
| 75 | def is_compatible_with(self, spec_or_tensor): |
no outgoing calls
no test coverage detected
searching dependent graphs…