<jupyter_start><jupyter_text>Convolutional Neural Networks - Mạng neuron tích chập<jupyter_code>import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Tải bộ dữ liệu MNIST<jupyter_code>from tensorflow.keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Dữ liệu sẽ được tải về và lưu ở C:\Users\thien\.keras\mnist.npz
X_train.shape
X_test.shape
X_train[0]
plt.imshow(X_train[0], cmap='gray')
X_train = X_train.reshape(-1, 28*28)
X_test = X_test.reshape(-1, 28*28)
# Số -1 là để cho python tự tính số còn lại cho phù hợp
X_train.shape
# Ép kiểu
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# Rescale 
X_train /= 255.0
X_test /= 255.0
X_train[0]
from tensorflow.keras.utils import to_categorical
y_train_cat = to_categorical(y_train)
y_test_cat = to_categorical(y_test)
y_train[0]
y_train_cat[0]
y_train_cat.shape
y_test_cat.shape<jupyter_output><empty_output><jupyter_text>Sử dụng mạng DNN<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import tensorflow.keras.backend as K
# Reset 
K.clear_session() 
# Tạo model
model = Sequential()
model.add(Dense(512, input_dim=28*28, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 512)               401920    
_________________________________________________________________
dense_1 (Dense)              (None, 256)               131328    
_________________________________________________________________
dense_2 (Dense)              (None, 128)               32896     
_________________________________________________________________
dense_3 (Dense)              (None, 32)                4128      
_________________________________________________________________
dense_4 (Dense)              (None, 10)                330       
=================================================================
Total params: 570,602
Trainable params: 570,602
Non-trainable params: 0
__________________________________________________[...]<jupyter_text>Training<jupyter_code>h = model.fit(X_train, y_train_cat, batch_size=128, epochs=10, verbose=1, validation_split=0.3)
plt.plot(h.history['accuracy'])
plt.plot(h.history['val_accuracy'])
plt.legend(['Training', 'Validation'])
plt.title('Accuracy')
plt.xlabel('Epochs');<jupyter_output><empty_output><jupyter_text>Đánh giá<jupyter_code>test_accuracy = model.evaluate(X_test, y_test_cat)[1]
test_accuracy<jupyter_output>313/313 [==============================] - 2s 5ms/step - loss: 0.1118 - accuracy: 0.9801<jupyter_text>Sử dụng Convolutional neural networks<jupyter_code>X_train = X_train.reshape(-1, 28, 28, 1)
X_test = X_test.reshape(-1, 28, 28, 1)
# Số -1 là để cho python tự tính số còn lại cho phù hợp
X_train.shape
from tensorflow.keras.layers import Flatten, Activation, MaxPool2D, Conv2D<jupyter_output><empty_output><jupyter_text>Tạo model CNN<jupyter_code># Reset
K.clear_session()
# Tạo model
model = Sequential()
# Thêm các layer
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Flatten())

model.add(Dense(128, activation='relu'))

model.add(Dense(10, activation='softmax'))
# Thiết lập thông số
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 32)        320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 11, 11, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)          0         
_________________________________________________________________
flatten (Flatten)            (None, 1600)              0         
_________________________________________________________________
dense (Dense)                (None, 128)               204928    
________________________________________________________[...]<jupyter_text>Training<jupyter_code>model.fit(X_train, y_train_cat, batch_size=128,
          epochs=5, verbose=1, validation_split=0.3)<jupyter_output>Epoch 1/5
329/329 [==============================] - 21s 39ms/step - loss: 0.2515 - accuracy: 0.9216 - val_loss: 0.0999 - val_accuracy: 0.9690
Epoch 2/5
329/329 [==============================] - 12s 37ms/step - loss: 0.0653 - accuracy: 0.9802 - val_loss: 0.0698 - val_accuracy: 0.9783
Epoch 3/5
329/329 [==============================] - 12s 37ms/step - loss: 0.0434 - accuracy: 0.9863 - val_loss: 0.0598 - val_accuracy: 0.9814
Epoch 4/5
329/329 [==============================] - 12s 37ms/step - loss: 0.0322 - accuracy: 0.9903 - val_loss: 0.0463 - val_accuracy: 0.9869
Epoch 5/5
329/329 [==============================] - 12s 37ms/step - loss: 0.0239 - accuracy: 0.9928 - val_loss: 0.0579 - val_accuracy: 0.9832<jupyter_text>Đánh giá<jupyter_code>model.evaluate(X_test, y_test_cat)<jupyter_output>313/313 [==============================] - 2s 5ms/step - loss: 0.0435 - accuracy: 0.9869<jupyter_text>Bài tập tự luyện: So sánh DNN và CNN về chất lượng đầu ra, tiêu thụ tài nguyên, số epoch cần chạy, thời gian cần chạy.Thay đổi các tham số như số layer, tăng giảm số Conv2D, số node trong mỗi layer, hàm activation... để xem sự thay đổi của kết quả. Lựa chọn tham số tối ưu. Bài tập (Khó nhẹ):Thử sức với bộ dữ liệu Cifar. Đây là bộ dữ liệu gồm 10 đối tượng khác nhau cần nhận diện gồm:- airplane 										- automobile 										- bird 										- cat 										- deer 										- dog 										- frog 										- horse 										- ship 										- truckDữ liệu có sẵn trên Keras**Bài tập**: Tạo model gồm các layer sau:    - conv2d    - conv2d    - maxpool    - conv2d    - conv2d    - maxpool    - flatten    - dense    - output<jupyter_code>from tensorflow.keras.datasets import cifar10
(X_train, y_train), (X_test, y_test) = cifar10.load_data()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15870967741935485, 'fraction_numerical': 0.06007168458781362, 'mean_word_length': 4.129411764705883, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '326879', 'license': 'MIT', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2416, 'n_words': 507, 'path': 'Code/.ipynb_checkpoints/Convolutional Neural Networks-checkpoint.ipynb', 'repo_name': 'nguyenhuynhthien/DeepLearningFreeCource', 'size': '6975'}