我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用keras.regularizers.l1l2()。
def test_W_reg(): (X_train, Y_train), (X_test, Y_test), test_ids = get_data() for reg in [regularizers.l1(), regularizers.l2(), regularizers.l1l2()]: model = create_model(weight_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 l1l2(l1_weight=0, l2_weight=0): if keras_2: from keras.regularizers import L1L2 return L1L2(l1_weight, l2_weight) else: from keras.regularizers import l1l2 return l1l2(l1_weight, l2_weight)
def l1l2_penalty_reg(alpha=1.0, l1_ratio=0.5): '''Calculate L1 and L2 penalties for a Keras layer This follows the same formulation as in the R package glmnet and Sklearn Args: alpha ([float]): amount of regularization. l1_ratio ([float]): portion of L1 penalty. Setting to 1.0 equals Lasso. ''' if l1_ratio == .0: return l2(alpha) elif l1_ratio == 1.: return l1(alpha) else: return l1l2(l1_ratio*alpha, 1./2*(1 - l1_ratio)*alpha)
def test_W_reg(self): for reg in [regularizers.identity(), regularizers.l1(), regularizers.l2(), regularizers.l1l2()]: model = create_model(weight_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_W_reg(): (X_train, Y_train), (X_test, Y_test), test_ids = get_data() for reg in [regularizers.l1(), regularizers.l2(), regularizers.l1l2()]: model = create_model(weight_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)
def regularizer(params): if 'l1' in params and 'l2' in params: return regularizers.l1l2(params['l1'], params['l2']) elif 'l1' in params: return regularizers.l1(params['l1']) elif 'l2' in params: return regularizers.l2(params['l2']) else: return None