梯度下降公式很簡單
import numpy as np
import torch
from torchvision.datasets import mnist
from torch.utils.data import dataloader
from torch import nn
from torch.autograd import variable
import time
import matplotlib.pyplot as plt
#隨機梯度下降法,從0開始自己實現
def data_tf(x):
x = np.array(x,dtype='float32')/255 #講資料變成0-1之間
x = (x-0.5)/0.5 #標準化
x = x.reshape((-1,)) #拉平
x = torch.from_numpy(x)
return x
#載入資料集,宣告定義的資料變換
train_set = mnist('./data',train=true,transform=data_tf,download=true)
test_set = mnist('./data',train=false,transform=data_tf,download=true)
#定義loss函式
criterion = nn.crossentropyloss()
#定義梯度下降公式:lr學習率,parameters引數
def sgd_update(parameters,lr):
for param in parameters:
param.data = param.data - lr * param.grad.data
train_data = dataloader(train_set,batch_size=64,shuffle=true)
net = nn.sequential(
nn.linear(784,200),
nn.relu(),
nn.linear(200,10)
)#開始訓練
losses1 =
idx = 0
#計時開始
start = time.time()
for e in range(5):
train_loss = 0
for im,label in train_data:
im = variable(im)
label = variable(label)
#向前傳播
out = net(im)
loss = criterion(out,label)
#反向傳播
net.zero_grad()
loss.backward()
sgd_update(net.parameters(),0.01)
#記錄誤差
train_loss += loss.data
if idx%30 == 0:
idx += 1
print('epoch:{},train loss:{}'.format(e,train_loss/len(train_data)))
end = time.time()
x_axis = np.linspace(0,5,len(losses1),endpoint=true)
plt.semilogy(x_axis,losses1,label='batch_size = 1')
plt.show()
batch_size看電腦配置,越小越不穩定
pytorch內建隨機梯度下降法
train_data = dataloader(train_set, batch_size=64, shuffle=true)
net = nn.sequential(
nn.linear(784, 200),
nn.relu(),
nn.linear(200, 10)
)optimzier = torch.optim.sgd(net.parameters(), 1e-2)
start = time.time()
for e in range(5):
train_loss = 0
for im, label in train_data:
im = variable(im)
label = variable(label)
out = net(im)
loss = criterion(out, label)
optimzier.zero_grad()
loss.backward()
optimzier.step()
train_loss += loss.data
print('epoch: {}, train loss: '
.format(e, train_loss / len(train_data)))
end = time.time() #
梯度下降法和隨機梯度下降法
批量梯度下降法 batch gradient descent 在更新引數時使用所有的樣本來進行更新 隨機梯度下降法 stochastic gradient descent 求梯度時沒有用所有的m個樣本的資料,而是僅僅選取乙個樣本j來求梯度。小批量梯度下降法 mini batch gradient d...
隨機梯度下降法
剛剛看完史丹福大學機器學習第四講 牛頓法 也對學習過程做一次總結吧。一 誤差準則函式與隨機梯度下降 數學一點將就是,對於給定的乙個點集 x,y 找到一條曲線或者曲面,對其進行擬合之。同時稱x中的變數為特徵 feature y值為 值。如圖 乙個典型的機器學習的過程,首先給出一組輸入資料x,我們的演算...
隨機梯度下降法
自定義虛擬資料集 import numpy as np import matplotlib.pyplot as plt m 100000 m個樣本 x np.random.normal size m x x.reshape 1 1 y 4.x 3 np.random.normal 0,3,size ...