# Lecture Note ML4AI 2021
## Session 1: Linear Predictor
. Input: data
. Output: Predictions
=> Machine learning
Có 2 loại bài toán dự báo
. Regression (liên tục)
. Classification (rời rạc
Bài toán tuyến tính:
Gt dự đoán: Mô hình Z + Mô hình W
Sự so khớp:
. Sử sụng tích chấm để đo sự giống nhau
. Đo khoảng cách gần hoặc xa
## Session 4: RecSys Recommender System
Ứng dụng của RecSys
. Trong Facebook khi kết bạn với 1 người thì hệ thống sẽ tự động đưa ra gợi ý kết bạn khác
. Có trong hệ thống robot, virtual assistant...
Vì sao RecSys có thể hoạt động tốt
. Generalization counts: có khà năng khái quát hóa từ nhiều người dùng
. Personalization: có khả năng cá nhân hóa
Bài toán RecSys:
. Gồm 2 nhóm: user và item
. Input: user, item
. output: predictions
Có 2 cách:
. user-based filtering
. item-based filtering
Một vài giải thuật:
+ Collaborative Filtering (CF)
1. Item-based CF
2. User-based CF
3. Matrix factorization
+ Personlized recommender

## Session 8: Convulutional Neutral Networks
Matching & Similarity, dùng dot product
Trong hình ảnh: spatial matching (sử dụng tích chập)
Filter nhúng => trượt qua các ảnh
Filter: là hình vuông (3x3 hoặc 4x4)
padding: giữ thông tin cho input, tránh bị thất thoát thông tin ở góc cạnh
- Padding


- Stride

- CNN output shape
- 
- Pooling
. Phép gộp giúp:
giảm kích thước đầu vào nhanh chóng
giúp mô hình vững chắc trước các sai lệch nhỏ
...
max pooling: sharp features
average pooling: smooth

DCNN
. Flatten

- Cnn effective receptive field


## Session 9: Recurrent neutral networks
Review
trích xuất đặc trưng: dùng PCA, sparse coding, convolution
Đưa ra dự đoán Y mũ: dùng SVM, Logistic Regression, Softmax,...
- Chuỗi dự báo, chuỗi con số để dự đoán xu hướng trong tương lai, dự báo những chuỗi tiếp theo (ví dụ như âm thanh, lời nói, nhu cầu khách hàng, giá vàng, chứng khoán,...) : sequential data, time series
- Moving in concept embedding space: được thực hiện khi cộng vào một delta z
σ: sigma
Lý do vì sao gọi là recurrent: do output được dùng đề dự báo output tiếp theo
Mô hình recurrent networks: trải các input qua thời gian
- Vanilla RNN

.

. Là backpropagation through time
- Vấn đề short term memory
Những cái xảy ra gần với chuỗi bắt đầu sẽ được sửa nhiều hơn so với những thông tin ở phía sau
=> dùng LSTM hoặc GRU
.
### Session 10: K-means Clustering: Unsupervised Learning
Review
Đầu vào x => trích xuất đặc trưng Z (tọa độ nhúng Z) => đưa ra dự báo Y mủ
Vì sao phải có tọa độ nhúng Z
=> đưa ra dự báo, prediction, làm mô hình tốt hơn
Clustering: dùng để giảm chiều dữ liệu (giống PCA)
Clustering:
. Unsupervised Classification
. Unsuperised Segmentation
Customer segmentation

Bài toán classification
. Cần phải biểu diễn các mô hình
. So sánh các input x vào các mô hình đó
. đưa qua hàm softmax để ra posibility
. dùng argmax để phân loại class
Input trong clustering thuộc cụm nào gần với input đó nhất
centroid: được tính bằng trung bình các đặc trưng X
-Để phân chia rõ ràng các điểm trong clustering cần phải tối ưu sự giống nhau, liên kết trong cùng một nhóm (intracluster) và sự khác nhau giữa các nhóm (intercluster)
### Session 11: MDP Planning
Z: nằm trong agents
Observation O: có trong enviroment
Planning example: Tower of Hanoi

Planning: là một chuỗi các hành động đến đạt được một hành động nào đó một cách hiệu quả nhất, nhanh nhất (ví dụ: chơi cờ, bài toán logistic, ...)
Mỗi hành động đều khác nhau do trạng thái khác nhau (Z)
- Mô hình MDP: hàm đơn giản trong planning (Simplest formulation)

Để có opimal plan, tại mỗi state sẽ có đầu ra là action A, hoặc tại action A thì sẽ cho ra reward như thế nào
Để đạt được opimal plan, plan nào có nhiều reward nhất (q(s,a) cao nhất) thì plan đó là lựa chọn tốt nhất

Muốn biết được xác xuất, giá trị của hành động đó tại một state, ta tính trung bình cộng các reward của cái action đó

## Final Test
---
tags: COTAI AI4HighSchool
slideOptions:
transition: slide
width: 1920
height: 1080
margin: 0.1
minScale: 0.2
maxScale: 1.5
---
# ML4AI Session 12 Final Exam (for Students)
<br>

<br>
CoTAI AI Training Program for Highschool Students
© [CoTAI](https://gem.cot.ai) 2021
---
Open book. Choose to answer as many questions as possible, as concisely as possible. Ask instructors if you have difficulty in understanding the questions.
**Questions**
1. [**8** Points] The unified TEFPA framework to build a ML model: task $\mathcal{T}$, experience $\mathcal{E}$, function space $\mathcal{F}$, performance $\mathcal{P}$, and algorithm $\mathcal{A}$ to search. What are the elements in TEFPA framework...
- 1.1 [**2** Point, 3.1] to build a face recognition model using a DCNN?
. Trong mô hình DCNN có:
-Matching & Similarity, dùng dot product để tính
-spatial matching (trong hình ảnh), (sử dụng tích chập để tính)
-các filter nhúng
-padding: giữ thông tin cho input, tránh bị thất thoát thông tin ở góc cạnh
- 1.2 [**2** Point, 3.1] to build a RecSys? (using one of the models you have learned: item-based, user-based, content-based, MF)
. Bài toán RecSys:
. Gồm 2 nhóm: user và item
. Input: user, item
. output: predictions
- 1.3 [**2** Point, 3.1] to build a customer segmentation model using k-means?
Bài toán customer segmentation
. So sánh các input x vào các mô hình đó
. đưa qua hàm softmax để ra posibility
. dùng argmax để phân loại class
-Input trong clustering thuộc cụm nào gần với input đó nhất
-centroid: được tính bằng trung bình các đặc trưng X.
- 1.4 [**2** Point, 3.1] to build a sentiment analysis model (good, bad, neutral comments) using RNN+Softmax Classifier?
. Mô hình recurrent networks: trải các input qua thời gian
-Tách các từ trong comment thành từng mảnh nhỏ rồi xem những từ đó có ý nghĩa tốt hay xấu, từ đó suy ra tính chất của comment đó như thế nào (tốt, xấu, neutral)
2. [**6** Points] Convolutional filters (or kernels)
- 2.1 [**1** Point, 1.1, 3.2] How do we extend them from dot product?
- 2.2 [**1** Point, 1.1, 3.2, 3.4] Why do we call their outputs "feature maps"?
- 2.3 [**1** Point, 3.2] Explain padding: how to do & main purpose
. padding: giữ thông tin cho input, tránh bị thất thoát thông tin ở góc cạnh
- 2.4 [**1** Point, 3.2] Explain pooling: how to do & main purpose
. Pooling giúp
-giảm kích thước đầu vào nhanh chóng
-giúp mô hình vững chắc trước các sai lệch nhỏ
- 2.5 [**1** Point, 3.2] Explain stride: how to do & main purpose
. Stride is a parameter of the neural network's filter that justify the number of movements in images
- 2.6 [**1** Point, 3.2, 3.4] Explain their **effective** receptive field: why do they produce highly absstract features?
. better initialization schemes can be employed to increase the receptive field in the beginning of the training, and that could be called effective receptive field
3. [**6** Points] Recurrent neural networks (RNNs) can be used for sequential modeling.
- 3.1 [**1** Point, 3.2] What does sequential data mean?
. Sequential data: Chuỗi dự báo, chuỗi con số để dự đoán xu hướng trong tương lai, dự báo những chuỗi tiếp theo (ví dụ như âm thanh, lời nói, nhu cầu khách hàng, giá vàng, chứng khoán,...)
- 3.2 [**1** Point, 1.1, 3.2, 3.4] Explain each element in this basic equation of RNNs $h_t = \mathsf{\gamma}(Ah_{t-1}+Wz_t)$
- 3.3 [**2** Point, 1.3, 2.1, 3.2] WWhat does back-propagation-through-time mean, why do we need it instead of using plain back-prop, and how does it work for training RNNs?
. Backpropagation through time: Lan truyền ngược liên hồi, được dùng để huấn luyện thuật toán RNN
- 3.4 [**1** Point, 1.3, 3.2] Explain vanishing gradient problem for simple RNNs.
. Những thông tin đầu ra đã được sử dụng sẽ trở thành input tại điểm tiếp theo và ta có thể tính được giá trị của function đó tại từng điểm
. Giải pháp: dừng backpropagating, đặt giới hạn cho gradient
- 3.5 [**1** Point, 3.1, 3.3] If we want to classify the sentiment of each user comment (good, bad, neutral) at the end of each sequence using RNN+Softmax classifier: explain briefly the model architecture.
4. [**6** Points] Planning in Markov Decision Process (MDP) $(S,A,T,R,\gamma)$.
- 4.1 [**1** Point, 3.1, 3.2] Explain 5 elements in MDP model (equation of each element if available).
. S: là chỉ state, trạng thái của vật tại một thời điểm
A: action, hành động của vật khi chuyển sang một một trạng thái nào đó
P: xác xuất cao hay thấp để máy có lựa chọn khi tìm kiếm một action phù hợp
R: reward, khi ta thực hiện hành động nào đó thì sẽ cho ra reward tương ứng với hành động đó
gamma: phụ số để cho mô hình được tốt hơn
- 4.2 [**1** Point, 3.2] Following a policy $\pi(s)$ to generate a trajectory of 10 time steps $(s_t,a_t,s_{t+1},r_{t+1})$. Compute the return. Equation of $a_t$?
- 4.3 [**1** Point, 1.2, 3.2] Repeat for 10 days: from $s_0 = \text{HOME}$ take action $a_0 = \text{GET_BUS}$ with cost $r_1 = 6000 \text{VNĐ}$ then following policy $\pi(s)$ to generate $K=10$ trajectories, each with total cost $G_k$. Compute the average cost of taking bus then following $\pi$: $Q^\pi(\text{HOME, GET_BUS})$.
- 4.4 [**1** Point, 1.1, 1.3, 2.1, 3.2] How do we compute an optimal plan (i.e., optimal policy $\pi^*$) of a known MDP $(S,A,T,R,\gamma)$?
. Optimal plan: reward + (gamma * sigma(possibility))
- 4.5 [**1** Point, 3.2] Why do we say that the action value function $Q^\pi(s,a)$ gives predictions into very far future?
- 4.6 [**1** Point, 1.2, 3.2] What is the meaning of action value function when we set $\gamma = 1$? $\gamma = 0$?
. Gamma là phụ số để giúp mô hình chạy tốt hơn, nên khi để gamma bằng 0 thì nó sẽ không tốt bằng khi gamma bằng 1
5. [**7** Points] Unified ML models
$\text{Input } X \xrightarrow[B_{\beta}]{\text{Features}}\text{ Embedding Coordinates }Z \xrightarrow[P_{\theta}]{\text{Predictor}}\text{ Predictions }\hat{Y} \xrightarrow[{\pi^*}]{\text{Policy}}\text{ Action }A$
- 5.1 [**2** Points] List all *taught* algorithms for feature extraction and their main ideas.
. PCA, sparse coding, convolution, CNN
- 5.2 [**2** Points] List all *taught* algorithms for making predictions and their main ideas.
. SVM, Logistic Regression, Softmax
- 5.3 [**2** Points] What are the main *general* differences between linear predictors? And in your opinion why do we need different algorithms?
.
- 5.4 [**1** Points] For MDPs, what are the predictions $\hat{Y}$ used to make decisions $A$?
-A (Action) và S (State)
6. [**2** Points] RecSys

We build item embeddings ${\bf z}_i \in \mathbb{R}^2$ as in table, and use **softmax regression** to predict ratings 1 to 5. Choose a specific user $X\in \{A,\dots,F\}$, what is the training set for learning $\theta_X$? What are the parameters $\theta_X$ to be learned (with their shapes)?
-Users
7. [**6** Points] MDP Planning for playing Chess. Let rewards = 1 for winning, -1 for losing, and 0 for a draw or unfinished game, and no discount.
- 7.1 [**2** Points] What is the range of value of the optimal action-value function $Q^*(s,a)$, and how to derive probability of win/loss from it?
- 7.2 [**2** Points] If we use all the games already played in history to compute $Q^*(s,a)$, explain the method?
-Ta sẽ
- 7.3 [**2** Points] Because there are so many state and action pairs $(s,a)$, we need to use *learning* to approximate and generalize for all $(s,a)$ pairs. If we use MLP to learn $Q^*(s,a)$, what is the dataset and possible network structure?