我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用keras.regularizers.activity_l1()。
def transform_model(weight_loss_pix=5e-4): inputs = Input(shape=( 128, 128, 3)) x1 = Convolution2D(64, 5, 5, border_mode='same')(inputs) x2 = LeakyReLU(alpha=0.3, name='wkcw')(x1) x3 = BatchNormalization()(x2) x4 = Convolution2D(128, 4, 4, border_mode='same', subsample=(2,2))(x3) x5 = LeakyReLU(alpha=0.3)(x4) x6 = BatchNormalization()(x5) x7 = Convolution2D(256, 4, 4, border_mode='same', subsample=(2,2))(x6) x8 = LeakyReLU(alpha=0.3)(x7) x9 = BatchNormalization()(x8) x10 = Deconvolution2D(128, 3, 3, output_shape=(None, 64, 64, 128), border_mode='same', subsample=(2,2))(x9) x11 = BatchNormalization()(x10) x12 = Deconvolution2D(64, 3, 3, output_shape=(None, 128, 128, 64), border_mode='same', subsample=(2,2))(x11) x13 = BatchNormalization()(x12) x14 = Deconvolution2D(3, 4, 4, output_shape=(None, 128, 128, 3), border_mode='same', activity_regularizer=activity_l1(weight_loss_pix))(x13) output = merge([inputs, x14], mode='sum') model = Model(input=inputs, output=output) return model
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 sparse_autoencoder(X, lam=1e-5): X = X.reshape(X.shape[0], -1) M, N = X.shape inputs = Input(shape=(N,)) h = Dense(64, activation='sigmoid', activity_regularizer=activity_l1(lam))(inputs) outputs = Dense(N)(h) model = Model(input=inputs, output=outputs) model.compile(optimizer='adam', loss='mse') model.fit(X, X, batch_size=64, nb_epoch=3) return model, Model(input=inputs, output=h)
def multilayer_autoencoder(X, lam=1e-5): X = X.reshape(X.shape[0], -1) M, N = X.shape inputs = Input(shape=(N,)) h = Dense(128, activation='relu')(inputs) encoded = Dense(64, activation='relu', activity_regularizer=activity_l1(lam))(h) h = Dense(128, activation='relu')(encoded) outputs = Dense(N)(h) model = Model(input=inputs, output=outputs) model.compile(optimizer='adam', loss='mse') model.fit(X, X, batch_size=64, nb_epoch=3) return model, Model(input=inputs, output=h)
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_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)