TensorFlow là framework học máy của Google, open-source, dùng với Python cho thuật toán, deep learning. Tối ưu hóa phép toán phức tạp. Tensor là cấu trúc dữ liệu đa chiều (mảng/list).
Keras là API deep learning viết bằng Python, chạy trên TensorFlow, giao diện cấp cao, thử nghiệm nhanh, mở rộng tốt.
Functional API hỗ trợ mô hình đa đầu vào/đầu ra, topo không tuyến tính.
Sử dụng Google Colab. Code huấn luyện mô hình với nhiều đầu vào (title, body, tags) và nhiều đầu ra (priority, department):
Ví dụ
print("Dữ liệu đầu vào mẫu")
title_data = np.random.randint(num_words, size=(1280, 10))
body_data = np.random.randint(num_words, size=(1280, 100))
tags_data = np.random.randint(2, size=(1280, num_tags)).astype("float32")
print("Dữ liệu mục tiêu mẫu")
priority_targets = np.random.random(size=(1280, 1))
dept_targets = np.random.randint(2, size=(1280, num_classes))
print("Huấn luyện mô hình")
model.fit(
{"title": title_data, "body": body_data, "tags": tags_data},
{"priority": priority_targets, "class": dept_targets},
epochs=2,
batch_size=32,
)
Nguồn: TensorFlow Guide
Đầu ra
Dữ liệu đầu vào mẫu Dữ liệu mục tiêu mẫu Huấn luyện mô hình Epoch 1/2 40/40 [==============================] - 5s 43ms/step - loss: 1.2738 - priority_loss: 0.7043 - class_loss: 2.8477 Epoch 2/2 40/40 [==============================] - 2s 44ms/step - loss: 1.2720 - priority_loss: 0.6997 - class_loss: 2.8612 <tensorflow.python.keras.callbacks.History at 0x7f48d0809e80>
Giải thích
- Tạo dữ liệu đầu vào và mục tiêu mẫu.
- Huấn luyện mô hình bằng cách truyền mảng NumPy của đầu vào và mục tiêu (dictionary cho đa đầu vào/đầu ra).