简体   繁体   English

ValueError:层 sequential_20 需要 1 个输入,但它收到了 2 个输入张量

[英]ValueError: Layer sequential_20 expects 1 inputs, but it received 2 input tensors

I am trying to build a simple Autoencoder using the KMNIST dataset from Tensorflow and some sample code from a textbook I'm using, but I keep getting an error when I try to fit the model.我正在尝试使用来自 Tensorflow 的 KMNIST 数据集和我正在使用的教科书中的一些示例代码构建一个简单的自动编码器,但是当我尝试拟合 model 时,我总是遇到错误。

The error says ValueError: Layer sequential_20 expects 1 inputs, but it received 2 input tensors.错误显示ValueError: Layer sequential_20 expects 1 inputs, but it received 2 input tensors.

I'm really new to TensorFlow, and all my research on this error has baffled me since it seems to involve things not in my code.我真的是 TensorFlow 的新手,我对这个错误的所有研究都让我感到困惑,因为它似乎涉及我的代码中没有的东西。 This thread wasn't helpful since I'm only using sequential layers. 这个线程没有帮助,因为我只使用顺序层。

Code in full:完整代码:

import numpy as np
import tensorflow as tf
from tensorflow import keras
import tensorflow_datasets as tfds
import pandas as pd
import matplotlib.pyplot as plt

#data = tfds.load(name = 'kmnist')

(img_train, label_train), (img_test, label_test) = tfds.as_numpy(tfds.load(
    name = 'kmnist',
    split=['train', 'test'],
    batch_size=-1,
    as_supervised=True,
))

img_train = img_train.squeeze()
img_test = img_test.squeeze()

## From Hands on Machine Learning Textbook, chapter 17

stacked_encoder = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28, 28]),
    keras.layers.Dense(100, activation="selu"),
    keras.layers.Dense(30, activation="selu"),
])

stacked_decoder = keras.models.Sequential([
    keras.layers.Dense(100, activation="selu", input_shape=[30]),
    keras.layers.Dense(28 * 28, activation="sigmoid"),
    keras.layers.Reshape([28, 28])
])

stacked_ae = keras.models.Sequential([stacked_encoder, stacked_decoder])
stacked_ae.compile(loss="binary_crossentropy",
                   optimizer=keras.optimizers.SGD(lr=1.5))

history = stacked_ae.fit(img_train, img_train, epochs=10,
                         validation_data=[img_test, img_test])

it helped me when I changed:当我改变时它帮助了我:
validation_data=[X_val, y_val] into validation_data=(X_val, y_val) validation_data=[X_val, y_val]转化为validation_data=(X_val, y_val)
Actually still wonder why?其实还是想知道为什么?

Use validation_data=(img_test, img_test) instead of validation_data=[img_test, img_test]使用validation_data=(img_test, img_test)而不是validation_data=[img_test, img_test]

Here the example with encoder and decoder combined together:这里将编码器和解码器组合在一起的示例:

stacked_ae = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28, 28]),
    keras.layers.Dense(100, activation="selu"),
    keras.layers.Dense(30, activation="selu"),
    keras.layers.Dense(100, activation="selu"),
    keras.layers.Dense(28 * 28, activation="sigmoid"),
    keras.layers.Reshape([28, 28])
])

stacked_ae.compile(loss="binary_crossentropy",
                   optimizer=keras.optimizers.SGD(lr=1.5))

history = stacked_ae.fit(img_train, img_train, epochs=10,
                         validation_data=(img_test, img_test))

As stated in the Keras API reference ( link ),如 Keras API 参考(链接)中所述,

validation_data : ... validation_data could be: - tuple (x_val, y_val) of Numpy arrays or tensors - tuple (x_val, y_val, val_sample_weights) of Numpy arrays - dataset... validation_data : ... validation_data could be: - tuple (x_val, y_val) of Numpy arrays or tensors - tuple (x_val, y_val, val_sample_weights) of Numpy arrays - dataset...

So, validation_data has to be a tuple rather than a list (of Numpy arrays or tensors).因此, validation_data必须是一个元组而不是一个列表(Numpy arrays 或张量)。 We should use parentheses (round brackets) (...) , not square brackets [...] .我们应该使用括号(圆括号) (...) ,而不是方括号[...]

According to my limited experience, however, TensorFlow 2.0.0 would be indifferent to the use of square brackets, but TensorFlow 2.3.0 would complain about it .然而,根据我有限的经验, TensorFlow 2.0.0 对使用方括号无所谓,但 TensorFlow 2.3.0 会抱怨它 Your script would be fine if it is run under TF 2.0 intead of TF 2.3.如果它在 TF 2.0 而不是 TF 2.3 下运行,您的脚本会很好。

In solutions, some says you need to change from bracelet to parentheses, but it was not working in Colab.在解决方案中,有人说您需要从手镯更改为括号,但它在 Colab 中不起作用。 And yes, turning validation_data=[X_val, y_val] into validation_data=(X_val, y_val) should work since it's required format, but in tf==2.5.0(in Google Colab) it doesn't solve the problem.是的,将validation_data=[X_val, y_val]转换为validation_data=(X_val, y_val)应该可以工作,因为它是必需的格式,但在 tf==2.5.0(在 Google Colab 中)它不能解决问题。 I changed from functional API to sequential API, that solves the problem.我从功能 API 更改为顺序 API,解决了问题。 Strange.奇怪的。

This error may also be triggered by submitting the wrong object to model.fit() .将错误的 object 提交给model.fit()也可能会触发此错误。 It happened to me when I mistakenly tried to execute当我错误地尝试执行时发生在我身上

model.fit(images)

when I wanted to execute当我想执行

model.fit(dataset)

with

dataset = tf.data.Dataset.from_tensor_slices((images, images))

You have given data instead of labels two times:您两次给出了数据而不是标签:

history = stacked_ae.fit(img_train, img_train, epochs=10,
                         validation_data=[img_test, img_test])

instead of代替

history = stacked_ae.fit(img_train, label_train, epochs=10,
                         validation_data=[img_test, label_test])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:层顺序需要 1 个输入,但它接收到 250 个输入张量 - ValueError: Layer sequential expects 1 inputs, but it received 250 input tensors ValueError:层顺序需要 1 个输入,但它在 tensorflow 2.0 中收到 211 个输入张量 - ValueError: Layer sequential expects 1 inputs, but it received 211 input tensors in tensorflow 2.0 tensorFlow 向我抛出错误ValueError:层顺序需要1个输入,但它收到2个输入张量 - tensorFlow throws me the error ValueError: Layer sequential expects 1 inputs, but it received 2 input tensors ValueError:层顺序需要 1 个输入,但它收到 239 个输入张量 - ValueError: Layer sequential expects 1 input(s), but it received 239 input tensors 使用 ValueError 构建自定义联合平均过程:层顺序需要 1 个输入,但它收到 3 个输入张量 - Build Custom Federated averaging process with ValueError: Layer sequential expects 1 inputs, but it received 3 input tensors ValueError:层顺序_16 需要 1 个输入,但它收到 8 个输入张量 - ValueError: Layer sequential_16 expects 1 input(s), but it received 8 input tensors Tensorflow & Keras 层“顺序”需要 1 个输入,但它接收到 2 个输入张量 - Tensorflow & Keras Layer "sequential" expects 1 input(s), but it received 2 input tensors ValueError:层鉴别器需要 1 个输入,但它收到 2 个输入张量 - ValueError: Layer Discriminator expects 1 input(s), but it received 2 input tensors ValueError:model 层需要 21 个输入,但它接收到 1 个输入张量 - ValueError: Layer model expects 21 input(s), but it received 1 input tensors 为什么在我拟合模型时代码会生成错误”ValueError:层“sequential_3”需要 1 个输入,但它接收到 2 个输入张量 - why may code generates the error while i fit the model" ValueError: Layer "sequential_3" expects 1 input(s), but it received 2 input tensors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM