![](/img/trans.png)
[英]while training the autoencoders loss is high and constant, what am I doing wrong here
[英](MINIST ML training) Why I keep getting high and fluctuating loss? What is wrong with my code?
我正在使用谷歌合作實驗室並嘗試使用 MINIST 數據集訓練機器。 數據集的第一列是數字標簽(0~9)。 數據集的大小為 60000 x 785(編號 label 1 + (28 x 28 =784)圖像)
'''
#print(x_data.shape, y_data.shape)
#(60000, 784) (60000, 1)
xy_data = np.loadtxt('/content/drive/MyDrive/Machine-Learning Study/GAN/MNIST_data/mnist_train.csv', delimiter=',', dtype=np.float32)
xy_test = np.loadtxt('/content/drive/MyDrive/Machine-Learning Study/GAN/MNIST_data/mnist_test.csv', delimiter=',', dtype=np.float32)
# 60000 x 785 array
# first column is number label (0 ~ 9)
x_data = xy_data[:, 1:]
y_data = xy_data[:, [0]]
nb_classes = 10
X = tf.placeholder(tf.float32, shape = [None, 784])
Y = tf.placeholder(tf.int32, shape = [None, nb_classes])
# used one_hot function to convert y_data [:, [-1]] to [:, 10]
Y_one_hot = tf.one_hot(y_data, nb_classes)
Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])
# since feed_dict cannot take tensor array, converting tensor to array so that we can plug the array into Y
# converting using .eavl only works in Tf 1 version
y_data_array = Y_one_hot.eval(session=tf.Session())
W = tf.Variable(tf.random_normal([784, nb_classes]))
b = tf.Variable(tf.random_normal([nb_classes]))
logits = tf.matmul(X, W) + b
hypothesis = tf.nn.softmax(logits)
# element-wise product loss function
loss_i = tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels = Y_one_hot)
loss = tf.reduce_mean(loss_i)
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.1).minimize(loss)
is_correct = tf.equal(tf.arg_max(hypothesis, 1), tf.arg_max(Y_one_hot, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
training_epochs = 101
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for epoch in range(training_epochs):
loss_val, acc, _ = sess.run([loss, accuracy, optimizer], feed_dict={X:x_data, Y:y_data_array})
if epoch % 5 == 0:
print("Epochs: {:}\tLoss: {:.4f}\tAcc: {:.2%}".format(epoch, loss_val, acc))
'''
結果:
時期:0 損失:4227.7871 累計:9.71%
時期:5 損失:17390.2520 累計:41.26%
時期:10 損失:8494.0889 累計:52.81%
時代:15 損失:1412.1642 累計:82.48%
時期:20 損失:1620.4032 累計:82.48%
時代:25 損失:1891.1475 累計:81.31%
時期:30 損失:2770.4656 累計:77.99%
時代:35 損失:1659.1884 累計:79.90%
時期:40 損失:1134.2424 累計:84.61%
時期:45 損失:2560.7073 累計:80.17%
時代:50 損失:1440.0392 累計:82.33%
時代:55 損失:1219.5104 累計:83.87%
時代:60 損失:1002.9220 累計:86.11%
時期:65 損失:635.6382 累計:89.84%
時期:70 損失:574.5991 累計:90.13%
時期:75 損失:544.4010 累計:90.15%
時代:80 損失:2215.5605 累計:80.56%
時代:85 損失:4700.1890 累計:77.99%
時代:90 損失:3243.2017 Acc:78.18%
時代:95 損失:1040.0907 累計:85.05%
時代:100 損失:1999.5754 累計:82.24%
你的代碼很好,問題在於你的高學習率。
我計算出lr=0.005
並監控了 150 個 epoch,它按您的預期工作。
Epochs: 0 Loss: 3659.2244 Acc: 4.97%
Epochs: 5 Loss: 1218.3916 Acc: 30.38%
Epochs: 10 Loss: 767.9141 Acc: 46.95%
Epochs: 15 Loss: 582.4928 Acc: 55.63%
Epochs: 20 Loss: 480.8191 Acc: 61.28%
Epochs: 25 Loss: 416.9088 Acc: 65.28%
Epochs: 30 Loss: 372.9733 Acc: 68.19%
Epochs: 35 Loss: 340.5632 Acc: 70.34%
Epochs: 40 Loss: 315.6934 Acc: 72.09%
Epochs: 45 Loss: 296.0419 Acc: 73.57%
Epochs: 50 Loss: 280.1195 Acc: 74.72%
Epochs: 55 Loss: 266.9192 Acc: 75.74%
Epochs: 60 Loss: 255.7594 Acc: 76.58%
Epochs: 65 Loss: 246.1218 Acc: 77.29%
Epochs: 70 Loss: 237.6666 Acc: 77.91%
Epochs: 75 Loss: 230.2098 Acc: 78.47%
Epochs: 80 Loss: 223.5687 Acc: 79.02%
Epochs: 85 Loss: 217.6027 Acc: 79.42%
Epochs: 90 Loss: 212.1969 Acc: 79.81%
Epochs: 95 Loss: 207.2774 Acc: 80.16%
Epochs: 100 Loss: 202.7701 Acc: 80.53%
Epochs: 105 Loss: 198.6335 Acc: 80.86%
Epochs: 110 Loss: 194.8041 Acc: 81.12%
Epochs: 115 Loss: 191.2343 Acc: 81.38%
Epochs: 120 Loss: 187.8969 Acc: 81.59%
Epochs: 125 Loss: 184.7562 Acc: 81.78%
Epochs: 130 Loss: 181.7817 Acc: 81.98%
Epochs: 135 Loss: 178.9837 Acc: 82.20%
Epochs: 140 Loss: 176.3420 Acc: 82.36%
Epochs: 145 Loss: 173.8274 Acc: 82.53%
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.