網(wǎng)站首頁 編程語言 正文
我們將研究一種判別式分類方法,其中直接學(xué)習(xí)評(píng)估 g(x)所需的 w 參數(shù)。我們將使用感知器學(xué)習(xí)算法。
感知器學(xué)習(xí)算法很容易實(shí)現(xiàn),但為了節(jié)省時(shí)間,我在下面為您提供了一個(gè)實(shí)現(xiàn)。該函數(shù)有幾個(gè)輸入:訓(xùn)練數(shù)據(jù)、訓(xùn)練標(biāo)簽、對(duì)權(quán)重的初始猜測(cè)和學(xué)習(xí)率。注意,對(duì)于這兩個(gè)類,類標(biāo)簽的值必須為+1和-1。
它將返回一個(gè)元組,其中包含:
- 1.學(xué)習(xí)w參數(shù)
- 2.執(zhí)行的迭代次數(shù)
- 3.錯(cuò)誤分類的樣本數(shù)
花些時(shí)間檢查代碼。如果不清楚每一行是如何工作的,不要擔(dān)心,只要讓你自己知道每一行的目的是什么就可以了。代碼中有一些注釋可以幫助大家。
def perce(X, y, w_init, rho, max_iter=1000): ? ?? ? ? (N, nfeatures) = X.shape ? ? # Augment the feature vectors by adding a 1 to each one. (see lecture notes) ? ? X = np.hstack((X, np.ones((N, 1)))) ? ? nfeatures += 1 ? ? w = w_init ?# initialise weights ? ? iter = 0 ? ? mis_class = N ?# start by assuming all samples are misclassified ? ? while mis_class > 0 and iter < max_iter: ? ? ? ? iter += 1 ? ? ? ? mis_class = 0 ? ? ? ? gradient = np.zeros(nfeatures) ?# initaliase the gradients to 0 ? ? ? ? # loop over every training sample. ? ? ? ? for i in range(N): ? ? ? ? ? ? # each misclassified point will cause the gradient to change ? ? ? ? ? ? if np.inner(X[i, :], w) * y[i] <= 0: ? ? ? ? ? ? ? ? mis_class += 1 ? ? ? ? ? ? ? ? gradient += -y[i] * X[i, :] ? ? ? ? # update the weight vector ready for the next iteration ? ? ? ? # Note, also that the learning rate decays over time (rho/iter) ? ? ? ? w -= rho / iter * gradient ? ? return w, iter, mis_class
解釋:
X-數(shù)據(jù)矩陣。每行代表一個(gè)單獨(dú)的樣本
y-與X-標(biāo)簽行對(duì)應(yīng)的整數(shù)類標(biāo)簽的一維數(shù)組必須為+1或-1
w_init-初始權(quán)重向量
rho-標(biāo)量學(xué)習(xí)率
最大迭代次數(shù)-最大迭代次數(shù)(默認(rèn)為1000)
def perce_fast(X, y, w_init, rho, max_iter=10000): ?? ? ? (N, nfeatures) = X.shape ? ? X = np.hstack((X, np.ones((N, 1)))) ? ? nfeatures += 1 ? ? w = w_init ? ? iter = 0 ? ? mis_class = N ? ? yy = np.tile(y, (nfeatures, 1)).T ? ? while mis_class > 0 and iter < max_iter: ? ? ? ? iter += 1 ? ? ? ? # Compute set of misclassified points ? ? ? ? mc = (np.dot(X, w.transpose()) * y) <= 0 ? ? ? ? mis_class = np.sum(mc) ? ? ? ? # Update weights. Note, the learning rate decays over time (rho/iter) ? ? ? ? w -= rho / iter * (np.sum(-yy[mc, :] * X[mc, :], axis=0)) ? ? return w, iter, np.sum(mc)
- 感知器算法的高效實(shí)現(xiàn)
- 對(duì)于筆記本電腦數(shù)據(jù),此版本的工作速度將提高x100!
原文鏈接:https://blog.csdn.net/kirsten111111/article/details/121429528
相關(guān)推薦
- 2022-12-23 C++成員函數(shù)如何當(dāng)作回調(diào)函數(shù)同時(shí)傳遞this指針_C 語言
- 2022-11-25 命令行下執(zhí)行TypeScript文件的三種方法_基礎(chǔ)知識(shí)
- 2023-05-15 sql語句中臨時(shí)表使用實(shí)例詳解_MsSql
- 2022-10-11 ingress-nginx-url重寫的經(jīng)驗(yàn)總結(jié)
- 2022-05-14 實(shí)現(xiàn)AJAX異步調(diào)用和局部刷新的基本步驟_AJAX相關(guān)
- 2023-07-04 spring之BeanDefinition
- 2022-05-15 react底層的四大核心內(nèi)容架構(gòu)詳解_React
- 2022-05-24 一起來學(xué)習(xí)C語言的輸入和輸出_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支