[英]Keras: “a tensor specified as input to your model was not an Input tensor.” How to properly transform the output tensor to input tensor?
I am building a Network like below. 我正在建立如下所示的网络。 model_A is a classification model, and its one-hot-encoding outputs combined with the original inputs becomes the inputs of model_B. 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)
The model_A part works just fine. model_A部分工作正常。 However, when I started to add the model_B, I got the following error: 但是,当我开始添加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))
Any idea what's the properly handle the inputs for model_B? 知道正确处理model_B的输入是什么吗? Thanks! 谢谢!
If you want the output of the first model, to be the input of the second model, while treating them as two separate models, then you should do it in the following way: 如果要将第一个模型的输出作为第二个模型的输入,同时将它们视为两个单独的模型,则应按以下方式进行操作:
# 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)
However, if you want the models to be interconnected, you just need to change the following line: 但是,如果您希望将模型互连,则只需更改以下行:
model_B = Model(inputs=inputs, outputs=predictions_B)
Actually, the inputs
of model_B is the same input layer as model_A, where you are just concatenating the input layer and the output of model_A. 实际上,model_B的inputs
与model_A是相同的输入层,在这里您只是串联了model_A的输入层和输出。 The whole code would be: 整个代码将是:
# 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.