擅长:python、mysql、java
<p>你忘了正常化图像。当前,<code>x_train</code>中的值在<code>[0,255]</code>范围内。这会导致大梯度更新和暂停训练过程。在这种情况下,一个简单的标准化方案是:</p>
<pre class="lang-py prettyprint-override"><code>x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
</code></pre>
<p>这会导致值落在<code>[0,1]</code>范围内。你一定会看到训练的进展。你知道吗</p>
<hr/>
<p>更复杂的标准化方案涉及特征(即像素)标准化或中心化。在这种方法中,我们对所有图像进行标准化,使得所有图像中的每个像素的平均值为0,标准偏差为1(即,它们大多在<code>[-1,1]</code>范围内):</p>
<pre class="lang-py prettyprint-override"><code># 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
</code></pre>
<p>注意最后一部分:<strong>永远不要</strong>用自己的平均值和标准来规范化测试数据。使用训练平均值和标准来代替。你知道吗</p>