Example #1
0
    def __init__(self, label=None, min_value=None, max_value=None, **kwargs):
        if not label:
            prefix = 'C'
            name = prefix + '_' + str(backend.get_uid(prefix))
        else:
            name = label

        dtype = backend.floatx()

        super(ConstantLayer, self).__init__(dtype=dtype, name=name)

        # backend.random_uniform_variable(shape=(20,), low=[0.]*20, high=[1.]*20)
        self._c = self.add_weight(name=label,
                                  shape=(1, len(min_value)),
                                  initializer=K.initializers.RandomUniform(
                                      minval=min_value, maxval=max_value),
                                  trainable=True)
        self.is_placeholder = False
        self.built = True
        self._batch_input_shape = (len(min_value), )
        #self.batch_size = len(min_value)

        #graph = backend.get_graph()
        #with graph.as_default():
        #    fake_input_tensor = backend.constant(backend.get_value(self._c))
        # Create an input node to add to self.outbound_node
        # and set output_tensors' _keras_history.
        fake_input_tensor = self._c
        fake_input_tensor._keras_history = base_layer.KerasHistory(self, 0, 0)
        fake_input_tensor._keras_mask = None
        fake_input_tensor._ltn_doms = []

        node_module.Node(self,
                         inbound_layers=[],
                         node_indices=[],
                         tensor_indices=[],
                         input_tensors=[fake_input_tensor],
                         output_tensors=[fake_input_tensor])
Example #2
0
    def __init__(self,
                 input_shape=None,
                 batch_size=None,
                 dtype=None,
                 input_tensor=None,
                 sparse=False,
                 name=None,
                 ragged=False,
                 **kwargs):
        strategy = distribution_strategy_context.get_strategy()
        if strategy and batch_size is not None and \
            distributed_training_utils.global_batch_size_supported(strategy):
            if batch_size % strategy.num_replicas_in_sync != 0:
                raise ValueError(
                    'The `batch_size` argument value {} cannot be '
                    'divisible by number of replicas {}'.format(
                        batch_size, strategy.num_replicas_in_sync))
            batch_size = batch_size // strategy.num_replicas_in_sync

        if 'batch_input_shape' in kwargs:
            batch_input_shape = kwargs.pop('batch_input_shape')
            if input_shape and batch_input_shape:
                raise ValueError('Only provide the input_shape OR '
                                 'batch_input_shape argument to '
                                 'InputLayer, not both at the same time.')
            batch_size = batch_input_shape[0]
            input_shape = batch_input_shape[1:]
        if kwargs:
            raise ValueError('Unrecognized keyword arguments:', kwargs.keys())

        if not name:
            prefix = 'input'
            name = prefix + '_' + str(backend.get_uid(prefix))

        if not dtype:
            if input_tensor is None:
                dtype = backend.floatx()
            else:
                dtype = backend.dtype(input_tensor)
        elif input_tensor is not None and input_tensor.dtype != dtype:
            raise ValueError(
                '`input_tensor.dtype` differs from `dtype`: %s vs. %s' %
                (input_tensor.dtype, dtype))
        super(InputLayer, self).__init__(dtype=dtype, name=name)
        self.built = True
        self.sparse = sparse
        self.batch_size = batch_size
        self.supports_masking = True

        if isinstance(input_shape, tensor_shape.TensorShape):
            input_shape = tuple(input_shape.as_list())
        elif isinstance(input_shape, int):
            input_shape = (input_shape, )

        if input_tensor is None:
            if input_shape is not None:
                batch_input_shape = (batch_size, ) + tuple(input_shape)
            else:
                batch_input_shape = None
            graph = backend.get_graph()
            with graph.as_default():
                input_tensor = backend.placeholder(shape=batch_input_shape,
                                                   dtype=dtype,
                                                   name=self.name,
                                                   sparse=sparse,
                                                   ragged=ragged)

            self.is_placeholder = True
            self._batch_input_shape = batch_input_shape
        else:
            if not tf_utils.is_symbolic_tensor(input_tensor):
                raise ValueError(
                    'You should not pass an EagerTensor to `Input`. '
                    'For example, instead of creating an '
                    'InputLayer, you should instantiate your model and '
                    'directly call it on your input.')
            self.is_placeholder = False
            self._batch_input_shape = tuple(input_tensor.shape.as_list())

        # Create an input node to add to self.outbound_node
        # and set output_tensors' _keras_history.
        input_tensor._keras_history = base_layer.KerasHistory(self, 0, 0)
        input_tensor._keras_mask = None
        node_module.Node(self,
                         inbound_layers=[],
                         node_indices=[],
                         tensor_indices=[],
                         input_tensors=[input_tensor],
                         output_tensors=[input_tensor])
Example #3
0
    def __init__(self,
                 input_shape=None,
                 batch_size=None,
                 dtype=None,
                 input_tensor=None,
                 sparse=False,
                 name=None,
                 **kwargs):
        if 'batch_input_shape' in kwargs:
            batch_input_shape = kwargs.pop('batch_input_shape')
            if input_shape and batch_input_shape:
                raise ValueError('Only provide the input_shape OR '
                                 'batch_input_shape argument to '
                                 'InputLayer, not both at the same time.')
            batch_size = batch_input_shape[0]
            input_shape = batch_input_shape[1:]
        if kwargs:
            raise ValueError('Unrecognized keyword arguments:', kwargs.keys())

        if not name:
            prefix = 'input'
            name = prefix + '_' + str(backend.get_uid(prefix))

        if not dtype:
            if input_tensor is None:
                dtype = backend.floatx()
            else:
                dtype = backend.dtype(input_tensor)
        elif input_tensor is not None and input_tensor.dtype != dtype:
            raise ValueError(
                '`input_tensor.dtype` differs from `dtype`: %s vs. %s' %
                (input_tensor.dtype, dtype))
        super(InputLayer, self).__init__(dtype=dtype, name=name)
        self.built = True
        self.sparse = sparse
        self.batch_size = batch_size
        self.supports_masking = True

        if isinstance(input_shape, tensor_shape.TensorShape):
            input_shape = tuple(input_shape.as_list())

        if input_tensor is None:
            if input_shape is not None:
                batch_input_shape = (batch_size, ) + tuple(input_shape)
            else:
                batch_input_shape = None
            graph = backend.get_graph()
            with graph.as_default():
                # In graph mode, create a graph placeholder to call the layer on.
                if sparse:
                    input_tensor = backend.placeholder(shape=batch_input_shape,
                                                       dtype=dtype,
                                                       name=self.name,
                                                       sparse=True)
                else:
                    input_tensor = backend.placeholder(shape=batch_input_shape,
                                                       dtype=dtype,
                                                       name=self.name)

            self.is_placeholder = True
            self._batch_input_shape = batch_input_shape
        else:
            if not tf_utils.is_symbolic_tensor(input_tensor):
                raise ValueError(
                    'You should not pass an EagerTensor to `Input`. '
                    'For example, instead of creating an '
                    'InputLayer, you should instantiate your model and '
                    'directly call it on your input.')
            self.is_placeholder = False
            self._batch_input_shape = tuple(input_tensor.get_shape().as_list())

        # Create an input node to add to self.outbound_node
        # and set output_tensors' _keras_history.
        input_tensor._keras_history = base_layer.KerasHistory(self, 0, 0)  # pylint: disable=protected-access
        base_layer.Node(self,
                        inbound_layers=[],
                        node_indices=[],
                        tensor_indices=[],
                        input_tensors=[input_tensor],
                        output_tensors=[input_tensor])