Coding做業
隨機選取數據
def GetBatchSamples(X,Y,batch_size,iteration):
num_feature = X.shape[0]
shuffled_sequence = np.arange(0, X.shape[1])
np.random.shuffle(shuffled_sequence)
batch_x = X[0:num_feature, shuffled_sequence[0: batch_size]].reshape(num_feature, batch_size)
batch_y = Y[0:num_feature, shuffled_sequence[0: batch_size]].reshape(num_feature, batch_size)
return batch_x, batch_y
- 重寫GetBatchSamples後直接運行便可,每次獲取數據時隨機產生一個亂序的index list,取前batch size個index做爲構造X和Y時使用的索引
取5,10,15的batch_size運行
- max_epoch取50,50,100;eta均取0.1,max_iteration = (int)(num_example / batch_size)
- max_epoch * max_iteration > 800便可,結果比較0=30-800之間loss的變化狀況
- GetSampleBatch使用隨機獲取
batch_size = 5,總收斂iteration = 48 * (200 / 5) + 37 = 1957html
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
batch_size = 10,總收斂iteration = 41 * 20 + 18 = 838python
- 相比batch_size = 5抖動明顯減弱不少,更大的batch平均後越能描述樣本總體的性質
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
batch_size = 15,總收斂iteration = 685dom
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
結論:
- batch越大收斂iteration總數越少(但每一個iteration的計算量越大),抖動越不明顯。大batch更反應樣本總體的特性,下降個體樣本的噪聲。
問題
![](http://static.javashuo.com/static/loading.gif)
問題2:爲何是橢圓不是圓?如何構造圓圖?
![](http://static.javashuo.com/static/loading.gif)
- 上式爲均方差損失函數,能夠很顯然,w和b對J貢獻不相等,即J對w和b的偏導數不等的時候J的平面映射圖會是橢圓。
- 要想構造平面映射圖爲圓的損失函數,顯然w和b地位對等,能夠對換,便可以寫成
![](http://static.javashuo.com/static/loading.gif)
- 則此時J(w,b) 可寫成J1(w)或J1(b),爲圓圖
問題3:爲何中心是個橢圓區域而不是一個點?
- 理論上均方差損失函數是有惟一最小值點的,但由於計算機計算時的精度有限,同心橢圓中心處在最小精度如下的數域(都四捨五入爲同一個值a)計算結果都只能用同樣的損失值(a的損失值)表示,故中心是橢圓區域。
- 據此,計算精度越高,中心的橢圓區域越小。當計算精度無限高(計算時使用無限多位小數時),中心會是一個點。
- 當步長(學習率)無限小時,loss能收斂到中心點。