我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用keras.regularizers.activity_l2()。
def test_dense(): from keras import regularizers from keras import constraints layer_test(core.Dense, kwargs={'output_dim': 3}, input_shape=(3, 2)) layer_test(core.Dense, kwargs={'output_dim': 3, 'W_regularizer': regularizers.l2(0.01), 'b_regularizer': regularizers.l1(0.01), 'activity_regularizer': regularizers.activity_l2(0.01), 'W_constraint': constraints.MaxNorm(1), 'b_constraint': constraints.MaxNorm(1)}, input_shape=(3, 2))
def test_maxout_dense(): from keras import regularizers from keras import constraints layer_test(core.MaxoutDense, kwargs={'output_dim': 3}, input_shape=(3, 2)) layer_test(core.MaxoutDense, kwargs={'output_dim': 3, 'W_regularizer': regularizers.l2(0.01), 'b_regularizer': regularizers.l1(0.01), 'activity_regularizer': regularizers.activity_l2(0.01), 'W_constraint': constraints.MaxNorm(1), 'b_constraint': constraints.MaxNorm(1)}, input_shape=(3, 2))
def test_timedistributeddense(): from keras import regularizers from keras import constraints layer_test(core.TimeDistributedDense, kwargs={'output_dim': 2, 'input_length': 2}, input_shape=(3, 2, 3)) layer_test(core.TimeDistributedDense, kwargs={'output_dim': 3, 'W_regularizer': regularizers.l2(0.01), 'b_regularizer': regularizers.l1(0.01), 'activity_regularizer': regularizers.activity_l2(0.01), 'W_constraint': constraints.MaxNorm(1), 'b_constraint': constraints.MaxNorm(1)}, input_shape=(3, 2, 3))
def __init__(self, dim_in, encoding_dim, sparsity): input_img = Input(shape=(dim_in,)) regulizer = regularizers.activity_l2(sparsity) encoded = Dense(encoding_dim, activation='relu', activity_regularizer=regulizer)(input_img) decoded = Dense(dim_in, activation='sigmoid')(encoded) self.autoencoder = Model(input=input_img, output=decoded) self.encoder = Model(input=input_img, output=encoded) encoded_input = Input(shape=(encoding_dim,)) decoder_layer = self.autoencoder.layers[-1] self.decoder = Model(input=encoded_input, output=decoder_layer(encoded_input)) self.autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
def test_highway(): from keras import regularizers from keras import constraints layer_test(core.Highway, kwargs={}, input_shape=(3, 2)) layer_test(core.Highway, kwargs={'W_regularizer': regularizers.l2(0.01), 'b_regularizer': regularizers.l1(0.01), 'activity_regularizer': regularizers.activity_l2(0.01), 'W_constraint': constraints.MaxNorm(1), 'b_constraint': constraints.MaxNorm(1)}, input_shape=(3, 2))
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') 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)
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)
def test_dense(): from keras import regularizers from keras import constraints layer_test(core.Dense, kwargs={'output_dim': 3}, input_shape=(3, 2)) layer_test(core.Dense, kwargs={'output_dim': 3}, input_shape=(3, 4, 2)) layer_test(core.Dense, kwargs={'output_dim': 3}, input_shape=(None, None, 2)) layer_test(core.Dense, kwargs={'output_dim': 3}, input_shape=(3, 4, 5, 2)) layer_test(core.Dense, kwargs={'output_dim': 3, 'W_regularizer': regularizers.l2(0.01), 'b_regularizer': regularizers.l1(0.01), 'activity_regularizer': regularizers.activity_l2(0.01), 'W_constraint': constraints.MaxNorm(1), 'b_constraint': constraints.MaxNorm(1)}, input_shape=(3, 2))
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)