本文整理汇总了Python中tensorflow.keras.layers.Lambda方法的典型用法代码示例。如果您正苦于以下问题:Python layers.Lambda方法的具体用法?Python layers.Lambda怎么用?Python layers.Lambda使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.layers
的用法示例。
在下文中一共展示了layers.Lambda方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_eval_model_from_trained_model
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def make_eval_model_from_trained_model(model, anchors, masks, num_classes=10, tiny=True):
if tiny:
output_0, output_1 = model.outputs
boxes_0 = Lambda(lambda x: yolo_boxes(x, anchors[masks[0]], num_classes, False), name='yolo_boxes_0')(output_0)
boxes_1 = Lambda(lambda x: yolo_boxes(x, anchors[masks[1]], num_classes, False), name='yolo_boxes_1')(output_1)
outputs = Lambda(lambda x: yolo_nms(x, anchors, masks, num_classes),
name='yolo_nms')((boxes_0[:3], boxes_1[:3]))
model = tf.keras.Model(model.inputs, outputs, name='yolov3_tiny')
else:
output_0, output_1, output_2 = model.outputs
boxes_0 = Lambda(lambda x: yolo_boxes(x, anchors[masks[0]], num_classes, False), name='yolo_boxes_0')(output_0)
boxes_1 = Lambda(lambda x: yolo_boxes(x, anchors[masks[1]], num_classes, False), name='yolo_boxes_1')(output_1)
boxes_2 = Lambda(lambda x: yolo_boxes(x, anchors[masks[2]], num_classes, False), name='yolo_boxes_1')(output_2)
outputs = Lambda(lambda x: yolo_nms(x, anchors, masks, num_classes),
name='yolo_nms')((boxes_0[:3], boxes_1[:3], boxes_2[:3]))
model = tf.keras.Model(model.inputs, outputs, name='yolov3')
return model
示例2: create_generator_loss
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def create_generator_loss(self, discrim_output):
"""Create the loss function for the generator.
The default implementation is appropriate for most cases. Subclasses can
override this if the need to customize it.
Parameters
----------
discrim_output: Tensor
the output from the discriminator on a batch of generated data. This is
its estimate of the probability that each sample is training data.
Returns
-------
A Tensor equal to the loss function to use for optimizing the generator.
"""
return Lambda(lambda x: -tf.reduce_mean(tf.math.log(x + 1e-10)))(
discrim_output)
示例3: create_discriminator_loss
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def create_discriminator_loss(self, discrim_output_train, discrim_output_gen):
"""Create the loss function for the discriminator.
The default implementation is appropriate for most cases. Subclasses can
override this if the need to customize it.
Parameters
----------
discrim_output_train: Tensor
the output from the discriminator on a batch of training data. This is
its estimate of the probability that each sample is training data.
discrim_output_gen: Tensor
the output from the discriminator on a batch of generated data. This is
its estimate of the probability that each sample is training data.
Returns
-------
A Tensor equal to the loss function to use for optimizing the discriminator.
"""
return Lambda(lambda x: -tf.reduce_mean(tf.math.log(x[0]+1e-10) + tf.math.log(1-x[1]+1e-10)))([discrim_output_train, discrim_output_gen])
示例4: __init__
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def __init__(self,
input_name: str = 'encoder_output',
output_name: str = 'contact_prob'):
super().__init__()
self._input_name = input_name
self._output_name = output_name
def concat_pairs(tensor):
input_mul = tensor[:, :, None] * tensor[:, None, :]
input_sub = tf.abs(tensor[:, :, None] - tensor[:, None, :])
output = tf.concat((input_mul, input_sub), -1)
return output
self.get_pairwise_feature_vector = Lambda(concat_pairs)
self.predict_contact_map = Stack()
self.predict_contact_map.add(Conv2D(32, 1, use_bias=True, padding='same', activation='relu'))
self.predict_contact_map.add(Conv2D(1, 7, use_bias=True, padding='same', activation='linear'))
示例5: triplet_network
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def triplet_network(base_network, embedding_dims=2, embedding_l2=0.0):
def output_shape(shapes):
shape1, shape2, shape3 = shapes
return (3, shape1[0],)
input_a = Input(shape=base_network.input_shape[1:])
input_p = Input(shape=base_network.input_shape[1:])
input_n = Input(shape=base_network.input_shape[1:])
embeddings = Dense(embedding_dims,
kernel_regularizer=l2(embedding_l2))(base_network.output)
network = Model(base_network.input, embeddings)
processed_a = network(input_a)
processed_p = network(input_p)
processed_n = network(input_n)
triplet = Lambda(K.stack,
output_shape=output_shape,
name='stacked_triplets')([processed_a,
processed_p,
processed_n],)
model = Model([input_a, input_p, input_n], triplet)
return model, processed_a, processed_p, processed_n
示例6: build_network
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def build_network(self, num_factor=30):
self.P = tf.Variable(tf.random.normal([self.num_user, num_factor], stddev=0.01))
self.Q = tf.Variable(tf.random.normal([self.num_item, num_factor], stddev=0.01))
# user_id = tf.squeeze(self.user_id)
# item_id = tf.squeeze(self.item_id)
# neg_item_id = tf.squeeze(self.neg_item_id)
user_id = Lambda(lambda x: tf.squeeze(x))(self.user_id)
item_id = Lambda(lambda x: tf.squeeze(x))(self.item_id)
neg_item_id = Lambda(lambda x: tf.squeeze(x))(self.neg_item_id)
user_latent_factor = EmbeddingLookup(self.P)(user_id)
item_latent_factor = EmbeddingLookup(self.Q)(item_id)
neg_item_latent_factor = EmbeddingLookup(self.Q)(neg_item_id)
# user_latent_factor = tf.nn.embedding_lookup(self.P, user_id)
# item_latent_factor = tf.nn.embedding_lookup(self.Q, item_id)
# neg_item_latent_factor = tf.nn.embedding_lookup(self.Q, neg_item_id)
pred_y = tf.reduce_sum(tf.multiply(user_latent_factor, item_latent_factor), 1)
pred_y_neg = tf.reduce_sum(tf.multiply(user_latent_factor, neg_item_latent_factor), 1)
self.model = Model(inputs=[self.user_id, self.item_id, self.neg_item_id],
outputs=[pred_y, pred_y_neg])
# self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.loss)
示例7: attention_3d_block
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def attention_3d_block(hidden_states):
"""
Many-to-one attention mechanism for Keras.
@param hidden_states: 3D tensor with shape (batch_size, time_steps, input_dim).
@return: 2D tensor with shape (batch_size, 128)
@author: felixhao28.
"""
hidden_size = int(hidden_states.shape[2])
# Inside dense layer
# hidden_states dot W => score_first_part
# (batch_size, time_steps, hidden_size) dot (hidden_size, hidden_size) => (batch_size, time_steps, hidden_size)
# W is the trainable weight matrix of attention Luong's multiplicative style score
score_first_part = Dense(hidden_size, use_bias=False, name='attention_score_vec')(hidden_states)
# score_first_part dot last_hidden_state => attention_weights
# (batch_size, time_steps, hidden_size) dot (batch_size, hidden_size) => (batch_size, time_steps)
h_t = Lambda(lambda x: x[:, -1, :], output_shape=(hidden_size,), name='last_hidden_state')(hidden_states)
score = dot([score_first_part, h_t], [2, 1], name='attention_score')
attention_weights = Activation('softmax', name='attention_weight')(score)
# (batch_size, time_steps, hidden_size) dot (batch_size, time_steps) => (batch_size, hidden_size)
context_vector = dot([hidden_states, attention_weights], [1, 1], name='context_vector')
pre_activation = concatenate([context_vector, h_t], name='attention_output')
attention_vector = Dense(128, use_bias=False, activation='tanh', name='attention_vector')(pre_activation)
return attention_vector
示例8: __init__
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def __init__(self, n_z, gate_dim=4, res=96, trainable_model=True):
super(CmvaeDirect, self).__init__()
# create the base models:
self.q_img = dronet.Dronet(num_outputs=n_z*2, include_top=True)
self.p_img = decoders.ImgDecoder()
self.p_R = transformer.NonLinearTransformer()
self.p_Theta = transformer.NonLinearTransformer()
self.p_Psi = transformer.NonLinearTransformer()
self.p_Phi = transformer.NonLinearTransformer()
# Create sampler
self.mean_params = Lambda(lambda x: x[:, : n_z])
self.stddev_params = Lambda(lambda x: x[:, n_z:])
self.R_params = Lambda(lambda x: x[:, 0])
self.Theta_params = Lambda(lambda x: x[:, 1])
self.Psi_params = Lambda(lambda x: x[:, 2])
self.Phi_params = Lambda(lambda x: x[:, 3])
示例9: get_yolo2_inference_model
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def get_yolo2_inference_model(model_type, anchors, num_classes, weights_path=None, input_shape=None, confidence=0.1):
'''create the inference model, for YOLOv2'''
#K.clear_session() # get a new session
num_anchors = len(anchors)
image_shape = Input(shape=(2,), dtype='int64', name='image_shape')
model_body, _ = get_yolo2_model(model_type, num_anchors, num_classes, input_shape=input_shape)
print('Create YOLOv2 {} model with {} anchors and {} classes.'.format(model_type, num_anchors, num_classes))
if weights_path:
model_body.load_weights(weights_path, by_name=False)#, skip_mismatch=True)
print('Load weights {}.'.format(weights_path))
boxes, scores, classes = Lambda(batched_yolo2_postprocess, name='yolo2_postprocess',
arguments={'anchors': anchors, 'num_classes': num_classes, 'confidence': confidence})(
[model_body.output, image_shape])
model = Model([model_body.input, image_shape], [boxes, scores, classes])
return model
示例10: yolo2_predictions
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def yolo2_predictions(feature_maps, feature_channel_nums, num_anchors, num_classes):
f1, f2 = feature_maps
f1_channel_num, f2_channel_num = feature_channel_nums
x1 = compose(
DarknetConv2D_BN_Leaky(f1_channel_num, (3, 3)),
DarknetConv2D_BN_Leaky(f1_channel_num, (3, 3)))(f1)
# Here change the f2 channel number to f2_channel_num//8 first,
# then expand back to f2_channel_num//2 with "space_to_depth_x2"
x2 = DarknetConv2D_BN_Leaky(f2_channel_num//8, (1, 1))(f2)
# TODO: Allow Keras Lambda to use func arguments for output_shape?
x2_reshaped = Lambda(
space_to_depth_x2,
output_shape=space_to_depth_x2_output_shape,
name='space_to_depth')(x2)
x = Concatenate()([x2_reshaped, x1])
x = DarknetConv2D_BN_Leaky(f1_channel_num, (3, 3))(x)
y = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1), name='predict_conv')(x)
return y
示例11: yolo2lite_predictions
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def yolo2lite_predictions(feature_maps, feature_channel_nums, num_anchors, num_classes):
f1, f2 = feature_maps
f1_channel_num, f2_channel_num = feature_channel_nums
x1 = compose(
Depthwise_Separable_Conv2D_BN_Leaky(filters=f1_channel_num, kernel_size=(3, 3), block_id_str='pred_1'),
Depthwise_Separable_Conv2D_BN_Leaky(filters=f1_channel_num, kernel_size=(3, 3), block_id_str='pred_2'))(f1)
# Here change the f2 channel number to f2_channel_num//8 first,
# then expand back to f2_channel_num//2 with "space_to_depth_x2"
x2 = DarknetConv2D_BN_Leaky(f2_channel_num//8, (1, 1))(f2)
# TODO: Allow Keras Lambda to use func arguments for output_shape?
x2_reshaped = Lambda(
space_to_depth_x2,
output_shape=space_to_depth_x2_output_shape,
name='space_to_depth')(x2)
x = Concatenate()([x2_reshaped, x1])
x = Depthwise_Separable_Conv2D_BN_Leaky(filters=f1_channel_num, kernel_size=(3, 3), block_id_str='pred_3')(x)
y = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1), name='predict_conv')(x)
return y
示例12: _se_block
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def _se_block(inputs, filters, se_ratio, prefix):
x = GlobalAveragePooling2D(name=prefix + 'squeeze_excite/AvgPool')(inputs)
if K.image_data_format() == 'channels_first':
x = Reshape((filters, 1, 1))(x)
else:
x = Reshape((1, 1, filters))(x)
x = Conv2D(_depth(filters * se_ratio),
kernel_size=1,
padding='same',
name=prefix + 'squeeze_excite/Conv')(x)
x = ReLU(name=prefix + 'squeeze_excite/Relu')(x)
x = Conv2D(filters,
kernel_size=1,
padding='same',
name=prefix + 'squeeze_excite/Conv_1')(x)
x = Activation(hard_sigmoid)(x)
#if K.backend() == 'theano':
## For the Theano backend, we have to explicitly make
## the excitation weights broadcastable.
#x = Lambda(
#lambda br: K.pattern_broadcast(br, [True, True, True, False]),
#output_shape=lambda input_shape: input_shape,
#name=prefix + 'squeeze_excite/broadcast')(x)
x = Multiply(name=prefix + 'squeeze_excite/Mul')([inputs, x])
return x
示例13: grouped_convolution_block
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def grouped_convolution_block(input, grouped_channels, cardinality, strides, weight_decay=5e-4):
init = input
group_list = []
if cardinality == 1:
# with cardinality 1, it is a standard convolution
x = Conv2D(grouped_channels, (3, 3), padding='same', use_bias=False, strides=(strides, strides),
kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(init)
x = BatchNormalization(axis=3)(x)
x = Activation('relu')(x)
return x
for c in range(cardinality):
x = Lambda(lambda z: z[:, :, :, c * grouped_channels:(c + 1) * grouped_channels])(input)
x = Conv2D(grouped_channels, (3, 3), padding='same', use_bias=False, strides=(strides, strides),
kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(x)
group_list.append(x)
group_merge = concatenate(group_list, axis=3)
x = BatchNormalization(axis=3)(group_merge)
x = Activation('relu')(x)
return x
示例14: call
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def call(self, inputs):
def brelu(x):
# get shape of X, we are interested in the last axis, which is constant
shape = K.int_shape(x)
# last axis
dim = shape[-1]
# half of the last axis (+1 if necessary)
dim2 = dim // 2
if dim % 2 != 0:
dim2 += 1
# multiplier will be a tensor of alternated +1 and -1
multiplier = K.ones((dim2,))
multiplier = K.stack([multiplier, -multiplier], axis=-1)
if dim % 2 != 0:
multiplier = multiplier[:-1]
# adjust multiplier shape to the shape of x
multiplier = K.reshape(multiplier, tuple(1 for _ in shape[:-1]) + (-1,))
return multiplier * tf.nn.relu(multiplier * x)
return Lambda(brelu)(inputs)
示例15: compile
# 需要导入模块: from tensorflow.keras import layers [as 别名]
# 或者: from tensorflow.keras.layers import Lambda [as 别名]
def compile(self, optimizer, metrics=[]):
metrics += [mean_q] # register default metrics
# We never train the target model, hence we can set the optimizer and loss arbitrarily.
self.target_model = clone_model(self.model, self.custom_model_objects)
self.target_model.compile(optimizer='sgd', loss='mse')
self.model.compile(optimizer='sgd', loss='mse')
# Compile model.
if self.target_model_update < 1.:
# We use the `AdditionalUpdatesOptimizer` to efficiently soft-update the target model.
updates = get_soft_target_model_updates(self.target_model, self.model, self.target_model_update)
optimizer = AdditionalUpdatesOptimizer(optimizer, updates)
def clipped_masked_error(args):
y_true, y_pred, mask = args
loss = huber_loss(y_true, y_pred, self.delta_clip)
loss *= mask # apply element-wise mask
return K.sum(loss, axis=-1)
# Create trainable model. The problem is that we need to mask the output since we only
# ever want to update the Q values for a certain action. The way we achieve this is by
# using a custom Lambda layer that computes the loss. This gives us the necessary flexibility
# to mask out certain parameters by passing in multiple inputs to the Lambda layer.
y_pred = self.model.output
y_true = Input(name='y_true', shape=(self.nb_actions,))
mask = Input(name='mask', shape=(self.nb_actions,))
loss_out = Lambda(clipped_masked_error, output_shape=(1,), name='loss')([y_true, y_pred, mask])
ins = [self.model.input] if type(self.model.input) is not list else self.model.input
trainable_model = Model(inputs=ins + [y_true, mask], outputs=[loss_out, y_pred])
assert len(trainable_model.output_names) == 2
combined_metrics = {trainable_model.output_names[1]: metrics}
losses = [
lambda y_true, y_pred: y_pred, # loss is computed in Lambda layer
lambda y_true, y_pred: K.zeros_like(y_pred), # we only include this for the metrics
]
trainable_model.compile(optimizer=optimizer, loss=losses, metrics=combined_metrics)
self.trainable_model = trainable_model
self.compiled = True