我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用keras.engine.merge()。
def test_learning_phase(): a = Input(shape=(32,), name='input_a') b = Input(shape=(32,), name='input_b') a_2 = Dense(16, name='dense_1')(a) dp = Dropout(0.5, name='dropout') b_2 = dp(b) assert dp.uses_learning_phase assert not a_2._uses_learning_phase assert b_2._uses_learning_phase # test merge m = merge([a_2, b_2], mode='concat') assert m._uses_learning_phase # Test recursion model = Model([a, b], [a_2, b_2]) print(model.input_spec) assert model.uses_learning_phase c = Input(shape=(32,), name='input_c') d = Input(shape=(32,), name='input_d') c_2, b_2 = model([c, d]) assert c_2._uses_learning_phase assert b_2._uses_learning_phase # try actually running graph fn = K.function(model.inputs + [K.learning_phase()], model.outputs) input_a_np = np.random.random((10, 32)) input_b_np = np.random.random((10, 32)) fn_outputs_no_dp = fn([input_a_np, input_b_np, 0]) fn_outputs_dp = fn([input_a_np, input_b_np, 1]) # output a: nothing changes assert fn_outputs_no_dp[0].sum() == fn_outputs_dp[0].sum() # output b: dropout applied assert fn_outputs_no_dp[1].sum() != fn_outputs_dp[1].sum()
def st_conv_inception_4_superresolution(input_shape, weights_path=None, mode=0, nb_res_layer=5): if K.image_dim_ordering() == 'tf': channel_axis = 3 else: channel_axis = 1 input = Input(shape=input_shape, name='input_node', dtype=K.floatx()) out = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) last_out = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.9, gamma_init='he_normal')(out) last_out = Activation('relu')(last_out) for i in range(nb_res_layer): out = inception_layer(last_out, K.image_dim_ordering(), channel_axis, mode) last_out = merge([last_out, out], mode='sum') out = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(last_out) out = ConvolutionTranspose2D(3, 5, 5, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(4, 4), border_mode='same', activation='linear')(out) out = ScaledSigmoid(scaling=255., name="output_node")(out) model = Model(input=[input], output=[out]) if weights_path: model.load_weights(weights_path) return model
def inception_layer(input, do, channel_axis, batchnorm_mode): # Branch 1 out = Convolution2D(32, 1, 1, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) out = BatchNormalization(mode=batchnorm_mode, axis=channel_axis, momentum=0.9, gamma_init='he_normal')(out) out1 = Activation('relu')(out) # Branch 2 out = Convolution2D(32, 1, 1, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) out = Activation('relu')(out) out = Convolution2D(32, 3, 3, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(out) out = BatchNormalization(mode=batchnorm_mode, axis=channel_axis, momentum=0.9, gamma_init='he_normal')(out) out2 = Activation('relu')(out) # Branch 3 out = Convolution2D(32, 1, 1, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) out = Activation('relu')(out) out = Convolution2D(32, 5, 5, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(out) out = BatchNormalization(mode=batchnorm_mode, axis=channel_axis, momentum=0.9, gamma_init='he_normal')(out) out3 = Activation('relu')(out) # Branch 4 out = Convolution2D(32, 1, 1, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) out = Activation('relu')(out) out = Convolution2D(32, 3, 3, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(out) out = Activation('relu')(out) out = Convolution2D(32, 3, 3, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(out) out = BatchNormalization(mode=batchnorm_mode, axis=channel_axis, momentum=0.9, gamma_init='he_normal')(out) out4 = Activation('relu')(out) m = merge([out1, out2, out3, out4], mode='concat', concat_axis=channel_axis) # 16 layers return m
def inception_layer_fast(input, do, channel_axis, batchnorm_mode, nb_layers): # Branch 1 out = Convolution2D(int(nb_layers/4), 1, 1, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) out = BatchNormalization(mode=batchnorm_mode, axis=channel_axis, momentum=0.5, gamma_init='he_normal')(out) out1 = Activation('relu')(out) # Branch 2 out = Convolution2D(int(nb_layers/4), 1, 1, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) out = Convolution2D(int(nb_layers/4), 3, 3, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(out) out = BatchNormalization(mode=batchnorm_mode, axis=channel_axis, momentum=0.5, gamma_init='he_normal')(out) out2 = Activation('relu')(out) # Branch 3 out = Convolution2D(int(nb_layers/4), 1, 1, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) out = Convolution2D(int(nb_layers/4), 3, 3, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(out) out = Convolution2D(int(nb_layers/4), 3, 3, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(out) out = BatchNormalization(mode=batchnorm_mode, axis=channel_axis, momentum=0.5, gamma_init='he_normal')(out) out3 = Activation('relu')(out) # Branch 4 out = Convolution2D(int(nb_layers/4), 1, 1, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) out = Convolution2D(int(nb_layers/4), 3, 3, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(out) out = Convolution2D(int(nb_layers/4), 3, 3, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(out) out = Convolution2D(int(nb_layers/4), 3, 3, dim_ordering=do, init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(out) out = BatchNormalization(mode=batchnorm_mode, axis=channel_axis, momentum=0.5, gamma_init='he_normal')(out) out4 = Activation('relu')(out) m = merge([out1, out2, out3, out4], mode='concat', concat_axis=channel_axis) # 16 layers m = BatchNormalization(mode=batchnorm_mode, axis=channel_axis, momentum=0.5, gamma_init='he_normal')(m) return m
def get_model( data_path, #Path to dataset hid_dim, #Dimension of the hidden GRU layers optimizer='rmsprop', #Optimization function to be used loss='categorical_crossentropy' #Loss function to be used ): metadata_dict = {} f = open(os.path.join(data_path, 'metadata', 'metadata.txt'), 'r') for line in f: entry = line.split(':') metadata_dict[entry[0]] = int(entry[1]) f.close() story_maxlen = metadata_dict['input_length'] query_maxlen = metadata_dict['query_length'] vocab_size = metadata_dict['vocab_size'] entity_dim = metadata_dict['entity_dim'] embed_weights = np.load(os.path.join(data_path, 'metadata', 'weights.npy')) word_dim = embed_weights.shape[1] ########## MODEL ############ story_input = Input(shape=(story_maxlen,), dtype='int32', name="StoryInput") x = Embedding(input_dim=vocab_size+2, output_dim=word_dim, input_length=story_maxlen, mask_zero=True, weights=[embed_weights])(story_input) query_input = Input(shape=(query_maxlen,), dtype='int32', name='QueryInput') x_q = Embedding(input_dim=vocab_size+2, output_dim=word_dim, input_length=query_maxlen, mask_zero=True, weights=[embed_weights])(query_input) concat_embeddings = masked_concat([x_q, x], concat_axis=1) lstm = GRU(hid_dim, consume_less='gpu')(concat_embeddings) reverse_lstm = GRU(hid_dim, consume_less='gpu', go_backwards=True)(concat_embeddings) merged = merge([lstm, reverse_lstm], mode='concat') result = Dense(entity_dim, activation='softmax')(merged) model = Model(input=[story_input, query_input], output=result) model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy']) print(model.summary()) return model
def get_model( data_path, #Path to dataset lstm_dim, #Dimension of the hidden LSTM layers optimizer='rmsprop', #Optimization function to be used loss='categorical_crossentropy', #Loss function to be used weights_path=None #If specified initializes model with weight file given ): metadata_dict = {} f = open(os.path.join(data_path, 'metadata', 'metadata.txt'), 'r') for line in f: entry = line.split(':') metadata_dict[entry[0]] = int(entry[1]) f.close() story_maxlen = metadata_dict['input_length'] query_maxlen = metadata_dict['query_length'] vocab_size = metadata_dict['vocab_size'] entity_dim = metadata_dict['entity_dim'] embed_weights = np.load(os.path.join(data_path, 'metadata', 'weights.npy')) word_dim = embed_weights.shape[1] ########## MODEL ############ story_input = Input(shape=(story_maxlen,), dtype='int32', name="StoryInput") x = Embedding(input_dim=vocab_size+2, output_dim=word_dim, input_length=story_maxlen, mask_zero=True, weights=[embed_weights])(story_input) query_input = Input(shape=(query_maxlen,), dtype='int32', name='QueryInput') x_q = Embedding(input_dim=vocab_size+2, output_dim=word_dim, input_length=query_maxlen, mask_zero=True, weights=[embed_weights])(query_input) concat_embeddings = masked_concat([x_q, x], concat_axis=1) lstm = LSTM(lstm_dim, consume_less='gpu')(concat_embeddings) reverse_lstm = LSTM(lstm_dim, consume_less='gpu', go_backwards=True)(concat_embeddings) merged = merge([lstm, reverse_lstm], mode='concat') result = Dense(entity_dim, activation='softmax')(merged) model = Model(input=[story_input, query_input], output=result) if weights_path: model.load_weights(weights_path) model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy']) print(model.summary()) return model
def st_convt(input_shape, weights_path=None, mode=0, nb_res_layer=5): if K.image_dim_ordering() == 'tf': channel_axis = 3 else: channel_axis = 1 input = Input(shape=input_shape, name='input_node', dtype=K.floatx()) # Downsampling c11 = Convolution2D(32, 9, 9, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(input) bn11 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c11) a11 = Activation('relu')(bn11) c12 = Convolution2D(64, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(a11) bn12 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c12) a12 = Activation('relu')(bn12) c13 = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(a12) bn13 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c13) last_out = Activation('relu')(bn13) for i in range(nb_res_layer): c = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(last_out) bn = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c) a = Activation('relu')(bn) c = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(a) bn = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c) # a = Activation('relu')(bn) last_out = merge([last_out, bn], mode='sum') # last_out = a ct71 = ConvolutionTranspose2D(64, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(last_out) bn71 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(ct71) a71 = Activation('relu')(bn71) ct81 = ConvolutionTranspose2D(32, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(a71) bn81 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(ct81) a81 = Activation('relu')(bn81) c91 = Convolution2D(3, 9, 9, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(a81) out = ScaledSigmoid(scaling=255., name="output_node")(c91) model = Model(input=[input], output=[out]) if weights_path: model.load_weights(weights_path) return model # Moving from 4 to 12 layers doesn't seem to improve much
def st_conv_inception(input_shape, weights_path=None, mode=0, nb_res_layer=5): if K.image_dim_ordering() == 'tf': channel_axis = 3 else: channel_axis = 1 input = Input(shape=input_shape, name='input_node', dtype=K.floatx()) # Downsampling c = Convolution2D(13, 9, 9, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(input) bn11 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c) a11 = Activation('relu')(bn11) mp11 = MaxPooling2D(pool_size=(2, 2), dim_ordering=K.image_dim_ordering(), border_mode='same')(input) m = merge([a11, mp11], mode='concat', concat_axis=channel_axis) # 16 layers c12 = Convolution2D(48, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(m) bn12 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c12) a12 = Activation('relu')(bn12) mp12 = MaxPooling2D(pool_size=(2, 2), dim_ordering=K.image_dim_ordering(), border_mode='same')(m) m = merge([a12, mp12], mode='concat', concat_axis=channel_axis) # 64 layers c13 = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(m) bn13 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(c13) last_out = Activation('relu')(bn13) for i in range(nb_res_layer): out = naive_inception_layer(last_out, K.image_dim_ordering(), channel_axis, mode) last_out = merge([last_out, out], mode='sum') ct = ConvolutionTranspose2D(64, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(last_out) bn = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(ct) a = Activation('relu')(bn) ct = ConvolutionTranspose2D(16, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(a) bn = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(ct) a = Activation('relu')(bn) c = Convolution2D(3, 9, 9, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(a) out = ScaledSigmoid(scaling=255., name="output_node")(c) model = Model(input=[input], output=[out]) if weights_path: model.load_weights(weights_path) return model
def st_convt_inception_prelu(input_shape, weights_path=None, mode=0, nb_res_layer=5): if K.image_dim_ordering() == 'tf': channel_axis = 3 else: channel_axis = 1 input = Input(shape=input_shape, name='input_node', dtype=K.floatx()) # Downsampling c = Convolution2D(13, 9, 9, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(input) bn11 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.9, gamma_init='he_normal')(c) a11 = PReLU()(bn11) mp11 = MaxPooling2D(pool_size=(2, 2), dim_ordering=K.image_dim_ordering(), border_mode='same')(input) m = merge([a11, mp11], mode='concat', concat_axis=channel_axis) # 16 layers c12 = Convolution2D(48, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(m) bn12 = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.9, gamma_init='he_normal')(c12) a12 = PReLU()(bn12) mp12 = MaxPooling2D(pool_size=(2, 2), dim_ordering=K.image_dim_ordering(), border_mode='same')(m) m = merge([a12, mp12], mode='concat', concat_axis=channel_axis) # 64 layers c13 = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(m) out = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.9, gamma_init='he_normal')(c13) last_out = PReLU()(out) for i in range(nb_res_layer): out = naive_inception_layer(last_out, K.image_dim_ordering(), channel_axis, mode, activation_type='prelu') last_out = merge([last_out, out], mode='sum') ct = ConvolutionTranspose2D(64, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(last_out) bn = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(ct) a = PReLU()(bn) ct = ConvolutionTranspose2D(16, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(a) bn = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.1, gamma_init='he_normal')(ct) a = PReLU()(bn) c = Convolution2D(3, 9, 9, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(a) out = ScaledSigmoid(scaling=255., name="output_node")(c) model = Model(input=[input], output=[out]) if weights_path: model.load_weights(weights_path) return model
def st_conv_inception_4(input_shape, weights_path=None, mode=0, nb_res_layer=5): if K.image_dim_ordering() == 'tf': channel_axis = 3 else: channel_axis = 1 input = Input(shape=input_shape, name='input_node', dtype=K.floatx()) # Downsampling c = Convolution2D(13, 7, 7, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(input) out = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.9, gamma_init='he_normal')(c) a = PReLU()(out) p = AveragePooling2D(pool_size=(2, 2), dim_ordering=K.image_dim_ordering(), border_mode='same')(input) m = merge([a, p], mode='concat', concat_axis=channel_axis) # 16 layers c = Convolution2D(48, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='same', activation='linear')(m) a = PReLU()(c) p = AveragePooling2D(pool_size=(2, 2), dim_ordering=K.image_dim_ordering(), border_mode='same')(m) m = merge([a, p], mode='concat', concat_axis=channel_axis) # 64 layers out = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(m) last_out = BatchNormalization(mode=mode, axis=channel_axis, momentum=0.9, gamma_init='he_normal')(out) last_out = Activation('relu')(last_out) for i in range(nb_res_layer): out = inception_layer(last_out, K.image_dim_ordering(), channel_axis, mode) last_out = merge([last_out, out], mode='sum') out = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='same', activation='linear')(last_out) out = ConvolutionTranspose2D(3, 5, 5, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(4, 4), border_mode='same', activation='linear')(out) out = ScaledSigmoid(scaling=255., name="output_node")(out) model = Model(input=[input], output=[out]) if weights_path: model.load_weights(weights_path) return model
def fast_st_ps(input_shape, weights_path=None, mode=0, nb_res_layer=5): input = Input(shape=input_shape, name='input_node', dtype=K.floatx()) # Downsampling p11 = ReflectPadding2D(padding=(4, 4))(input) c11 = Convolution2D(32, 9, 9, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='valid', activation='linear')(p11) bn11 = InstanceNormalization('inorm-1')(c11) a11 = Activation('relu')(bn11) p12 = ReflectPadding2D(padding=(1, 1))(a11) c12 = Convolution2D(64, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='valid', activation='linear')(p12) bn12 = InstanceNormalization('inorm-2')(c12) a12 = Activation('relu')(bn12) p13 = ReflectPadding2D(padding=(1, 1))(a12) c13 = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(2, 2), border_mode='valid', activation='linear')(p13) bn13 = InstanceNormalization('inorm-3')(c13) last_out = Activation('relu')(bn13) for i in range(nb_res_layer): p = ReflectPadding2D(padding=(1, 1))(last_out) c = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='valid', activation='linear')(p) bn = InstanceNormalization('inorm-res-%d' % i)(c) a = Activation('relu')(bn) p = ReflectPadding2D(padding=(1, 1))(a) c = Convolution2D(128, 3, 3, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='valid', activation='linear')(p) bn = InstanceNormalization('inorm-5-%d' % i)(c) # a = Activation('relu')(bn) last_out = merge([last_out, bn], mode='sum') # last_out = a out = PhaseShift(ratio=4, color=False)(last_out) out = ReflectPadding2D(padding=(4, 4))(out) out = Convolution2D(3, 9, 9, dim_ordering=K.image_dim_ordering(), init='he_normal', subsample=(1, 1), border_mode='valid', activation='linear')(out) out = ScaledSigmoid(scaling=255., name="output_node")(out) model = Model(input=[input], output=[out]) if weights_path: model.load_weights(weights_path) return model