小编典典

Keras不训练整个数据集

python

因此,我一直遵循Google的官方tensorflow指南,并尝试使用Keras构建一个简单的神经网络。但是,在训练模型时,它不使用整个数据集(具有60000个条目),而是仅使用1875个条目进行训练。有可能解决吗?

import tensorflow as tf
from tensorflow import keras
import numpy as np

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

train_images = train_images / 255.0
test_images = test_images / 255.0

class_names = ['T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot']

model = keras.Sequential([
                          keras.layers.Flatten(input_shape=(28, 28)),
                          keras.layers.Dense(128, activation='relu'), 
                          keras.layers.Dense(10)
])

model.compile(optimizer='adam',
              loss= tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=10)

输出:

Epoch 1/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3183 - accuracy: 0.8866
Epoch 2/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3169 - accuracy: 0.8873
Epoch 3/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3144 - accuracy: 0.8885
Epoch 4/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3130 - accuracy: 0.8885
Epoch 5/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3110 - accuracy: 0.8883
Epoch 6/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3090 - accuracy: 0.8888
Epoch 7/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3073 - accuracy: 0.8895
Epoch 8/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3057 - accuracy: 0.8900
Epoch 9/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3040 - accuracy: 0.8905
Epoch 10/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3025 - accuracy: 0.8915

<tensorflow.python.keras.callbacks.History at 0x7fbe0e5aebe0>

这是我一直在为此工作的原始Google
colab笔记本:https
://colab.research.google.com/drive/1NdtzXHEpiNnelcMaJeEm6zmp34JMcN38


阅读 295

收藏
2021-01-20

共1个答案

小编典典

1875模型拟合期间显示的数字不是训练样本;它是 数。

model.fit包括一个可选参数batch_size,根据文档所述

如果未指定,batch_size则默认为32。

因此,这里发生的是-您适合默认批处理大小32(因为您没有指定其他任何东西),因此数据的批处理总数为

60000/32 = 1875
2021-01-20