为什么卷积网络使用每64张图像进行训练?

Why does convolutional network use every 64 image for training?

我正在寻找来自 here 的 Python 3.5 + TensorFlow + TFLearn 的代码:

# -*- coding: utf-8 -*-

""" Convolutional Neural Network for MNIST dataset classification task.

References:
    Y. LeCun, L. Bottou, Y. Bengio, and P. Haffner. "Gradient-based
    learning applied to document recognition." Proceedings of the IEEE,
    86(11):2278-2324, November 1998.

Links:
    [MNIST Dataset] http://yann.lecun.com/exdb/mnist/

"""

from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression

# Data loading and preprocessing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=True)
X = X.reshape([-1, 28, 28, 1])
testX = testX.reshape([-1, 28, 28, 1])

# Building convolutional network
network = input_data(shape=[None, 28, 28, 1], name='input')
network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = fully_connected(network, 128, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 256, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='adam', learning_rate=0.01,
                     loss='categorical_crossentropy', name='target')

# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit({'input': X}, {'target': Y}, n_epoch=20,
           validation_set=({'input': testX}, {'target': testY}),
           snapshot_step=100, show_metric=True, run_id='convnet_mnist')

好的,它有效。但它在学习时仅使用集合中的每 64 张图像。为什么会这样? 如果我的场景很小,想让网络使用每一张第一张图片怎么办?

培训信息示例

Training Step: 1  | time: 2.416s
| Adam | epoch: 001 | loss: 0.00000 -- iter: 064/55000
Training Step: 2  | total loss: 0.24470 | time: 4.621s
| Adam | epoch: 001 | loss: 0.24470 -- iter: 128/55000
Training Step: 3  | total loss: 0.10852 | time: 6.876s
| Adam | epoch: 001 | loss: 0.10852 -- iter: 192/55000
Training Step: 4  | total loss: 0.20421 | time: 9.129s
| Adam | epoch: 001 | loss: 0.20421 -- iter: 256/55000

它不仅使用每 64 张图像,而且正在加载 64 张图像的批次。这就是为什么您会看到 iter 每次都增加 64,因为它在每个训练步骤中处理了 64 张图像。 查看回归层 http://tflearn.org/layers/estimator/ 的文档,您可以在此处设置批量大小。

我自己做过。 此参数由model.fit中的batch_size规定。默认为 64。 因此,要使用每个第一张图片,您需要按以下方式重写最后一个字符串:

model.fit({'input': X}, {'target': Y}, n_epoch=20,
           validation_set=({'input': testX}, {'target': testY}),
           snapshot_step=100, batch_size=1, 
           show_metric=True,run_id='convnet_mnist')