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['Input3'], 'l1': net['Regularization']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], '', 'l0')['l0']
net = regularization(net['l1'], [inp], 'l1')
model = Model(inp, net['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'ActivityRegularization')
python类l1()的实例源码
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['Masking']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], '', 'l0')['l0']
net = masking(net['l1'], [inp], 'l1')
model = Model(inp, net['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'Masking')
# ********** Vision Layers Test **********
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['Input'], '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'], [inp], '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'], [inp], 'l3')
model = Model(inp, temp['l3'])
self.assertEqual(model.layers[2].__class__.__name__, '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, 226, 226, 18]
inp = data(net['l2'], '', 'l2')['l2']
temp = convolution(net['l3'], [inp], 'l3')
model = Model(inp, temp['l3'])
self.assertEqual(model.layers[2].__class__.__name__, 'Conv3D')
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['Input'], 'l1': net['Input2'], 'l2': net['Input4'], '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, 12]
inp = data(net['l1'], '', 'l1')['l1']
temp = pooling(net['l3'], [inp], 'l3')
model = Model(inp, temp['l3'])
self.assertEqual(model.layers[2].__class__.__name__, '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, 226, 226]
inp = data(net['l0'], '', 'l0')['l0']
temp = pooling(net['l3'], [inp], 'l3')
model = Model(inp, temp['l3'])
self.assertEqual(model.layers[2].__class__.__name__, '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, 226, 226, 18]
inp = data(net['l2'], '', 'l2')['l2']
temp = pooling(net['l3'], [inp], 'l3')
model = Model(inp, temp['l3'])
self.assertEqual(model.layers[2].__class__.__name__, 'MaxPooling3D')
# ********** Locally-connected Layers **********
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['Input'], 'l1': net['Input2'], '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'], [inp], 'l3')
model = Model(inp, 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'], [inp], 'l3')
model = Model(inp, temp['l3'])
self.assertEqual(model.layers[1].__class__.__name__, 'LocallyConnected2D')
# ********** Recurrent Layers Test **********
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['RNN']}
net['l0']['connection']['output'].append('l1')
# # net = get_shapes(net)
inp = data(net['l0'], '', 'l0')['l0']
net = recurrent(net['l1'], [inp], 'l1')
model = Model(inp, net['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'SimpleRNN')
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['LSTM']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], '', 'l0')['l0']
net = recurrent(net['l1'], [inp], 'l1')
model = Model(inp, net['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'LSTM')
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['GRU']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], '', 'l0')['l0']
net = recurrent(net['l1'], [inp], 'l1')
model = Model(inp, net['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'GRU')
# ********** Embed Layer Test *********
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['Input'], 'l1': net['Eltwise']}
net['l0']['connection']['output'].append('l1')
# Test 1
inp = data(net['l0'], '', 'l0')['l0']
temp = eltwise(net['l1'], [inp, inp], 'l1')
model = Model(inp, temp['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'Multiply')
# Test 2
net['l1']['params']['layer_type'] = 'Sum'
inp = data(net['l0'], '', 'l0')['l0']
temp = eltwise(net['l1'], [inp, inp], 'l1')
model = Model(inp, temp['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'Add')
# Test 3
net['l1']['params']['layer_type'] = 'Average'
inp = data(net['l0'], '', 'l0')['l0']
temp = eltwise(net['l1'], [inp, inp], 'l1')
model = Model(inp, temp['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'Average')
# Test 4
net['l1']['params']['layer_type'] = 'Dot'
inp = data(net['l0'], '', 'l0')['l0']
temp = eltwise(net['l1'], [inp, inp], 'l1')
model = Model(inp, temp['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'Dot')
# Test 5
net['l1']['params']['layer_type'] = 'Maximum'
inp = data(net['l0'], '', 'l0')['l0']
temp = eltwise(net['l1'], [inp, inp], 'l1')
model = Model(inp, temp['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'Maximum')
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['Input'], 'l1': net['Concat']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], '', 'l0')['l0']
net = concat(net['l1'], [inp, inp], 'l1')
model = Model(inp, net['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'Concatenate')
# ********** Noise Layers Test **********
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['Input'], 'l1': net['GaussianDropout']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], '', 'l0')['l0']
net = gaussian_dropout(net['l1'], [inp], 'l1')
model = Model(inp, net['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'GaussianDropout')
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['Input'], 'l1': net['AlphaDropout']}
net['l0']['connection']['output'].append('l1')
inp = data(net['l0'], '', 'l0')['l0']
net = alpha_dropout(net['l1'], [inp], 'l1')
model = Model(inp, net['l1'])
self.assertEqual(model.layers[1].__class__.__name__, 'AlphaDropout')
# ********** Normalisation Layers Test **********
def regularization(layer, layer_in, layerId):
l1 = layer['params']['l1']
l2 = layer['params']['l2']
out = {layerId: ActivityRegularization(l1=l1, l2=l2)(*layer_in)}
return out
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,
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_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 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 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
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)