iT邦幫忙

1

Day30 參加職訓(機器學習與資料分析工程師培訓班),Tensorflow.keras

  • 分享至 

  • xImage
  •  

今日教學CNN
了解捲積層、池化層、平坦層、丟棄層各層相關係數的設定影響
https://ithelp.ithome.com.tw/upload/images/20210810/20139039Aty3YtLFj4.png

  • 捲基層: 積層是一組平行的特徵圖(feature map),它通過在輸入圖像上滑動不同的卷積核並執行一定的運算而組成

  • 池化層: 主要是對卷積層的輸出進行篩選或者擷取統計上的特徵,並將重要的特徵或資訊保留下來,同時將資料的維度減少

  • 平坦層: 在 CNN 前面幾層都是卷積層跟池化層交互轉換,後半段會使用多層感知器來穩定判斷結果。所以再接入多層感知器前,先必須將矩陣打平成一維的陣列作為輸入,然後再串到後面的隱藏層跟輸出層。

  • 丟棄層: 提供一個簡單且有效率的方式來避免overfitting在訓練過程中,每次隨機關閉某些神經元,對剩下的神經元進行訓練

使用CIFAR10來練習

from tensorflow.keras.datasets import cifar10

# Load Data

(train_X, train_y), (test_X, test_y) = cifar10.load_data()

# Prepare X, y 

X_train = train_X.astype('float')/255
X_test = test_X.astype('float')/255

from tensorflow.keras.utils import to_categorical

y_train = to_categorical(train_y, 10)
y_test = to_categorical(test_y, 10)

#建構CNN網路

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

CIFAR10_Model = Sequential()
CIFAR10_Model.add(Conv2D(32, (3,3), activation = 'relu', input_shape = (32, 32, 3)))
CIFAR10_Model.add(MaxPooling2D((2,2)))
CIFAR10_Model.add(Conv2D(64, (3,3), activation = 'relu'))
CIFAR10_Model.add(MaxPooling2D((2,2)))
CIFAR10_Model.add(Flatten())
CIFAR10_Model.add(Dense(128, activation = 'relu'))
CIFAR10_Model.add(Dense(10, activation = 'softmax'))

CIFAR10_Model.summary()

CIFAR10_Model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', 
                      metrics = ['acc'])

from tensorflow.keras.callbacks import TensorBoard

CIFAR10_TB = TensorBoard(log_dir = './CIFAR100810', histogram_freq = 1, write_images = True)

CIFAR10_Model_History = CIFAR10_Model.fit(X_train, y_train, epochs = 20, batch_size = 256,
                                         callbacks = [CIFAR10_TB],
                                         validation_split = 0.2)

#評估模型
Test_loss, Test_acc = CIFAR10_Model.evaluate(X_test, y_test)
print(f'Testing loss is {Test_loss} and Testing accuracy is {Test_acc}')

https://ithelp.ithome.com.tw/upload/images/20210810/20139039jpMrucd3Hs.png


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言