Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: `batch_size` or `steps` is required for `Tensor` or `NumPy` input data

Consider the following TensorFlow code:

import numpy as np
import tensorflow as tf

import tensorflow_datasets as tfds

mnist_dataset, mnist_info = tfds.load(name = 'mnist', with_info=True, as_supervised=True)

mnist_train, mnist_test = mnist_dataset['train'], mnist_dataset['test']

num_validation_samples = 0.1 * mnist_info.splits['train'].num_examples
num_validation_samples = tf.cast(num_validation_samples, tf.int64)

num_test_samples = mnist_info.splits['test'].num_examples
num_test_samples = tf.cast(num_test_samples, tf.int64)

def scale(image, label):
    image = tf.cast(image, tf.float32)
    image /= 255.
    return image, label

scaled_train_and_validation_data = mnist_train.map(scale)
test_data = mnist_test.map(scale)

BUFFER_SIZE = 10_000

shuffled_train_and_validation_data = scaled_train_and_validation_data.shuffle(BUFFER_SIZE)

validation_data = shuffled_train_and_validation_data.take(num_validation_samples)
train_data = shuffled_train_and_validation_data.skip(num_validation_samples)

BATCH_SIZE = 100
train_data = train_data.batch(BATCH_SIZE)
validation_data = validation_data.batch(num_validation_samples) # Single batch, having size equal to number of validation samples
test_data = test_data.batch(num_test_samples)

validation_inputs, validation_targets = next(iter(validation_data))

input_size = 784 # One for each pixel of the 28 * 28 image
output_size = 10
hidden_layer_size = 50 # Arbitrary chosen

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28,1)),
    tf.keras.layers.Dense(hidden_layer_size, activation='relu'), # First hidden layer
    tf.keras.layers.Dense(hidden_layer_size, activation='relu'),
    tf.keras.layers.Dense(output_size, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
NUM_EPOCHS = 5
model.fit(train_data, epochs = NUM_EPOCHS, validation_data=(validation_inputs, validation_targets), verbose=2)

On running it tf gives the error:

ValueError: batch_size or steps is required for Tensor or NumPy input data.

When batch_size is added in the call to fit():

model.fit(train_data, batch_size = BATCH_SIZE, epochs = NUM_EPOCHS, validation_data=(validation_inputs, validation_targets), verbose=2)

It then complains:

ValueError: The batch_size argument must not be specified for the given input type. Received input: , batch_size: 100

What is the error here?

like image 910
Kshitiz Sharma Avatar asked Nov 07 '19 15:11

Kshitiz Sharma


People also ask

What is a tf data dataset?

The tf. data API enables you to build complex input pipelines from simple, reusable pieces. For example, the pipeline for an image model might aggregate data from files in a distributed file system, apply random perturbations to each image, and merge randomly selected images into a batch for training.

Why use tf dataset?

Dataset can perform shuffling and batching of samples efficiently. Useful for large datasets as well as small datasets. It could combine train and test datasets.

What is a good TensorFlow batch size?

What Is A Good Batch Size Tensorflow? a set of 32 or 25 instances, so the epoch values are at least 100 unless the Dataset size is large. With a batch size of 10 on top of 100 epochs, it is possible to use a datasets of 100,000 and larger. What Is Batch Size?

How does batching work in TensorFlow?

The simplest form of batching stacks n consecutive elements of a dataset into a single element. The Dataset.batch () transformation does exactly this, with the same constraints as the tf.stack () operator, applied to each component of the elements: i.e. for each component i, all elements must have a tensor of the exact same shape.

What file formats does TensorFlow support?

The tf.data API supports a variety of file formats so that you can process large datasets that do not fit in memory. For example, the TFRecord file format is a simple record-oriented binary format that many TensorFlow applications use for training data.

How does TensorFlow work with Python?

The first time you run the tf.function, although it executes in Python, it captures a complete, optimized graph representing the TensorFlow computations done within the function. On subsequent calls TensorFlow only executes the optimized graph, skipping any non-TensorFlow steps.


1 Answers

The error happens because a tf.Dataset is provided to the argument validation_data of Model.fit, but Keras does not know how many steps to validate for. To solve this problem, you can just set the argument validation_steps. For example:

model.fit(train_data,
    batch_size=BATCH_SIZE,
    epochs=NUM_EPOCHS,
    validation_data=(validation_inputs, validation_targets),
    validation_steps=10)
like image 127
rvinas Avatar answered Nov 15 '22 13:11

rvinas