五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

Python實戰(zhàn)之MNIST手寫數(shù)字識別詳解

發(fā)布日期:2022-02-01 15:34 | 文章來源:源碼之家

數(shù)據(jù)集介紹

MNIST數(shù)據(jù)集是機器學習領(lǐng)域中非常經(jīng)典的一個數(shù)據(jù)集,由60000個訓練樣本和10000個測試樣本組成,每個樣本都是一張28 * 28像素的灰度手寫數(shù)字圖片,且內(nèi)置于keras。本文采用Tensorflow下Keras(Keras中文文檔)神經(jīng)網(wǎng)絡(luò)API進行網(wǎng)絡(luò)搭建。

開始之前,先回憶下機器學習的通用工作流程( √表示本文用到,×表示本文沒有用到 )

1.定義問題,收集數(shù)據(jù)集(√)

2.選擇衡量成功的指標(√)

3.確定評估的方法(√)

4.準備數(shù)據(jù)(√)

5.開發(fā)比基準更好的模型(×)

6.擴大模型規(guī)模(×)

7.模型正則化與調(diào)節(jié)參數(shù)(×)

關(guān)于最后一層激活函數(shù)與損失函數(shù)的選擇

下面開始正文~

1.數(shù)據(jù)預處理

首先導入數(shù)據(jù),要使用mnist.load()函數(shù)

我們來看看它的源碼聲明:

def load_data(path='mnist.npz'):
  """Loads the [MNIST dataset](http://yann.lecun.com/exdb/mnist/).
  This is a dataset of 60,000 28x28 grayscale images of the 10 digits,
  along with a test set of 10,000 images.
  More info can be found at the
  [MNIST homepage](http://yann.lecun.com/exdb/mnist/).

  Arguments:
path: path where to cache the dataset locally
 (relative to `~/.keras/datasets`).
  Returns:
Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
**x_train, x_test**: uint8 arrays of grayscale image data with shapes
  (num_samples, 28, 28).
**y_train, y_test**: uint8 arrays of digit labels (integers in range 0-9)
  with shapes (num_samples,).
  """

可以看到,里面包含了數(shù)據(jù)集的下載鏈接,以及數(shù)據(jù)集規(guī)模、尺寸以及數(shù)據(jù)類型的聲明,且函數(shù)返回的是四個numpy array組成的兩個元組。

導入數(shù)據(jù)集并reshape至想要形狀,再標準化處理。

其中內(nèi)置于keras的to_categorical()就是one-hot編碼——將每個標簽表示為全零向量,只有標簽索引對應(yīng)的元素為1.

eg: col=10

[0,1,9]-------->[ [1,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1] ]  

我們可以手動實現(xiàn)它:

def one_hot(sequences,col):
  resuts=np.zeros((len(sequences),col))
  # for i,sequence in enumerate(sequences):
  #resuts[i,sequence]=1
  for i in range(len(sequences)):
 for j in range(len(sequences[i])):resuts[i,sequences[i][j]]=1
  return resuts

下面是預處理過程

def data_preprocess():
 (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
 train_images = train_images.reshape((60000, 28, 28, 1))
 train_images = train_images.astype('float32') / 255
 #print(train_images[0])
 test_images = test_images.reshape((10000, 28, 28, 1))
 test_images = test_images.astype('float32') / 255
 train_labels = to_categorical(train_labels)
 test_labels = to_categorical(test_labels)
 return train_images,train_labels,test_images,test_labels

2.網(wǎng)絡(luò)搭建

這里我們搭建的是卷積神經(jīng)網(wǎng)絡(luò),就是包含一些卷積、池化、全連接的簡單線性堆積。我們知道多個線性層堆疊實現(xiàn)的仍然是線性運算,添加層數(shù)并不會擴展假設(shè)空間(從輸入數(shù)據(jù)到輸出數(shù)據(jù)的所有可能的線性變換集合),因此需要添加非線性或激活函數(shù)。relu是最常用的激活函數(shù),也可以用prelu、elu

def build_module():
 model = models.Sequential()
 #第一層卷積層,首層需要指出input_shape形狀
 model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)))
 #第二層最大池化層
 model.add(layers.MaxPooling2D((2,2)))
 #第三層卷積層
 model.add(layers.Conv2D(64, (3,3), activation='relu'))
 #第四層最大池化層
 model.add(layers.MaxPooling2D((2,2)))
 #第五層卷積層
 model.add(layers.Conv2D(64, (3,3), activation='relu'))
 #第六層Flatten層,將3D張量平鋪為向量
 model.add(layers.Flatten())
 #第七層全連接層
 model.add(layers.Dense(64, activation='relu'))
 #第八層softmax層,進行分類
 model.add(layers.Dense(10, activation='softmax'))
 return model

使用model.summary()查看搭建的網(wǎng)路結(jié)構(gòu):

3.網(wǎng)絡(luò)配置

網(wǎng)絡(luò)搭建好之后還需要關(guān)鍵的一步設(shè)置配置。比如:優(yōu)化器——網(wǎng)絡(luò)梯度下降進行參數(shù)更新的具體方法、損失函數(shù)——衡量生成值與目標值之間的距離、評估指標等。配置這些可以通過 model.compile() 參數(shù)傳遞做到。

我們來看看model.compile()的源碼分析下:

  def compile(self,
  optimizer='rmsprop',
  loss=None,
  metrics=None,
  loss_weights=None,
  weighted_metrics=None,
  run_eagerly=None,
  steps_per_execution=None,
  **kwargs):
 """Configures the model for training.

關(guān)于優(yōu)化器

優(yōu)化器:字符串(優(yōu)化器名稱)或優(yōu)化器實例。

字符串格式:比如使用優(yōu)化器的默認參數(shù)

實例優(yōu)化器進行參數(shù)傳入:

keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0)
model.compile(optimizer='rmsprop',loss='mean_squared_error')

建議使用優(yōu)化器的默認參數(shù) (除了學習率 lr,它可以被自由調(diào)節(jié))

參數(shù):

lr: float >= 0. 學習率。
rho: float >= 0. RMSProp梯度平方的移動均值的衰減率.
epsilon: float >= 0. 模糊因子. 若為 None, 默認為 K.epsilon()。
decay: float >= 0. 每次參數(shù)更新后學習率衰減值。

類似還有好多優(yōu)化器,比如SGD、Adagrad、Adadelta、Adam、Adamax、Nadam等

關(guān)于損失函數(shù)

取決于具體任務(wù),一般來說損失函數(shù)要能夠很好的刻畫任務(wù)。比如

1.回歸問題

希望神經(jīng)網(wǎng)絡(luò)輸出的值與ground-truth的距離更近,選取能刻畫距離的loss應(yīng)該會更合適,比如L1 Loss、MSE Loss等

2.分類問題

希望神經(jīng)網(wǎng)絡(luò)輸出的類別與ground-truth的類別一致,選取能刻畫類別分布的loss應(yīng)該會更合適,比如cross_entropy

具體常見選擇可查看文章開始處關(guān)于損失函數(shù)的選擇

關(guān)于指標

常規(guī)使用查看上述列表即可。下面說說自定義評價函數(shù):它應(yīng)該在編譯的時候(compile)傳遞進去。該函數(shù)需要以 (y_true, y_pred) 作為輸入?yún)?shù),并返回一個張量作為輸出結(jié)果。

import keras.backend as K
def mean_pred(y_true, y_pred):
 return K.mean(y_pred)
model.compile(optimizer='rmsprop',
  loss='binary_crossentropy',
  metrics=['accuracy', mean_pred])

4.網(wǎng)絡(luò)訓練與測試

1.訓練(擬合)

使用model.fit(),它可以接受的參數(shù)列表

def fit(self,
 x=None,
 y=None,
 batch_size=None,
 epochs=1,
 verbose=1,
 callbacks=None,
 validation_split=0.,
 validation_data=None,
 shuffle=True,
 class_weight=None,
 sample_weight=None,
 initial_epoch=0,
 steps_per_epoch=None,
 validation_steps=None,
 validation_batch_size=None,
 validation_freq=1,
 max_queue_size=10,
 workers=1,
 use_multiprocessing=False):

這個源碼有300多行長,具體的解讀放在下次。

我們對訓練數(shù)據(jù)進行劃分,以64個樣本為小批量進行網(wǎng)絡(luò)傳遞,對所有數(shù)據(jù)迭代5次

model.fit(train_images, train_labels, epochs = 5, batch_size=64)

2.測試

使用model.evaluates()函數(shù)

test_loss, test_acc = model.evaluate(test_images, test_labels)

關(guān)于測試函數(shù)的返回聲明:

Returns:
  Scalar test loss (if the model has a single output and no metrics)
  or list of scalars (if the model has multiple outputs
  and/or metrics). The attribute `model.metrics_names` will give you
  the display labels for the scalar outputs.

5.繪制loss和accuracy隨著epochs的變化圖

model.fit()返回一個History對象,它包含一個history成員,記錄了訓練過程的所有數(shù)據(jù)。

我們采用matplotlib.pyplot進行繪圖,具體見后面完整代碼。

Returns:
  A `History` object. Its `History.history` attribute is
  a record of training loss values and metrics values
  at successive epochs, as well as validation loss values
  and validation metrics values (if applicable).
def draw_loss(history):
 loss=history.history['loss']
 epochs=range(1,len(loss)+1)
 plt.subplot(1,2,1)#第一張圖
 plt.plot(epochs,loss,'bo',label='Training loss')
 plt.title("Training loss")
 plt.xlabel('Epochs')
 plt.ylabel('Loss')
 plt.legend()
 plt.subplot(1,2,2)#第二張圖
 accuracy=history.history['accuracy']
 plt.plot(epochs,accuracy,'bo',label='Training accuracy')
 plt.title("Training accuracy")
 plt.xlabel('Epochs')
 plt.ylabel('Accuracy')
 plt.suptitle("Train data")
 plt.legend()
 plt.show()

6.完整代碼

from tensorflow.keras.datasets import mnist
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
import numpy as np
def data_preprocess():
 (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
 train_images = train_images.reshape((60000, 28, 28, 1))
 train_images = train_images.astype('float32') / 255
 #print(train_images[0])
 test_images = test_images.reshape((10000, 28, 28, 1))
 test_images = test_images.astype('float32') / 255
 train_labels = to_categorical(train_labels)
 test_labels = to_categorical(test_labels)
 return train_images,train_labels,test_images,test_labels
#搭建網(wǎng)絡(luò)
def build_module():
 model = models.Sequential()
 #第一層卷積層
 model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)))
 #第二層最大池化層
 model.add(layers.MaxPooling2D((2,2)))
 #第三層卷積層
 model.add(layers.Conv2D(64, (3,3), activation='relu'))
 #第四層最大池化層
 model.add(layers.MaxPooling2D((2,2)))
 #第五層卷積層
 model.add(layers.Conv2D(64, (3,3), activation='relu'))
 #第六層Flatten層,將3D張量平鋪為向量
 model.add(layers.Flatten())
 #第七層全連接層
 model.add(layers.Dense(64, activation='relu'))
 #第八層softmax層,進行分類
 model.add(layers.Dense(10, activation='softmax'))
 return model
def draw_loss(history):
 loss=history.history['loss']
 epochs=range(1,len(loss)+1)
 plt.subplot(1,2,1)#第一張圖
 plt.plot(epochs,loss,'bo',label='Training loss')
 plt.title("Training loss")
 plt.xlabel('Epochs')
 plt.ylabel('Loss')
 plt.legend()
 plt.subplot(1,2,2)#第二張圖
 accuracy=history.history['accuracy']
 plt.plot(epochs,accuracy,'bo',label='Training accuracy')
 plt.title("Training accuracy")
 plt.xlabel('Epochs')
 plt.ylabel('Accuracy')
 plt.suptitle("Train data")
 plt.legend()
 plt.show()
if __name__=='__main__':
 train_images,train_labels,test_images,test_labels=data_preprocess()
 model=build_module()
 print(model.summary())
 model.compile(optimizer='rmsprop', loss = 'categorical_crossentropy', metrics=['accuracy'])
 history=model.fit(train_images, train_labels, epochs = 5, batch_size=64)
 draw_loss(history)
 test_loss, test_acc = model.evaluate(test_images, test_labels)
 print('test_loss=',test_loss,'  test_acc = ', test_acc)

迭代訓練過程中l(wèi)oss和accuracy的變化

由于數(shù)據(jù)集比較簡單,隨便的神經(jīng)網(wǎng)絡(luò)設(shè)計在測試集的準確率可達到99.2%

以上就是Python實戰(zhàn)之MNIST手寫數(shù)字識別詳解的詳細內(nèi)容,更多關(guān)于Python MNIST手寫數(shù)字識別的資料請關(guān)注本站其它相關(guān)文章!

香港穩(wěn)定服務(wù)器

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部