課程
/后端開發
/Python
/機器學習-實現簡單神經網絡
代碼沒有地方下載嗎
2017-07-12
源自:機器學習-實現簡單神經網絡 3-2
正在回答
```
import numpy as np
class perceptron(object):
? ? """
? ? eta :學習率
? ? n_iter: 權重向量的訓練次數
? ? w_: 神經分叉權重向量
? ? errors_: 用于記錄神經元判斷出錯次數
? ? def _int_(self,eta=0.01,n_iter=10):
? ? ? ? self.eta=eta;
? ? ? ? self.n_iter=n_iter;
? ? ? ? pass
? ? def fit(self,x,y):
? ? ? ? """
? ? ? ? 輸入訓練數據,培訓神經元,x輸入樣本向量,y對應樣本分類
? ? ? ? x:shape[n_samples,n_features]
? ? ? ? x:[[1,2,3],[4,5,6]]
? ? ? ? n_samples: 2 (輸入樣本量)
? ? ? ? n_features:3 (輸入的信號有多少個)
? ? ? ??
? ? ? ? y:[1,-1]
? ? ? ? 初始化權重向量為0
? ? ? ? self.w_=np.zero(1+x.shape[1]);
? ? ? ? self.errors_=[]
? ? ? ? 訓練次數循環
? ? ? ? for _ in range(self.n_iter):
? ? ? ? ? ? errors =0
? ? ? ? ? ? """
? ? ? ? ? ? x:[[1,2,3],[4,5,6]]
? ? ? ? ? ? y:[1,-1]
? ? ? ? ? ? zip(x,y)=[[1,2,3,1],[4,5,6,-1]]
? ? ? ? ? ? for xi,target in zip(x,y):
? ? ? ? ? ? ? ? """
? ? ? ? ? ? ? ? update=η*(y-y')
? ? ? ? ? ? ? ? update =self.eta*(target-self.predict(xi))
? ? ? ? ? ? ? ? xi 是一個向量
? ? ? ? ? ? ? ? self.w_[1:]+=update*xi等價:
? ? ? ? ? ? ? ? [▽w(1)=x[1]*update,▽w(2)=x[2]*update,▽w(3)=x[3]*update,]
? ? ? ? ? ? ? ? self.w_[1:]+=update*xi
? ? ? ? ? ? ? ? self.w_[0]+=update;
? ? ? ? ? ? ? ? errors+=int(update !=0.0)
? ? ? ? ? ? ? ? self.errors_.appand(errors)
? ? ? ? ? ? ? ? pass
? ? ? ? ? ??
? ? ? ? ? ? pass
? ? ? ? def net_input(self,x):
? ? ? ? ? ? z= W0*1+ W1*X1+ ....Wn*Xn
? ? ? ? ? ? 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 回答現在的視頻不能下載到本地離線學習了嗎?
3 回答有木有完整的代碼
1 回答代碼調試有屬性錯誤啊
2 回答運行視頻中的代碼有問題
1 回答代碼運行有誤,大神們幫忙看看
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-10-14
```
import numpy as np
class perceptron(object):
? ? """
? ? eta :學習率
? ? n_iter: 權重向量的訓練次數
? ? w_: 神經分叉權重向量
? ? errors_: 用于記錄神經元判斷出錯次數
? ? """
? ? def _int_(self,eta=0.01,n_iter=10):
? ? ? ? self.eta=eta;
? ? ? ? self.n_iter=n_iter;
? ? ? ? pass
? ? def fit(self,x,y):
? ? ? ? """
? ? ? ? 輸入訓練數據,培訓神經元,x輸入樣本向量,y對應樣本分類
? ? ? ? x:shape[n_samples,n_features]
? ? ? ? x:[[1,2,3],[4,5,6]]
? ? ? ? n_samples: 2 (輸入樣本量)
? ? ? ? n_features:3 (輸入的信號有多少個)
? ? ? ??
? ? ? ? y:[1,-1]
? ? ? ? """
? ? ? ? """
? ? ? ? 初始化權重向量為0
? ? ? ??
? ? ? ? """
? ? ? ? self.w_=np.zero(1+x.shape[1]);
? ? ? ? self.errors_=[]
? ? ? ??
? ? ? ? """
? ? ? ? 訓練次數循環
? ? ? ? """
? ? ? ? for _ in range(self.n_iter):
? ? ? ? ? ? errors =0
? ? ? ? ? ? """
? ? ? ? ? ? x:[[1,2,3],[4,5,6]]
? ? ? ? ? ? y:[1,-1]
? ? ? ? ? ? zip(x,y)=[[1,2,3,1],[4,5,6,-1]]
? ? ? ? ? ? """
? ? ? ? ? ? for xi,target in zip(x,y):
? ? ? ? ? ? ? ? """
? ? ? ? ? ? ? ? update=η*(y-y')
? ? ? ? ? ? ? ? """
? ? ? ? ? ? ? ? update =self.eta*(target-self.predict(xi))
? ? ? ? ? ? ? ? """
? ? ? ? ? ? ? ? xi 是一個向量
? ? ? ? ? ? ? ? self.w_[1:]+=update*xi等價:
? ? ? ? ? ? ? ? [▽w(1)=x[1]*update,▽w(2)=x[2]*update,▽w(3)=x[3]*update,]
? ? ? ? ? ? ? ? """
? ? ? ? ? ? ? ? self.w_[1:]+=update*xi
? ? ? ? ? ? ? ? self.w_[0]+=update;
? ? ? ? ? ? ? ? errors+=int(update !=0.0)
? ? ? ? ? ? ? ? self.errors_.appand(errors)
? ? ? ? ? ? ? ? pass
? ? ? ? ? ??
? ? ? ? ? ? pass
? ? ? ? def net_input(self,x):
? ? ? ? ? ? """
? ? ? ? ? ? z= W0*1+ W1*X1+ ....Wn*Xn
? ? ? ? ? ? """
? ? ? ? ? ? return np.dot(x,self.w_[1:])+self.w_[0]
? ? ? ? ? ? pass
? ? ? ? def predict(self,x):
? ? ? ? ? ? return np.where(self,net_input(x)>=0.0,1,-1)
? ? ? ? pass
```
2017-08-23
沒有下載!