tensorflow2.0筆記11:誤差計算之MSE,Cross Enropy!

誤差計算!

一、誤差計算

1.1、均方誤差-MSE

提示: 關於loss的放縮可以參考:2.3、關於loss的放縮

  • 實戰演練:
  • 代碼如下:
import tensorflow as tf

y = tf.constant([1, 2, 3, 0, 2])
y = tf.one_hot(y, depth=4)
y = tf.cast(y, dtype=tf.float32)
print(y.numpy())

out = tf.random.normal([5, 4])

loss1 = tf.reduce_mean(tf.square(y-out))
loss2 = tf.square(tf.norm(y-out))/(5*4)
loss3 = tf.reduce_mean(tf.losses.MSE(y, out))
print(loss1)
print(loss2)
print(loss3)
  • 輸出結果:這三個結果是一樣的。
ssh://[email protected]:22/home/zhangkf/anaconda3/envs/tf2.0/bin/python -u 
[[0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]
 [1. 0. 0. 0.]
 [0. 0. 1. 0.]]
tf.Tensor(0.9701576, shape=(), dtype=float32)
tf.Tensor(0.9701576, shape=(), dtype=float32)
tf.Tensor(0.9701575, shape=(), dtype=float32)
Process finished with exit code 0

1.2、交叉熵-Entropy

  • 熵的概念。

補充: 交叉熵的具體理解可以參考文檔:交叉熵(Cross-Entropy):什麼是信息量,什麼熵,什麼是交叉熵!

注意: 我們一般的log都是以2爲底的,但是tensorflow中都是以e爲底的。

  • 代碼
import tensorflow as tf
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

a = tf.fill([4],0.25)
b = tf.math.log(a) / tf.math.log(2.)  #tensorflow中默認以e爲底,變爲2爲底。
print(b)
print(-tf.reduce_sum(a*b).numpy())

a1 = tf.constant([0.1, 0.1, 0.1, 0.7])
b1 = tf.math.log(a1) / tf.math.log(2.)  #tensorflow中默認以e爲底,變爲2爲底。
print(-tf.reduce_sum(a1*b1).numpy())

a2 = tf.constant([0.01, 0.01, 0.01, 0.97])
b2 = tf.math.log(a2) / tf.math.log(2.)  #tensorflow中默認以e爲底,變爲2爲底。
print(-tf.reduce_sum(a2*b2).numpy())
  • 測試結果:
ssh://[email protected]:22/home/zhangkf/anaconda3/envs/tf2.0/bin/python -u /home/zhangkf/tmp/pycharm_project_258/demo/TF2/out.py
tf.Tensor([-2. -2. -2. -2.], shape=(4,), dtype=float32)
2.0
1.3567797
0.24194068

Process finished with exit code 0
  • 交叉熵的概念。指2個分佈之間的信息的衡量標準。
1.2.1、二分類的2中方式
  • Single output(第二種方式)
  • 第一種方式;多分類一樣。

以五分類爲例子分析,如何優化loss。

  • 實例分析1:
import tensorflow as tf
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
loss = tf.losses.categorical_crossentropy([0,1,0,0],[0.25,0.25,0.25,0.25]) #tensorflow中計算多分類的交叉熵。
print(loss.numpy())    #交叉熵表示不確定度,預測概率越大越確定,交叉熵越小。

loss1 = tf.losses.categorical_crossentropy([0,1,0,0],[0.1,0.1,0.8,0.1])
print(loss1.numpy())
  • 運行結果:
ssh://[email protected]:22/home/zhangkf/anaconda3/envs/tf2.0/bin/python -u /home/zhangkf/tmp/pycharm_project_258/demo/TF2/out.py
1.3862944
2.3978953

Process finished with exit code 0
  • 代碼如下
import tensorflow as tf
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

#函數大寫形式
criteon = tf.losses.BinaryCrossentropy() #首先聲明這樣一個類,對instance做一個調用。
loss = criteon([1], [0.1])
print(loss)

#函數小寫的形式。直接調用就可以了。
loss1 = tf.losses.binary_crossentropy([1],[0.1])
print(loss1)
  • 測試結果:
ssh://[email protected]:22/home/zhangkf/anaconda3/envs/tf2.0/bin/python -u /home/zhangkf/tmp/pycharm_project_258/demo/TF2/out.py
tf.Tensor(2.3025842, shape=(), dtype=float32)
tf.Tensor(2.3025842, shape=(), dtype=float32)

Process finished with exit code 0

1.3、爲什麼不直接MSE而是交叉熵

  • 代碼如下:
import tensorflow as tf
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

x = tf.random.normal([1,784])
w = tf.random.normal([784,2])
b = tf.zeros(2)
logits = x@w+b
print("前向傳播結果:",logits.numpy())

prob = tf.math.softmax(logits, axis=1)
print("經過softmax數值爲:",prob.numpy())

loss = tf.losses.categorical_crossentropy([0,1],logits,from_logits=True)
print("交叉熵數值爲:{0}".format(loss))
  • 測試結果:
ssh://[email protected]:22/home/zhangkf/anaconda3/envs/tf2.0/bin/python -u /home/zhangkf/tmp/pycharm_project_258/demo/TF2/out.py
前向傳播結果: [[17.160229 28.54478 ]]
經過softmax數值爲: [[1.1369646e-05 9.9998868e-01]]
交叉熵數值爲:[0.47073382]

Process finished with exit code 0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章