无论输入如何,Keras上的CNN都收敛到相同的值 [英] CNNs on Keras converge to the same value no matter the input
问题描述
我最近一直在学习Keras,并在带有CNN的CIFAR10数据集上进行了尝试.但是,我训练的模型(可以运行代码此处)返回相同的答案无论如何每次输入.我在模型定义中忘记了什么吗?
I've been learning Keras recently and I tried my hand at the CIFAR10 dataset with CNNs. However, the model I trained (you can run the code here) returns the same answer for every input, no matter what. Did I forget something in the model definition?
推荐答案
您忘记了对图像进行标准化.当前,x_train
中的值在[0,255]
范围内.这会导致较大的梯度更新并拖延训练过程.在这种情况下,一种简单的标准化方案是:
You have forgotten to normalize the images. Currently, the values in x_train
are in the range [0,255]
. This causes large gradient updates and stalls training process. One simple normalization scheme in this case would be:
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
这将导致这些值落在[0,1]
范围内.然后,您肯定会看到培训的进展.
This causes the values to fall in the range [0,1]
. Then you would surely see that the training progresses.
更复杂的归一化方案涉及按特征(即按像素)归一化或居中.在这种方法中,我们对所有图像进行归一化,以使所有图像中的每个像素的平均值为零,标准偏差为1(即,它们大部分落在[-1,1]
范围内):
A more sophisticated normalization scheme involves feature-wise (i.e. pixel-wise) normalization or centering. In this approach we normalize all the images such that each pixel in all the images have a mean of zero and a standard deviation of one (i.e. they mostly fall in the range [-1,1]
):
# make sure values are float
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_mean = x_train.mean(axis=0)
x_train -= x_mean
x_std = x_train.std(axis=0)
x_train /= x_std + 1e-8 # add a small constant to prevent division by zero
# normalize test data using the mean and std of training data
x_test -= x_mean
x_test /= x_std + 1e-8
请注意最后一部分:从不通过其自身的均值和标准差对测试数据进行标准化.改用训练平均值和std.
Note the last part: NEVER EVER normalize test data by its own mean and std. Use the training mean and std instead.
这篇关于无论输入如何,Keras上的CNN都收敛到相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!