I am trying to convert strings (which I read from a JSON) to arguments that can be used by Keras layers. However when I find that all the regularizer objects created by the eval function are the same.
a = eval('l1(0.1)')
b = eval('l2(0.1)')
c = eval('l1_l2(0.1)')
print(a,b,c)
gives:
<tensorflow.python.keras.regularizers.L1L2 object at 0x0000013F003C2F60>
<tensorflow.python.keras.regularizers.L1L2 object at 0x0000013F003C2D68>
<tensorflow.python.keras.regularizers.L1L2 object at 0x0000013F0032F160>
Shouldn't eval('l1(0.1)') give
<function tensorflow.python.keras.regularizers.l1(l=0.01)>
Any thoughts on why this is happening would be much appreciated.
L1L2
stores both l1
and l2
; on the regularizer, run, for example:
print(model.layers[1].kernel_regularizer.__dict__)
# {'l1': array(0., dtype=float32), 'l2': array(1., dtype=float32)}
To access one or the other:
print(model.layers[1].kernel_regularizer.l1) # 0.0
print(model.layers[1].kernel_regularizer.l2) # 1.0
In your code, a
sets l1
, b
sets l2
, and c
sets both.
from keras.layers import Input, Dense
from keras.regularizers import l2
from keras.models import Model
ipt = Input(shape=(100,4))
x = Dense(10, activation='relu', kernel_regularizer=l2(1))(ipt)
out = Dense(1, activation='sigmoid')(x)
model = Model(ipt, out)
model.compile(optimizer='adam', loss='binary_crossentropy')
print(model.layers[1].kernel_regularizer.__dict__)
print(model.layers[1].kernel_regularizer.l1)
print(model.layers[1].kernel_regularizer.l2)