两个模型或者特征的融合-concatenate笔记

keras的序贯式不够灵活,采用函数式。融合要注意concatenate后面的axis的选择。除了叠加的维度之外,其他维度都必须相同。
注意两个input,然后fit里面的输入是[input1,input2]
可以定义两个model函数,然后定义第三个model表示为前两个的融合。
下面是在FC层的融合,实际上特征数目增加了。
完全可以不用这么麻烦,直接定义在一个函数里面即可,连接的时候把前面定义的直接连到后面要输入的层中。

def model_ff(input2):
    n_hidden_5 = 512  # hidden layer5
    weights_decay = 1e-2
    n_classes1 = 512  # dense layer
    x = Flatten()(input2)
    x_fft = Dense(n_classes1, kernel_regularizer=keras.regularizers.l2(weights_decay))(x)
    model = keras.Model(inputs=input2, outputs=x_fft)
    return model


def model_f(input):
    # input = Input(shape=shape1)
    n_hidden_1 = 8  # hidden layer1
    n_input = 4  # input layer
    n_classes1 = 512  # dense layer
    model_dir = 'models'
    conv_initialize = "he_uniform"  # "uniform", "he_normal", "glorot_normal", "glorot_uniform"
    weights_decay = 1e-2
    x = Convolution2D(n_hidden_1, (1, 1), activation='relu', padding='same', bias=False, kernel_initializer=\
        conv_initialize, kernel_regularizer=keras.regularizers.l2(weights_decay))(input)
    x = AveragePooling2D(pool_size=(1, 2))(x)
    x = BatchNormalization()(x)
    x = Flatten()(x)
    x = Dense(n_classes1, kernel_regularizer=keras.regularizers.l2(weights_decay))(x)
    model = keras.Model(inputs=input, outputs=x)
    return model

def merge_model(win_length=256, fft_points=512):
    inputs = Input(shape=(1, win_length, 3))
    input2 = Input(shape=(1, fft_points, 3))
    n_input = 4  # input layer
    n_classes1 = 512  # dense layer
    # n_classes2 = 64  # dense layer
    n_classes3 = 1  # output layer
    model_dir = 'models'
    weights_decay = 1e-2

    model1 = model_f(inputs)
    model2 = model_ff(input2)
    r1 = model1.output
    r2 = model2.output
    x = keras.layers.Concatenate(axis=1)([r1, r2])
    x = Dense(n_classes3, kernel_regularizer=keras.regularizers.l2(weights_decay))(x)
    **model = Model(inputs=[inputs, input2], outputs=x)**
    return model

model = merge_model()
model.summary()
  • 6
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值