Softmax分类器的实现

本文介绍了Softmax分类器的实现过程,强调其与SVM在使用验证集确定超参数、计算概率和梯度更新上的区别。通过代码展示, Softmax分类器在实验中达到约39%的稳定准确率。
摘要由CSDN通过智能技术生成

和SVM的实现存在一定的相似性,要通过划分训练集的一部分作为验证集来确定超参数。在得出对应的评分之后,要求出对应的概率,同时利用梯度更新的时候,注意求导与SVM存在不同即可,具体实现见如下代码,准确率基本稳定在39%左右。

import numpy as np
import pickle as pic

class softmax:
    weight=[]
    l_rate=0
    reg=0

    def readData(self,file):
        with open(file,'rb') as fo:
            dict=pic.load(fo,encoding='bytes')
        return dict

    def getGradient(self,sample_train_data,sample_train_label,reg):
        amount=sample_train_data.shape[0]
        score=self.weight.dot(sample_train_data.T)
        per_amount=np.max(score,axis=0)
        score=score-per_amount
        exp_score=np.exp(score)
        per_sum=np.sum(exp_score,axis=0)
        exp_score=exp_score/per_sum
        exp_score[sample_train_label,range(amount)]-=1
        gred=exp_score.dot(sample_train_data)/amount+reg*self.weight
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值