1 of 38

HW2 TA hours

TAs

ntu.mlta@gmail.com

2 of 38

Outline

  • 如何在shell scripts與python檔中接收arguments�
  • Logistic Regresion�
  • Generative model

3 of 38

Shell Scripts

  • What is Linux Shell?

Shell accepts your instruction or commands (usually in English) and pass it to kernel.

4 of 38

Shell Scripts

2. What is Shell Script?

… 把很多指令寫成一個檔案,可以一次做很多事情。

甚至是做判斷式或是迴圈。

5 of 38

Script Tutorial and Example

6 of 38

Passing Arguments

How to pass arguments to your srctipts or .py files?

  • 在terminal或cmd中的輸入與script中變數對應關係:

/path/to/scriptname /path/to/data /path/to/output

$0 $1 $2

2. 在terminal或cmd中的輸入與.py中變數對應關係:

/path/to/pythonfile /path/to/data /path/to/output

sys.argv[0] sys.argv[1] sys.argv[2]

7 of 38

Passing Arguments

script:

python:

8 of 38

Passing Arguments

Let’s run…

9 of 38

PATH

  • 絕對路徑:相對於根目錄的路徑。
    • Ex: /Users/MLTA/Desktop/hw2
  • 相對路徑:相對於目前資料夾的路徑。
    • Ex: ./hw2/logistic.py、../hw1/linear_regression.py

  • 助教會在git clone整個 ML2017/hw2資料夾
  • 並在 ML2017/hw2資料夾執行程式(Ex : hw2_best.sh)
  • 若要 讀/存model請用相對路徑
  • Data的Path助教會用絕對路徑下在hw2_best.sh的argument裡。

10 of 38

Logistic Regression

Implementation:

  • feature normalization
  • logistic regression using gradient descent with batch

Code Example:

http://codepad.org/RvIY9BU2

11 of 38

Logistic Regression

If we let batch_size=25...

12 of 38

Logistic Regression

-

13 of 38

Gaussian Distribution-Training(mean)

14 of 38

Gaussian Distribution-Training(sigma)

15 of 38

Gaussian Distribution-Training(sigma)

Shared sigma:

16 of 38

Gaussian Distribution-predict

17 of 38

Gaussian Distribution-sigmoid

使用np.clip() 避免數值太小或太大而overflow

18 of 38

Generative model -Naive Bayes Classifier

  • Calculate the probability in each category
  • P(C1 | X) = 連乘P(C1 | X in each category)
  • log(P(C1 | X) ) = sigma(log(P(C1 | X_i)))
  • 比較 log(P(C1 | X) ) 和 log(P(C2 | X_i))

19 of 38

code

20 of 38

HW2 TA hours

TAs

ntu.mlta@gmail.com

21 of 38

Outline

  • 如何在shell scripts與python檔中接收arguments�
  • Logistic Regresion�
  • Generative model

22 of 38

Shell Scripts

  • What is Linux Shell?

Shell accepts your instruction or commands (usually in English) and pass it to kernel.

23 of 38

Shell Scripts

2. What is Shell Script?

… 把很多指令寫成一個檔案,可以一次做很多事情。

甚至是做判斷式或是迴圈。

24 of 38

Script Tutorial and Example

25 of 38

Passing Arguments

How to pass arguments to your srctipts or .py files?

  • 在terminal或cmd中的輸入與script中變數對應關係:

/path/to/scriptname /path/to/data /path/to/output

$0 $1 $2

2. 在terminal或cmd中的輸入與.py中變數對應關係:

/path/to/pythonfile /path/to/data /path/to/output

sys.argv[0] sys.argv[1] sys.argv[2]

26 of 38

Passing Arguments

script:

python:

27 of 38

Passing Arguments

Let’s run…

28 of 38

PATH

  • 絕對路徑:相對於根目錄的路徑。
    • Ex: /Users/MLTA/Desktop/hw2
  • 相對路徑:相對於目前資料夾的路徑。
    • Ex: ./hw2/logistic.py、../hw1/linear_regression.py

  • 助教會在git clone整個 ML2017/hw2資料夾
  • 並在 ML2017/hw2資料夾執行程式(Ex : hw2_best.sh)
  • 若要 讀/存model請用相對路徑
  • Data的Path助教會用絕對路徑下在hw2_best.sh的argument裡。

29 of 38

Logistic Regression

Implementation:

  • feature normalization
  • logistic regression using gradient descent with batch

Code Example:

http://codepad.org/RvIY9BU2

30 of 38

Logistic Regression

If we let batch_size=25...

31 of 38

Logistic Regression

-

32 of 38

Gaussian Distribution-Training(mean)

33 of 38

Gaussian Distribution-Training(sigma)

34 of 38

Gaussian Distribution-Training(sigma)

Shared sigma:

35 of 38

Gaussian Distribution-predict

36 of 38

Gaussian Distribution-sigmoid

使用np.clip() 避免數值太小或太大而overflow

37 of 38

Generative model -Naive Bayes Classifier

  • Calculate the probability in each category
  • P(C1 | X) = 連乘P(C1 | X in each category)
  • log(P(C1 | X) ) = sigma(log(P(C1 | X_i)))
  • 比較 log(P(C1 | X) ) 和 log(P(C2 | X_i))

38 of 38

code