machine-learning - Tensorflow model.train() 没有循环遍历所有数据
问题描述
我正在尝试为 mnist 训练模型。
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
print(x_train.shape)
我得到的是(60000, 28, 28)
,数据集中有 60,000 个项目。
然后,我使用以下代码创建模型。
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
但是,每个时期我只得到 1875 个项目。
2020-06-02 04:33:45.706474: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-06-02 04:33:45.706617: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-06-02 04:33:47.437837: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-06-02 04:33:47.437955: E tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: UNKNOWN ERROR (303)
2020-06-02 04:33:47.441329: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-H3BEO7F
2020-06-02 04:33:47.441480: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-H3BEO7F
2020-06-02 04:33:47.441876: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-06-02 04:33:47.448274: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x27fc6b2c210 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-06-02 04:33:47.448427: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
Epoch 1/5
1875/1875 [==============================] - 1s 664us/step - loss: 0.2971 - accuracy: 0.9140
Epoch 2/5
1875/1875 [==============================] - 1s 661us/step - loss: 0.1421 - accuracy: 0.9582
Epoch 3/5
1875/1875 [==============================] - 1s 684us/step - loss: 0.1068 - accuracy: 0.9675
Epoch 4/5
1875/1875 [==============================] - 1s 695us/step - loss: 0.0868 - accuracy: 0.9731
Epoch 5/5
1875/1875 [==============================] - 1s 682us/step - loss: 0.0764 - accuracy: 0.9762
Process finished with exit code 0
解决方案
您正在使用整个数据,不用担心!
由于 Keras 文档,https://github.com/keras-team/keras/blob/master/keras/engine/training.py
当您使用model.fit
并且未指定批量大小时,默认分配为 32 .
batch_size 整数或 NULL。每次梯度更新的样本数。如果未指定,batch_size 将默认为 32
这意味着对于每个 epoch,您有 1875 个步骤,并且在每个步骤中,您的模型已将 32 个数据示例计入帐户。猜猜看,1875*32 等于 60,000。
推荐阅读
- c# - 试图从变量中减去 1,但它减去了 3。C# Unity
- mysql - 从 STRING 优化“SELECT datetime”的最佳方法
- vim - 如何在 vimrc 中设置环境变量?
- python - 使用 matplotlib 库中的 FuncAnimation (Python3)
- multithreading - 将数据“按块”写入 OpenCL 缓冲区
- mongodb - 如何将新元素添加到数组中,该数组是 MongoDB 文档中的字段
- mysql - MySQL 我无法将丹麦货币字符串列重新计算为瑞典货币值(整数)
- javascript - 如何使用js或jquery继承html中的高度
- postgresql - Hasura Graphql-如何将一个查询的输出作为输入传递给另一个查询?
- flutter - Flutter:为什么提供的数据在 Drawer 小部件中不可用