gpt4 book ai didi

python - 无论输入如何,Keras 上的 CNN 都会收敛到相同的值

转载 作者:行者123 更新时间:2023-11-30 09:28:37 25 4
gpt4 key购买 nike

我最近一直在学习 Keras,并尝试使用 CNN 来处理 CIFAR10 数据集。但是,无论如何,我训练的模型(您可以运行代码 here )都会为每个输入返回相同的答案。我是否忘记了模型定义中的某些内容?

最佳答案

您忘记标准化图像。目前,x_train 中的值在 [0,255] 范围内。这会导致较大的梯度更新并停止训练过程。在这种情况下,一个简单的标准化方案是:

x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

这会导致值落在[0,1]范围内。然后你一定会看到训练正在进行。

<小时/>

更复杂的标准化方案涉及特征级(即像素级)标准化或居中。在这种方法中,我们对所有图像进行标准化,使得所有图像中的每个像素的均值为零,标准差为一(即它们大多落在[-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

请注意最后一部分:永远不会通过测试数据本身的均值和标准差对测试数据进行标准化。请改用训练均值和标准差。

关于python - 无论输入如何,Keras 上的 CNN 都会收敛到相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51117763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com