Tensorflow-model.fit中的值错误-如何修复

时间:2019-10-29 10:10:15

标签: python-3.x tensorflow machine-learning neural-network mnist

我正在尝试使用MNIST数据集训练深度神经网络。

BATCH_SIZE = 100
train_data = train_data.batch(BATCH_SIZE)
validation_data = validation_data.batch(num_validation_samples)
test_data = scaled_test_data.batch(num_test_samples)

validation_inputs, validation_targets = next(iter(validation_data))

input_size = 784
output_size = 10
hidden_layer_size = 50

model = tf.keras.Sequential([
                    tf.keras.layers.Flatten(input_shape=(28,28,1)),
                    tf.keras.layers.Dense(hidden_layer_size, activation='relu'),
                    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))

model.fit引发以下错误

-------------------------------------------------------------------------

--
ValueError                                Traceback (most recent call last)
<ipython-input-58-c083185dafc6> in <module>
      1 NUM_EPOCHS = 5
----> 2 model.fit(train_data, epochs=NUM_EPOCHS, validation_data=(validation_inputs,validation_targets))

~/anaconda3/envs/py3-TF2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    726         max_queue_size=max_queue_size,
    727         workers=workers,
--> 728         use_multiprocessing=use_multiprocessing)
    729 
    730   def evaluate(self,

~/anaconda3/envs/py3-TF2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
    222           validation_data=validation_data,
    223           validation_steps=validation_steps,
--> 224           distribution_strategy=strategy)
    225 
    226       total_samples = _get_total_number_of_samples(training_data_adapter)

~/anaconda3/envs/py3-TF2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in _process_training_inputs(model, x, y, batch_size, epochs, sample_weights, class_weights, steps_per_epoch, validation_split, validation_data, validation_steps, shuffle, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    562                                     class_weights=class_weights,
    563                                     steps=validation_steps,
--> 564                                     distribution_strategy=distribution_strategy)
    565     elif validation_steps:
    566       raise ValueError('`validation_steps` should not be specified if '

~/anaconda3/envs/py3-TF2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py in _process_inputs(model, x, y, batch_size, epochs, sample_weights, class_weights, shuffle, steps, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    604       max_queue_size=max_queue_size,
    605       workers=workers,
--> 606       use_multiprocessing=use_multiprocessing)
    607   # As a fallback for the data type that does not work with
    608   # _standardize_user_data, use the _prepare_model_with_inputs.

~/anaconda3/envs/py3-TF2/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, batch_size, epochs, steps, shuffle, **kwargs)
    252     if not batch_size:
    253       raise ValueError(
--> 254           "`batch_size` or `steps` is required for `Tensor` or `NumPy`"
    255           " input data.")
    256 

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

训练和验证数据是从MNIST数据集中获得的。数据的一部分用作训练数据,而某些用作测试数据。

我在这里做什么错了?

更新 根据Dominques的建议,我已将model.fit更改为

model.fit(train_data, batch_size=128, epochs=NUM_EPOCHS, validation_data=(validation_inputs,validation_targets))

但是现在,我收到以下错误消息

ValueError: The `batch_size` argument must not be specified for the given input type. Received input: <BatchDataset shapes: ((None, 28, 28, 1), (None,)), types: (tf.float32, tf.int64)>, batch_size: 128

5 个答案:

答案 0 :(得分:0)

您需要指定批量大小,即每次迭代中应包含多少个数据点。如果您查看文档,将会看到没有设置默认值。

https://www.tensorflow.org/api_docs/python/tf/keras/Sequential

您可以通过在fit命令中添加batch_size来设置值。好的值通常是沿着2 ** n的数字,因为这样可以更有效地处理多个核。对您来说,这应该不会有很大的不同:)

model.fit(train_data, 
          batch_size=128
          epochs=NUM_EPOCHS, 
          validation_data=(validation_inputs,validation_targets))

答案 1 :(得分:0)

tf文档将为您提供更多提示您为什么会出错的线索。

https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit

validation_data: Data on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. validation_data will override validation_split. 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 

对于前两种情况,必须提供batch_size。对于最后一种情况,必须提供validation_steps。

由于您已经批处理了验证数据集,请考虑直接使用它并指定以下验证步骤。

BATCH_SIZE = 100
train_data = train_data.batch(BATCH_SIZE)
validation_data = validation_data.batch(BATCH_SIZE)
...
model.fit(train_data, epochs=NUM_EPOCHS, validation_data=validation_data,validation_steps=1)

答案 2 :(得分:0)

为什么没人提到我不知道,但是您的问题是 Y_train 数据。您不将其作为模型的参数提供。

model.fit(X_Train, y_train, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1, max_queue_size=10, workers=1, use_multiprocessing=False)

您给的不是y_train:

model.fit(train_data, batch_size=128 ....

出现错误提示:

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

希望对您有帮助。

答案 3 :(得分:0)

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

更改为(通过添加validation_steps = 1)将达到目的

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

答案 4 :(得分:0)

我将input_shape=(28,28,1)更改为input_shape=(28,28,3),它对我有用。