本文整理汇总了Python中keras.layers.Subtract方法的典型用法代码示例。如果您正苦于以下问题:Python layers.Subtract方法的具体用法?Python layers.Subtract怎么用?Python layers.Subtract使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers
的用法示例。
在下文中一共展示了layers.Subtract方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _build_model
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Subtract [as 别名]
def _build_model(input_shape, hidden_layer_sizes, activation):
"""
Build Keras Ranker NN model (Ranknet / LambdaRank NN).
"""
# Neural network structure
hidden_layers = []
for i in range(len(hidden_layer_sizes)):
hidden_layers.append(Dense(hidden_layer_sizes[i], activation=activation[i], name=str(activation[i]) + '_layer' + str(i)))
h0 = Dense(1, activation='linear', name='Identity_layer')
input1 = Input(shape=(input_shape,), name='Input_layer1')
input2 = Input(shape=(input_shape,), name='Input_layer2')
x1 = input1
x2 = input2
for i in range(len(hidden_layer_sizes)):
x1 = hidden_layers[i](x1)
x2 = hidden_layers[i](x2)
x1 = h0(x1)
x2 = h0(x2)
# Subtract layer
subtracted = Subtract(name='Subtract_layer')([x1, x2])
# sigmoid
out = Activation('sigmoid', name='Activation_layer')(subtracted)
# build model
model = Model(inputs=[input1, input2], outputs=out)
return model
示例2: DnCNN
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Subtract [as 别名]
def DnCNN():
inpt = Input(shape=(None,None,1))
# 1st layer, Conv+relu
x = Conv2D(filters=64, kernel_size=(3,3), strides=(1,1), padding='same')(inpt)
x = Activation('relu')(x)
# 15 layers, Conv+BN+relu
for i in range(15):
x = Conv2D(filters=64, kernel_size=(3,3), strides=(1,1), padding='same')(x)
x = BatchNormalization(axis=-1, epsilon=1e-3)(x)
x = Activation('relu')(x)
# last layer, Conv
x = Conv2D(filters=1, kernel_size=(3,3), strides=(1,1), padding='same')(x)
x = Subtract()([inpt, x]) # input - noise
model = Model(inputs=inpt, outputs=x)
return model
示例3: test_merge_subtract
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Subtract [as 别名]
def test_merge_subtract():
i1 = layers.Input(shape=(4, 5))
i2 = layers.Input(shape=(4, 5))
i3 = layers.Input(shape=(4, 5))
i4 = layers.Input(shape=(3, 5))
o = layers.subtract([i1, i2])
assert o._keras_shape == (None, 4, 5)
model = models.Model([i1, i2], o)
subtract_layer = layers.Subtract()
o2 = subtract_layer([i1, i2])
assert subtract_layer.output_shape == (None, 4, 5)
x1 = np.random.random((2, 4, 5))
x2 = np.random.random((2, 4, 5))
out = model.predict([x1, x2])
assert out.shape == (2, 4, 5)
assert_allclose(out, x1 - x2, atol=1e-4)
assert subtract_layer.compute_mask([i1, i2], [None, None]) is None
assert np.all(K.eval(subtract_layer.compute_mask(
[i1, i2], [K.variable(x1), K.variable(x2)])))
# Test invalid use case
with pytest.raises(ValueError):
subtract_layer.compute_mask([i1, i2], x1)
with pytest.raises(ValueError):
subtract_layer.compute_mask(i1, [None, None])
with pytest.raises(ValueError):
subtract_layer([i1, i2, i3])
with pytest.raises(ValueError):
subtract_layer([i1])
示例4: baseline_model
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Subtract [as 别名]
def baseline_model():
input_1 = Input(shape=(224, 224, 3))
input_2 = Input(shape=(224, 224, 3))
base_model = VGGFace(model='resnet50', include_top=False)
for x in base_model.layers[:-3]:
x.trainable = True
x1 = base_model(input_1)
x2 = base_model(input_2)
# x1_ = Reshape(target_shape=(7*7, 2048))(x1)
# x2_ = Reshape(target_shape=(7*7, 2048))(x2)
#
# x_dot = Dot(axes=[2, 2], normalize=True)([x1_, x2_])
# x_dot = Flatten()(x_dot)
x1 = Concatenate(axis=-1)([GlobalMaxPool2D()(x1), GlobalAvgPool2D()(x1)])
x2 = Concatenate(axis=-1)([GlobalMaxPool2D()(x2), GlobalAvgPool2D()(x2)])
x3 = Subtract()([x1, x2])
x3 = Multiply()([x3, x3])
x = Multiply()([x1, x2])
x = Concatenate(axis=-1)([x, x3])
x = Dense(100, activation="relu")(x)
x = Dropout(0.01)(x)
out = Dense(1, activation="sigmoid")(x)
model = Model([input_1, input_2], out)
model.compile(loss="binary_crossentropy", metrics=['acc'], optimizer=Adam(0.00001))
model.summary()
return model
示例5: baseline_model
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Subtract [as 别名]
def baseline_model():
input_1 = Input(shape=(224, 224, 3))
input_2 = Input(shape=(224, 224, 3))
base_model = ResNet50(weights='imagenet', include_top=False)
for x in base_model.layers[:-3]:
x.trainable = True
x1 = base_model(input_1)
x2 = base_model(input_2)
# x1_ = Reshape(target_shape=(7*7, 2048))(x1)
# x2_ = Reshape(target_shape=(7*7, 2048))(x2)
#
# x_dot = Dot(axes=[2, 2], normalize=True)([x1_, x2_])
# x_dot = Flatten()(x_dot)
x1 = Concatenate(axis=-1)([GlobalMaxPool2D()(x1), GlobalAvgPool2D()(x1)])
x2 = Concatenate(axis=-1)([GlobalMaxPool2D()(x2), GlobalAvgPool2D()(x2)])
x3 = Subtract()([x1, x2])
x3 = Multiply()([x3, x3])
x = Multiply()([x1, x2])
x = Concatenate(axis=-1)([x, x3])
x = Dense(100, activation="relu")(x)
x = Dropout(0.01)(x)
out = Dense(1, activation="sigmoid")(x)
model = Model([input_1, input_2], out)
model.compile(loss="binary_crossentropy", metrics=['acc'], optimizer=Adam(0.00001))
model.summary()
return model
示例6: get_Discriminator
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Subtract [as 别名]
def get_Discriminator(input_shape_1, input_shape_2, Encoder):
dis_inputs_1 = Input(shape=input_shape_1) # Image
dis_inputs_2 = Input(shape=input_shape_2) # Segmentation
mul_1 = Multiply()([dis_inputs_1, dis_inputs_2]) # Getting segmented part
encoder_output_1 = Encoder(dis_inputs_1)
encoder_output_2 = Encoder(mul_1)
subtract_dis = Subtract()([encoder_output_1, encoder_output_2])
dis_conv_block = Conv3D(128, (3, 3, 3), strides=(1, 1, 1), padding='same')(subtract_dis)
dis_conv_block = Activation('relu')(dis_conv_block)
dis_conv_block = Conv3D(128, (3, 3, 3), strides=(1, 1, 1), padding='same')(dis_conv_block)
dis_conv_block = Activation('relu')(dis_conv_block)
dis_conv_block = MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2))(dis_conv_block)
dis_conv_block = Conv3D(64, (3, 3, 3), strides=(1, 1, 1), padding='same')(dis_conv_block)
dis_conv_block = Activation('relu')(dis_conv_block)
dis_conv_block = Conv3D(64, (3, 3, 3), strides=(1, 1, 1), padding='same')(dis_conv_block)
dis_conv_block = Activation('relu')(dis_conv_block)
dis_conv_block = Conv3D(32, (3, 3, 3), strides=(1, 1, 1), padding='same')(dis_conv_block)
dis_conv_block = Activation('relu')(dis_conv_block)
dis_conv_block = Conv3D(32, (3, 3, 3), strides=(1, 1, 1), padding='same')(dis_conv_block)
dis_conv_block = Activation('relu')(dis_conv_block)
flat_1 = Flatten()(dis_conv_block)
dis_fc_1 = Dense(256)(flat_1)
dis_fc_1 = Activation('relu')(dis_fc_1)
dis_drp_1 = Dropout(0.5)(dis_fc_1)
dis_fc_2 = Dense(128)(dis_drp_1)
dis_fc_2 = Activation('relu')(dis_fc_2)
dis_drp_2 = Dropout(0.5)(dis_fc_2)
dis_fc_3 = Dense(1)(dis_drp_2)
dis_similarity_output = Activation('sigmoid')(dis_fc_3)
Discriminator = Model(inputs=[dis_inputs_1, dis_inputs_2], outputs=dis_similarity_output)
Discriminator.compile(optimizer=Adadelta(lr=0.01), loss='binary_crossentropy', metrics=['accuracy'])
print('Discriminator Architecture:')
print(Discriminator.summary())
return Discriminator