Python keras.regularizers.activity_l2() Examples

The following are code examples for showing how to use keras.regularizers.activity_l2(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: CAPTCHA-breaking   Author: lllcho   File: test_regularizers.py    MIT License 5 votes vote down vote up
def test_A_reg(self):
        for reg in [regularizers.activity_l1(), regularizers.activity_l2()]:
            model = create_model(activity_reg=reg)
            model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
            model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
            model.evaluate(X_test[test_ids, :], Y_test[test_ids, :], verbose=0) 
Example 2
Project: synthesis-database-public   Author: olivettigroup   File: token_classifier.py    MIT License 5 votes vote down vote up
def build_nn_model(self, model_type='dense_ff', input_dim=1, inner_dim=64, embedding_dim=100, window_size=3):
    model = None

    if model_type == 'dense_ff':
      model = Sequential()
      model.add(Dense(output_dim=inner_dim, input_dim=input_dim, activation="relu"))
      model.add(Dense(output_dim=len(self.token_classes.keys()), activation="softmax"))

    elif model_type == 'hierarchical':
      heuristic_layer = Sequential()
      heuristic_layer.add(Dense(output_dim=inner_dim, input_dim=input_dim, activation="relu", W_regularizer=l2(1.0), activity_regularizer=activity_l2(1.0)))
      heuristic_layer.add(Dropout(0.5))

      embedding_layer = Sequential()
      embedding_layer.add(Dense(output_dim=inner_dim*(window_size+1), input_dim=embedding_dim*(window_size+1), activation="relu", W_regularizer=l2(1.0), activity_regularizer=activity_l2(1.0)))
      embedding_layer.add(Dropout(0.5))

      merge_layer = Merge([heuristic_layer, embedding_layer], mode='concat')

      model = Sequential()
      model.add(merge_layer)
      model.add(Dense(output_dim=len(self.token_classes.keys()), activation="softmax"))

    model.compile(
      optimizer='adam',
      loss='categorical_crossentropy',
      metrics=['categorical_accuracy', self._non_null_accuracy]
    )

    self.model = model
    self.model_type = model_type 
Example 3
Project: workspace_2017   Author: nwiizo   File: test_regularizers.py    MIT License 5 votes vote down vote up
def test_A_reg():
    (X_train, Y_train), (X_test, Y_test), test_ids = get_data()
    for reg in [regularizers.activity_l1(), regularizers.activity_l2()]:
        model = create_model(activity_reg=reg)
        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
        assert len(model.losses) == 1
        model.fit(X_train, Y_train, batch_size=batch_size,
                  nb_epoch=nb_epoch, verbose=0)
        model.evaluate(X_test[test_ids, :], Y_test[test_ids, :], verbose=0)