本文整理汇总了Python中tensorflow.python.keras.layers.Lambda方法的典型用法代码示例。如果您正苦于以下问题:Python layers.Lambda方法的具体用法?Python layers.Lambda怎么用?Python layers.Lambda使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.keras.layers
的用法示例。
在下文中一共展示了layers.Lambda方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build
# 需要导入模块: from tensorflow.python.keras import layers [as 别名]
# 或者: from tensorflow.python.keras.layers import Lambda [as 别名]
def build(self, input_layer):
last_layer = input_layer
input_shape = K.int_shape(input_layer)
if self.with_embedding:
if input_shape[-1] != 1:
raise ValueError("Only one feature (the index) can be used with embeddings, "
"i.e. the input shape should be (num_samples, length, 1). "
"The actual shape was: " + str(input_shape))
last_layer = Lambda(lambda x: K.squeeze(x, axis=-1),
output_shape=K.int_shape(last_layer)[:-1])(last_layer) # Remove feature dimension.
last_layer = Embedding(self.embedding_size, self.embedding_dimension,
input_length=input_shape[-2])(last_layer)
for _ in range(self.num_layers):
last_layer = Dense(self.num_units, activation=self.activation)(last_layer)
if self.with_bn:
last_layer = BatchNormalization()(last_layer)
if not np.isclose(self.p_dropout, 0):
last_layer = Dropout(self.p_dropout)(last_layer)
return last_layer
示例2: call
# 需要导入模块: from tensorflow.python.keras import layers [as 别名]
# 或者: from tensorflow.python.keras.layers import Lambda [as 别名]
def call(self, inputs, mask=None, **kwargs):
input_fw = inputs
input_bw = inputs
for i in range(self.layers):
output_fw = self.fw_lstm[i](input_fw)
output_bw = self.bw_lstm[i](input_bw)
output_bw = Lambda(lambda x: K.reverse(
x, 1), mask=lambda inputs, mask: mask)(output_bw)
if i >= self.layers - self.res_layers:
output_fw += input_fw
output_bw += input_bw
input_fw = output_fw
input_bw = output_bw
output_fw = input_fw
output_bw = input_bw
if self.merge_mode == "fw":
output = output_fw
elif self.merge_mode == "bw":
output = output_bw
elif self.merge_mode == 'concat':
output = K.concatenate([output_fw, output_bw])
elif self.merge_mode == 'sum':
output = output_fw + output_bw
elif self.merge_mode == 'ave':
output = (output_fw + output_bw) / 2
elif self.merge_mode == 'mul':
output = output_fw * output_bw
elif self.merge_mode is None:
output = [output_fw, output_bw]
return output
示例3: create_model
# 需要导入模块: from tensorflow.python.keras import layers [as 别名]
# 或者: from tensorflow.python.keras.layers import Lambda [as 别名]
def create_model(numNodes, embedding_size, order='second'):
v_i = Input(shape=(1,))
v_j = Input(shape=(1,))
first_emb = Embedding(numNodes, embedding_size, name='first_emb')
second_emb = Embedding(numNodes, embedding_size, name='second_emb')
context_emb = Embedding(numNodes, embedding_size, name='context_emb')
v_i_emb = first_emb(v_i)
v_j_emb = first_emb(v_j)
v_i_emb_second = second_emb(v_i)
v_j_context_emb = context_emb(v_j)
first = Lambda(lambda x: tf.reduce_sum(
x[0]*x[1], axis=-1, keep_dims=False), name='first_order')([v_i_emb, v_j_emb])
second = Lambda(lambda x: tf.reduce_sum(
x[0]*x[1], axis=-1, keep_dims=False), name='second_order')([v_i_emb_second, v_j_context_emb])
if order == 'first':
output_list = [first]
elif order == 'second':
output_list = [second]
else:
output_list = [first, second]
model = Model(inputs=[v_i, v_j], outputs=output_list)
return model, {'first': first_emb, 'second': second_emb}
示例4: trivial_model
# 需要导入模块: from tensorflow.python.keras import layers [as 别名]
# 或者: from tensorflow.python.keras.layers import Lambda [as 别名]
def trivial_model(num_classes):
"""Trivial model for ImageNet dataset."""
input_shape = (224, 224, 3)
img_input = layers.Input(shape=input_shape)
x = layers.Lambda(lambda x: backend.reshape(x, [-1, 224 * 224 * 3]),
name='reshape')(img_input)
x = layers.Dense(1, name='fc1')(x)
x = layers.Dense(num_classes, name='fc1000')(x)
x = layers.Activation('softmax', dtype='float32')(x)
return models.Model(img_input, x, name='trivial')
开发者ID:ShivangShekhar,项目名称:Live-feed-object-device-identification-using-Tensorflow-and-OpenCV,代码行数:15,代码来源:trivial_model.py