涉及 python, c, c++ 的基礎編程知識,以及不同深度學習框架的小應用(主要集中在 keras,也有部分 tensorflow,caffe,pytorch)
未完待續,不斷更新中
文章目錄
- 【python】(★★★★★)
- 【C / C++】(★★★★★)
- 【Keras】(★★★★★)
- 【TensorFlow Demos】(★★★★★)
- 【Pytorch】(★★★★★)
- 1 Keras / Tensorflow
- 1.1 人工智能、機器學習與深度學習簡介
- 1.2 深度學習原理
- 1.3 TensorFlow 與 Keras 介紹
- 1.3.1 TensorFlow 架構圖
- 1.3.2 TensorFlow 簡介
- 1.3.3 TensorFlow 程序設計模式
- 1.3.4 Keras 介紹
- 1.3.5 Keras 程序設計模式
- 1.3.6 Keras 與 TensorFlow 比較
- 1.4 在Windows / Linux Ubuntu 中安裝TensorFlow 與 Keras
- 附錄 A: Use GPU to speed up training
【python】(★★★★★)
The Library of Python
【python】基本數據類型(1)
【python】基本運算和表達式(2)
【python】導入模塊和輸入輸出(3)
【python】基本控制流程(4)
【python】list(5)
【python】tuple(6)
【python】dict(7)
【python】set(8)
【python】函數(9)
【python】異常處理(10)
【python】類(11)
【python】File(12)
【python】Regular Expression(13)
【python】Coding(Interview)
【python】Leetcode(Data Structure / Algorithm)
【python】Leetcode(Dynamic Programming)
【python】Leetcode(Map)
【python】Leetcode(primer)
【python】Single / Single Cycle / Double Link List
【python】Leetcode(String)
【python】Leetcode(Tree)
【python】Sort Algorithm
【python】axis
【python】Draw a straight line
【python】Linear Algebra(矩陣,向量)
【python】Linux終端沒有GUI,如何使用matplotlib繪圖
【python】matplotlib(上)
【python】matplotlib & seaborn(下)
【python】Multithreading (多線程)
【python】Neural Network for MNIST classification
【python】numpy & pandas
【python】OpenCV—RGB, Rectangle, Circle, SS(1)
【python】OpenCV—Blur, Threshold, Gradient, Morphology(2)
【python】OpenCV—Edge, Corner, Face Detection(3)
【python】Peppa Pig(turtle、if __name__ == ‘__main__’)
【python】PIL(上)
【python】PIL(下)
【python】random
【python】Scikit-learn中決策樹可視化方法
【python】sympy
【python】Tkinter(GUI)
【python】Visualization of field data(NBA)
【python】Word Cloud
【Python】《零基礎入門學python》Learning notes
【C / C++】(★★★★★)
【Keras】(★★★★★)
莫煩 PYTHON 教學視頻(MLP、CNN、RNN for MNIST
classification,MLP、LSTM for regression)
Keras 建立 MLP、CNN 模型進行 MNIST
數據集手寫數字分類
- 【Keras-MLP】MNIST(只用 python 實現的 demo 可以參考 【python】Neural Network for MNIST classification)
- 【Keras-CNN】MNIST
Keras 建立 MLP 模型預測泰坦尼克號
上的生存概率
Keras 建立 CNN 模型進行 CIFAR-10
數據集圖像分類
- 【Keras-CNN】CIFAR-10
- 【Keras-LeNet】CIFAR-10
- 【Keras-AlexNet】CIFAR-10
- 【Keras-NIN】CIFAR-10
- 【Keras-VGG】CIFAR-10
- 【Keras-ResNet】CIFAR-10
- 【Keras-DenseNet】CIFAR-10
- 【Keras-Inception v1】CIFAR-10
- 【Keras-Inception v2】CIFAR-10
- 【Keras-Inception v3】CIFAR-10
- 【Keras-Inception v4】CIFAR-10
- 【Keras-Inception-resnet v1】CIFAR-10
- 【Keras-Inception-resnet v2】CIFAR-10
- 【Keras-MobileNet v1】CIFAR-10
- 【Keras-Xception】CIFAR-10
- 【Keras-SqueezeNet】CIFAR-10(上)
- 【Keras-SqueezeNet】CIFAR-10(下)
- 【Keras-ResNeXt】CIFAR-10
Keras 建立 MLP、RNN、LSTM模型進行 IMDb
情感分析
Keras 建立 GAN 模型進行圖像的生成
【TensorFlow Demos】(★★★★★)
- 《Tensorflow | 莫煩 》learning notes
- 【TensorFlow】Programming model + 小試牛刀(仿真Neural Network )
- 【TensorFlow-MLP】MNIST
- 【TensorFlow-CNN】MNIST
【Pytorch】(★★★★★)
《PyTorch | MorvanZhou 》learning notes
下面介紹下 keras 和 TensorFlow
1 Keras / Tensorflow
1.1 人工智能、機器學習與深度學習簡介
增強式學習,藉助定義 Actions、States、Rewards 的方式不斷訓練機器循序漸進,學會執行某項任務的算法。常用算法有Q-Learning、TD(Temporal Difference)、Sarsa
總結的比較好的一個機器學習框架
1.2 深度學習原理
主要從神經傳導原理導入,以 Multilayer Perceptron (MLP) 爲例
1.2.1 神經傳導原理
是我看過的比較銷魂的神經網絡模型
1×2 = 1×3 × 3×2 + 1×2
1.2.2 MLP
MNIST
train時,梯度更新,mini-batch,每次讀取一批數據進行反向傳播算法訓練(多樣本求均值)
損失函數
one-hot encoding
梯度更新過程
一個樣本,算一次梯度,就有一個新的w1和w2。mini-batch,多個樣本,多個w1和w2求均值。
1.3 TensorFlow 與 Keras 介紹
- TensorFlow:低級深度學習鏈接庫,Brain Team 開發,15年11月公開源碼
- Keras:高級深度學習鏈接庫
1.3.1 TensorFlow 架構圖
可以跨平臺使用,Raspberry Pi樹莓派
具備分佈式計算能力
前端語言,對 python 支持最好
高級 API,Keras、TF-Learn、TF-Slim、TF-Layer
1.3.2 TensorFlow 簡介
1)Tensor(張量)
零維的 Tensor 是標量,1維的是向量,2維以上的是矩陣
2)Flow(數據流)
去陌生的城市,不會當地的語言,最好的方式,畫一張圖告訴司機你的目的地
computational graph
Node:代表運算(圓圈)
edge:張量的數據流
1.3.3 TensorFlow 程序設計模式
核心是computational graph
分爲:建立 computational graph 和 建立 Session 執行 computational graph
Session(會話)的作用是在客戶端和執行設備之間建立連接。有了這個連接,就可以將computational graph在各個不同的設備中執行,後續任何與設備之間的數據傳輸都必須通過Session來進行,並且最後獲得執行後的結果。
tf.Variable() 構造函數需要變量的初始值,它可以是任何類型和形狀的Tensor(張量)。 初始值定義變量的類型和形狀。 施工後,變量的類型和形狀是固定的。 該值可以使用其中一種賦值方式進行更改。
placeholder, 譯爲佔位符,官方說法:”TensorFlow provides a placeholder operation that must be fed with data on execution.” 即必須在執行時feed值。
placeholder 實例通常用來爲算法的實際輸入值作佔位符。例如,在MNIST例子中,定義輸入和輸出:
x = tf.placeholder(tf.float32, [None, 784])
#表示成員類型float32, [None, 784]是tensor的shape, None表示第一維是任意數量,784表示第二維是784維
y_ = tf.placeholder(tf.float32, [None, 10])
舉個 的例子
import tensorflow as tf
import numpy as np
W =tf.Variable(tf.random_normal([3,2]),name='w') #random_normal,從正態分佈中輸出隨機值。
b =tf.Variable(tf.random_normal([3,2]),name='b')
X = tf.placeholder("float",[None,3],name='X')# placeholder,實例通常用來爲算法的實際輸入值作佔位符
y = tf.nn.sigmoid(tf.matmul(X,W)+b,'y') #激活函數
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
X_array = np.array([[0.4,0.2,0.4],
[0.3,0.4,0.5],
[0.3,-0.4,0.5]])
(_b,_W,_X,_y) = sess.run((b,W,X,y),feed_dict={X:X_array})
1.3.4 Keras 介紹
Keras是一個model-level的深度學習鏈接庫,Keras必須配合backend engine 後端引擎(Theano、TensorFlow)進行計算(底層的Tensor計算)。
1.3.5 Keras 程序設計模式
蛋糕架+加蛋糕
1)蛋糕架
model = Sequential()
2)加蛋糕
- add input and hidden layer in the model
- add output layer in the model
1.3.6 Keras 與 TensorFlow 比較
1.4 在Windows / Linux Ubuntu 中安裝TensorFlow 與 Keras
附錄 A: Use GPU to speed up training
測試下矩陣運算GPU與CPU的差別
A.1 導入tensorflow模塊
import tensorflow as tf
import time
A.2 建立和執行計算圖
默認用GPU跑
tf.reduce_sum
計算矩陣的和
#建立計算圖
size=500
W = tf.random_normal([size, size],name='W') # 500,500
X = tf.random_normal([size, size],name='X') # 500,500
mul = tf.matmul(W, X,name='mul')
sum_result = tf.reduce_sum(mul,name='sum')# 矩陣內的值加總
# 執行計算圖
with tf.Session() as sess:
result = sess.run(sum_result)
print('result=',result)
output
result= -10656.27
A.3 測試GPU與CPU性能的差別
def performanceTest(device_name,size):
with tf.device(device_name):
W = tf.random_normal([size, size],name='W')
X = tf.random_normal([size, size],name='X')
mul = tf.matmul(W, X,name='mul')
sum_result = tf.reduce_sum(mul,name='sum')
startTime = time.time()
# 使用 tf.ConfigProto 建立 session 的配置設置 tfconfig,傳入參數 log_device_placement設置爲True
tfconfig=tf.ConfigProto(log_device_placement=True)
with tf.Session(config=tfconfig) as sess:
result = sess.run(sum_result)
takeTimes=time.time() - startTime
print(device_name," size=",size,"Time:",takeTimes )
return takeTimes
調用
gpu_set=[];cpu_set=[];i_set=[]
for i in range(0,5001,500):
g=performanceTest("/gpu:0",i) # 0號GPU
c=performanceTest("/cpu:0",i)
gpu_set.append(g);cpu_set.append(c);i_set.append(i)
print("--")
output
/gpu:0 size= 0 Time: 0.14670062065124512
/cpu:0 size= 0 Time: 0.16228890419006348
--
/gpu:0 size= 500 Time: 0.16929364204406738
/cpu:0 size= 500 Time: 0.18462872505187988
--
/gpu:0 size= 1000 Time: 0.13805508613586426
/cpu:0 size= 1000 Time: 0.13151001930236816
--
/gpu:0 size= 1500 Time: 0.1536424160003662
/cpu:0 size= 1500 Time: 0.2314302921295166
--
/gpu:0 size= 2000 Time: 0.21573805809020996
/cpu:0 size= 2000 Time: 0.4350099563598633
--
/gpu:0 size= 2500 Time: 0.37288379669189453
/cpu:0 size= 2500 Time: 0.6350183486938477
--
/gpu:0 size= 3000 Time: 0.5283641815185547
/cpu:0 size= 3000 Time: 0.9774112701416016
--
/gpu:0 size= 3500 Time: 0.7861192226409912
/cpu:0 size= 3500 Time: 1.4520719051361084
--
/gpu:0 size= 4000 Time: 1.1301662921905518
/cpu:0 size= 4000 Time: 2.030012845993042
--
/gpu:0 size= 4500 Time: 1.5385856628417969
/cpu:0 size= 4500 Time: 2.7173430919647217
--
/gpu:0 size= 5000 Time: 2.0486159324645996
/cpu:0 size= 5000 Time: 3.6564781665802
--
GPU的RAM不夠就把size設置小一些,可視化一下結果
%matplotlib inline
import matplotlib.pyplot as plt
fig = plt.gcf()
fig.set_size_inches(6,4)
plt.plot(i_set, gpu_set, label = 'gpu')
plt.plot(i_set, cpu_set, label = 'cpu')
plt.legend()
output
參考
【1】TensorFlow中文社區
【2】tensorflow 生成隨機數 tf.random_normal 和 tf.random_uniform 和 tf.truncated_normal 和 tf.random_shuffle
【3】TensorFlow函數——tf.variable( )
【4】tensorflow框架學習(一)placeholder 與variable
【5】 《TensorFlow+Keras深度學習人工智能實踐應用》 林大貴
聲明:第三章行文結構,拍攝圖片均來源 《TensorFlow+Keras深度學習人工智能實踐應用》 林大貴版,引用、轉載請註明出處,如有興趣,可以買一本看看,謝謝!