我正在建立如下所示的网络。 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的输入吗?谢谢!
答案 0 :(得分:1)
如果要将第一个模型的输出作为第二个模型的输入,同时将它们视为两个单独的模型,则应按以下方式进行操作:
# 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)