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
orsteps
is required forTensor
orNumPy
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?
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.
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 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?
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With