本文整理汇总了Python中keras.regularizers.L1L2属性的典型用法代码示例。如果您正苦于以下问题:Python regularizers.L1L2属性的具体用法?Python regularizers.L1L2怎么用?Python regularizers.L1L2使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类keras.regularizers
的用法示例。
在下文中一共展示了regularizers.L1L2属性的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build
# 需要导入模块: from keras import regularizers [as 别名]
# 或者: from keras.regularizers import L1L2 [as 别名]
def build(self, input_shape):
self._filter = self.add_weight(name=f'filter_{self.filter}',
shape=(self.filter, self.label, 1, 1),
regularizer=L1L2(0.00032),
initializer='uniform',
trainable=True)
self.class_w = self.add_weight(name='class_w',
shape=(self.label, self.embed_size),
regularizer=L1L2(0.0000032),
initializer='uniform',
trainable=True)
self.b = self.add_weight(name='bias',
shape=(1,),
regularizer=L1L2(0.00032),
initializer='uniform',
trainable=True)
super().build(input_shape)
示例2: Token_Embedding
# 需要导入模块: from keras import regularizers [as 别名]
# 或者: from keras.regularizers import L1L2 [as 别名]
def Token_Embedding(x, input_dim, output_dim, embed_weights=None,
mask_zero=False, input_length=None, dropout_rate=0,
embed_l2=1E-6, name='', time_distributed=False, **kwargs):
"""
Basic token embedding layer, also included some dropout layer.
"""
embed_reg = L1L2(l2=embed_l2) if embed_l2 != 0 else None
embed_layer = Embedding(input_dim=input_dim,
output_dim=output_dim,
weights=embed_weights,
mask_zero=mask_zero,
input_length=input_length,
embeddings_regularizer=embed_reg,
name=name)
if time_distributed:
embed = TimeDistributed(embed_layer)(x)
else:
embed = embed_layer(x)
# entire embedding channels are dropped out instead of the
# normal Keras embedding dropout, which drops all channels for entire words
# many of the datasets contain so few words that losing one or more words can alter the emotions completely
if dropout_rate != 0:
embed = SpatialDropout1D(dropout_rate)(embed)
return embed
示例3: __call__
# 需要导入模块: from keras import regularizers [as 别名]
# 或者: from keras.regularizers import L1L2 [as 别名]
def __call__(self, inputs):
x = inputs[0]
kernel_regularizer = kr.L1L2(self.l1_decay, self.l2_decay)
x = kl.Conv1D(128, 11,
kernel_initializer=self.init,
kernel_regularizer=kernel_regularizer)(x)
x = kl.Activation('relu')(x)
x = kl.MaxPooling1D(4)(x)
x = kl.Flatten()(x)
kernel_regularizer = kr.L1L2(l1=self.l1_decay, l2=self.l2_decay)
x = kl.Dense(self.nb_hidden,
kernel_initializer=self.init,
kernel_regularizer=kernel_regularizer)(x)
x = kl.Activation('relu')(x)
x = kl.Dropout(self.dropout)(x)
return self._build(inputs, x)
示例4: __call__
# 需要导入模块: from keras import regularizers [as 别名]
# 或者: from keras.regularizers import L1L2 [as 别名]
def __call__(self, inputs):
x = self._merge_inputs(inputs)
shape = getattr(x, '_keras_shape')
replicate_model = self._replicate_model(kl.Input(shape=shape[2:]))
x = kl.TimeDistributed(replicate_model)(x)
kernel_regularizer = kr.L1L2(l1=self.l1_decay, l2=self.l2_decay)
x = kl.Bidirectional(kl.GRU(128, kernel_regularizer=kernel_regularizer,
return_sequences=True),
merge_mode='concat')(x)
kernel_regularizer = kr.L1L2(l1=self.l1_decay, l2=self.l2_decay)
gru = kl.GRU(256, kernel_regularizer=kernel_regularizer)
x = kl.Bidirectional(gru)(x)
x = kl.Dropout(self.dropout)(x)
return self._build(inputs, x)
示例5: build
# 需要导入模块: from keras import regularizers [as 别名]
# 或者: from keras.regularizers import L1L2 [as 别名]
def build(self, input_shape):
# W、K and V
self.kernel = self.add_weight(name='WKV',
shape=(3, input_shape[2], self.output_dim),
initializer='uniform',
regularizer=L1L2(0.0000032),
trainable=True)
super().build(input_shape)
示例6: test_dense
# 需要导入模块: from keras import regularizers [as 别名]
# 或者: from keras.regularizers import L1L2 [as 别名]
def test_dense():
layer_test(layers.Dense,
kwargs={'units': 3},
input_shape=(3, 2))
layer_test(layers.Dense,
kwargs={'units': 3},
input_shape=(3, 4, 2))
layer_test(layers.Dense,
kwargs={'units': 3},
input_shape=(None, None, 2))
layer_test(layers.Dense,
kwargs={'units': 3},
input_shape=(3, 4, 5, 2))
layer_test(layers.Dense,
kwargs={'units': 3,
'kernel_regularizer': regularizers.l2(0.01),
'bias_regularizer': regularizers.l1(0.01),
'activity_regularizer': regularizers.L1L2(l1=0.01, l2=0.01),
'kernel_constraint': constraints.MaxNorm(1),
'bias_constraint': constraints.max_norm(1)},
input_shape=(3, 2))
layer = layers.Dense(3,
kernel_regularizer=regularizers.l1(0.01),
bias_regularizer='l1')
layer.build((None, 4))
assert len(layer.losses) == 2
示例7: build_mlp
# 需要导入模块: from keras import regularizers [as 别名]
# 或者: from keras.regularizers import L1L2 [as 别名]
def build_mlp(last_layer, p_dropout=0.0, num_layers=1, with_bn=True, dim=None, l2_weight=0.0,
last_activity_regulariser=None, propensity_dropout=None, normalize=False):
if dim is None:
dim = K.int_shape(last_layer)[-1]
for i in range(num_layers):
last_layer = Dense(dim,
kernel_regularizer=L1L2(l2=l2_weight),
bias_regularizer=L1L2(l2=l2_weight),
use_bias=not with_bn,
activity_regularizer=last_activity_regulariser if i == num_layers-1 else None)\
(last_layer)
if with_bn:
last_layer = BatchNormalization(gamma_regularizer=L1L2(l2=l2_weight),
beta_regularizer=L1L2(l2=l2_weight))(last_layer)
last_layer = ELU()(last_layer)
last_layer = Dropout(p_dropout)(last_layer)
if propensity_dropout is not None:
last_layer = PerSampleDropout(propensity_dropout)(last_layer)
if normalize:
last_layer = Lambda(lambda x: x / safe_sqrt(tf.reduce_sum(tf.square(x),
axis=1,
keep_dims=True)))(last_layer)
if last_activity_regulariser is not None:
identity_layer = Lambda(lambda x: x)
identity_layer.activity_regularizer = last_activity_regulariser
last_layer = identity_layer(last_layer)
return last_layer
示例8: _res_unit
# 需要导入模块: from keras import regularizers [as 别名]
# 或者: from keras.regularizers import L1L2 [as 别名]
def _res_unit(self, inputs, nb_filter, size=3, stride=1, stage=1, block=1):
name = '%02d-%02d/' % (stage, block)
id_name = '%sid_' % (name)
res_name = '%sres_' % (name)
# Residual branch
x = kl.BatchNormalization(name=res_name + 'bn1')(inputs)
x = kl.Activation('relu', name=res_name + 'act1')(x)
kernel_regularizer = kr.L1L2(l1=self.l1_decay, l2=self.l2_decay)
x = kl.Conv1D(nb_filter, size,
name=res_name + 'conv1',
border_mode='same',
subsample_length=stride,
kernel_initializer=self.init,
kernel_regularizer=kernel_regularizer)(x)
x = kl.BatchNormalization(name=res_name + 'bn2')(x)
x = kl.Activation('relu', name=res_name + 'act2')(x)
kernel_regularizer = kr.L1L2(l1=self.l1_decay, l2=self.l2_decay)
x = kl.Conv1D(nb_filter, size,
name=res_name + 'conv2',
border_mode='same',
kernel_initializer=self.init,
kernel_regularizer=kernel_regularizer)(x)
# Identity branch
if nb_filter != inputs._keras_shape[-1] or stride > 1:
kernel_regularizer = kr.L1L2(l1=self.l1_decay, l2=self.l2_decay)
identity = kl.Conv1D(nb_filter, size,
name=id_name + 'conv1',
border_mode='same',
subsample_length=stride,
kernel_initializer=self.init,
kernel_regularizer=kernel_regularizer)(inputs)
else:
identity = inputs
x = kl.merge([identity, x], name=name + 'merge', mode='sum')
return x
示例9: _replicate_model
# 需要导入模块: from keras import regularizers [as 别名]
# 或者: from keras.regularizers import L1L2 [as 别名]
def _replicate_model(self, input):
kernel_regularizer = kr.L1L2(l1=self.l1_decay, l2=self.l2_decay)
x = kl.Dense(256, kernel_initializer=self.init,
kernel_regularizer=kernel_regularizer)(input)
x = kl.Activation(self.act_replicate)(x)
return km.Model(input, x)
示例10: __call__
# 需要导入模块: from keras import regularizers [as 别名]
# 或者: from keras.regularizers import L1L2 [as 别名]
def __call__(self, models):
layers = []
for layer in range(self.nb_layer):
kernel_regularizer = kr.L1L2(l1=self.l1_decay, l2=self.l2_decay)
layers.append(kl.Dense(self.nb_hidden,
kernel_initializer=self.init,
kernel_regularizer=kernel_regularizer))
layers.append(kl.Activation('relu'))
layers.append(kl.Dropout(self.dropout))
return self._build(models, layers)