Converts the given `value` to an `EagerTensor`. Note that this function could return cached copies of created constants for performance reasons. Args: value: value to convert to EagerTensor. ctx: value of context.context(). dtype: optional desired dtype of the converted EagerTens
(value, ctx, dtype=None)
| 66 | |
| 67 | |
| 68 | def convert_to_eager_tensor(value, ctx, dtype=None): |
| 69 | """Converts the given `value` to an `EagerTensor`. |
| 70 | |
| 71 | Note that this function could return cached copies of created constants for |
| 72 | performance reasons. |
| 73 | |
| 74 | Args: |
| 75 | value: value to convert to EagerTensor. |
| 76 | ctx: value of context.context(). |
| 77 | dtype: optional desired dtype of the converted EagerTensor. |
| 78 | |
| 79 | Returns: |
| 80 | EagerTensor created from value. |
| 81 | |
| 82 | Raises: |
| 83 | TypeError: if `dtype` is not compatible with the type of t. |
| 84 | """ |
| 85 | if isinstance(value, ops.EagerTensor): |
| 86 | if dtype is not None and value.dtype != dtype: |
| 87 | raise TypeError("Expected tensor with type %r not %r" % ( |
| 88 | dtype, value.dtype)) |
| 89 | return value |
| 90 | if dtype is not None: |
| 91 | try: |
| 92 | dtype = dtype.as_datatype_enum |
| 93 | except AttributeError: |
| 94 | dtype = dtypes.as_dtype(dtype).as_datatype_enum |
| 95 | ctx.ensure_initialized() |
| 96 | return ops.EagerTensor(value, ctx.device_name, dtype) |
| 97 | |
| 98 | |
| 99 | @tf_export(v1=["constant"]) |
no test coverage detected