我正在尝试使用Tensorflow的KMNIST数据集和我正在使用的教科书中的一些示例代码来构建一个简单的自动编码器,但是当我尝试拟合模型时却不断出现错误。
错误显示ValueError: Layer sequential_20 expects 1 inputs, but it received 2 input tensors.我真的是TensorFlow的新手,我对这个错误的所有研究都使我感到困惑,因为它似乎不包含我的代码中的内容。
This thread没有帮助,因为我仅使用顺序图层。
完整代码:

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)其实还是想知道为什么吗?

10-08 18:39