Python keras.initializers.Zeros() Examples

The following are 8 code examples of keras.initializers.Zeros(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module keras.initializers , or try the search function .
Example #1
Source File: layer_normalization.py    From keras-utility-layer-collection with MIT License 5 votes vote down vote up
def build(self, input_shape):
        self._g = self.add_weight(
            name='gain', 
            shape=(input_shape[-1],),
            initializer=Ones(),
            trainable=True
        )
        self._b = self.add_weight(
            name='bias', 
            shape=(input_shape[-1],),
            initializer=Zeros(),
            trainable=True
        ) 
Example #2
Source File: layers.py    From BERT with Apache License 2.0 5 votes vote down vote up
def build(self, input_shape):
        self.gamma = self.add_weight(name='gamma', shape=input_shape[-1:], initializer=Ones(), trainable=True)
        self.beta = self.add_weight(name='beta', shape=input_shape[-1:], initializer=Zeros(), trainable=True)
        super().build(input_shape) 
Example #3
Source File: layers.py    From BERT-keras with GNU General Public License v3.0 5 votes vote down vote up
def build(self, input_shape):
        self.gamma = self.add_weight(name='gamma', shape=input_shape[-1:], initializer=Ones(), trainable=True)
        self.beta = self.add_weight(name='beta', shape=input_shape[-1:], initializer=Zeros(), trainable=True)
        super().build(input_shape) 
Example #4
Source File: sequence_blocks.py    From Neural-Chatbot with GNU General Public License v3.0 5 votes vote down vote up
def build(self, input_shape):
        assert len(input_shape) >= 3
        self.input_spec = [InputSpec(shape=input_shape)]

        if not self.layer.built:
            self.layer.build(input_shape)
            self.layer.built = True

        super(AttentionWrapper, self).build()

        if hasattr(self.attention_vec, '_keras_shape'):
            attention_dim = self.attention_vec._keras_shape[1]
        else:
            raise Exception(
                'Layer could not be build: No information about expected input shape.')

        kernel_initializer = self.layer.kernel_initializer
        self.U_a = self.layer.add_weight((self.layer.units, self.layer.units), name='{}_U_a'.format(
            self.name), initializer=kernel_initializer)
        self.b_a = self.layer.add_weight(
            (self.layer.units,), name='{}_b_a'.format(self.name), initializer=Zeros())

        self.U_m = self.layer.add_weight((attention_dim, self.layer.units), name='{}_U_m'.format(
            self.name), initializer=kernel_initializer)
        self.b_m = self.layer.add_weight(
            (self.layer.units,), name='{}_b_m'.format(self.name), initializer=Zeros())

        if self.single_attention_param:
            self.U_s = self.layer.add_weight((self.layer.units, 1), name='{}_U_s'.format(
                self.name), initializer=kernel_initializer)
            self.b_s = self.layer.add_weight(
                (1,), name='{}_b_s'.format(self.name), initializer=Zeros())
        else:
            self.U_s = self.layer.add_weight((self.layer.units, self.layer.units), name='{}_U_s'.format(
                self.name), initializer=kernel_initializer)
            self.b_s = self.layer.add_weight(
                (self.layer.units,), name='{}_b_s'.format(self.name), initializer=Zeros()) 
Example #5
Source File: engine.py    From recurrentshop with MIT License 5 votes vote down vote up
def reset_states(self, states_value=None):
        if len(self.states) == 0:
            return
        if not self.stateful:
            raise AttributeError('Layer must be stateful.')
        if not hasattr(self, 'states') or self.states[0] is None:
            state_shapes = list(map(K.int_shape, self.model.input[1:]))
            self.states = list(map(K.zeros, state_shapes))

        if states_value is not None:
            if type(states_value) not in (list, tuple):
                states_value = [states_value] * len(self.states)
            assert len(states_value) == len(self.states), 'Your RNN has ' + str(len(self.states)) + ' states, but was provided ' + str(len(states_value)) + ' state values.'
            if 'numpy' not in type(states_value[0]):
                states_value = list(map(np.array, states_value))
            if states_value[0].shape == tuple():
                for state, val in zip(self.states, states_value):
                    K.set_value(state, K.get_value(state) * 0. + val)
            else:
                for state, val in zip(self.states, states_value):
                    K.set_value(state, val)
        else:
            if self.state_initializer:
                for state, init in zip(self.states, self.state_initializer):
                    if isinstance(init, initializers.Zeros):
                        K.set_value(state, 0 * K.get_value(state))
                    else:
                        K.set_value(state, K.eval(init(K.get_value(state).shape)))
            else:
                for state in self.states:
                    K.set_value(state, 0 * K.get_value(state))

    # EXECUTION 
Example #6
Source File: core.py    From transformer-keras with Apache License 2.0 5 votes vote down vote up
def build(self, input_shape):
        self.gamma = self.add_weight(name='gamma', shape=input_shape[-1:],
                                     initializer=Ones(), trainable=True)
        self.beta = self.add_weight(name='beta', shape=input_shape[-1:],
                                    initializer=Zeros(), trainable=True)
        super(LayerNormalization, self).build(input_shape) 
Example #7
Source File: engine.py    From recurrentshop with MIT License 4 votes vote down vote up
def get_initial_state(self, inputs):
        if type(self.model.input) is not list:
            return []
        try:
            batch_size = K.int_shape(inputs)[0]
        except:
            batch_size = None
        state_shapes = list(map(K.int_shape, self.model.input[1:]))
        states = []
        if self.readout:
            state_shapes.pop()
            # default value for initial_readout is handled in call()
        for shape in state_shapes:
            if None in shape[1:]:
                raise Exception('Only the batch dimension of a state can be left unspecified. Got state with shape ' + str(shape))
            if shape[0] is None:
                ndim = K.ndim(inputs)
                z = K.zeros_like(inputs)
                slices = [slice(None)] + [0] * (ndim - 1)
                z = z[slices]  # (batch_size,)
                state_ndim = len(shape)
                z = K.reshape(z, (-1,) + (1,) * (state_ndim - 1))
                z = K.tile(z, (1,) + tuple(shape[1:]))
                states.append(z)
            else:
                states.append(K.zeros(shape))
        state_initializer = self.state_initializer
        if state_initializer:
            # some initializers don't accept symbolic shapes
            for i in range(len(state_shapes)):
                if state_shapes[i][0] is None:
                    if hasattr(self, 'batch_size'):
                        state_shapes[i] = (self.batch_size,) + state_shapes[i][1:]
                if None in state_shapes[i]:
                    state_shapes[i] = K.shape(states[i])
            num_state_init = len(state_initializer)
            num_state = self.num_states
            assert num_state_init == num_state, 'RNN has ' + str(num_state) + ' states, but was provided ' + str(num_state_init) + ' state initializers.'
            for i in range(len(states)):
                init = state_initializer[i]
                shape = state_shapes[i]
                try:
                    if not isinstance(init, initializers.Zeros):
                        states[i] = init(shape)
                except:
                    raise Exception('Seems the initializer ' + init.__class__.__name__ + ' does not support symbolic shapes(' + str(shape) + '). Try providing the full input shape (include batch dimension) for you RecurrentModel.')
        return states 
Example #8
Source File: graph_emb.py    From ccg2lambda with Apache License 2.0 4 votes vote down vote up
def tp1_node_update(graph_node_embs, node_rel, node_rel_weight, max_nodes, max_bi_relations, embed_dim, label):
    """
    graph_node_embs has shape (batch_size, max_nodes per graph, embed_dim feats).
    """
    dense_dim = embed_dim

    x = gather_layer([graph_node_embs, node_rel])
    logging.debug('After gather3 shape: {0}'.format(x.shape))

    x = Reshape((max_nodes * max_bi_relations, 2 * embed_dim))(x)

    x = TimeDistributed(
        Dense(
            dense_dim,
            kernel_initializer=initializers.Ones(),
            bias_initializer=initializers.Zeros(),
            name=label + '_dense1'))(x)
    # TODO: re-enable the batch normalization.
    # x = BatchNormalization(axis=2, name=label + '_bn1')(x)
    x = Activation('relu')(x)
    x = TimeDistributed(
        Dense(
            dense_dim,
            kernel_initializer=initializers.Ones(),
            bias_initializer=initializers.Zeros(),
            name=label + '_dense2'))(x)
    # x = BatchNormalization(axis=2, name=label + '_bn2')(x)
    x = Activation('relu')(x)

    normalizer = Reshape((max_nodes * max_bi_relations,))(node_rel_weight)
    normalizer = RepeatVector(dense_dim)(normalizer)
    normalizer = Permute((2, 1))(normalizer)

    x = Multiply()([x, normalizer])
    x = Reshape((max_nodes, max_bi_relations, dense_dim))(x)

    x = Lambda(
        lambda xin: K.sum(xin, axis=2),
        output_shape=(None, max_nodes * max_bi_relations, dense_dim),
        name=label + '_integrate')(x)
    return x

# TODO: Dense use_bias=True