本文整理汇总了Python中keras.layers.Lambda方法的典型用法代码示例。如果您正苦于以下问题:Python layers.Lambda方法的具体用法?Python layers.Lambda怎么用?Python layers.Lambda使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers
的用法示例。
在下文中一共展示了layers.Lambda方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: crop
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def crop(dimension, start, end):
# Crops (or slices) a Tensor on a given dimension from start to end
# example : to crop tensor x[:, :, 5:10]
# call slice(2, 5, 10) as you want to crop on the second dimension
def func(x):
if dimension == 0:
return x[start: end]
if dimension == 1:
return x[:, start: end]
if dimension == 2:
return x[:, :, start: end]
if dimension == 3:
return x[:, :, :, start: end]
if dimension == 4:
return x[:, :, :, :, start: end]
return Lambda(func)
示例2: get_model_41
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def get_model_41(params):
embedding_weights = pickle.load(open("../data/datasets/train_data/embedding_weights_w2v-google_MSD-AG.pk","rb"))
# main sequential model
model = Sequential()
model.add(Embedding(len(embedding_weights[0]), params['embedding_dim'], input_length=params['sequence_length'],
weights=embedding_weights))
#model.add(Dropout(params['dropout_prob'][0], input_shape=(params['sequence_length'], params['embedding_dim'])))
model.add(LSTM(2048))
#model.add(Dropout(params['dropout_prob'][1]))
model.add(Dense(output_dim=params["n_out"], init="uniform"))
model.add(Activation(params['final_activation']))
logging.debug("Output CNN: %s" % str(model.output_shape))
if params['final_activation'] == 'linear':
model.add(Lambda(lambda x :K.l2_normalize(x, axis=1)))
return model
# CRNN Arch for audio
示例3: yolo_body
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def yolo_body(inputs, num_anchors, num_classes):
"""Create YOLO_V2 model CNN body in Keras."""
darknet = Model(inputs, darknet_body()(inputs))
conv13 = darknet.get_layer('batchnormalization_13').output
conv20 = compose(
DarknetConv2D_BN_Leaky(1024, 3, 3),
DarknetConv2D_BN_Leaky(1024, 3, 3))(darknet.output)
# TODO: Allow Keras Lambda to use func arguments for output_shape?
conv13_reshaped = Lambda(
space_to_depth_x2,
output_shape=space_to_depth_x2_output_shape,
name='space_to_depth')(conv13)
# Concat conv13 with conv20.
x = merge([conv13_reshaped, conv20], mode='concat')
x = DarknetConv2D_BN_Leaky(1024, 3, 3)(x)
x = DarknetConv2D(num_anchors * (num_classes + 5), 1, 1)(x)
return Model(inputs, x)
示例4: yolo_body
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def yolo_body(inputs, num_anchors, num_classes):
"""Create YOLO_V2 model CNN body in Keras."""
darknet = Model(inputs, darknet_body()(inputs))
conv20 = compose(
DarknetConv2D_BN_Leaky(1024, (3, 3)),
DarknetConv2D_BN_Leaky(1024, (3, 3)))(darknet.output)
conv13 = darknet.layers[43].output
conv21 = DarknetConv2D_BN_Leaky(64, (1, 1))(conv13)
# TODO: Allow Keras Lambda to use func arguments for output_shape?
conv21_reshaped = Lambda(
space_to_depth_x2,
output_shape=space_to_depth_x2_output_shape,
name='space_to_depth')(conv21)
x = concatenate([conv21_reshaped, conv20])
x = DarknetConv2D_BN_Leaky(1024, (3, 3))(x)
x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(x)
return Model(inputs, x)
示例5: GenerateMCSamples
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def GenerateMCSamples(inp, layers, K_mc=20):
if K_mc == 1:
return apply_layers(inp, layers)
output_list = []
for _ in xrange(K_mc):
output_list += [apply_layers(inp, layers)] # THIS IS BAD!!! we create new dense layers at every call!!!!
def pack_out(output_list):
#output = K.pack(output_list) # K_mc x nb_batch x nb_classes
output = K.stack(output_list) # K_mc x nb_batch x nb_classes
return K.permute_dimensions(output, (1, 0, 2)) # nb_batch x K_mc x nb_classes
def pack_shape(s):
s = s[0]
assert len(s) == 2
return (s[0], K_mc, s[1])
out = Lambda(pack_out, output_shape=pack_shape)(output_list)
return out
# evaluation for classification tasks
示例6: SiameseNetwork
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def SiameseNetwork(input_shape=(5880,)):
base_network = create_base_network(input_shape)
input_a = Input(shape=input_shape)
input_b = Input(shape=input_shape)
processed_a = base_network(input_a)
processed_b = base_network(input_b)
distance = Lambda(euclidean_distance,
output_shape=eucl_dist_output_shape)([processed_a, processed_b])
model = Model([input_a, input_b], distance)
rms = RMSprop()
model.compile(loss=contrastive_loss, optimizer=rms, metrics=[accuracy])
return model, base_network
示例7: Highway
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def Highway(x, num_layers=1, activation='relu', name_prefix=''):
'''
Layer wrapper function for Highway network
# Arguments:
x: tensor, shape = (B, input_size)
# Optional Arguments:
num_layers: int, dafault is 1, the number of Highway network layers
activation: keras activation, default is 'relu'
name_prefix: str, default is '', layer name prefix
# Returns:
out: tensor, shape = (B, input_size)
'''
input_size = K.int_shape(x)[1]
for i in range(num_layers):
gate_ratio_name = '{}Highway/Gate_ratio_{}'.format(name_prefix, i)
fc_name = '{}Highway/FC_{}'.format(name_prefix, i)
gate_name = '{}Highway/Gate_{}'.format(name_prefix, i)
gate_ratio = Dense(input_size, activation='sigmoid', name=gate_ratio_name)(x)
fc = Dense(input_size, activation=activation, name=fc_name)(x)
x = Lambda(lambda args: args[0] * args[2] + args[1] * (1 - args[2]), name=gate_name)([fc, x, gate_ratio])
return x
示例8: model_masking
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def model_masking(discrete_time, init_alpha, max_beta):
model = Sequential()
model.add(Masking(mask_value=mask_value,
input_shape=(n_timesteps, n_features)))
model.add(TimeDistributed(Dense(2)))
model.add(Lambda(wtte.output_lambda, arguments={"init_alpha": init_alpha,
"max_beta_value": max_beta}))
if discrete_time:
loss = wtte.loss(kind='discrete', reduce_loss=False).loss_function
else:
loss = wtte.loss(kind='continuous', reduce_loss=False).loss_function
model.compile(loss=loss, optimizer=RMSprop(
lr=lr), sample_weight_mode='temporal')
return model
示例9: train
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def train(model, image_data, y_true, log_dir='logs/'):
'''retrain/fine-tune the model'''
model.compile(optimizer='adam', loss={
# use custom yolo_loss Lambda layer.
'yolo_loss': lambda y_true, y_pred: y_pred})
logging = TensorBoard(log_dir=log_dir)
checkpoint = ModelCheckpoint(log_dir + "ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5",
monitor='val_loss', save_weights_only=True, save_best_only=True)
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')
model.fit([image_data, *y_true],
np.zeros(len(image_data)),
validation_split=.1,
batch_size=32,
epochs=30,
callbacks=[logging, checkpoint, early_stopping])
model.save_weights(log_dir + 'trained_weights.h5')
# Further training.
示例10: channel_shuffle_lambda
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def channel_shuffle_lambda(channels,
groups,
**kwargs):
"""
Channel shuffle layer. This is a wrapper over the same operation. It is designed to save the number of groups.
Parameters:
----------
channels : int
Number of channels.
groups : int
Number of groups.
Returns
-------
Layer
Channel shuffle layer.
"""
assert (channels % groups == 0)
return nn.Lambda(channel_shuffle, arguments={"groups": groups}, **kwargs)
示例11: get_variational_encoder
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def get_variational_encoder(node_num, d,
n_units, nu1, nu2,
activation_fn):
K = len(n_units) + 1
# Input
x = Input(shape=(node_num,))
# Encoder layers
y = [None] * (K + 3)
y[0] = x
for i in range(K - 1):
y[i + 1] = Dense(n_units[i], activation=activation_fn,
W_regularizer=Reg.l1_l2(l1=nu1, l2=nu2))(y[i])
y[K] = Dense(d, activation=activation_fn,
W_regularizer=Reg.l1_l2(l1=nu1, l2=nu2))(y[K - 1])
y[K + 1] = Dense(d)(y[K - 1])
# y[K + 1] = Dense(d, W_regularizer=Reg.l1_l2(l1=nu1, l2=nu2))(y[K - 1])
y[K + 2] = Lambda(sampling, output_shape=(d,))([y[K], y[K + 1]])
# Encoder model
encoder = Model(input=x, outputs=[y[K], y[K + 1], y[K + 2]])
return encoder
示例12: profile_contrib
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def profile_contrib(p):
return kl.Lambda(lambda p:
K.mean(K.sum(K.stop_gradient(tf.nn.softmax(p, dim=-2)) * p, axis=-2), axis=-1)
)(p)
示例13: channel_split
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def channel_split(self, x):
def splitter(y):
# keras Lambda saving bug!!!
# x_left = layers.Lambda(lambda y: y[:, :, :, :int(int(y.shape[-1]) // self.groups)])(x)
# x_right = layers.Lambda(lambda y: y[:, :, :, int(int(y.shape[-1]) // self.groups):])(x)
# return x_left, x_right
return tf.split(y, num_or_size_splits=self.groups, axis=-1)
return layers.Lambda(lambda y: splitter(y))(x)
开发者ID:JACKYLUO1991,项目名称:Face-skin-hair-segmentaiton-and-skin-color-evaluation,代码行数:11,代码来源:lednet.py
示例14: get_model_3
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def get_model_3(params):
# metadata
inputs2 = Input(shape=(params["n_metafeatures"],))
x2 = Dropout(params["dropout_factor"])(inputs2)
if params["n_dense"] > 0:
dense2 = Dense(output_dim=params["n_dense"], init="uniform", activation='relu')
x2 = dense2(x2)
logging.debug("Output CNN: %s" % str(dense2.output_shape))
x2 = Dropout(params["dropout_factor"])(x2)
if params["n_dense_2"] > 0:
dense3 = Dense(output_dim=params["n_dense_2"], init="uniform", activation='relu')
x2 = dense3(x2)
logging.debug("Output CNN: %s" % str(dense3.output_shape))
x2 = Dropout(params["dropout_factor"])(x2)
dense4 = Dense(output_dim=params["n_out"], init="uniform", activation=params['final_activation'])
xout = dense4(x2)
logging.debug("Output CNN: %s" % str(dense4.output_shape))
if params['final_activation'] == 'linear':
reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
xout = reg(xout)
model = Model(input=inputs2, output=xout)
return model
# Metadata 2 inputs, post-merge with dense layers
示例15: get_model_32
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Lambda [as 别名]
def get_model_32(params):
# metadata
inputs = Input(shape=(params["n_metafeatures"],))
reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
x1 = reg(inputs)
inputs2 = Input(shape=(params["n_metafeatures2"],))
reg2 = Lambda(lambda x :K.l2_normalize(x, axis=1))
x2 = reg2(inputs2)
# merge
x = merge([x1, x2], mode='concat', concat_axis=1)
x = Dropout(params["dropout_factor"])(x)
if params['n_dense'] > 0:
dense2 = Dense(output_dim=params["n_dense"], init="uniform", activation='relu')
x = dense2(x)
logging.debug("Output CNN: %s" % str(dense2.output_shape))
dense4 = Dense(output_dim=params["n_out"], init="uniform", activation=params['final_activation'])
xout = dense4(x)
logging.debug("Output CNN: %s" % str(dense4.output_shape))
if params['final_activation'] == 'linear':
reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
xout = reg(xout)
model = Model(input=[inputs,inputs2], output=xout)
return model
# Metadata 3 inputs, pre-merge and l2