Tensorflow:`Tensor`或`NumPy`输入数据需要`batch_size`或`steps`

时间:2019-11-07 15:57:46

标签: python tensorflow keras

考虑以下TensorFlow代码:

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)

在运行tf时出现错误:

  

ValueError:batch_sizesteps是必需的   Tensor输入数据。

在对NumPy的调用中添加了batch_size时:

fit()

然后抱怨:

  

ValueError:不得为model.fit(train_data, batch_size = BATCH_SIZE, epochs = NUM_EPOCHS, validation_data=(validation_inputs, validation_targets), verbose=2) 参数指定   给定输入类型。收到输入:,batch_size:100

这是什么错误?

3 个答案:

答案 0 :(得分:2)

发生错误是因为为tf.Dataset的自变量validation_data提供了Model.fit,但是Keras不知道要验证多少步骤。要解决此问题,您只需设置参数validation_steps。例如:

model.fit(train_data,
    batch_size=BATCH_SIZE,
    epochs=NUM_EPOCHS,
    validation_data=(validation_inputs, validation_targets),
    validation_steps=10)

答案 1 :(得分:2)

NUM_EPOCHS=5
    model.fit(train_data,epochs= NUM_EPOCHS,
    validation_data=(validation_inputs, validation_targets),
    validation_steps=10,verbose=2)

答案 2 :(得分:1)

如果您访问此链接-https://www.tensorflow.org/api_docs/python/tf/keras/Model,则将发现fit()要求参数-validation_steps,前提是提供了validate_data并且它是tf.data数据集。就像在代码中一样,好像您是通过拆分数据集的训练部分来创建了validation_data。