---
# System prepended metadata

title: ml lecture 1 - 2 Predicting the views of this channel - Introduction of ML / DL
tags: [ml]

---

{%hackmd BJrTq20hE %}

# ml lecture 1 - 2 Predicting the views of this channel - Introduction of ML / DL
[slides](https://drive.google.com/file/d/12ri8Na55Z3gvqZginH4s9EGKo9v9a3Cl/view?usp=sharing)

## Training
1. Function with Unknow Parameters
2. Define **Loss** from Training Data
3. Optimization
   > ### Gradient Descent
   > 1. (Randomly) Pick an initial value $w^0$ (w can generalize to any parameter of L)
   > 2. Compute $$m={\partial L \over \partial w} | _{w=w^0}$$
   >    $w^1 \leftarrow w^0 - \eta m | _{w=w^0}$ (步伐大小由斜率和$\eta$決定，$\eta$: learning rate)
   > 3. Update $w$ iteratively (update all parameters to the general case)

## Linear Model

$$
y = b + \sum_j w_jx_j
$$
where $x_j$ are features and $w_j$ are weights


## Sophisticated Models
- red curve = constant + sum of a set of blue line (piecewise) (hard sigmoid)
  ![](https://github.com/qwer87511/HackMD/blob/master/ml/xZ3TMWQ.png?raw=true =300x)
- Sigmoid (s型的function) (用很多藍直線組合出一個平滑(藍)曲線)
  $$
  sigmoid(b+wx_1) = {1 \over 1 + e^{-(b+wx_1)}}
  $$
- a blue line $$
  y = c \cdot sigmoid(b+wx_1)
  $$
- ![](https://github.com/qwer87511/HackMD/blob/master/ml/DyjsNRt.png?raw=true =300x)
- a red curve (with 1 feature) ($b+wx_1$放到sigmoid裡面) $$
  y = \sum_i c_i \cdot sigmoid(b_i + w_ix_1)
  $$
- new model (with j features) (put linear model into sigmoid)
  (此處僅將linear model平滑化) (只看i和只看j比較好理解)
  (i是將linear model分割成piecewise functions所需的藍線個數(自己決定)，即需要幾個sigmoid functions來近似它, j是features的個數)
  $$
  y = b + \sum_i c_i \cdot sigmoid(b_i + \sum_j w_{ij}x_j)
  $$
- let i = 3 be no. of features, j = 3 be no. of sigmoid for example
  let $r_i$ be $b_i + \sum_j w_{ij}x_j$ (即上式sigmoid的裡面)
  so that $r_1 = b_1 + w_{11}x_1 + w_{12}x_2 + w_{13}x_3$ (即linear model做piecewise的第一個藍線)
  so that
  $$
  \begin{bmatrix}
  r_1 \\
  r_2 \\
  r_3 \\
  \end{bmatrix}=
  \begin{bmatrix}
  b_1 \\
  b_2 \\
  b_3 \\
  \end{bmatrix}+
  \begin{bmatrix}
  w_{11} & w_{12} & w_{13} \\
  w_{21} & w_{22} & w_{23} \\
  w_{31} & w_{32} & w_{33} \\
  \end{bmatrix}
  \begin{bmatrix}
  x_1 \\
  x_2 \\
  x_3 \\
  \end{bmatrix}
  $$
  $\to r=b+Wx$
  let $a_i = sigmoid(r_i) = {1 \over 1 + e^{-r_i}}$ 
  $\to a = \sigma (r)$ ($\sigma$ is $sigmoid$ function to vector)
  $y = b + c_1a_1 + c_2a_2 + c_3a_3$
  $\to y = b + c^Ta$
  ![](https://github.com/qwer87511/HackMD/blob/master/ml/tWylEtp.png?raw=true =300x)

- $y = b + c^T \sigma (b + Wx)$ (前面的b是常數後面是向量)

## Back to ML Framework
- $$
  \theta = 
  \begin{bmatrix}
  \theta_1 \\
  \theta_2 \\
  \vdots \\
  \end{bmatrix}=
  \begin{bmatrix}
  columns\;of\;W \\
  b \\
  c^T \\
  b \\
  \end{bmatrix}
  $$ (所有參數一律統稱$\theta$)
- Back to ML Framework
- $\theta$ 做為新的features再做一次[Training steps](##Training)
- Define loss function $L(\theta)$
- Optimization of New Model
  - $\theta^* = arg\;\displaystyle\min_\theta\;L$
  - (Randomly) Pick initial values $\theta^*$
  - gradient $$
    g = \begin{bmatrix}
    \ {\partial L \over \partial \theta_1} | _{\theta=\theta^0} \\
    \ {\partial L \over \partial \theta_2} | _{\theta=\theta^0} \\
    \ \vdots \\
    \end{bmatrix}
    $$ (把所有參數都拿去對L做微分)
  - $g = \nabla L(\theta^0)$
  - $\theta^1 \leftarrow \theta^0 - \eta g$ (重覆到不想做為止)
  - split the data into multiple batches and calculate gradients separately(p. 44)
  - $g = \nabla L^1(\theta^0)$ (by batch 1), and update  $\theta^1$, ...
  - 1 **epoch** = see all the batches once
- Sigmoid $\to$ ReLU
  $$y = b + \sum_i c_i sigmoid(b_i + \sum_j w_{ij}x_j)$$
  $$\to y = b + \sum_{2i} c_i max(0, b_i + \sum_j w_{ij}x_j)$$
- Back to ML framework $a=\sigma(b+Wx)$, 算出來的**a**再作為input, 再放進一組新的function(具不同參數), 得到**a'**, 可以反覆做多次
  ![](https://github.com/qwer87511/HackMD/blob/master/ml/uXOh1NU.png?raw=true =300x)
- 一個activation function稱作 Neuron
- 每一層稱作 hidden layer
- 很多層合起來稱作 Neural Network
- many layers means **Deep** $\to$ Deep learning
- Better on traning data, worse on unseen data $\to$ **Overfitting**


## Terminologies
- sophisticated
- piecewise 分段的
- rectified Linear Unit (ReLU) (修正線性單元) (用2個可以合成一個hard sigmoid) (p. 47) (效果比sigmoid好)
- activation function (激勵函式) (包含sigmoid, ReLU)
- epoch 時期
- neuron 神經元
- neural Network

###### tags: `ml`