我正在构建一个如下所示的网络。 model_A是一个分类模型,其单热编码输出与原始输入相结合,成为model_B的输入。
import keras
from keras.layers import Input, Dense
from keras.models import Model
inputs = Input(shape=(12,))
# ---------------------------------------
# model_A
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions_A = Dense(3, activation='softmax')(x)
model_A = Model(inputs=inputs, outputs=predictions_A)
# ---------------------------------------
# model_B
inputs_B = keras.layers.concatenate([inputs, predictions_A])
x1 = Dense(64, activation='relu')(inputs_B)
x1 = Dense(64, activation='relu')(x1)
predictions_B = Dense(1, activation='sigmoid')(x1)
model_B = Model(inputs=inputs_B, outputs=predictions_B)
model_A部分工作得很好。但是,当我开始添加model_B时,出现以下错误:
workspace/git/tensorplay/venv/lib/python3.7/site-packages/keras/engine/network.py:180: UserWarning: Model inputs must come from `keras.layers.Input` (thus holding past layer metadata), they cannot be the output of a previous non-Input layer. Here, a tensor specified as input to your model was not an Input tensor, it was generated by layer concatenate_7.
Note that input tensors are instantiated via `tensor = keras.layers.Input(shape)`.
The tensor that caused the issue was: concatenate_7/concat:0
str(x.name))
知道如何正确处理model_B的输入吗?谢谢!
如果您希望第一个模型的输出成为第二个模型的输入,同时将它们视为两个单独的模型,则应按以下方式执行此操作:
# Joint input layer for both model A and B
inputs = Input(shape=(12,))
# ---------------------------------------
# model_A
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions_A = Dense(3, activation='softmax')(x)
model_A = Model(inputs=inputs, outputs=predictions_A)
# ---------------------------------------
# model_B
# The output of model A, will be provided as input to model B from this layer
# (Make sure to adjust the dimension to correspond to the output of model A)
input_B_out_A = Input(shape=(3,))
# Concatenating the two input layers
concat = keras.layers.concatenate([inputs, input_B_out_A])
x1 = Dense(64, activation='relu')(concat)
x1 = Dense(64, activation='relu')(x1)
predictions_B = Dense(1, activation='sigmoid')(x1)
# When creating the model, I am specifying that there are two inputs layers
# one that will get the joint input, and the other that will get the output
# from model A.
model_B = Model(inputs=[inputs, input_B_out_A], outputs=predictions_B)
但是,如果您希望模型相互连接,则只需更改以下行:
model_B = Model(inputs=inputs, outputs=predictions_B)
实际上,model_B的inputs
与model_A是相同的输入层,您只是连接输入层和model_A的输出。整个代码将是:
# Joint input layer for both model A and B
inputs = Input(shape=(12,))
# ---------------------------------------
# model_A
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions_A = Dense(3, activation='softmax')(x)
model_A = Model(inputs=inputs, outputs=predictions_A)
# ---------------------------------------
# model_B
inputs_B = keras.layers.concatenate([inputs, predictions_A])
x1 = Dense(64, activation='relu')(inputs_B)
x1 = Dense(64, activation='relu')(x1)
predictions_B = Dense(1, activation='sigmoid')(x1)
model_B = Model(inputs=inputs, outputs=predictions_B)