Exemplo n.º 1
0
    def create_variables(self, input_shape):
        input_shape = tf.TensorShape(input_shape)

        if input_shape.ndims is None:
            raise WeightInitializationError(
                "Cannot initialize variables for the batch normalization "
                "layer, because input shape is undefined. Layer: {}"
                "".format(self))

        if self.axes is None:
            # If ndims == 4 then axes = (0, 1, 2)
            # If ndims == 2 then axes = (0,)
            self.axes = tuple(range(input_shape.ndims - 1))

        if any(axis >= input_shape.ndims for axis in self.axes):
            raise LayerConnectionError(
                "Batch normalization cannot be applied over one of "
                "the axis, because input has only {} dimensions. Layer: {}"
                "".format(input_shape.ndims, self))

        parameter_shape = tuple([
            input_shape[axis].value if axis not in self.axes else 1
            for axis in range(input_shape.ndims)
        ])

        if any(parameter is None for parameter in parameter_shape):
            unknown_dim_index = parameter_shape.index(None)

            raise WeightInitializationError(
                "Cannot create variables for batch normalization, because "
                "input has unknown dimension #{} (0-based indices). "
                "Input shape: {}, Layer: {}".format(unknown_dim_index,
                                                    input_shape, self))

        self.input_shape = input_shape
        self.running_mean = self.variable(value=self.running_mean,
                                          shape=parameter_shape,
                                          name='running_mean',
                                          trainable=False)

        self.running_inv_std = self.variable(value=self.running_inv_std,
                                             shape=parameter_shape,
                                             name='running_inv_std',
                                             trainable=False)

        self.gamma = self.variable(value=self.gamma,
                                   name='gamma',
                                   shape=parameter_shape)

        self.beta = self.variable(value=self.beta,
                                  name='beta',
                                  shape=parameter_shape)
Exemplo n.º 2
0
    def init_weights(self, X_train):
        if self.initialized:
            raise WeightInitializationError(
                "Weights have been already initialized")

        weight_initializer = self.weight
        self.weight = weight_initializer(X_train, self.features_grid)
        self.initialized = True

        if self.distance.name == 'cosine':
            self.weight /= np.linalg.norm(self.weight, axis=0)
Exemplo n.º 3
0
    def __init__(self, **options):
        super(BaseAssociative, self).__init__(**options)

        if self.n_outputs is None and self.features_grid is None:
            raise ValueError("One of the following parameters has to be "
                             "specified: n_outputs, features_grid")

        elif self.n_outputs is None:
            self.n_outputs = np.prod(self.features_grid)

        n_grid_elements = np.prod(self.features_grid)
        invalid_feature_grid = (
            self.features_grid is not None and
            n_grid_elements != self.n_outputs)

        if invalid_feature_grid:
            raise ValueError(
                "Feature grid should contain the same number of elements "
                "as in the output layer: {0}, but found: {1} (shape: {2})"
                "".format(
                    self.n_outputs,
                    n_grid_elements,
                    self.features_grid))

        if self.features_grid is None:
            self.features_grid = (self.n_outputs, 1)

        if len(self.features_grid) > 2 and self.grid_type.name == 'hexagon':
            raise ValueError("SOFM with hexagon grid type should have "
                             "one or two dimensional feature grid, but got "
                             "{}d instead (shape: {!r})".format(
                                len(self.features_grid),
                                self.features_grid))

        is_pca_init = (
            isinstance(options.get('weight'), six.string_types) and
            options.get('weight') == 'init_pca'
        )

        self.initialized = False
        if not callable(self.weight):
            super(Kohonen, self).init_weights()
            self.initialized = True

            if self.distance.name == 'cosine':
                self.weight /= np.linalg.norm(self.weight, axis=0)

        elif is_pca_init and self.grid_type.name != 'rectangle':
            raise WeightInitializationError(
                "Cannot apply PCA weight initialization for non-rectangular "
                "grid. Grid type: {}".format(self.grid_type.name))
Exemplo n.º 4
0
    def create_variables(self, input_shape):
        n_channels = input_shape[3]

        if n_channels.value is None:
            raise WeightInitializationError(
                "Cannot initialize variables when number of "
                "channels is unknown. Input shape: {}, Layer: {}"
                "".format(input_shape, self))

        parameter_shape = (1, 1, 1, n_channels)

        self.gamma = self.variable(value=self.gamma,
                                   name='gamma',
                                   shape=parameter_shape)

        self.beta = self.variable(value=self.beta,
                                  name='beta',
                                  shape=parameter_shape)
Exemplo n.º 5
0
    def create_variables(self, input_shape):
        if self.n_units is None:
            return

        input_shape = tf.TensorShape(input_shape)
        self.input_shape = input_shape
        _, n_input_features = input_shape

        if n_input_features.value is None:
            raise WeightInitializationError(
                "Cannot create variables for the layer `{}`, because "
                "number of input features is unknown. Input shape: {}"
                "Layer: {}".format(self.name, input_shape, self))

        self.weight = self.variable(value=self.weight,
                                    name='weight',
                                    shape=as_tuple(n_input_features,
                                                   self.n_units))

        if self.bias is not None:
            self.bias = self.variable(value=self.bias,
                                      name='bias',
                                      shape=as_tuple(self.n_units))