"""Gets the Tensorflow TypeSpec corresponding to the passed dataset.

    Args:
      value: A HDF5Matrix object.

    Returns:
      A tf.TensorSpec.
    """
        if not isinstance(value, HDF5Matrix):
            raise TypeError(
                'Expected value to be a HDF5Matrix, but saw: {}'.format(
                    type(value)))
        return tensor_spec.TensorSpec(shape=value.shape, dtype=value.dtype)


type_spec.register_type_spec_from_value_converter(HDF5Matrix,
                                                  HDF5Matrix._to_type_spec)  # pylint: disable=protected-access


def ask_to_proceed_with_overwrite(filepath):
    """Produces a prompt asking about overwriting a file.

  Arguments:
      filepath: the path to the file to be overwritten.

  Returns:
      True if we can proceed with overwrite, False otherwise.
  """
    overwrite = six.moves.input('[WARNING] %s already exists - overwrite? '
                                '[y/n]' % (filepath)).strip().lower()
    while overwrite not in ('y', 'n'):
        overwrite = six.moves.input('Enter "y" (overwrite) or "n" '
    def from_value(cls, value):
        if isinstance(value, SparseTensor):
            return cls(value.shape, value.dtype)
        if isinstance(value, SparseTensorValue):
            if isinstance(value.values, np.ndarray):
                return cls(value.dense_shape, value.values.dtype)
            else:
                return cls.from_value(SparseTensor.from_value(value))
        else:
            raise TypeError("Expected SparseTensor or SparseTensorValue")


# TODO(b/133606651) Delete the SparseTensor registration when CompositeTensor
# is updated to define a _type_spec field (since registration will be
# automatic).  Do *not* delete the SparseTensorValue registration.
type_spec.register_type_spec_from_value_converter(SparseTensor,
                                                  SparseTensorSpec.from_value)
type_spec.register_type_spec_from_value_converter(SparseTensorValue,
                                                  SparseTensorSpec.from_value)


@tf_export(v1=["convert_to_tensor_or_sparse_tensor"])
def convert_to_tensor_or_sparse_tensor(value, dtype=None, name=None):
    """Converts value to a `SparseTensor` or `Tensor`.

  Args:
    value: A `SparseTensor`, `SparseTensorValue`, or an object whose type has a
      registered `Tensor` conversion function.
    dtype: Optional element type for the returned tensor. If missing, the type
      is inferred from the type of `value`.
    name: Optional name to use if a new `Tensor` is created.
  @property
  def maximum(self):
    """Returns a NumPy array specifying the maximum bounds (inclusive)."""
    return self._maximum

  def __repr__(self):
    s = "BoundedTensorSpec(shape={}, dtype={}, name={}, minimum={}, maximum={})"
    return s.format(self.shape, repr(self.dtype), repr(self.name),
                    repr(self.minimum), repr(self.maximum))

  def __eq__(self, other):
    tensor_spec_eq = super(BoundedTensorSpec, self).__eq__(other)
    return (tensor_spec_eq and np.allclose(self.minimum, other.minimum) and
            np.allclose(self.maximum, other.maximum))

  def __reduce__(self):
    return BoundedTensorSpec, (self._shape, self._dtype, self._minimum,
                               self._maximum, self._name)

  def _serialize(self):
    return (self._shape, self._dtype, self._minimum, self._maximum, self._name)


pywrap_tensorflow.RegisterType("TensorSpec", TensorSpec)


# Note: we do not include Tensor names when constructing TypeSpecs.
type_spec.register_type_spec_from_value_converter(
    ops.Tensor,
    lambda tensor: TensorSpec(tensor.shape, tensor.dtype))
    def _from_components(self, components):
        x, y = components
        return TwoTensors(x, y, self.color)

    def _serialize(self):
        return (self.x_shape, self.x_dtype, self.y_shape, self.y_dtype,
                self.color)

    @classmethod
    def from_value(cls, value):
        return cls(value.x.shape, value.x.dtype, value.y.shape, value.y.dtype,
                   value.color)


type_spec.register_type_spec_from_value_converter(
    TwoTensors, TwoTensorsSpecNoOneDtype.from_value)


class InputLayerTest(keras_parameterized.TestCase):
    @combinations.generate(combinations.combine(mode=['graph', 'eager']))
    def testBasicOutputShapeNoBatchSize(self):
        # Create a Keras Input
        x = input_layer_lib.Input(shape=(32, ), name='input_a')
        self.assertAllEqual(x.shape.as_list(), [None, 32])

        # Verify you can construct and use a model w/ this input
        model = functional.Functional(x, x * 2.0)
        self.assertAllEqual(model(array_ops.ones((3, 32))),
                            array_ops.ones((3, 32)) * 2.0)

    @combinations.generate(combinations.combine(mode=['graph', 'eager']))
Beispiel #5
0
    def from_value(value):
        return NoneTensorSpec()

    def _batch(self, batch_size):
        return NoneTensorSpec()

    def _unbatch(self):
        return NoneTensorSpec()

    def _to_batched_tensor_list(self, value):
        return []

    def _to_legacy_output_types(self):
        return self

    def _to_legacy_output_shapes(self):
        return self

    def _to_legacy_output_classes(self):
        return self

    def most_specific_compatible_shape(self, other):
        if type(self) is not type(other):
            raise ValueError("No TypeSpec is compatible with both %s and %s" %
                             (self, other))
        return self


type_spec.register_type_spec_from_value_converter(type(None),
                                                  NoneTensorSpec.from_value)
Beispiel #6
0
        s = "BoundedTensorSpec(shape={}, dtype={}, name={}, minimum={}, maximum={})"
        return s.format(self.shape, repr(self.dtype), repr(self.name),
                        repr(self.minimum), repr(self.maximum))

    def __eq__(self, other):
        tensor_spec_eq = super(BoundedTensorSpec, self).__eq__(other)
        return (tensor_spec_eq and np.allclose(self.minimum, other.minimum)
                and np.allclose(self.maximum, other.maximum))

    def __hash__(self):
        return hash((self._shape, self.dtype))

    def __reduce__(self):
        return BoundedTensorSpec, (self._shape, self._dtype, self._minimum,
                                   self._maximum, self._name)

    def _serialize(self):
        return (self._shape, self._dtype, self._minimum, self._maximum,
                self._name)


trace_type.register_serializable(BoundedTensorSpec)
_pywrap_utils.RegisterType("TensorSpec", TensorSpec)

# Note: we do not include Tensor names when constructing TypeSpecs.
type_spec.register_type_spec_from_value_converter(
    ops.Tensor, lambda tensor: TensorSpec(tensor.shape, tensor.dtype))

type_spec.register_type_spec_from_value_converter(
    np.ndarray, lambda array: TensorSpec(array.shape, array.dtype))
Beispiel #7
0
    def _from_components(self, components):
        x, y = components
        return TwoTensors(x, y, self.color)

    def _serialize(self):
        return (self.x_shape, self.x_dtype, self.y_shape, self.y_dtype,
                self.color)

    @classmethod
    def from_value(cls, value):
        return cls(value.x.shape, value.x.dtype, value.y.shape, value.y.dtype,
                   value.color)


type_spec.register_type_spec_from_value_converter(TwoTensors,
                                                  TwoTensorsSpec.from_value)


class TwoComposites(object):
    """A simple value type to test TypeSpec.

  Contains two composite tensorstensors (x, y) and a string (color).
  """
    def __init__(self, x, y, color="red"):
        assert isinstance(color, str)
        self.x = ops.convert_to_tensor_or_composite(x)
        self.y = ops.convert_to_tensor_or_composite(y)
        self.color = color


@type_spec.register("tf.TwoCompositesSpec")
Beispiel #8
0
  @property
  def _component_specs(self):
    return [tensor_spec.TensorSpec((), dtypes.variant)]

  def _to_components(self, value):
    return [value._variant_tensor]  # pylint: disable=protected-access

  def _from_components(self, flat_value):
    # pylint: disable=protected-access
    return _OptionalImpl(flat_value[0], self._value_structure)

  @staticmethod
  def from_value(value):
    return OptionalStructure(value.value_structure)

  def _to_legacy_output_types(self):
    return self

  def _to_legacy_output_shapes(self):
    return self

  def _to_legacy_output_classes(self):
    return self


type_spec.register_type_spec_from_value_converter(Optional,
                                                  OptionalStructure.from_value)
type_spec.register_type_spec_from_value_converter(_OptionalImpl,
                                                  OptionalStructure.from_value)
Beispiel #9
0
    def _to_legacy_output_shapes(self):
        return nest.map_structure(lambda s: s._to_legacy_output_shapes(),
                                  self._nested_structure)

    def _to_legacy_output_classes(self):
        return nest.map_structure(lambda s: s._to_legacy_output_classes(),
                                  self._nested_structure)

    def _batch(self, batch_size):
        return NestedStructure(
            nest.map_structure(lambda s: s._batch(batch_size),
                               self._nested_structure))

    def _unbatch(self):
        return NestedStructure(
            nest.map_structure(lambda s: s._unbatch(), self._nested_structure))


type_spec.register_type_spec_from_value_converter(tuple,
                                                  NestedStructure.from_value,
                                                  allow_subclass=True)
type_spec.register_type_spec_from_value_converter(dict,
                                                  NestedStructure.from_value,
                                                  allow_subclass=True)

# Re-register SparseTensorValue -- it's a subclass of tuple, but we don't
# want the NestedStructure registration to take precedence.
type_spec.register_type_spec_from_value_converter(
    sparse_tensor.SparseTensorValue, sparse_tensor.SparseTensorSpec.from_value)