李宏毅課程mnist源代碼和實現小實驗(keras框架)

需要下載keras框架!

大概解釋下整體過程:

1.定義下載數據的函數 load_data(),在網絡上進行下載訓練集和測試集文件。

2.定義神經網絡模型

  • 輸入維數 :圖片是28 *28,共10000張,則輸入維數是10000 * 784
  • 輸出維數:九種數字類型,那麼輸出維數是10000 * 10
  • 神經網絡層數 :根據自己的需要進行設定;
  • 每層神經元的:同上
  • 激活函數:這裏用的是sigmoid函數
  • 損失函數:根據自己許需要進行選擇;
  • 學習率:設定合適的學習率,使訓練效果儘量好;

3.開始訓練神經網絡

4.驗證神經網絡的好壞

    • 下面是在jupyter上運算的主要過程和代碼,能比較清晰的看到整個實驗過程。最後訓練集上精度是0.1018,測試集上精度是0.1094,都不理想,說明這是欠擬合;在訓練集上訓練效果就不好,測試集自然也不會好。

下面將代碼註釋一下:

1.引入各種包和框架。

import numpy as np
# 引入很多包
import keras
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers import Convolution2D, MaxPooling2D, Flatten
from keras.optimizers import SGD, Adam
from keras.utils import np_utils
from keras.datasets import mnist

2.定義下載數據的函數

def load_data():
   #網絡下載失敗
    (x_train, y_train), (x_test, y_test) = mnist.load_data('mnist.npz') # 網絡下載數據
   #讀取本地數據
   # path = './mnist.npz'
   # f = np.load(path)
   #x_train, y_train = f['x_train'], f['y_train']
   #x_test, y_test = f['x_test'], f['y_test']
   #f.close()
    number = 10000
    x_train = x_train[0: number]
    y_train = y_train[0: number]
    x_train = x_train.reshape(number, 28 * 28)
    x_test = x_test.reshape(x_test.shape[0], 28 * 28)
    x_train = x_train.astype('float32')
    x_test = x_train.astype('float32')
    # convert class vectors to binary class matrices
    y_train = np_utils.to_categorical(y_train, 10)
    y_test = np_utils.to_categorical(y_test, 10)
    x_train = x_train
    x_test = x_test
    # x_test = np.random.normal(x_test)
    x_train = x_train / 255
    x_test = x_test / 255

    return (x_train, y_train), (x_test, y_test)
	
#下載	
(x_train, y_train), (x_test, y_test) = load_data()

3.引入模型 訓練並且測試 輸出測試結果


# 引入模型 訓練並且測試 輸出測試結果
model = Sequential()
model.add(Dense(input_dim = 28 * 28, units = 633, activation = 'sigmoid'))
model.add(Dense(units = 633, activation = 'sigmoid'))
model.add(Dense(units = 633,activation = 'sigmoid'))
model.add(Dense(units = 10, activation = 'softmax'))

model.compile(loss = 'mse', optimizer = SGD(lr = 0.001), metrics = ['accuracy'])

model.fit(x_train, y_train, batch_size = 100, epochs = 20)

score = model.evaluate(x_train, y_train)
print('\n Train Acc: ', score[1])

score = model.evaluate(x_test, y_test)
print('\n Test Acc: ', score[1])

這就是整個實驗過程,參數自己可以調整,自己運行,查看訓練結果。

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