深度學習學習心得 第四篇 === ![grade](https://img.shields.io/badge/Grade-新手-brightgreen) ![build](https://img.shields.io/badge/Build-keras-important) --- ## FNN 實作 1. 打開 juypter notebook 2. 開啟一個新的編輯頁面 3. 改一個你喜歡的Title:kissing_heart: 4. Start learning! ---- 基礎宣告 --- **我們需要引入keras還有相關函式庫** ```python= # 設定所需 library import numpy as np from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.layers import Conv2D, MaxPooling2D, Flatten from keras.optimizers import SGD, Adam from keras.utils import np_utils from keras.datasets import mnist ``` 複製貼上後按 *shift + enter* ---- 資料處理 --- ```python= # categorical_crossentropy def load_data(): (x_train, y_train), (x_test, y_test) = mnist.load_data() 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(number, 28 * 28) x_train = x_train.astype('float32') x_test = x_test.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) ``` 複製貼上後按 *shift + enter* ---- 模型宣告 --- ```python= (x_train, y_train), (x_test, y_test) = load_data() # define network structure model = Sequential() model.add(Dense(input_dim=28*28, units=500, activation='relu')) model.add(Dense(units=500, activation='relu')) model.add(Dense(units=10, activation='softmax')) model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy']) model.fit(x_train, y_train, batch_size=100, epochs=20) result = model.evaluate(x_test, y_test) print('Test Accuracy:', result[1]) ``` **複製貼上後先不要執行** --- 模型參數解釋 --- ```python= model = Sequential() model.add(Dense(input_dim=28*28, units=500, activation='relu')) ``` 1. 輸入影像大小為28*28 => 將它存成(28x28)維的向量 2. 先宣告一個線性model: model = Sequential() 3. 要加入一層layer就使用 add() 函數 ---- 模型參數解釋 --- ```python= model = Sequential() model.add(Dense(input_dim=28*28, units=500, activation='relu')) ``` :::info input_dim = 輸入維度 units = nueral 數量 activation = 激勵函數 ::: [更多細節](https://keras.io/zh/activations/) ---- 模型參數解釋 --- ```python= #第二層hidden layer model.add(Dense(units=500, activation='relu')) #輸出層 model.add(Dense(units=10, activation='softmax')) ``` :::info 第二層hidden layer不需要再寫一次input_dim 輸出層的units 就是你想要分類的數目量 如手寫字辨識的話就是units = 10(數字0~9) ::: ---- 激勵函數 --- ```python= model.compile(loss='categorical_crossentropy' ,optimizer='adam' ,metrics=['accuracy']) ``` 1. hidden layers裡面有多種可以選 tanh / sigmoid / relu.... 2. 輸出層也是可以選其他的激勵函數,不過通常用softmax 3. softmax可以將值轉換成介於0~1代表機率 4. 損失函數(loss function)= optimizer(優化器)– metrics(評價函數) ---- 訓練設置 --- ```python= model.compile(loss='categorical_crossentropy' ,optimizer='adam' ,metrics=['accuracy']) ``` >model.compile(loss = ‘損失函數’, optimizer = ‘優化器’, metrics=’評價函數’) ---- 訓練設置 --- 1. 評價函數與損失函數相似 只是前者的結果不會用於模型內 2. optimizer 都是基於梯度下降法的處理 3. metrics : 可以自訂義但keras已經有蠻多種了 [更多optimizer細節](https://keras.io/zh/optimizers/) [更多metrics細節](https://keras.io/zh/metrics/) ---- 訓練設置 --- ```python= model.fit(x_train, y_train, batch_size=100, epochs=20) ``` :::info x_train = images y_train = images label (對應的數字是哪個,只會有1或是0出現) batch 跟 epochs 可以看先前筆記 ::: ---- 訓練設置 --- 1. x_train & y_train都是matrix 都有一軸代表有幾筆training data ; 2. x : 另外一維代表每筆資料的大小(如:28x28) y : 另外一維代表有幾個不同的class (如數字0~9 : 所以需要10維) 3. y_train只會有1或是0出現 ![](https://i.imgur.com/bcfglx0.png) --- 模型評估 --- ```python= result = model.evaluate(x_test,y_test) print('Test Accuracy:', result[1]) ``` 1. 訓練時評價模型表現使用evaluate() 2. result = model.evaluate(x_test,y_test) 3. result 是一個二維向量 第一個維度是total loss 第二個維度是accuracy ---- test a model --- 1. testing = model.predict(x_test) 2. 現在可以按 *shift + enter* 3. 跑完一次後可以式式換成其他激勵函數看看 4. relu的準確率是大於sigmoid的 ---- ## Next Lesson ... 1. CNN介紹/捲基層/池化層/全連階層 2. CNN code 講解 + 應用實例 > ###### tags: `Deep learning` `beginner` `python` `keras` `tutorial`
{"metaMigratedAt":"2023-06-15T05:46:27.824Z","metaMigratedFrom":"YAML","title":"深度學習學習心得-4","breaks":true,"disqus":"hackmd","contributors":"[{\"id\":\"6bb13895-9bc1-441a-8b8f-cc9578e3b5bc\",\"add\":7146,\"del\":2944}]"}
    931 views