简体   繁体   English

Keras:“指定为模型输入的张量不是输入张量。”如何将输出张量正确转换为输入张量?

[英]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.

相关问题 如何在keras.model.fit_generator中包含多个输入张量 - How to include multiple input tensor in keras.model.fit_generator Pytorch,INPUT(正常张量)和 WEIGHT(cuda 张量)不匹配 - Pytorch, INPUT (normal tensor) and WEIGHT (cuda tensor) mismatch 缺少必需的 arguments:input_tensor - Missing required arguments: input_tensor ValueError:输入张量必须具有等级 4 TensorFlow - ValueError: input tensor must have rank 4 TensorFlow Tensorflow:如何从张量流模型(注意 OCR 模型)中获取预期的输入图像大小 - Tensorflow: How to get the Expected input image size form tensor flow Model(Attention OCR Model) 将 keras 序列模型转换为张量 rt - convert a keras sequential model to tensor rt 重塑的输入是张量为788175的张量,但请求的形状为1050900 - Input to reshape is a tensor with 788175 values, but the requested shape has 1050900 张量和张量的区别 - Difference between Tensor and tensor ValueError:使用转换器 AutoTokenizer(MT5ForConditionalGerneration 模型)解码输入张量时,字节必须在范围 (0, 256) 内 - ValueError: bytes must be in range(0, 256) while decoding input tensor using transformer AutoTokenizer (MT5ForConditionalGerneration Model) 张量流对象检测API将输出图像的分辨率与输入测试图像相匹配 - Tensor-flow object detection API match resolution of output images to the input test images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM