生活随笔
收集整理的這篇文章主要介紹了
【神经网络计算】——神经网络实现鸢尾花分类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本blog為觀看MOOC視頻與網易云課堂所做的筆記
課堂鏈接:
人工智能實踐:TensorFlow筆記
吳恩達機器學習
疑問與思考
為什么按照batch喂入數據
之前看的視頻里面處理數據都是一次性將所有數據喂入,現在看的這個視頻對數據進行了分組投入。這是為何?
參考鏈接:
深度學習中的batch理解(batch size一次喂給神經網絡的數據大小量)
用CNN做圖像分類的時候,為什么要一批一批地輸入數據?
對樣本數據的處理方法:
傳統的梯度下降法:用全部樣本計算迭代時的梯度
隨機梯度下降法(在線梯度下降法):一次只喂一個樣本
batch梯度下降法:每次喂一部分樣本讓其完成本輪迭代
區別舉例:一次性喂500個樣本并迭代一次,跟一次喂1個樣本迭代500次相比
第一種是將參數一次性更新500個樣本的量,第二種是迭代的更新500次參數。
1、在同等的計算量之下(一定的時間內),使用整個樣本集的收斂速度要遠慢于使用少量樣本的情況。換句話說,要想收斂到同一個最優點,使用整個樣本集時,雖然迭代次數少,但是每次迭代的時間長,耗費的總時間是大于使用少量樣本多次迭代的情況的。
2、樣本量少的時候會帶來很大的方差,會導致在下降到很差的局部最小值、鞍點震蕩出收斂處,有利于向全局最小值邁進。
當樣本量很多時,方差很小,對梯度的估計要準確和穩定的多,可能導致深陷局部最小值、鞍點,導致訓練效果不如意
3、與GPU性能有關,GPU性能越好,同時訓練的數據就越多,batch就可以越大。
代碼以及展示
把打亂后的數據集中前120個數據取出來作為訓練集,后30個為測試集
輸入特征是4個,所以輸入節點是4。只用一層網絡,輸出節點是分類數:3
第一層for循環針對數據集,第二層for循環針對batch。
訓練集120個數據,batch是32個,,每個step只能喂入32組數據,需要batch級別循環4次。
所以除以4,得到每個循環得到的平均loss。
代碼:
import tensorflow
as tf
from sklearn
import datasets
from matplotlib
import pyplot
as plt
import numpy
as np
x_data
= datasets
.load_iris
().data
y_data
= datasets
.load_iris
().target
np
.random
.seed
(116)
np
.random
.shuffle
(x_data
)
np
.random
.seed
(116)
np
.random
.shuffle
(y_data
)
tf
.random
.set_seed
(116)
x_train
= x_data
[:-30]
y_train
= y_data
[:-30]
x_test
= x_data
[-30:]
y_test
= y_data
[-30:]
x_train
= tf
.cast
(x_train
, tf
.float32
)
x_test
= tf
.cast
(x_test
, tf
.float32
)
train_db
= tf
.data
.Dataset
.from_tensor_slices
((x_train
, y_train
)).batch
(32)
test_db
= tf
.data
.Dataset
.from_tensor_slices
((x_test
, y_test
)).batch
(32)
w1
= tf
.Variable
(tf
.random
.truncated_normal
([4, 3], stddev
=0.1, seed
=1))
b1
= tf
.Variable
(tf
.random
.truncated_normal
([3], stddev
=0.1, seed
=1))lr
= 0.1
train_loss_results
= []
test_acc
= []
epoch
= 500
loss_all
= 0
for epoch
in range(epoch
): for step
, (x_train
, y_train
) in enumerate(train_db
): with tf
.GradientTape
() as tape
: y
= tf
.matmul
(x_train
, w1
) + b1 y
= tf
.nn
.softmax
(y
) y_
= tf
.one_hot
(y_train
, depth
=3) loss
= tf
.reduce_mean
(tf
.square
(y_
- y
)) loss_all
+= loss
.numpy
() grads
= tape
.gradient
(loss
, [w1
, b1
])w1
.assign_sub
(lr
* grads
[0]) b1
.assign_sub
(lr
* grads
[1]) print("Epoch {}, loss: {}".format(epoch
, loss_all
/4))train_loss_results
.append
(loss_all
/ 4) loss_all
= 0 total_correct
, total_number
= 0, 0for x_test
, y_test
in test_db
:y
= tf
.matmul
(x_test
, w1
) + b1y
= tf
.nn
.softmax
(y
)pred
= tf
.argmax
(y
, axis
=1) pred
= tf
.cast
(pred
, dtype
=y_test
.dtype
)correct
= tf
.cast
(tf
.equal
(pred
, y_test
), dtype
=tf
.int32
)correct
= tf
.reduce_sum
(correct
)total_correct
+= int(correct
)total_number
+= x_test
.shape
[0]acc
= total_correct
/ total_numbertest_acc
.append
(acc
)print("Test_acc:", acc
)print("--------------------------")
plt
.title
('Loss Function Curve')
plt
.xlabel
('Epoch')
plt
.ylabel
('Loss')
plt
.plot
(train_loss_results
, label
="$Loss$")
plt
.legend
()
plt
.show
()
plt
.title
('Acc Curve')
plt
.xlabel
('Epoch')
plt
.ylabel
('Acc')
plt
.plot
(test_acc
, label
="$Accuracy$")
plt
.legend
()
plt
.show
()
總結
以上是生活随笔為你收集整理的【神经网络计算】——神经网络实现鸢尾花分类的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。