跳到主要內容

[Development] Deep Learning over Python 深度學習基於 Python 1/3

  用 Python 練習實作深度學習,主要包含 Tensorflow 以及 SVM,著重於實作範例,未深入涉及各 DL 演算法模型原理及其數學。

大綱

[Development] Deep Learning over Python 深度學習基於 Python 1/3
  • Basic Tools: 基於 python- numpy, matplotlib, scipy, Pandas
[Development] Deep Learning over Python 深度學習基於 Python 2/3
  • Keras (基於 Tensorflow) 
  • Tensorflow
[Development] Deep Learning over Python 深度學習基於 Python 3/3
  • Support Vector Machine (SVM)
  • 強化學習 Reinforcement Learning (RL)

Reference: 本篇基於 Udemy 課程 "吳佳諺- Python 深度學習" 為架構出發衍生,其他引用資料個別在段落中標示

Basic Tools

numpy

import numpy as np
from scipy import linalg  # linear algebra
import matplotlib.pyplot as plt

myarray = np.array([[1, 2, 3], [4, 5, 6]])
print('myarray:', myarray)
myarray_allzero = np.zeros((3, 5), dtype = np.int16)
print('myarray_allzero:', myarray_allzero)
myarray_2 = np.arange(15, dtype = np.int64)
print('myarray_2:', myarray_2)
myarray_2 = myarray_2.reshape((3, 5))
print('myarray_2 reshape:', myarray_2)

myarray: [[1 2 3]
 [4 5 6]]
myarray_allzero: [[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
myarray_2: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
myarray_2 reshape: [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]

myarray_7 = np.zeros((10, 3))
myarray_8 = myarray_7.T
print('myarray_8:', myarray_8)
myarray_9 = np.reshape(myarray_8, (5, 6))
print('myarray_9:', myarray_9)
myarray_10 = np.ravel(myarray_9)
print('myarray_10:', myarray_10)

myarray_8: [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
myarray_9: [[0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]]
myarray_10: [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

myarray_3 = np.linspace(0, 100, 5)  # 1包含首尾取平均 5 點
print('myarray_3:', myarray_3)
myarray_4 = np.random.random((2, 3))  # random 值填 2*3 矩陣 
print('myarray_4:', myarray_4)

myarray_3: [  0.  25.  50.  75. 100.]
myarray_4: [[0.81214287 0.69335586 0.1207997 ]
 [0.50709532 0.52782355 0.99811017]]

myarray_5 = np.arange(25)
myarray_5 = myarray_5.reshape((5, 5))
myarray_6 = np.arange(25)
myarray_6 = myarray_6.reshape((5, 5))
print('plus:', myarray_5 + myarray_6)
print('minus:', myarray_5 - myarray_6)
print('multiply:', myarray_5 * myarray_6)
print('divide:', myarray_5 / myarray_6)
print('square:', myarray_5 ** 2)
print('compare:', myarray_5 < myarray_6)
print('dot:', myarray_5.dot(myarray_6))

plus: [[ 0  2  4  6  8]
 [10 12 14 16 18]
 [20 22 24 26 28]
 [30 32 34 36 38]
 [40 42 44 46 48]]
minus: [[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
multiply: [[  0   1   4   9  16]
 [ 25  36  49  64  81]
 [100 121 144 169 196]
 [225 256 289 324 361]
 [400 441 484 529 576]]
divide: [[nan  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]]
square: [[  0   1   4   9  16]
 [ 25  36  49  64  81]
 [100 121 144 169 196]
 [225 256 289 324 361]
 [400 441 484 529 576]]
compare: [[False False False False False]
 [False False False False False]
 [False False False False False]
 [False False False False False]
 [False False False False False]]
dot: [[ 150  160  170  180  190]
 [ 400  435  470  505  540]
 [ 650  710  770  830  890]
 [ 900  985 1070 1155 1240]
 [1150 1260 1370 1480 1590]]

x = np.arange(0, 3 * np.pi, 0.1)  # 每隔 0.1 選一點
y = np.sin(x)
plt.plot(x, y)
plt.show()
mu, sigma = 2, 0.5
v = np.random.normal(mu, sigma, 10000)  # 平均 2, 標準差 0.5
plt.hist(v, bins = 500, normed = 1)
plt.show()

matplotlib 畫圖

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')  # red dot
plt.xlabel('item')
plt.ylabel('value')
plt.axis([0, 6, 0, 20])
plt.show()

import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0., 5., 0.2)  # [0, 0,2, 0,4, ..., 4.8]
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.xlabel('index')
plt.ylabel('value')
plt.show()
m1 = [1, 2, 3, 4, 5, 6, 10, 12]
s1 = [2, 4, 6, 8, 10, 12, 14, 16]
m2 = [1, 3, 4, 5, 6, 7, 10, 12]
s2 = [1, 2, 3, 15, 12, 8, 6, 21]
plt.plot(m1, s1, lw=2, label='Mary')  # lw- line width
plt.plot(m2, s2, lw=2, label='Mary')
plt.legend()  # 圖例
plt.title('example')
plt.show()

scipy 科學函式庫

linear algebra

# inverse(反矩陣) and dot(內積)
a1 = np.array([[1, 3, 5], [2, 5, 1], [2, 3, 8]])
print(a1)
a1_inv = linalg.inv(a1)
print(a1_inv)
print(a1.dot(a1_inv))

[[1 3 5]
 [2 5 1]
 [2 3 8]]
[[-1.48  0.36  0.88]
 [ 0.56  0.08 -0.36]
 [ 0.16 -0.12  0.04]]
[[ 1.00000000e+00 -1.11022302e-16 -6.24500451e-17]
 [ 3.05311332e-16  1.00000000e+00  1.87350135e-16]
 [ 2.22044605e-16 -1.11022302e-16  1.00000000e+00]]

# determinant (行列式)
a2 = np.array([[1, 2], [3, 4]])
print(linalg.det(a2))  # 1*4-3*2

-2.0

# eigenvalues and eigenvectors
a3 = np.array([[1, 5, 2], [2, 4, 1], [3, 6, 2]])
eigenvalues_a3, eigenvectors_a3 = linalg.eig(a3)
l1, l2, l3 = eigenvalues_a3
print('eigenvalues:', l1, l2, l3)
print('eigenvectors', eigenvectors_a3)

eigenvalues_a3_column0 = np.array(eigenvectors_a3[:,0]).T  # transport
print('make column 0 of eigenvectors to an array:', eigenvalues_a3_column0)
print('normalize:', linalg.norm(a3.dot(eigenvalues_a3_column0) - l1 * eigenvalues_a3_column0))

eigenvalues: (7.957916204910748+0j) (-1.2576647056775332+0j) (0.2997485007667829+0j)
eigenvectors [[-0.5297175  -0.90730751  0.28380519]
 [-0.44941741  0.28662547 -0.39012063]
 [-0.71932146  0.30763439  0.87593408]]
make column 0 of eigenvectors to an array: [-0.5297175  -0.44941741 -0.71932146]
normalize: 3.233018248352212e-15

Pandas

Pandas 提供兩種主要的資料結構,Series 與 DataFrame。
Series 顧名思義就是用來處理時間序列相關的資料(如感測器資料等),主要為建立索引的一維陣列。
DataFrame 則是用來處理結構化(Table like)的資料,有列索引與欄標籤的二維資料集,例如關聯式資料庫、CSV 等等 
- from https://oranwind.org/python-pandas-ji-chu-jiao-xue/
[TBC]


繼續閱讀 [Development] Deep Learning over Python 2/3

留言

這個網誌中的熱門文章

[Development] git

本篇介紹幾個 git 會用到的基本 command line 指令 安裝 on Mac OS [tested on macOS Catalina 10.15.1] (可以用 Brew 安裝,若尚未安裝 Brew:  https://brew.sh/index_zh-tw ) $ brew install git $ git --version git version 2.21.0 (Apple Git-122.2)   github 官網就有提供 GUI 的應用 ,想用 GUI 的直接去下載安裝就行了,也有簡單明瞭的教學,非常容易。另外也有好幾個第三方的好用 GUI 介面,Google 一下比較一下選自己喜歡的也行。   但以下還是用 command line 的方法來操作,因為這還是最 general 到哪都可以用的基本方法。因為實務上,比如 code 都放在公司的 server,你可能也是都要 ssh 到 sever 上去改 code,改完之後要上傳到 github。而公司的 server 就是一台 Linux 環境,很可能是沒有提供 GUI 讓你使用的,所以你就只能用 command line 的方式完成 git 的上傳。 Create Repo   去本地一個你想要放置 git 專案的地方,比如我想把我之後的 git code 都放在我 Mac local 的 /Users/chungchris/git $ cd /Users/chungchris/git $ git init   就會看到在此 git 目錄下產生一個隱藏的 .git 資料夾,這樣就完成了: (base) Chris-MBP:git chungchris$ ls -al total 0 drwxr-xr-x   3 chungchris   staff     96 11 21 11:01 . drwxr-xr-x+ 53 chungchris   staff   1696 11 21 10:45 .. drwxr-xr-x   9 chungchris   staff  ...

[Coding] Compiler

Something about compiling. #compiler #link #gcc Reference PTT LinuxDev, 作者: cole945 (躂躂..) 看板: LinuxDev, 標題: [心得] 用gcc 自製Library, 時間: Sun Nov 5 04:15:45 2006 Static Link Compile 時將 library 加入程式碼,執行快但佔空間,code size 和 mem 使用都比較多 Compile source codes to generate object files $ gcc -c file1.c file2.c file3.c -c 編出 object 檔 Create a static library named libmylib.a $ ar rcs lib mylib .a file1.o file2.o file3.o 把一堆 object 檔用 ar(archiver) 包裝集合起來,檔名以`.a’ 結尾 Using a Static Library to generate a executable files $ gcc -o main main.c -L. -l mylib -L: the directory of the library. 可以指定多次 -LDIR -l: the name of the library (注意 藍色 部分是 match 的) Dynamic Link Compile 時不將 library 加入程式碼,執行程式的時後再將 library 載入程式碼,若有多個程式共用同一個 library,只需載一個 library 進 memory Compile source code $ gcc -c -fPIC file1.c file2.c file3.c -fPIC 表示要編成 position-independent code,這樣不同 process 載入 shared library 時,library 的程式和資料才能放到記憶體不同位置。-fPIC 較通用於不同平台,但產生的 code 較大,而且編譯速度較慢 Create a shared library...

Let's Move On

今天決定要建個部落格 身為資工系的學生, 又那麼愛做筆記 XD 卻到今天才想做這件事似乎有點落漆 還沒明確知道想用來記錄些什麼, 有可能只是與程式相關的東西, 也有可能會很雜 但也很有可能過了一年什麼屁都沒有, 到時候也可以拿出來嘲笑自己一下自我檢討一番 Decide creating the blog today. It seems abnormal for a computer science student to do this so late. After all, I like taking note a lots... Still not sure about what am gonna write here. Maybe all about programming. Maybe it will be really mixed. Nevertheless... very likely that it will still be empty after a year... Then I can open it and piss myself. 迅速 Google 了一下該用哪個平台, 只想找個簡單好用的 外貌先決下 wordpress 和 blogger 脫穎而出, 進一步看了一下覺得並沒有複雜架站的需求所以最後選了 blogger