tags: study

ML 學習

Regression

Training data

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

將所有的 data 呈現出來,並且希望能夠找出一條 function 最能夠 fit 圖上的點。

loss function

但是這個 "fit" 到底是如何數學式表達出來呢?那就要依靠 loss function

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

y-hat 是 training data,而後面的

f(xcpn) 是這個 function 預測的值,希望這個 gap 越小越好。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

以一維參數來看,能夠畫出像上圖一樣的圖,而我們想找出的就是 local minima

gradient descent

接著,既然知道要找出 loss cost 最小的 function,就必須去嘗試許多的參數,以找出能使之達到最小 cost 的參數。如何找呢?這時就要使用 gradient descent

想法就是 如果斜率是正的那就往左靠並且更新,反之,直到算出更新的值跟原本的值已經幾乎相同就停止

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • limitation of GD
    • local minima

      • 如果起始點的位置不好,那就會卡在 local 而找不到 global 了
        Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →
    • saddle point

      • 以某一軸來看是 minima,但是另一軸來看是 maxima
        Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →

overfitting

當找出參數了之後,所產生的 model 有可能使得 loss cost 相當的小,基本上是一個完全能 fit training data 的 function,但這樣真的好嗎?可以看到以下的兩個 model

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

underfitting

當 function 都無法 fit 原本的 training data 時,即稱為 underfitting

bias & variance

通常預測結果會和實際的結果有所誤差,但為什麼會發生這件事呢?這個 error 來自 bias + variance

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Modify model in accordance public set

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在找 model 時,有可能因為 testing set 的結果而回去調整 training 的 model,但此舉會使得真正去預測 real testing set 時的 model 變得不準,因為該 model 加入了 testing set 的 bias 了。

N-fold cross validation

真正該做的是使用 training data 去驗證每個 model,而不該使用 testing set 去驗證,這樣才能保證該 model 都是依據 training set 。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

如上圖,這樣做出來的 model 在 public testing set 呈現的樣子,才能反映出放到 real testing set 的結果,而 N-fold cross validation 就是將 training set 切成多份並且取平均。

Gradient descent advanced

當在使用 GD 時,會有一個錯誤的觀念是,當 gradient 越大,則 learning rate 應該越大才對(gradient 越大則希望快越大步才能趕快接近最低點)
但事實上這是以一個參數的方向去想的,若有多個參數的話則未必,示意圖如下

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

以 A 跟 C 來看,C 的 gradient 比 A 大,但事實上 C 更接近 local minima ,所以其 learning rate 應該越小才對。

Adagrad (Adaptive learning rate)

續上面的說明,為何 "一次微分後的 gradient 越大,則 learning rate 越大" 這件事是錯的呢?以下圖來看,因為我們只考慮了

|2ax0+b| 這個分子的部分,而沒有考慮的
2a
這個分母,而這分母是來自二次微分來的。
|(2ax0+b)/2a|
的來源詳細在 L.(3-1)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

透過

|(2ax0+b)/2a| 算出來的 learning rate 才是跟 local minima 成比例的數值,而 Adagrad 就是依據這個公式去找 learning rate 的,其公式如下

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

分母以那根號呈現而不是以二次微分呈現,是因為若是算二次微分時要花太多時間了,那又為什麼可以以這根號公式代替呢?詳細

stochastic GD

在原始的 GD 中,每次 update 一個 iteration 時,都要將所有的 training set 給跑過,公式如下

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

但若 training set 過多時,其計算量將會特別的大,於是就有專家提出 SGD,也就是不將所有的 train set 跑過,而是只隨機取出其中的一筆,這樣的優點在於相當快速。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

classfication

probablistic generative model

他概念是說,給定一堆的 data,想要找出這些 data 的高斯分佈。
什麼意思?

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

以上圖為例,可以知道水系的分佈大概是這樣子,直觀來看可以說出如果 SP 接近 60~80 ,有很高的機率是水系的,而這裡就是想要找出這樣的高斯分佈,得到一個未知的 X 在水系的機率為多少。

他的最簡單的概念,是想要找出目前這個 test data 從那個 class 出來的機率是多少,公式如下

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

以這例子來講有 79 個 train data,要找出一個高斯分佈使得這 79 個點丟入公式,可以得到最大的 likelihood(可能性)。要怎麼做呢?找高斯分佈需要 mean 以及 variance,公式如下

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

將此高斯算出來後,就可以套入 probablistic generative model 去做了。
png)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

以上的數學中的分子,是 x 在 C1 裡的機率,分母是 x 出現在 class C1 和 C2 的機率,以例子來講 x=test, C1=水系, C2=普通系,並且想問 x 是水系的機率為何

可以看以上的 formula,其實他是從貝氏定理來的

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

分母的部分原本應該要是
P(X)
,而其實
P(X)=P(x|c1)P(c1)+P(x|c2)P(c2)
,所以才會有上面的 generative model function。Bayes' 可以應用在當在已知 A 時找 B 的機率(
P(B|A)
)不好找時,但找已知 B 時找 A 的機率相當容易時的情況 (
P(A|B)
)。

個人覺得這邊蠻饒口的需要想一下,以上課的神奇寶貝的例子來看,想找出 "一隻很像水系的怪物,他是水系的機率" 很難找,這是我們的目標。但是找出 "水系裡面有這一隻很像水系怪" 的機率會是相對好找的,因為我們可以求出 mean & covariance,得到水系的高斯分佈後,能得出在水系 class 的所有 input 機率。

shared covariance

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

當有多個不同的 class 時,會計算出不同的 covariance,這會使得 model 的參數過多導致 overfitting 的現象。想要改善這問題,就是使得所有的 class 都使用相同的 covariance,這除了讓參數變少之外,更會使 model 變成 linear 的(跟後面的 posterior function 簡化有關,最後可以簡化成

sigmoid(wx+b) 的線性函式),使得求解的 accuracy 變高(老師這邊沒有特別說為何,只說這就是機器學習不可理解的部分)。

為什麼不能用 regression 解 classification ?

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

以上圖為例,右圖的紫色是 regression 可能找出來的 function,這代表什麼意思?為什麼會更偏右下呢?因為在找 regression 的時候,會希望藍色的 class(這邊是訂藍色 class=1) 越接近 1 越好,可以明顯發現右下角的 data 離 function 太遠了,所以會相當遠離 1,這時 regression 會針對此去做修正。但是,classification 來看,綠色的線才是更好的分類,會有此原因是因為 regression 會懲罰 "過度正確" 的 data,使他們不要離 1 那麼遠,但以分類角度來講這件事是不合理的。

另外,若是 multi-case 來看的話,把 class 1 訂為 1, class 2 訂為 2 等,這樣也不對,原因是因為這樣表示 class 1 跟 3 的關係比 class 1 跟 2 還要遠,但這些 class 有可能是獨立的。

Generative model & logistic regression

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

以上圖為例,若是假設跟 probability 是高斯分佈,則可以寫出

P(C1|x)=P(x|C1)P(C1)/P(x|c1)P(c1)P(x|c2)P(c2),整理一下同除分子可以得到
sigmoid(z)
,所以可以知道說 posterior probability 其實就等於
sigmoid(z)

HW1

  • code

    ​​​​data = pd.read_csv('./train.csv', encoding = 'big5') ​​​​data = data.iloc[:, 3:] ​​​​data[data == 'NR'] = 0 ​​​​frame = data.to_numpy() ​​​​month_data = {} ​​​​for m in range(0, 12): ​​​​ j=0 ​​​​ sample = np.empty([18, 480]) ​​​​ for i in range(0, 360): ​​​​ day = divmod(i,18)[0] ​​​​ sample[i%18][0+(24*day):24+(24*day)] = frame[i+360*m] ​​​​ month_data[m] = sample ​​​​students = {'hours': list(range(0, 24*20)),'test_results': month_data[0][0]} ​​​​sdata = pd.DataFrame(data=students) ​​​​X = sdata.iloc[:, 0].values.reshape(-1, 1) # values converts it into a numpy array ​​​​Y = sdata.iloc[:, 1].values.reshape(-1, 1) ​​​​X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0) ​​​​regressor = LinearRegression() ​​​​regressor.fit(X_train, y_train) ​​​​y_pred = regressor.predict(X_test) ​​​​df = pd.DataFrame({'Actual': y_test.flatten(), 'Predicted': y_pred.flatten()}) ​​​​df1 = df.head(25) ​​​​df1.plot(kind='bar',figsize=(16,10)) ​​​​plt.grid(which='major', linestyle='-', linewidth='0.5', color='green') ​​​​plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black') ​​​​plt.show()
  • ref

HW2

Normalize

  • why normalize

    什麼時候需要 normalize? 當 feature 的 range 不同時就會需要

    這是上述文章的重點圖,左邊是沒有 normalize 的,右邊則是有 normalize,左邊可以明顯看到在 epoch 26 的時候跟 epoch 1 時是一樣的,因為 data range 的差異(文章例子兩者差 1000 倍),在做 GD 的時候會需要更長的時間才能找到 local/global minima ,所以做了 normalize 可以加速 GD 的計算時間。

    • formula

      • (x-mean) / std,對每一行都做此步驟
    • without normalize


      可以看到作業二在 GD 時,如果沒有將原本的 input data 正規化的話,其 loss cost 經過 10 次 iteration 之後仍然很高

    • with normalize


      加入 normalize 後,其 GD 在 converge 時就快很多,這邊可以看到使用 normalize 的重要性

  • Mini-batch training

    • 他將 train set 分成 train/dev set 之後,要對 train set 做 GD,但因為 GD 計算於一整個的 train set 量過於龐大,於是這邊是使用 batch 的方式去做計算的。概念是將 train set 分成多個 batch,一次 epoch 就把這些 batch 跑完,這樣可以使得 GD 的過程快了一些。

Deep learning

概念圖

其為 input 將 data 放入網路,並且乘上每個 neuron 的 weight 以及加上 bias 之後,通過 active function(此處為 sigmoid function) 會得到新的值

這樣的結果,其實就是在解

Ax+b 的過程,並且將
Ax+b=y
y
代入到下個式子的
Ay+b=y2
如此一直運算下去。

  • 手寫辨識為例

    input 就是一張 16*16 (256) 的一維陣列,output 就是一個 size=10 的一維陣列,而要去找出中間此段 layer 的 weight

Back propagation(BP)

因為在 train 的過程中,需要去找出 NN 的所有參數,數目可能高達幾百萬個,BP 就是用來找出這些參數的方法,其就是 gradient descent 的。

要先記好其最終的目標,是希望計算出

cn/w 這個東西,為何?因為想要知道
L()/w
,也就是 L 這個 total loss function 對某個參數的 local minima,這樣才找得出最好的參數值。

那要如何找

cn/w?

cn/w 的意思表示一個 weight w 對於整體的
Cn
的改變量,這個
Cn
是包含了很多層之後的 function,這邊使用 chain rule 的概念是指,
c/w
=
z/wC/z
,也就是該 weight w 對於成為 active function input 的改變量(如下圖),乘上 active function 對於
C
的改變量,在 weight w 跟
C
之間多插入一層 active function。

找出參數:

C/w =
z/wC/z

  • z/w

    Chain rule 保證其正確性,且這樣做能夠使
    z/w
    這項很好算,這部分可以使用 forward pass 得出

  • C/z

這邊要先注意

a 的意思,代表
sigmoid(z)
,所以說他想看
z
對最後那個
C
的變化量,等於
z
sigmoid() 的變化量乘上 sigmoid 對
C
的變化量。

  • summation

    C/w =
    z/wC/z

    找一個參數 w ->

    z ->
    a=sigmoid(z)
    ->
    a
    指出去的
    z
    ,若
    z
    已經是 output layer ,則計算,若不是,則遞迴再跑
    z
    ->
    a=sigmoid(z)
    ->
    a
    指出去的
    z
    ,直到遇到 output layer。

Tip for DNN

  • overfitting

    這邊在講一個定義問題,並不是層數越多之後所產出來的 error 越差,就叫做 overfitting

    在 20 層可以做到的效果,在 56 層也絕對做得到,只要把後面 36 層的係數設為 0 即可,他必須要會去看在 train 的過程,而這時發現在 train 的時候就已經可以知道 20 的 error 優於 56 層了。

  • recipe

    在做 DL 的過程,如果是 train 過程壞掉就回去看下面兩層,test 壞掉就看上面三層

  • Vanish gradient problem

    為何加越多層會使得 accuracy 減少?

    因為如果是用 sigmoid 的話,其特性是會將越大的值都拉到趨近於 1,所以變化量的大其實對此函數影響不大。當層數越多時,因為 sigmoid 的關係,會使往下傳遞的 gradient 越來越小,而第一層的 input layer 離最後一層要經過最多層,所以對於整體的改變量來講,影響很小

    簡單來講,gradient 在經過一層後會有遞減的效果,經過太多層就會遞減到太小的值了。反之,圖上綠色的部分是最靠近 output layer 的,所以一改變會對整體的 loss function 改變大。

    • 解法

      ReLu

      這裡的解法是將 sigmoid 換成 ReLU

      這個做法可以將整個層縮小,只要是 0 的 neuron 就可以不用理會,另外,他在大於 0 的時候是線性上去的,這使得整體計算變快之外,也不會有一層一層遞減的問題

      RMSProp

      資料很少,適用於更多為的資料,過去 Adagrad 的特性是 learning rate 的大小和 function 微分後的值並不相關,並不是說微分後變大就要把 l-rate 變大,下圖是 Adagrad

      但是在現實生活中,同一條軸不可能 l-rate 這麼的固定,有可能大小不依,所以適用 RMSProp,但這部分沒有特別去看數學式,概念大概是想要找出新的 gradient 跟上一步的 gradient 的權重差別。

      Momentum

      他是將 gd 去做改良版,原本的 gd 是沿著 gradient 的反方向去跑,而這個 momentum 的概念是希望加入慣性的物理現象,他除了算出新的 gradient 之外,還會去考慮上一步的方向,並且使用一個 rate 去做調整(靠近上一個方向,還是新的 gradient 的方向)。

      這樣的做法是希望能夠藉由 "慣性" 去跳出 local minima ,不要直接卡在那個位置

      Adam = RMSProp + momentum

early stopping

因為 test 跟 train 的 distribution 不同,目的是希望能夠找出 validation set 的 local minima,之後就不要再繼續 train 下去了

regularization

在做 regularization 時,會加入一個新的 term,先看上面第一排的話,右邊的 gradient 就是對左邊的式子作微分,因為

theta2 作微分再乘上 1/2 時,剛好可以消掉才會得到
lamdaw
的式子。

Dropout

在 train 的過程中,會將部分的 neuron 給刪掉(每個 neuron 都有 p% 的機率要刪掉),使得整個 network 較小,他的概念是說,在 dropout 之後,會得到比較少的 neuron,這使得這些 neuron 要負擔整個網路使得 neuron 訓練更強大(?),而到了沒有 dropout 的 test 時,因為這些 neuron 很強大所以可以使精準度提高

random forest 就是實現這樣的方法,。

結論:maxout + Dropout 可使結果變好,因為 dropout 適合在線性

CNN

目的是因為當在做影像時,假如是 100x100x3,進第一層假如有 1000 個 neuron,deep learning 的 fully-connected 就會使得計算量太複雜了,於是發展出了 CNN 架構。

why CNN?

  1. 找 pattern 不需要整張圖,而是圖的某個部份即可辨識出 feature

    譬如說找鳥嘴,不需要整張圖去找,而是鳥的頭的前緣那一區即可辨識出

  2. filter

    鳥嘴會在圖的不同的位置,所以不會是針對鳥嘴出現在圖的什麼位置做訓練,而是透過 filter 找出是否有鳥嘴的特徵

  3. subsampling

    把圖片的偶數列基數行 pixel 拿掉,並不會影響太大,如圖

  4. 簡化 fully-connected

    • less parameters
      因為 CNN 是使用 filter 去做 convolution,但其實概念上跟 DNN 是一樣的,唯一差別就在於它沒有 DNN 那麼多的參數,他只有 "filter size" 這麼多。
      以上圖為例,原本是要將全部的 input 連接到該 neuron,變成,特定幾個 input 連接到 neuron 即可。
    • shared parameters
      不同的 neuron 需要 share 相同的 weight,使得所需的 weight 又變更少。

what does CNN learn?

這邊去探討說每個 filter 的 update。以上圖為例,因為每個 filter 都是需要透過學習才能夠找到該值,現在想要找出一個 filter 可以使得現在的這第 k 個 filter 可以使得 input 進來之後能夠得到最大值,使他非常的 active。方法就是使用 gradient ascent,找 MAX 使用 GA,找 min 使用 GD。

這部分是想要去看出,每個 filter 學到的東西是否可以更讓人看得懂一點。一般來說,CNN 的 filter 跟人的認知是很不同的,那要做到這點,要做的就是加上 summation 那段。

因為有了 overall pixel values 那段 summation,會使得白色有筆跡的地方越少越好,於是就變成像是左邊那個樣子。

Application

這邊探討 convolution 方向的重要性,以聲音為例,如果 filter 是沿著 time 的時間去移動的話,則意義上並不大,但沿著 frequency 移動才能找出特徵,why?

該 filter 可以找出某種特徵,而已聲音而言,男女生的頻率不同,但是對於說 "同一串字串" 的特徵有相同性,最大差別有可能只是在頻率上,所以 filter 找出來之後,沿著頻率去掃可以掃出相似波形的特徵。

以 Text 為例,字串跟字串之間的相依性是重要的,所以沿著 sequence 去掃會有意義,反之,若是沿著 one-hot encoding 的所有字的方向掃,掃出來的則意義不大。

RNN

RNN 會考慮到 input 的 sequence,作法就是把 neuron 的 output 存起來,待下個 input 進來的時候可以使用。

LSTM

其架構會有 3 個 gate,input gate 控制 input 是否能夠寫到 memory cell 裡面,forget gate 控制 memory cell 是否要清空,output gate 控制是否能把此值給提出來。

可以看到上圖中的

f(zf)
f(zi)
兩者,不是 0 就是 1,就是分別控制 input 以及是否要使用 memory cell 的值的。

Learning target

在做 RNN 的時候,會希望能夠去 minimize 跟 ref vector 的 cost,而 ref vector 裡頭有許多的 slot,每個 slot 都有它自己的 type,譬如說要是 input 是 Taipei,那就會希望 output 的 dest. slot 為 1,其他的 slot 為 0。

The error surface is rough

RNN 的 error surface 不是很平滑,就是相當的陡峭,為何會發生這樣的事呢?這是因為 RNN 會不斷重複使用 transition 這個參數,所以當 transition 參數在大於 1 時,若有很小的變化,對於 output 會有很大的改變。若小於 1 時,有小小的變化卻又是小小的改變,這樣的不一致使得沒辦法從 learning rate 上做改變。

這個 transition 參數是指說會將上一次的 neuron 記錄下來,傳給下一次來用。

Why LSTM?

如上面所說的,RNN 會有 gradient explode/vanishing 的問題,而 LSTM 的發明,就是為了解決 vanishing,他的 error surface 雖然仍然會陡峭,但是不會過度平坦(可以解決 vanishing 問題)。

為何可以解決?這邊yt的解釋有點沒有懂,老師是說因為 RNN 會把過去的 memory cell 給覆蓋掉,也就是每次新的 input 進來之後都會覆蓋掉,但是 LSTM 是使用 add 的方式,所以過去對 memory 的影響都會一直留傳下去,除非 reset memory cell。

semantic

  • CTC (connectionist temporal classfication)

    加入 null,用來解決疊字的問題(但課堂沒有特別細講)

  • bag of word

    可以用來分析 document,缺點是會忽略掉 word 的 sequence order

Semi-supervised

  • why?

    因為缺少被 labeled 的 data

    當有一些 unlabel data 時,則就不能只依據過去那些 label 過的資料去找平均跟 corvariance,因為會不準確。

  • self-training

    先針對原本有 label 的 data 訓練出一個 model,接著再把 unlabel data 丟到 model 裡面去。唯一要注意的是這邊使用的是 hard label 而不是 soft label

  • How to update parameters?

    相當直觀,從新計算 P(C1) 的機率就是把原本已經 label 過的 N1 加上未 label 的,未 label 的部分就要使用之前的 generative model 去找

  • entropy-based regularization

    這是進階的 self-training,其想法是不希望是 hard label,但是希望這個分佈是非常集中的。
    可以看到上圖,越集中的 distribution 可以得到越小的

    E(yu),於是這邊的 loss function 除了原本希望能跟 labelled data 的差距越小之外,還加入了後面的一項(且有一個 lamda weight 做調整)unlabeled,這一項就是希望能使 distribution 集中,不要分散。

    continue

  • prediction-based


    這裡的概念就是拿前面兩個字,來預測第三個字。直接看數學式的話,他會希望

    wi1,wi2 有相同的 sharing param ,所以他跟過去的 GD 不一樣,在這邊有再多扣掉另一項的 gradient。

  • Skip-gram and CBOW

    文章裡頭提到,為何會需要 word-embedding (字串量化)?因為希望將字串做量化,以便計算“語意”。如果使用 one-hot encoding 的話,會有高維度的問題出現。而這邊提出了兩個方法, CBOW(Continuous Bag of Words Model) & Skip-gram

    因為想要預測一個沒有被 label 過的字串 (unsupervised),這要如何做呢?

    概念大概就是這樣,沒特別說數學式,只有說就是以 context words 去預測 focus word(CBOW),或者反過來 (Skip-gram)

pyTorch

  • 環境

    • 在 vscode 裡頭要調整下面這個東西

      原先以為在 terminal 跟 zsh 把 pythonpath 調整好就行了,殊不知在 vscode 裡頭有屬於 vscode 的 interpreter,所以還要再調整。

    • 在 terminal 裡面就是調整 .zshrc

      ​​​​​​​​export PATH="~/opt/anaconda3/bin":$PATH ​​​​​​​​export PYTHONPATH="~/opt/anaconda3/bin/python3"

      當打下 which python3 之後有出現 ~/opt/anaconda3/bin/python3 即可

  • Ref

  • Code

  • 待研究

    • backward 還不是很懂,只知道是沿著 loss 去做 gradient,但是怎麼做還不熟,待研究 參考網站

Other

cost function

左圖在給定一堆 data 具有 (x, y) 時,我希望找到一個 function 使得 cost 最小
右圖是透過改變斜率的方式(因為 function 是 θ1x,要找出最好的 θ1)去掃,找這個 function 的參數

Logistic regression( =neuron )

因為 hθ1(x) 的值可能大於 1 以及小於 0,但又希望能透過 regreesion 的方式來分類(只能有 0 或者 1),所以把它超過的部分壓縮進來,限縮在 0~1 之間,這就是 sigmoid function

Logistic regression goal

希望能找到一個 function(像下面的粉紅色那一圈),使得給他 input x 並且放入 sigmoid g() 之後,出來的值不是 0 就是 1,去做分類。

Logistic regression cost function

如果正確答案 y 是 1 的話,那我就希望我的 model 經過 sigmoid 後吐出來的值越接近 1 越好,反之。所以如果 y=1 時,吐出來的值越靠近 1 則 cost 越小

其中會用 -log() 的原因是因為,log x 的 x 要是小於 1 的話, log x 會是負的, x 越小則 log x 越小,因為 y=1 時要是 hθ1x 越遠離 1,則應該受到越大的懲罰(cost 越高)

把上述的內容整合之後會變成下面的數學式
當 y=1 時代入下述,則會把後項給去除掉

neural network(NN)

neuron 的意思就是單一個神經元,一個 (wx+b) 後進去 active function(sigmoid) 變成 g(wx+b) 之後吐出 y' 為 1 或者 0

perceptron 就是多個 neuron(垂直生長),這些 y' 們最後可以表達成一個一維矩陣,最後從裡面值最高的當作預測

deep neural network(DNN)

就是多層(水平生長)的 perceptron (perceptron 就是多個 neuron 能夠判斷多個目標)

hidden layer 就是下圖紅色虛線的部分,因為既是上一層的輸出,也可以當作下一層的輸入,所以通常不會把它畫出來

上一層的輸出是下一層的輸入,如下表示

LSTM