两个模型或者特征的融合-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()

你可能感兴趣的:(Tensorflow学习)