如何结合来自两个CNN模型的特征?

我有两个遵循相同架构的CNN模型。我在cnn1上训练了’训练集1’,在cnn2上训练了’训练集2’。然后我使用以下代码提取了特征。

# cnn1

    model.pop() #移除softmax层    model.pop() #移除dropout层    model.pop() #移除激活层    model.pop() #移除批量归一化层    model.build() #这里是密集512层    features1 = model.predict(train set 1)    print(features1.shape) #600,512

# cnn2

    model.pop() #移除softmax层    model.pop() #移除dropout层    model.pop() #移除激活层    model.pop() #移除批量归一化层    model.build() #这里是密集512层    features2 = model.predict(train set 2)    print(features2.shape) #600,512

如何将这些特征1和特征2结合起来,使输出形状为600,1024?


回答:

最简单的解决方案:

你可以简单地以这种方式连接两个网络的输出:

features = np.concatenate([features1, features2], 1)

替代方案:

对于两个具有相同结构的已训练模型,无论它们的结构如何,你可以以这种方式结合它们

# 生成虚拟数据n_sample = 600set1 = np.random.uniform(0,1, (n_sample,30))set2 = np.random.uniform(0,1, (n_sample,30))# 模型1inp1 = Input((30,))x1 = Dense(512,)(inp1)x1 = Dropout(0.3)(x1)x1 = BatchNormalization()(x1)out1 = Dense(3, activation='softmax')(x1)m1 = Model(inp1, out1)# m1.fit(...)# 模型2inp2 = Input((30,))x2 = Dense(512,)(inp2)x2 = Dropout(0.3)(x2)x2 = BatchNormalization()(x2)out2 = Dense(3, activation='softmax')(x2)m2 = Model(inp2, out2)# m2.fit(...)# 连接所需的输出concat = Concatenate()([m1.layers[1].output, m2.layers[1].output]) # 获取密集512层的输出merge = Model([m1.input, m2.input], concat)# 进行合并预测merge.predict([set1,set2]).shape  # (n_sample, 1024)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注