第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 MSE 將二次函數擬合到數據

使用 MSE 將二次函數擬合到數據

慕勒3428872 2023-09-19 14:47:57
所以我的想法是(借用神經網絡人員),如果我有數據集 D,我可以通過首先計算誤差相對于參數(a、b 和 c)的導數來擬合二次曲線,然后然后做一個小的更新來最小化錯誤。我的問題是,下面的代碼實際上并不能適應曲線。對于線性的東西,類似的方法是有效的,但二次似乎由于某種原因失敗了。你能看到我做錯了什么嗎(假設或只是實現錯誤)編輯:問題不夠具體:以下代碼不能很好地處理數據偏差。由于某種原因,它以某種方式更新了 a 和 b 參數,而 c 被拋在了后面。這種方法類似于機器人技術(使用雅可比行列式查找路徑)和神經網絡(根據錯誤查找參數),因此它不是不合理的算法,現在的問題是,為什么這種特定實現不會產生我期望的結果。在下面的Python代碼中,我使用math作為m,MSE是計算兩個數組之間的均方誤差的函數。除此之外,代碼是獨立的代碼:def quadraticRegression(data, dErr):    a = 1 #Starting values    b = 1    c = 1    a_momentum = 0 #Momentum to counter steady state error    b_momentum = 0    c_momentum = 0    estimate = [a*x**2 + b*x + c for x in range(len(data))] #Estimate curve    error = MSE(data, estimate) #Get errors 'n stuff    errorOld = 0    lr = 0.0000000001 #learning rate    while abs(error - errorOld) > dErr:        #Fit a (dE/da)        deda = sum([ 2*x**2 * (a*x**2 + b*x + c - data[x]) for x in range(len(data)) ])/len(data)        correction = deda*lr        a_momentum = (a_momentum)*0.99 + correction*0.1 #0.99 is to slow down momentum when correction speed changes        a = a - correction - a_momentum            #fit b (dE/db)        dedb = sum([ 2*x*(a*x**2 + b*x + c - data[x]) for x in range(len(data))])/len(data)        correction = dedb*lr        b_momentum = (b_momentum)*0.99 + correction*0.1        b = b - correction - b_momentum        #fit c (dE/dc)        dedc = sum([ 2*(a*x**2 + b*x + c - data[x]) for x in range(len(data))])/len(data)        correction = dedc*lr        c_momentum = (c_momentum)*0.99 + correction*0.1        c = c - correction - c_momentum        #Update model and find errors        estimate = [a*x**2 +b*x + c for x in range(len(data))]        errorOld = error        print(error)        error = MSE(data, estimate)    return a, b, c, error
查看完整描述

1 回答

?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

對我來說,你的代碼看起來完全正確!至少算法是正確的。我已經更改了您的代碼以用于numpy快速計算而不是純Python。另外,我還配置了一些參數,例如改變了動量和學習率,也實現了MSE。

然后我用來matplotlib畫情節(jié)動畫。最后,在動畫上,看起來您的回歸實際上試圖將曲線擬合到數據。盡管在最后一次擬合迭代中它sin(x)看起來像線性近似,但仍然盡可能接近二次曲線的數據點。但對于for?in來說,它看起來像是理想的近似(它從迭代周圍開始擬合)。x[0; 2 * pi]sin(x)x[0; pi]12-th

i-th動畫幀只是用 進行回歸dErr = 0.7 ** (i + 15)。

我的動畫運行腳本有點慢,但是如果您save像這樣添加參數python script.py save,它將渲染/保存以line.gif繪制繪圖動畫。如果您在沒有參數的情況下運行腳本,它將在您的 PC 屏幕上實時繪制/擬合動畫。

完整的代碼在圖形之后,代碼需要通過運行一次安裝一些Python模塊python -m pip install numpy matplotlib。

接下來是sin(x)x(0, pi)


接下來是sin(x)x(0, 2 * pi)


接下來是abs(x)x(-1, 1)


# Needs: python -m pip install numpy matplotlib

import math, sys

import numpy as np, matplotlib.pyplot as plt, matplotlib.animation as animation

from matplotlib.animation import FuncAnimation



x_range = (0., math.pi, 0.1) # (xmin, xmax, xstep)

y_range = (-0.2, 1.2) # (ymin, ymax)

num_iterations = 50


def f(x):

? ? return np.sin(x)


def derr(iteration):

? ? return 0.7 ** (iteration + 15)

? ??

? ??

def MSE(a, b):

? ? return (np.abs(np.array(a) - np.array(b)) ** 2).mean()


def quadraticRegression(*, x, data, dErr):

? ? x, data = np.array(x), np.array(data)

? ? assert x.size == data.size, (x.size, data.size)


? ? a = 1 #Starting values

? ? b = 1

? ? c = 1


? ? a_momentum = 0.1 #Momentum to counter steady state error

? ? b_momentum = 0.1

? ? c_momentum = 0.1


? ? estimate = a*x**2 + b*x + c #Estimate curve

? ? error = MSE(data, estimate) #Get errors 'n stuff

? ? errorOld = 0.


? ? lr = 10. ** -4 #learning rate

? ? while abs(error - errorOld) > dErr:

? ? ? ? #Fit a (dE/da)

? ? ? ? deda = np.sum(2*x**2 * (a*x**2 + b*x + c - data))/len(data)

? ? ? ? correction = deda*lr

? ? ? ? a_momentum = (a_momentum)*0.99 + correction*0.1 #0.99 is to slow down momentum when correction speed changes

? ? ? ? a = a - correction - a_momentum

? ??

? ? ? ? #fit b (dE/db)

? ? ? ? dedb = np.sum(2*x*(a*x**2 + b*x + c - data))/len(data)

? ? ? ? correction = dedb*lr

? ? ? ? b_momentum = (b_momentum)*0.99 + correction*0.1

? ? ? ? b = b - correction - b_momentum


? ? ? ? #fit c (dE/dc)

? ? ? ? dedc = np.sum(2*(a*x**2 + b*x + c - data))/len(data)

? ? ? ? correction = dedc*lr

? ? ? ? c_momentum = (c_momentum)*0.99 + correction*0.1

? ? ? ? c = c - correction - c_momentum


? ? ? ? #Update model and find errors

? ? ? ? estimate = a*x**2 +b*x + c

? ? ? ? errorOld = error

? ? ? ? #print(error)

? ? ? ? error = MSE(data, estimate)


? ? return a, b, c, error



? ? ? ??

fig, ax = plt.subplots()

fig.set_tight_layout(True)


x = np.arange(x_range[0], x_range[1], x_range[2])

#ax.scatter(x, x + np.random.normal(0, 3.0, len(x)))

line0, line1 = None, None


do_save = len(sys.argv) > 1 and sys.argv[1] == 'save'


def g(x, derr):

? ? a, b, c, error = quadraticRegression(x = x, data = f(x), dErr = derr)

? ? return a * x ** 2 + b * x + c

? ??

def dummy(x):

? ? return np.ones_like(x, dtype = np.float64) * 100.


def update(i):

? ? global line0, line1

? ??

? ? de = derr(i)

? ??

? ? if line0 is None:

? ? ? ? assert line1 is None

? ? ? ? line0, = ax.plot(x, f(x), 'r-', linewidth=2)

? ? ? ? line1, = ax.plot(x, g(x, de), 'r-', linewidth=2, color = 'blue')

? ? ? ? ax.set_ylim(y_range[0], y_range[1])

? ? ? ??

? ? if do_save:

? ? ? ? sys.stdout.write(str(i) + ' ')

? ? ? ? sys.stdout.flush()


? ? label = 'iter {0} derr {1}'.format(i, round(de, math.ceil(-math.log(de) / math.log(10)) + 2))

? ? line1.set_ydata(g(x, de))

? ? ax.set_xlabel(label)

? ? return line1, ax


if __name__ == '__main__':

? ? anim = FuncAnimation(fig, update, frames = np.arange(0, num_iterations), interval = 200)

? ? if do_save:

? ? ? ? anim.save('line.gif', dpi = 200, writer = 'imagemagick')

? ? else:

? ? ? ? plt.show()


查看完整回答
反對 回復 2023-09-19
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號