问题描述
我正在尝试使用来自 Tensorflow 的 KMNIST 数据集和我正在使用的教科书中的一些示例代码构建一个简单的自动编码器,但是当我尝试拟合模型时总是出现错误.
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.
错误说 ValueError: LayerSequence_20 需要 1 个输入,但它收到了 2 个输入张量.
我对 TensorFlow 真的很陌生,我对这个错误的所有研究都让我感到困惑,因为它似乎涉及我的代码中没有的东西.这个话题没有帮助,因为我只使用顺序层.
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.This thread wasn't helpful since I'm only using sequential layers.
完整代码:
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])
推荐答案
当我改变时它帮助了我:validation_data=[X_val, y_val]
转化为validation_data=(X_val, y_val)
其实还想知道为什么?
it helped me when I changed:validation_data=[X_val, y_val]
into validation_data=(X_val, y_val)
Actually still wonder why?
这篇关于ValueError:层顺序_20 需要 1 个输入,但它收到 2 个输入张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!