Python之隨機梯度下降


實現:
# -*- coding: UTF-8 -*-
"""
練習使用隨機梯度下降算法
"""
import numpy as np
import math

__author__ = 'zhen'
# 生成測試數據
x = 2 * np.random.rand(100, 1) # 隨機生成100*1的二維數組,值分別在0~2之間

y = 4 + 3 * x + np.random.randn(100, 1) # 隨機生成100*1的二維數組,值分別在4~11之間

x_b = np.c_[np.ones((100, 1)), x]
print("x矩陣內容如下:\n{}".format(x_b[0:3]))
n_epochs = 100
t0, t1 = 1, 10

m = n_epochs
def learning_schedule(t): # 模擬實現動態修改步長
return t0 / (t + t1)

theta = np.random.randn(2, 1)

for epoch in range(n_epochs):
for i in range(m):
random_index = np.random.randint(m)
x_i = x_b[random_index:random_index+1]
y_i = y[random_index:random_index+1]
gradients = 2 * x_i.T.dot(x_i.dot(theta)-y_i) # 調用公式
learning_rate = learning_schedule(epoch * m + i)
theta = theta - learning_rate * gradients

if epoch % 30 == 0:
print("抽樣查看:\n{}".format(theta))
print("最終結果:\n{}".format(theta))
# 計算誤差
error = math.sqrt(math.pow((theta[0][0] - 4), 2) + math.pow((theta[1][0] - 3), 2))
print("誤差:\n{}".format(error))
結果:

 








免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM