Ejemplo n.º 1
0
 def build(self, input_shape):
     input_shape = tensor_shape.TensorShape(input_shape)
     if input_shape[-1].value is None:
         raise ValueError('The last dimension of the inputs to `Dense` '
                          'should be defined. Found `None`.')
     self.input_spec = base.InputSpec(min_ndim=2,
                                      axes={-1: input_shape[-1].value})
     self.kernel = self.add_variable(
         'kernel',
         shape=[input_shape[-1].value, self.units],
         initializer=self.kernel_initializer,
         regularizer=self.kernel_regularizer,
         dtype=self.dtype,
         trainable=True)
     if self.use_bias:
         self.bias = self.add_variable('bias',
                                       shape=[
                                           self.units,
                                       ],
                                       initializer=self.bias_initializer,
                                       regularizer=self.bias_regularizer,
                                       dtype=self.dtype,
                                       trainable=True)
     else:
         self.bias = None
     self.built = True
Ejemplo n.º 2
0
    def build(self, input_shape):
        input_shape = tensor_shape.TensorShape(input_shape)
        if not input_shape.ndims:
            raise ValueError('Input has undefined rank:', input_shape)
        ndim = input_shape.ndims
        if self.axis < 0:
            axis = ndim + self.axis
        else:
            axis = self.axis
        if axis < 0 or axis >= ndim:
            raise ValueError('Value of `axis` argument ' + str(self.axis) +
                             ' is out of range for input with rank ' + str(ndim))
        if axis + 1 == ndim:
            self._data_format = 'NHWC'
        elif axis == 1:
            self._data_format = 'NCHW'
        else:
            raise ValueError(
                'Only axis 1 or last axis are currently supported dimensions for '
                    'batch norm. Got `axis` dimension: ', axis)

        param_dim = input_shape[axis]
        if not param_dim.value:
            raise ValueError('Input has undefined `axis` dimension. Input shape: ', input_shape)
        self.input_spec = base.InputSpec(ndim=ndim, axes={self.axis: param_dim.value})

        self.moving_mean = self.add_variable(
            name='moving_mean',
            shape=(param_dim.value,),
            initializer=self.moving_mean_initializer,
            trainable=False)

        self.moving_variance = self.add_variable(
            name='moving_variance',
            shape=(param_dim.value,),
            initializer=self.moving_variance_initializer,
            trainable=False)

        self.gamma = self.add_variable(
            name='gamma',
            shape=(param_dim.value,),
            initializer=self.gamma_initializer,
            regularizer=self.gamma_regularizer,
            trainable=self.scale)

        self.beta = self.add_variable(
            name='beta',
            shape=(param_dim.value,),
            initializer=self.beta_initializer,
            regularizer=self.beta_regularizer,
            trainable=self.center)

        self.built = True
Ejemplo n.º 3
0
    def build(self, input_shape):
        input_shape = tensor_shape.TensorShape(input_shape)

        if self.data_format == 'channels_first':
            channel_axis = 1
        else:
            channel_axis = -1

        if input_shape[channel_axis].value is None:
            raise ValueError('The channel dimension of the inputs '
                             'should be defined. Found `None`.')

        input_dim = input_shape[channel_axis].value

        if self.data_format == 'channels_first':
            # For channels first: (n_out, n_in, k_h, k_w)
            kernel_shape = (self.filters, input_dim) + self.kernel_size
        else:
            # For channels last: (k_h, k_w, n_in, n_out)
            kernel_shape = self.kernel_size + (input_dim, self.filters)

        self.kernel = self.add_variable(
            name='kernel',
            shape=kernel_shape,
            initializer=self.kernel_initializer,
            regularizer=self.kernel_regularizer,
            dtype=self.dtype,
        )

        if self.use_bias:
            self.bias = self.add_variable(
                name='bias',
                shape=(self.filters, ),
                initializer=self.bias_initializer,
                regularizer=self.bias_regularizer,
                dtype=self.dtype,
            )
        else:
            self.bias = None

        self.input_spec = base.InputSpec(ndim=self.rank + 2,
                                         axes={channel_axis: input_dim})
        self.built = True