gpt4 book ai didi

python - Tensorflow - 准确度从 1.0 开始,随着损失而降低

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

我正在 10K 灰度图像上训练 CNN。该网络有 6 个卷积层、1 个全连接层和 1 个输出层。

当我开始训练时,损失非常高,但逐渐下降,但我的准确度从 1.0 开始,然后也下降。从 72% 左右波动到 30%,然后又回升。另外,当我运行 acc.eval({x: test_images, y: test_lables}) 时对于未见过的图像,准确率约为 16%。

此外,我有 6 个类,所有这些类都是 one-hot 编码的。

我认为我可能错误地比较了预测输出,但在我的代码中看不到错误...

这是我的代码

pred = convolutional_network(x)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y, logits = pred))
train_op = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)

prediction = tf.nn.softmax(pred)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
acc = tf.reduce_mean(tf.cast(correct, 'float'))

with tf.Session() as sess:
sess.run(tf.global_variables_initializer()) # Initialize all the variables
saver = tf.train.Saver()

time_full_start = time.clock()
print("RUNNING SESSION...")
for epoch in range(num_epochs):
train_batch_x = []
train_batch_y = []
epoch_loss = 0
i = 0
while i < len(images):
start = i
end = i+ batch_size
train_batch_x = images[start:end]
train_batch_y = labels[start:end]
op , ac, loss_value = sess.run([train_op, acc, loss], feed_dict={x: train_batch_x, y: train_batch_y})
epoch_loss += loss_value
i += batch_size
print('Epoch : ', epoch+1, ' of ', num_epochs, ' - Loss for epoch: ', epoch_loss, ' Accuracy: ', ac)

time_full_end = time.clock()
print('Full time elapse:', time_full_end - time_full_start)


print('Accuracy:', acc.eval({x: test_images, y: test_labels}))

save_path = saver.save(sess, MODEL_PATH)
print("Model saved in file: " , save_path)

这是输出

Epoch :  1  of  100  - Loss for epoch:  8.94737603121e+13  Accuracy:  1.0

Epoch : 2 of 100 - Loss for epoch: 212052447727.0 Accuracy: 1.0

Epoch : 3 of 100 - Loss for epoch: 75150603462.2 Accuracy: 1.0

Epoch : 4 of 100 - Loss for epoch: 68164116617.4 Accuracy: 1.0

Epoch : 5 of 100 - Loss for epoch: 18505190718.8 Accuracy: 0.99

Epoch : 6 of 100 - Loss for epoch: 11373286689.0 Accuracy: 0.96

Epoch : 7 of 100 - Loss for epoch: 3129798657.75 Accuracy: 0.07

Epoch : 8 of 100 - Loss for epoch: 374790121.375 Accuracy: 0.58

Epoch : 9 of 100 - Loss for epoch: 105383792.938 Accuracy: 0.72

Epoch : 10 of 100 - Loss for epoch: 49705202.4844 Accuracy: 0.66

Epoch : 11 of 100 - Loss for epoch: 30214170.7909 Accuracy: 0.36

Epoch : 12 of 100 - Loss for epoch: 18653020.5084 Accuracy: 0.82

Epoch : 13 of 100 - Loss for epoch: 14793638.35 Accuracy: 0.39

Epoch : 14 of 100 - Loss for epoch: 10196079.7003 Accuracy: 0.73

Epoch : 15 of 100 - Loss for epoch: 6727522.37319 Accuracy: 0.47

Epoch : 16 of 100 - Loss for epoch: 4593769.05838 Accuracy: 0.68

Epoch : 17 of 100 - Loss for epoch: 3669332.09406 Accuracy: 0.44

Epoch : 18 of 100 - Loss for epoch: 2850924.81662 Accuracy: 0.59

Epoch : 19 of 100 - Loss for epoch: 1780678.12892 Accuracy: 0.51

Epoch : 20 of 100 - Loss for epoch: 1855037.40652 Accuracy: 0.61

Epoch : 21 of 100 - Loss for epoch: 1012934.52827 Accuracy: 0.53

Epoch : 22 of 100 - Loss for epoch: 649319.432669 Accuracy: 0.55

Epoch : 23 of 100 - Loss for epoch: 841660.786938 Accuracy: 0.57

Epoch : 24 of 100 - Loss for epoch: 490148.861691 Accuracy: 0.55

Epoch : 25 of 100 - Loss for epoch: 397315.021568 Accuracy: 0.5

......................

Epoch : 99 of 100 - Loss for epoch: 4412.61703086 Accuracy: 0.57

Epoch : 100 of 100 - Loss for epoch: 4530.96991658 Accuracy: 0.62

Full time elapse: 794.5787720000001

**Test Accuracy: 0.158095**

我尝试了多种学习率和网络大小,但似乎可以让它发挥作用。任何帮助将不胜感激

最佳答案

请注意,我的答案也是通过审查和调试完整的代码(在问题中不可见)得出的。我仍然相信,如果有人面临类似的问题,下面的问题足够普遍,值得审查 - 你可能只是在这里得到解决方案!

<小时/>极高的损失值可能意味着您没有将输入图像从 int8 正确转换为小的 float32 值(事实上,他确实这样做了)并且您没有使用批量归一化和/或正则化(事实上,两者都缺失。)此外,在此代码中:

prediction = tf.nn.softmax(pred)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))

计算softmax值是完全没有必要的,因为softmax是一个严格单调的函数,它只对预测进行缩放,pred中的最大值将是最大的prediction ,您会得到相同的结果

correct = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))

鉴于您的网络运行的值非常高,tf.nn.softmax() 在进行求幂和除以总和时,可能会无意中将所有内容减少到零,然后tf.argmax() 只是选择类 0,直到数字稍微下降。添加到您不累积的 ac:

op , ac, loss_value = sess.run([train_op, acc, loss], feed_dict={x: train_batch_x, y: train_batch_y})

所以您打印的纪元精度并不是这样,它只是上一批的精度。如果您的图像是按类排序的并且您没有随机化批处理,那么您可能会在每个时期结束时获得零类图像。这可以解释为什么你在前几个时期内获得 100% 的准确率,直到超高的数字稍微下降并且 softmax 不再将所有内容归零。 (事实证明确实如此。)

即使修复了上述问题,网络也没有学到任何东西。事实证明,当他添加随机化时,图像和标签的随机化方式不同,自然会产生恒定的 1/6 准确度。

解决了所有问题后,网络在 100 个周期后能够以 98% 的准确率学习此任务。

Epoch: 100/100 loss: 6.20184610883 total loss: 25.4021390676 acc: 97.976191%

关于python - Tensorflow - 准确度从 1.0 开始,随着损失而降低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49958888/

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