python整合图层,python-在keras的合并层上进行培训

from keras.layers import *

from keras.models import Model

print 'Compiling two-path model...'

# Input of the model

input_model = Input(shape=(4,33,33))

# Local pathway

#Add first convolution

model_l = Convolution2D(64,7,7,

border_mode='valid',

activation='relu',

W_regularizer=l1l2(l1=0.01, l2=0.01))(input_model)

model_l = BatchNormalization(mode=0,axis=1)(model_l)

model_l = MaxPooling2D(pool_size=(2,2),strides=(1,1))(model_l)

model_l = Dropout(0.5)(model_l)

#Add second convolution

model_l = Convolution2D(64,3,3,

border_mode='valid',

W_regularizer=l1l2(l1=0.01, l2=0.01),

input_shape=(4,33,33))(model_l)

model_l = BatchNormalization(mode=0,axis=1)(model_l)

model_l = MaxPooling2D(pool_size=(4,4),strides=(1,1))(model_l)

model_l = Dropout(0.5)(model_l)

#global pathway

model_g = Convolution2D(160,12,12,

border_mode='valid',

activation='relu',

W_regularizer=l1l2(l1=0.01, l2=0.01))(input_model)

model_g = BatchNormalization(mode=0,axis=1)(model_g)

model_g = MaxPooling2D(pool_size=(2,2), strides=(1,1))(model_g)

model_g = Dropout(0.5)(model_g)

# merge local and global pathways

merge = Merge(mode='concat', concat_axis=1)([model_l,model_g])

merge = Convolution2D(5,21,21,

border_mode='valid',

W_regularizer=l1l2(l1=0.01, l2=0.01))(merge)

merge = Flatten()(merge)

predictions = Dense(5, activation='softmax')(merge)

model_merged = Model(input=input_model,output=predictions)

sgd = SGD(lr=0.001, decay=0.01, momentum=0.9)

model_merged.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

print('Done')

return model_merged

这等效于您发布但使用Functional API定义的网络

如您所见,只有1个Input图层,使用了两次.然后,您可以像说的那样训练它:

model_merged.fit(X_train, Y_train, batch_size=self.batch_size, nb_epoch=self.n_epoch, validation_split=0.1, verbose=1)

这有帮助吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值