28.04.2020 Views

Sách Deep Learning cơ bản

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

9.3 Python code 131

Thử đánh giá hàm L. Giả sử ảnh là số 5 thì L = −log( yˆ

6 ).

Nhận xét:

• Hàm L giảm dần từ 0 đến 1

• Khi model dự đoán yˆ

6 gần 1, tức giá trị dự đoán gần với giá trị thật y 6 thì L nhỏ, xấp xỉ 0

• Khi model dự đoán yˆ

6 gần 0, tức giá trị dự đoán ngược lại giá trị thật y 6 thì L rất lớn

=> Hàm L nhỏ khi giá trị model dự đoán gần với giá trị thật và rất lớn khi model dự đoán sai, hay

nói cách khác L càng nhỏ thì model dự đoán càng gần với giá trị thật. => Bài toán tìm model trở

thành tìm giá trị nhỏ nhất của L.

Hàm loss function định nghĩa như trên trong keras gọi là "categorical_crossentropy"

9.3 Python code

@author: DELL

# 1. Thêm các thư viện cần thiết

import numpy as np

import matplotlib.pyplot as plt

from keras.models import Sequential

from keras.layers import Dense, Dropout, Activation, Flatten

from keras.layers import Conv2D, MaxPooling2D

from keras.utils import np_utils

from keras.datasets import mnist

# 2. Load dữ liệu MNIST

(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_val, y_val = X_train[50000:60000,:], y_train[50000:60000]

X_train, y_train = X_train[:50000,:], y_train[:50000]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!