Python keras.regularizers 模块,l1() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.regularizers.l1()。
def test_activity_regularization():
from keras.engine import Input, Model
layer = core.ActivityRegularization(l1=0.01, l2=0.01)
# test in functional API
x = Input(shape=(3,))
z = core.Dense(2)(x)
y = layer(z)
model = Model(input=x, output=y)
model.compile('rmsprop', 'mse', mode='FAST_COMPILE')
model.predict(np.random.random((2, 3)))
# test serialization
model_config = model.get_config()
model = Model.from_config(model_config)
model.compile('rmsprop', 'mse')
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)}, 2))
def test_maxout_dense():
from keras import regularizers
from keras import constraints
layer_test(core.MaxoutDense, 2))
layer_test(core.MaxoutDense, 2))
def test_timedistributeddense():
from keras import regularizers
from keras import constraints
layer_test(core.TimedistributedDense,
kwargs={'output_dim': 2, 'input_length': 2}, 2, 3))
layer_test(core.TimedistributedDense, 3))
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'tests', 'unit', 'keras_app',
'keras_export_test.json'), 'r')
response = json.load(tests)
tests.close()
net = yaml.safe_load(json.dumps(response['net']))
net = {'l0': net['Input2'], 'l1': net['InnerProduct']}
net['l0']['connection']['output'].append('l1')
# Test 1
inp = data(net['l0'], '', 'l0')['l0']
temp = dense(net['l1'], [inp], 'l1')
model = Model(inp, temp['l1'])
self.assertEqual(model.layers[2].__class__.__name__, 'Dense')
# Test 2
net['l1']['params']['weight_filler'] = 'glorot_normal'
net['l1']['params']['bias_filler'] = 'glorot_normal'
inp = data(net['l0'], 'Dense')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'r')
response = json.load(tests)
tests.close()
net = yaml.safe_load(json.dumps(response['net']))
net = {'l0': net['Input'], 'l1': net['ReLU']}
# Test 1
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
temp = activation(net['l1'], temp['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'Activation')
# Test 2
net['l1']['params']['negative_slope'] = 1
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'LeakyReLU')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['Deconvolution']}
net['l0']['connection']['output'].append('l1')
# Test 1
inp = data(net['l0'], 'l0')['l0']
temp = deconvolution(net['l1'], 'Conv2DTranspose')
# Test 2
net['l1']['params']['weight_filler'] = 'xavier'
net['l1']['params']['bias_filler'] = 'xavier'
inp = data(net['l0'], 'Conv2DTranspose')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['GaussianNoise']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = gaussian_noise(net['l1'], net['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'GaussianNoise')
def test_activity_regularization():
from keras.engine import Input, 'mse')
def test_maxout_dense():
from keras import regularizers
from keras import constraints
layer_test(core.MaxoutDense, 2))
def test_timedistributeddense():
from keras import regularizers
from keras import constraints
layer_test(core.TimedistributedDense, 3))
def create_trainable_model(self,nb_event,nb_type,nb_feature):
from keras.layers import Input, Dense, Flatten, Convolution2D, Activation, Dropout, merge
from keras.models import Model
from keras.regularizers import l1,l2
x = Input(batch_shape=(1, nb_event, nb_type, nb_feature), dtype='float')
y = Convolution2D(128, kernel_size=[nb_event-10+1, 1], strides=(2,1), activation='relu')(x)
y = Dropout(0.5)(y)
y = Convolution2D(128, kernel_size=[3, nb_type], activation='relu')(y)
y = Dropout(0.5)(y)
y = Flatten()(y)
y = Dense(2,activation='softmax')(y)
model = Model(inputs=[x], outputs=[y], name='dis_output')
self.model = model
return model
def test_activity_regularization():
from keras.engine import Input, 'mse')
def test_maxout_dense():
from keras import regularizers
from keras import constraints
layer_test(core.MaxoutDense, 2))
def test_timedistributeddense():
from keras import regularizers
from keras import constraints
layer_test(core.TimedistributedDense, 3))
def test_keras_export(self):
# Test 1
img_input = Input((224, 224, 3))
model = Conv2D(64, (3, 3), padding='same', dilation_rate=1, use_bias=True,
kernel_regularizer=regularizers.l1(), bias_regularizer='l1',
activity_regularizer='l1', kernel_constraint='max_norm',
bias_constraint='max_norm')(img_input)
model = Batchnormalization(center=True, scale=True, beta_regularizer=regularizers.l2(0.01),
gamma_regularizer=regularizers.l2(0.01),
beta_constraint='max_norm', gamma_constraint='max_norm',)(model)
model = Model(img_input, model)
json_string = Model.to_json(model)
with open(os.path.join(settings.BASE_DIR, 'media', 'test.json'), 'w') as out:
json.dump(json.loads(json_string), out, indent=4)
sample_file = open(os.path.join(settings.BASE_DIR, 'r')
response = self.client.post(reverse('keras-import'), {'file': sample_file})
response = json.loads(response.content)
response = self.client.post(reverse('keras-export'), {'net': json.dumps(response['net']),
'net_name': ''})
response = json.loads(response.content)
self.assertEqual(response['result'], 'success')
# Test 2
tests = open(os.path.join(settings.BASE_DIR, 'ide',
'caffe_export_test.json'), 'r')
response = json.load(tests)
tests.close()
net = yaml.safe_load(json.dumps(response['net']))
net = {'l0': net['HDF5Data']}
response = self.client.post(reverse('keras-export'), {'net': json.dumps(net), 'error')
# ********** Import json tests **********
def test_keras_import(self):
model = Sequential()
model.add(ActivityRegularization(l1=2, input_shape=(10,)))
model.build()
self.keras_type_test(model, 0, 'Regularization')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['PReLU']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = activation(net['l1'], 'PReLU')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['ThresholdedReLU']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'ThresholdedReLU')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['Sigmoid']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'Activation')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['TanH']}
inp = data(net['l0'], 'l1': net['softmax']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l1': net['SELU']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l1': net['Softsign']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l1': net['HardSigmoid']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'r')
response = json.load(tests)
tests.close()
net = yaml.safe_load(json.dumps(response['net']))
net = {'l0': net['Input3'], 'l1': net['Dropout']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = dropout(net['l1'], 'Dropout')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['Flatten']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = flatten(net['l1'], 'Flatten')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['Reshape']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = reshape(net['l1'], 'Reshape')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['RepeatVector']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = repeat_vector(net['l1'], 'RepeatVector')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['Regularization']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = regularization(net['l1'], 'ActivityRegularization')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['Masking']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = masking(net['l1'], 'Masking')
# ********** Vision Layers Test **********
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['Input2'], 'l2': net['Input4'], 'l3': net['Convolution']}
# Conv 1D
net['l1']['connection']['output'].append('l3')
net['l3']['connection']['input'] = ['l1']
net['l3']['params']['layer_type'] = '1D'
net['l3']['shape']['input'] = net['l1']['shape']['output']
net['l3']['shape']['output'] = [128, 12]
inp = data(net['l1'], 'l1')['l1']
temp = convolution(net['l3'], 'l3')
model = Model(inp, temp['l3'])
self.assertEqual(model.layers[2].__class__.__name__, 'Conv1D')
# Conv 2D
net['l0']['connection']['output'].append('l0')
net['l3']['connection']['input'] = ['l0']
net['l3']['params']['layer_type'] = '2D'
net['l3']['shape']['input'] = net['l0']['shape']['output']
net['l3']['shape']['output'] = [128, 226, 226]
inp = data(net['l0'], 'l0')['l0']
temp = convolution(net['l3'], 'Conv2D')
# Conv 3D
net['l2']['connection']['output'].append('l3')
net['l3']['connection']['input'] = ['l2']
net['l3']['params']['layer_type'] = '3D'
net['l3']['shape']['input'] = net['l2']['shape']['output']
net['l3']['shape']['output'] = [128, 18]
inp = data(net['l2'], 'l2')['l2']
temp = convolution(net['l3'], 'Conv3D')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l3': net['Pooling']}
# Pool 1D
net['l1']['connection']['output'].append('l3')
net['l3']['connection']['input'] = ['l1']
net['l3']['params']['layer_type'] = '1D'
net['l3']['shape']['input'] = net['l1']['shape']['output']
net['l3']['shape']['output'] = [12, 'l1')['l1']
temp = pooling(net['l3'], 'MaxPooling1D')
# Pool 2D
net['l0']['connection']['output'].append('l0')
net['l3']['connection']['input'] = ['l0']
net['l3']['params']['layer_type'] = '2D'
net['l3']['shape']['input'] = net['l0']['shape']['output']
net['l3']['shape']['output'] = [3, 'l0')['l0']
temp = pooling(net['l3'], 'MaxPooling2D')
# Pool 3D
net['l2']['connection']['output'].append('l3')
net['l3']['connection']['input'] = ['l2']
net['l3']['params']['layer_type'] = '3D'
net['l3']['shape']['input'] = net['l2']['shape']['output']
net['l3']['shape']['output'] = [3, 'l2')['l2']
temp = pooling(net['l3'], 'MaxPooling3D')
# ********** Locally-connected Layers **********
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l3': net['LocallyConnected']}
# LocallyConnected 1D
net['l1']['connection']['output'].append('l3')
net['l3']['connection']['input'] = ['l1']
net['l3']['params']['layer_type'] = '1D'
inp = data(net['l1'], 'l1')['l1']
temp = locally_connected(net['l3'], temp['l3'])
self.assertEqual(model.layers[1].__class__.__name__, 'LocallyConnected1D')
# LocallyConnected 2D
net['l0']['connection']['output'].append('l0')
net['l0']['shape']['output'] = [3, 10, 10]
net['l3']['connection']['input'] = ['l0']
net['l3']['params']['layer_type'] = '2D'
inp = data(net['l0'], 'l0')['l0']
temp = locally_connected(net['l3'], 'LocallyConnected2D')
# ********** Recurrent Layers Test **********
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['RNN']}
net['l0']['connection']['output'].append('l1')
# # net = get_shapes(net)
inp = data(net['l0'], 'l0')['l0']
net = recurrent(net['l1'], 'SimpleRNN')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['LSTM']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'LSTM')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['GRU']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'GRU')
# ********** Embed Layer Test *********
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['Eltwise']}
net['l0']['connection']['output'].append('l1')
# Test 1
inp = data(net['l0'], 'l0')['l0']
temp = eltwise(net['l1'], [inp, inp], 'Multiply')
# Test 2
net['l1']['params']['layer_type'] = 'Sum'
inp = data(net['l0'], 'Add')
# Test 3
net['l1']['params']['layer_type'] = 'Average'
inp = data(net['l0'], 'Average')
# Test 4
net['l1']['params']['layer_type'] = 'Dot'
inp = data(net['l0'], 'Dot')
# Test 5
net['l1']['params']['layer_type'] = 'Maximum'
inp = data(net['l0'], 'Maximum')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['Concat']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = concat(net['l1'], 'Concatenate')
# ********** Noise Layers Test **********
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['GaussianDropout']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = gaussian_dropout(net['l1'], 'GaussianDropout')
def test_keras_export(self):
tests = open(os.path.join(settings.BASE_DIR, 'l1': net['AlphaDropout']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], 'l0')['l0']
net = alpha_dropout(net['l1'], 'AlphaDropout')
# ********** normalisation Layers Test **********
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_dense():
from keras import regularizers
from keras import constraints
layer_test(core.Dense, 4,
input_shape=(None, None, 5, 2))
def test_highway():
from keras import regularizers
from keras import constraints
layer_test(core.Highway, 2))
def build_simple_autoencoder(input_dim=784, encoding_dim=32, l1_penalty=0.):
# this is the size of our encoded representations
# 32 floats -> compression of factor 24.5,assuming the input is 784 floats
# this is our input placeholder
input_img = Input(shape=(input_dim,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu', activity_regularizer=regularizers.l1(l1_penalty))(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(input_dim, activation='sigmoid')(encoded)
# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
return encoder, decoder, autoencoder
def sst2_run(index_embedding, dataset, num_words=5000, embedding_len=100, max_len=50):
(x_train, y_train), (x_test, y_test), (x_dev, y_dev) = ds.load_data(dataset, num_words, has_dev=True)
x_train = sequence.pad_sequences(x_train, maxlen=max_len)
x_test = sequence.pad_sequences(x_test, maxlen=max_len)
x_dev = sequence.pad_sequences(x_dev, maxlen=max_len)
model = Sequential()
model.add(Embedding(num_words, embedding_len, input_length=max_len, weights=[index_embedding]))
model.add(LSTM(max_len, dropout=0.5, recurrent_dropout=0.5))
model.add(Dense(1, activation='sigmoid',
kernel_regularizer=regularizers.l2(0.01),
activity_regularizer=regularizers.l1(0.01)
))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, epochs=4, batch_size=32,
validation_data=(x_dev, y_dev), verbose=2)
score, acc = model.evaluate(x_test, y_test, verbose=0)
print('Test score:', score)
print('Test accuracy:', acc)