tensorflow2知識總結---5、softmax多分類


tensorflow2知識總結---5、softmax多分類

一、總結

一句話總結:

softmax多分類適用於神經網絡輸出層是一個多分類的輸出的情況

 

1、tensorflow的輸出層注意?

如果輸出層是一個連續的數字,就不進行其它操作,直接輸出
如果輸出層是一個二分類(是和否),可以對輸出層做一個sigmoid操作,然后輸出
如果輸出層是一個多分類的輸出,我們就可以對輸出層做一個softmax輸出


二分類:輸出層是sigmoid
model.add(tf.keras.layers.Dense(1,activation='sigmoid'))
多分類:輸出層是softmax時
model.add(tf.keras.layers.Dense(10,activation='softmax'))

 

2、在tf.keras里,對於多分類問題我們使用categorical_crossentropy和sparse_categorical_crossentropy來計算softmax交叉熵,這兩者分別的使用情況是什么?

categorical_crossentropy 使用情況為 one-hot編碼,比如北京 [1,0,0]、上海 [0,1,0]、深圳 [0,0,1]
sparse_categorical_crossentropy 使用情況為普通數字編碼,比如 0表示北京,1表示上海,2表示深圳

 

 

3、Fashion MNIST 數據集?

Fashion MNIST 數據集包含70000張灰度圖像,涵蓋10個類別。60000張圖像訓練網絡,10000張測試。


(train_image,train_label),(test_image,test_label) = tf.keras.datasets.fashion_mnist.load_data()

 

4、matplotlib顯示圖片?

matplotlib的imshow的方法:plt.imshow(train_image[0])

 

 

5、如何對圖片數據進行歸一化?

因為圖片是由像素值組成,rgb,而值得取值范圍值0-255,所以除255即可
# 圖片數據如何歸一化
# 直接除255即可
train_image = train_image/255
test_image = test_image/255

 

 

6、用測試數據評估模型?

model.evaluate(test_image,test_label)

 

 

二、softmax多分類

博客對應課程的視頻位置:

 

對數幾率回歸解決的是二分類的問題,

對於多個選項的問題,我們可以使用softmax函數

它是對數幾率回歸在N個可能不同的值上的推廣

這就是Softmax層的作用

神經網絡的原始輸出不是一個概率值,

實質上只是輸入的數值做了復雜的加權和與非線性處理之后的一個值而已,

那么如何將這個輸出變為概率分布?

softmax要求每個樣本必須屬於某個類別,且所有可能的樣本均被覆蓋

softmax個樣本分量之和為1

當只有兩個類別時,與對數幾率回歸完全相同


在tf.keras里,對於多分類問題我們使用

categorical_crossentropy和sparse_categorical_crossentropy

來計算softmax交叉熵


categorical_crossentropy 使用情況為 one-hot編碼,比如

北京 [1,0,0]
上海 [0,1,0]
深圳 [0,0,1]

sparse_categorical_crossentropy 使用情況,

0表示北京,1表示上海,2表示深圳

Fashion MNIST 數據集

Fashion MNIST的作用是成為經典MNIST數據集的簡易替換,

MNIST數據集包含手寫數字(0、1、2等)的圖像,這些圖像的格式與本節課中使用的服飾圖像的格式相同。

Fashion MNIST 比常規MNIST手寫數據集更具挑戰性。

這兩個數據集都相對較小,用於驗證某個算法能否如期正常運行。它們都是測試和調試代碼的良好起點。

MNIST手寫數據集將作為作業交給大家自己完成。


Fashion MNIST 數據集包含70000張灰度圖像,涵蓋10個類別。

我們將使用60000張圖像訓練網絡,並使用10000張圖像評估經過學習的網絡分類圖像的准確率。

可以從TensorFlow 直接訪問Fashion MNIST,只需導入和加載數據即可

In [1]:
import tensorflow as tf import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline 
In [2]:
# 獲取訓練數據集和測試數據集
# 路徑:C:\Users\Fan Renyi\.keras\datasets\fashion-mnist (train_image,train_label),(test_image,test_label) = tf.keras.datasets.fashion_mnist.load_data() 
In [3]:
train_image.shape 
Out[3]:
(60000, 28, 28)
In [4]:
train_label.shape 
Out[4]:
(60000,)
In [5]:
test_image.shape,test_label.shape 
Out[5]:
((10000, 28, 28), (10000,))
In [6]:
plt.imshow(train_image[0]) 
Out[6]:
<matplotlib.image.AxesImage at 0x14ea9ac7e48>
In [7]:
# 像素值 RGB
np.max(train_image[0]) 
Out[7]:
255
In [8]:
train_label
Out[8]:
array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)

數據歸一化

In [9]:
# 圖片數據如何歸一化
# 直接除255即可 train_image = train_image/255 test_image = test_image/255 

創建模型

In [10]:
# 順序模型
model = tf.keras.Sequential() # 將多維數據(60000, 28, 28)變成一維 # 把圖像扁平化成一個向量 model.add(tf.keras.layers.Flatten(input_shape=(28,28))) # 這里的神經元也不能太多,神經元太多容易產生過擬合 # 神經元太少,會舍棄很多信息 model.add(tf.keras.layers.Dense(128,activation='relu')) # 輸出10個概率值,輸出圖片屬於哪一類 model.add(tf.keras.layers.Dense(10,activation='softmax')) 

編譯模型

In [11]:
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc']) 
In [12]:
history = model.fit(train_image,train_label,epochs=5) 
Epoch 1/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.5017 - acc: 0.8235
Epoch 2/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3768 - acc: 0.8635
Epoch 3/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3377 - acc: 0.8774
Epoch 4/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3125 - acc: 0.8864
Epoch 5/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2949 - acc: 0.8920

用測試數據評價模型

In [14]:
model.evaluate(test_image,test_label) 
313/313 [==============================] - 0s 901us/step - loss: 0.3468 - acc: 0.8765
Out[14]:
[0.34679675102233887, 0.8765000104904175]

在測試的訓練集上的准確率為0.87

將順序編碼編程one-hot編碼

In [15]:
train_label_onehot=tf.keras.utils.to_categorical(train_label) 
In [16]:
train_label_onehot
Out[16]:
array([[0., 0., 0., ..., 0., 0., 1.],
       [1., 0., 0., ..., 0., 0., 0.],
       [1., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [1., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
In [17]:
test_label_onehot=tf.keras.utils.to_categorical(test_label) 
In [18]:
test_label
Out[18]:
array([9, 2, 1, ..., 8, 1, 5], dtype=uint8)
In [20]:
test_label_onehot
Out[20]:
array([[0., 0., 0., ..., 0., 0., 1.],
       [0., 0., 1., ..., 0., 0., 0.],
       [0., 1., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 1., 0.],
       [0., 1., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]], dtype=float32)

9也就是對應[0., 0., 0., ..., 0., 0., 1.],

In [23]:
model = tf.keras.Sequential() model.add(tf.keras.layers.Flatten(input_shape=(28,28))) model.add(tf.keras.layers.Dense(128,activation='relu')) model.add(tf.keras.layers.Dense(10,activation='softmax')) # 模型從 sparse_categorical_crossentropy 變成了 categorical_crossentropy model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc']) # train_label變成了train_label_onehot history = model.fit(train_image,train_label_onehot,epochs=5) 
Epoch 1/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4968 - acc: 0.8243
Epoch 2/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3793 - acc: 0.8636
Epoch 3/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.3422 - acc: 0.8754
Epoch 4/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3155 - acc: 0.8850
Epoch 5/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2984 - acc: 0.8900
In [24]:
predict=model.predict(test_image) predict.shape 
Out[24]:
(10000, 10)
In [26]:
# 對第一張圖片的預測
predict[0] 
Out[26]:
array([1.6070395e-05, 3.5059514e-07, 4.9826558e-06, 2.1827427e-07,
       2.1989422e-06, 1.2660904e-01, 3.0896514e-05, 1.1124686e-02,
       1.2446012e-04, 8.6208713e-01], dtype=float32)

所有樣本的可能性加起來為1

In [27]:
np.argmax(predict[0]) 
Out[27]:
9
In [28]:
test_label[0] 
Out[28]:
9
In [ ]:
 

 

 

 

softmax多分類適用於神經網絡輸出層是一個多分類的輸出的情況


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM