課程
/后端開發(fā)
/Python
/機(jī)器學(xué)習(xí)-實(shí)現(xiàn)簡單神經(jīng)網(wǎng)絡(luò)
總是找不到predict函數(shù),是怎么回事?求大俠幫忙!
源代碼:
2018-11-05
源自:機(jī)器學(xué)習(xí)-實(shí)現(xiàn)簡單神經(jīng)網(wǎng)絡(luò) 3-1
正在回答
覺得有用點(diǎn)個(gè)贊哦~
import?numpy?as?npclass?Perceptron(object): ????#?注釋1 ????def?__init__(self,?eta?=?0.01,?n_iter?=?10):????????self.eta?=?eta????????self.n_iter?=?n_iter????def?fit(self,?X,?y):????????#?注釋2 ????????self.w_?=?np.zeros(1?+?X.shape[1])????????self.errors_?=?[]????????for?_?in?range(self.n_iter): ????????????errors?=?0 ????????????#?注釋3 ????????????for?xi,?target?in?zip(X,?y): ????????????????update?=?self.eta?*?(target?-?self.predict(xi))????????????????#?注釋4 ????????????????self.w_[1:]?+=?update?*?xi????????????????self.w_[0]?+=?update ????????????????errors?+=?int(update?!=?0.0)????????????????self.errors_.append(errors)????def?net_input(self,?X):????????#?注釋5 ????????return?np.dot(X,?self.w_[1:])?+?self.w_[0]????def?predict(self,?X):????????return?np.where(self.net_input(X)?>=?0.0,?1,?-1)
注釋1: ????eta:學(xué)習(xí)率 ????n_iter:權(quán)重向量的訓(xùn)練次數(shù) ????w_:神經(jīng)分叉權(quán)重向量 ????errors_:用于記錄神經(jīng)元判斷出錯(cuò)次數(shù) 注釋2: ????輸入訓(xùn)練數(shù)據(jù),培訓(xùn)神經(jīng)元,X是輸入樣本向量,y是對應(yīng)的樣本分類 ????X:shape[n_samples,?n_features] ????比如:X:[[1,?2,?3],?[4,?5,?6]] ????那么:n_samples:?2,n_features:?3,y:[1,?-1] ????初始化權(quán)重向量為0,加1是因?yàn)樘岬降膚0,即步調(diào)函數(shù)的閾值 注釋3: ????比如:X:[[1,?2,?3],?[4,?5,?6] ????所以y:[1,?-1],zip(X,?y):[[1,?2,?3,?1].?[4,?5,?6,?-1]] ????update?=?n?*?(y?-?y') 注釋4: ????xi是一個(gè)向量 ????update?*?xi等價(jià)于:[?w1?=?x1*update,?w2?=?x2*update,?...] 注釋5: ????z?=?w0*1?+?w1*x1?+?w2*x2?+?...? ????np.dot()是做點(diǎn)積
把net_input? predict這兩個(gè)函數(shù)從fit函數(shù)拿到外部? 與fit函數(shù)平級? 然后檢查下自己的函數(shù)名有沒有寫錯(cuò)
慕神2257579
舉報(bào)
人工智能時(shí)代,你準(zhǔn)備好成為抓住機(jī)遇的那百分之二嗎。
4 回答AttributeError: 'Perception' object has no attribute 'w_'
2 回答AttributeError: 'Perceptron' object has no attribute 'predict'
2 回答'Perceptron' object has no attribute 'predict'
1 回答'Perceptron' object has no attribute 'predict'
1 回答'numpy' has no attribute 'predict'
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2020-07-20
覺得有用點(diǎn)個(gè)贊哦~
2020-07-20
2018-12-21
把net_input? predict這兩個(gè)函數(shù)從fit函數(shù)拿到外部? 與fit函數(shù)平級? 然后檢查下自己的函數(shù)名有沒有寫錯(cuò)