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

时间:2017-04-07 08:36:59

标签: python python-3.x tensorflow tflearn

我正在寻找来自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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

我自己完成了。 此参数由 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')