iT邦幫忙

0

為什麼 model 的 loss 在一開始沒幾次後就不會再變化了

各位好,最近剛接觸,還不是很清楚,問題很奇怪的話請見諒 (ง๑ •̀_•́)ง

我使用 keras 裡的數據來練習

可是發現我的 loss 不知道為什麼維持不變,看不出來哪裡怪怪的

從 epoch 1 開始就沒再改變過了

請問要怎麼修改才會比較正常?

謝謝

output

https://ithelp.ithome.com.tw/upload/images/20210115/20130364XQ3GYeEIBo.png

my_model.py

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets

# x: (60000, 28, 28)
# y: (60000,)
(x, y), _ = tf.keras.datasets.mnist.load_data()

# 轉換成 tensor
x = tf.convert_to_tensor(x, dtype=tf.float32)/255   # 讓值範圍在 0 ~ 1
y = tf.convert_to_tensor(y, dtype=tf.int32)
 
train_db = tf.data.Dataset.from_tensor_slices((x, y)).batch(128)

# [b, 784] -> [b, 256] -> [b, 128] -> [b, 10] 
w1 = tf.Variable(tf.random.truncated_normal([784, 256], stddev=0.1))
b1 = tf.Variable(tf.zeros([256]))
w2 = tf.Variable(tf.random.truncated_normal([256, 128], stddev=0.1))
b2 = tf.Variable(tf.zeros([128]))
w3 = tf.Variable(tf.random.truncated_normal([128, 10], stddev=0.1))
b3 = tf.Variable(tf.zeros([10]))

lr = 1e-3

for epoch in range(10):
    print('---', epoch, '---')
    for step, (x, y) in enumerate(train_db):
        # x [128, 28, 28]
        # y [128]
        x = tf.reshape(x, [-1, 28*28])  # [128, 784]
        
        with tf.GradientTape() as tape: # tf.variable
            h1 = x@w1+b1
            h1 = tf.nn.relu(h1)
            h2 = h1@w2+b2
            h2 = tf.nn.relu(h2)
            y_out = h2@w3+b3

            # compute loss
            y_onehot = tf.one_hot(y, depth=10)
            loss = tf.reduce_mean(tf.square(y_onehot-y_out))

        # compute gradient
        grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])

        # update 
        w1.assign_sub(w1 - lr*grads[0]) 
        b1.assign_sub(b1 - lr*grads[1]) 
        w2.assign_sub(w2 - lr*grads[2]) 
        b2.assign_sub(b2 - lr*grads[3]) 
        w3.assign_sub(w3 - lr*grads[4]) 
        b3.assign_sub(b3 - lr*grads[5]) 

        if step%100 == 0:
            print(step, '\tloss:\t', float(loss))
obarisk iT邦研究生 2 級 ‧ 2021-01-16 21:48:22 檢舉
one hot encoding 和 loss 看起來怪怪的
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
a26833765
iT邦新手 5 級 ‧ 2021-01-15 08:56:46
最佳解答

2019-2020年有摸過keras,那時keras還沒有整合在tensorflow裡面,所以我給的建議都是從pytorch來的,參考就好

  1. 我看不到隱藏層的設定,看起來你全部都是設為線性,這在分類問題上會有問題
  2. LOSS看起來是MSE,分類問題要用CrossEntropy,詳細可以參考下面資料
  3. 看起來是按層順序下去建模,然後用腳本下去做訓練,那應該在迴圈裡要有清除梯度的代碼,可是我沒看到
  4. 訓練模型改函數方式取代腳本,可以參考下面資料,如果結果是ok的,那就是驗證第三點論述

參考資料:
Keras with MNIST

  1. https://machinelearningmastery.com/handwritten-digit-recognition-using-convolutional-neural-networks-python-keras/
  2. https://medium.com/bryanyang0528/deep-learning-keras-%E6%89%8B%E5%AF%AB%E8%BE%A8%E8%AD%98-mnist-b41757567684
    LOSS:
  3. https://chih-sheng-huang821.medium.com/%E6%A9%9F%E5%99%A8-%E6%B7%B1%E5%BA%A6%E5%AD%B8%E7%BF%92-%E5%9F%BA%E7%A4%8E%E4%BB%8B%E7%B4%B9-%E6%90%8D%E5%A4%B1%E5%87%BD%E6%95%B8-loss-function-2dcac5ebb6cb

學習資源:
tensorflow的免費學習資源(tf2.0)
https://youtu.be/hvgnX1gbsLA
https://github.com/lyhue1991/eat_tensorflow2_in_30_days
https://machinelearningmastery.com/how-to-develop-a-cnn-from-scratch-for-fashion-mnist-clothing-classification/

下面連接都是AMAZON佔據排行榜多年的ML/DL參考書,選一套看就好
想學ML+DL就買下面這套

  1. https://www.books.com.tw/products/0010869555?utm_source=www&utm_medium=share&utm_content=copy&utm_campaign=product&utm_term=0010869555 =>ML
  2. https://www.books.com.tw/products/0010871454?utm_source=www&utm_medium=share&utm_content=copy&utm_campaign=product&utm_term=0010871454 =>DL
    想學DL就買下面這本keras作者自己寫的書
  3. https://www.books.com.tw/products/0010822932?utm_source=www&utm_medium=share&utm_content=copy&utm_campaign=product&utm_term=0010822932
    想學ML+DL就買下面這本,但我覺得這比較像是字典,不適合學習
  4. https://www.books.com.tw/products/0010854043?utm_source=www&utm_medium=share&utm_content=copy&utm_campaign=product&utm_term=0010854043

最後我想說
from tensorflow import keras as torch

obarisk iT邦研究生 2 級 ‧ 2021-01-16 21:46:46 檢舉

他沒有用到 keras 其實

a26833765 iT邦新手 5 級 ‧ 2021-01-16 23:02:00 檢舉

真的耶,只是載入MNIST而已

yun1231 iT邦新手 3 級 ‧ 2021-02-02 18:12:56 檢舉

我後來重開電腦在 run 一次就正常了,不知道問題在哪!

我要發表回答

立即登入回答