Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

521
Views
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.

The error says 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. 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])
over 4 years ago · Santiago Trujillo
5 answers
Answer question

0

In solutions, some says you need to change from bracelet to parentheses, but it was not working in Colab. And yes, turningvalidation_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. I changed from functional API to sequential API, that solves the problem. Strange.

over 4 years ago · Santiago Trujillo Report

0

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])
over 4 years ago · Santiago Trujillo Report

0

Use validation_data=(img_test, img_test) instead of 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))
over 4 years ago · Santiago Trujillo Report

0

it helped me when I changed:
validation_data=[X_val, y_val] into validation_data=(X_val, y_val)
Actually still wonder why?

over 4 years ago · Santiago Trujillo Report

0

As stated in the Keras API reference (link),

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). 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. Your script would be fine if it is run under TF 2.0 intead of TF 2.3.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!