Keras不考虑batch_input参数



我正在用keras训练一个神经网络,它似乎不能正确解释batch_size参数。

请看下面的代码(这个应用程序很愚蠢,我关心的是输出)。

import numpy as np 
from keras.models import Sequential
from keras.layers import Activation, Dense, Reshape
import keras 
class LossHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.losses = []
    def on_batch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))
history = LossHistory()

X = np.random.normal(0, 1, (1000, 2))
Y = np.random.normal(0, 1, (1000, 3))
model = Sequential()
model.add(Dense(20, input_shape = (2,), name='input layer dude'))
model.add(Activation('relu'))
model.add(Dense(12))
model.add(Activation('relu'))
model.add(Dense(8))
model.add(Activation('linear'))
model.add(Dense(3))
model.add(Activation('linear'))
model.add(Reshape(target_shape=(3,), name='output layer dude'))
model.compile(optimizer='adam', loss='mse', )

当我调用这个模型时:

model.fit(X, Y, batch_size=10, nb_epoch=10, callbacks=[history])

输出似乎表明它不是每批处理10个项目,而是1000个(这是总样本数)。

Epoch 1/10
1000/1000 [==============================] - 0s - loss: 898.6197      
Epoch 2/10
1000/1000 [==============================] - 0s - loss: 31.5123     
Epoch 3/10
1000/1000 [==============================] - 0s - loss: 16.7140     
Epoch 4/10
1000/1000 [==============================] - 0s - loss: 11.4034     
Epoch 5/10
1000/1000 [==============================] - 0s - loss: 8.9275     
Epoch 6/10
1000/1000 [==============================] - 0s - loss: 7.4699     
Epoch 7/10
1000/1000 [==============================] - 0s - loss: 6.5648     
Epoch 8/10
1000/1000 [==============================] - 0s - loss: 5.9576     
Epoch 9/10
1000/1000 [==============================] - 0s - loss: 5.5064     
Epoch 10/10
1000/1000 [==============================] - 0s - loss: 5.1514     

有什么问题吗?

他实际上正在考虑这件事。epoch是对整个数据集的迭代,因此是1000/1000。

我将批大小更改为128,使其更具可读性,并添加回调以在每个批处理后打印损失,我得到的是这样的(我还增加了数据量以提高可读性):

Using Theano backend.
Using gpu device 1: GeForce GTX 770 (CNMeM is disabled, cuDNN 5105)
Epoch 1/10
 mbloss 1.00058555603 lr 0.0010000000475
  128/10000 [..............................] - ETA: 3s - loss: 1.0006 mbloss 1.00051558018 lr 0.0010000000475
  256/10000 [..............................] - ETA: 4s - loss: 1.0006 mbloss 1.00094401836 lr 0.0010000000475
  384/10000 [>.............................] - ETA: 4s - loss: 1.0007 mbloss 1.00001847744 lr 0.0010000000475
  512/10000 [>.............................] - ETA: 3s - loss: 1.0005 mbloss 1.00019526482 lr 0.0010000000475
  640/10000 [>.............................] - ETA: 3s - loss: 1.0005 mbloss 0.999684214592 lr 0.0010000000475
  768/10000 [=>............................] - ETA: 3s - loss: 1.0003 mbloss 0.999649345875 lr 0.0010000000475
  896/10000 [=>............................] - ETA: 3s - loss: 1.0002 mbloss 1.00126934052 lr 0.0010000000475
 1024/10000 [==>...........................] - ETA: 3s - loss: 1.0004 mbloss 1.00039303303 lr 0.0010000000475
 1152/10000 [==>...........................] - ETA: 3s - loss: 1.0004 mbloss 1.00083625317 lr 0.0010000000475
 1280/10000 [==>...........................] - ETA: 3s - loss: 1.0004 mbloss 1.00036990643 lr 0.0010000000475
 1408/10000 [===>..........................] - ETA: 2s - loss: 1.0004 mbloss 0.999625504017 lr 0.0010000000475
 1536/10000 [===>..........................] - ETA: 2s - loss: 1.0003 mbloss 1.0005017519 lr 0.0010000000475
 1664/10000 [===>..........................] - ETA: 2s - loss: 1.0004 mbloss 0.999049901962 lr 0.0010000000475
 1792/10000 [====>.........................] - ETA: 2s - loss: 1.0003 mbloss 0.999758243561 lr 0.0010000000475
 1920/10000 [====>.........................] - ETA: 2s - loss: 1.0002 mbloss 0.99894207716 lr 0.0010000000475
 2048/10000 [=====>........................] - ETA: 2s - loss: 1.0001 mbloss 1.00113630295 lr 0.0010000000475
 2176/10000 [=====>........................] - ETA: 2s - loss: 1.0002 mbloss 0.999107062817 lr 0.0010000000475

如果你需要它,回调在批处理结束时打印一些东西:

class MBLossPrint(Callback):
    def on_batch_end(self, batch, logs={}):
        print ' mbloss', logs['loss'], 'lr', self.model.optimizer.lr.get_value()

希望这对你有帮助

相关内容

  • 没有找到相关文章

最新更新