从零和使用mxnet实现softmax分类

1.softmax从零实现(1797,64)(1797,)(1797,10)epoch:50,loss:[1.9941667],accuracy:0.3550361713967724

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

1.softmax从零实现

from mxnet.gluon import data as gdata
from sklearn import datasets
from mxnet import nd,autograd
# 加载数据集
digits = datasets.load_digits()
features,labels = nd.array(digits['data']),nd.array(digits['target'])
print(features.shape,labels.shape)
labels_onehot = nd.one_hot(labels,10)
print(labels_onehot.shape)
(1797, 64) (1797,)
(1797, 10)
class softmaxClassifier:
    def __init__(self,inputs,outputs):
        self.inputs = inputs
        self.outputs = outputs
        
        self.weight = nd.random.normal(scale=0.01,shape=(inputs,outputs))
        self.bias = nd.zeros(shape=(1,outputs))
        self.weight.attach_grad()
        self.bias.attach_grad()
        
    def forward(self,x):
        output = nd.dot(x,self.weight) + self.bias
        return self._softmax(output)
        
    def _softmax(self,x):
        step1 = x.exp()
        step2 = step1.sum(axis=1,keepdims=True)
        return step1 / step2
    
    def _bgd(self,params,learning_rate,batch_size):
        '''
        批量梯度下降
        '''
        for param in params:       # 直接使用mxnet的自动求梯度
            param[:] = param - param.grad * learning_rate / batch_size
            
    def loss(self,y_pred,y):
        return nd.sum((-y * y_pred.log())) / len(y)
            
    def dataIter(self,x,y,batch_size):
        dataset = gdata.ArrayDataset(x,y)
        return gdata.DataLoader(dataset,batch_size,shuffle=True)
    
    def fit(self,x,y,learning_rate,epoches,batch_size):
        for epoch in range(epoches):
            for x_batch,y_batch in self.dataIter(x,y,batch_size):
                with autograd.record():
                    y_pred = self.forward(x_batch)
                    l = self.loss(y_pred,y_batch)
                l.backward()
                self._bgd([self.weight,self.bias],learning_rate,batch_size)
            if epoch % 50 == 0:
                y_all_pred = self.forward(x)
                print('epoch:{},loss:{},accuracy:{}'.format(epoch+50,self.loss(y_all_pred,y),self.accuracyScore(y_all_pred,y)))
            
    def predict(self,x):
        y_pred = self.forward(x)
        return y_pred.argmax(axis=0)
    
    def accuracyScore(self,y_pred,y):
        acc_sum = (y_pred.argmax(axis=1) == y.argmax(axis=1)).sum().asscalar()
        return acc_sum / len(y)
sfm_clf = softmaxClassifier(64,10)
sfm_clf.fit(features,labels_onehot,learning_rate=0.1,epoches=500,batch_size=200)
epoch:50,loss:
[1.9941667]
<NDArray 1 @cpu(0)>,accuracy:0.3550361713967724
epoch:100,loss:
[0.37214527]
<NDArray 1 @cpu(0)>,accuracy:0.9393433500278241
epoch:150,loss:
[0.25443634]
<NDArray 1 @cpu(0)>,accuracy:0.9549248747913188
epoch:200,loss:
[0.20699367]
<NDArray 1 @cpu(0)>,accuracy:0.9588202559821926
epoch:250,loss:
[0.1799827]
<NDArray 1 @cpu(0)>,accuracy:0.9660545353366722
epoch:300,loss:
[0.1619963]
<NDArray 1 @cpu(0)>,accuracy:0.9677239844184753
epoch:350,loss:
[0.14888664]
<NDArray 1 @cpu(0)>,accuracy:0.9716193656093489
epoch:400,loss:
[0.13875261]
<NDArray 1 @cpu(0)>,accuracy:0.9738452977184195
epoch:450,loss:
[0.13058177]
<NDArray 1 @cpu(0)>,accuracy:0.9760712298274903
epoch:500,loss:
[0.12379646]
<NDArray 1 @cpu(0)>,accuracy:0.9777406789092933
print('预测结果:',sfm_clf.predict(features[:10]))
print('真实结果:',labels[:10])
预测结果: 
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
<NDArray 10 @cpu(0)>
真实结果: 
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
<NDArray 10 @cpu(0)>

2.使用mxnet实现softmax分类

from mxnet import gluon,nd,autograd,init
from mxnet.gluon import nn,trainer,loss as gloss,data as gdata
# 定义模型
net = nn.Sequential()
net.add(nn.Dense(10))

# 初始化模型
net.initialize(init=init.Normal(sigma=0.01))

# 损失函数
loss = gloss.SoftmaxCrossEntropyLoss(sparse_label=False)

# 优化算法
optimizer = trainer.Trainer(net.collect_params(),'sgd',{'learning_rate':0.1})

# 训练
epoches = 500
batch_size = 200

dataset = gdata.ArrayDataset(features, labels_onehot)
data_iter = gdata.DataLoader(dataset,batch_size,shuffle=True)
for epoch in range(epoches):
    for x_batch,y_batch in data_iter:
        with autograd.record():
            l = loss(net.forward(x_batch), y_batch).sum() / batch_size
        l.backward()
        optimizer.step(batch_size)
    if epoch % 50 == 0:
        y_all_pred = net.forward(features)
        acc_sum = (y_all_pred.argmax(axis=1) == labels_onehot.argmax(axis=1)).sum().asscalar()
        print('epoch:{},loss:{},accuracy:{}'.format(epoch+50,loss(y_all_pred,labels_onehot).sum() / len(labels_onehot),acc_sum/len(y_all_pred)))
epoch:50,loss:
[2.1232333]
<NDArray 1 @cpu(0)>,accuracy:0.24652198107957707
epoch:100,loss:
[0.37193483]
<NDArray 1 @cpu(0)>,accuracy:0.9410127991096272
epoch:150,loss:
[0.25408813]
<NDArray 1 @cpu(0)>,accuracy:0.9543683917640512
epoch:200,loss:
[0.20680156]
<NDArray 1 @cpu(0)>,accuracy:0.9627156371730662
epoch:250,loss:
[0.1799252]
<NDArray 1 @cpu(0)>,accuracy:0.9666110183639399
epoch:300,loss:
[0.16203885]
<NDArray 1 @cpu(0)>,accuracy:0.9699499165275459
epoch:350,loss:
[0.14899409]
<NDArray 1 @cpu(0)>,accuracy:0.9738452977184195
epoch:400,loss:
[0.13890252]
<NDArray 1 @cpu(0)>,accuracy:0.9749582637729549
epoch:450,loss:
[0.13076076]
<NDArray 1 @cpu(0)>,accuracy:0.9755147468002225
epoch:500,loss:
[0.1239901]
<NDArray 1 @cpu(0)>,accuracy:0.9777406789092933
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/120005.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 常见的图像增强方法有_图像中值滤波的算法实现

    常见的图像增强方法有_图像中值滤波的算法实现1.对比度拉升采用了线性函数对图像的灰度值进行变换2.Gamma校正采用了非线性函数(指数函数)对图像的灰度值进行变换这两种方式的实质是对感兴趣的图像区域进行展宽,对不感兴趣的背景区域进行

  • 笔记本计算机卡,笔记本电脑越来越卡怎么办 不同原因解决方法不同[通俗易懂]

    笔记本计算机卡,笔记本电脑越来越卡怎么办 不同原因解决方法不同[通俗易懂]1、由于系统原因导致笔记本越用越卡的问题,懂装系统并且不怕麻烦的用户可以重装新系统,这样可以彻底解决系统卡顿问题,适当的清理一些不要的垃圾文件软件等,若是不懂得小白或者怕麻烦的人,也可以选择市面上的各类XX安全卫士、XX电脑管家带有的垃圾清理功能清理一些垃圾文件及注册表等无用文件、软件,并且养成定期清理垃圾的好习惯。2、由于硬件原因导致笔记本越用越卡的问题,则需要看看笔记本是因为哪些硬件导致的,决…

  • matlab中wavedec2,wavedec2函数详解[通俗易懂]

    matlab中wavedec2,wavedec2函数详解[通俗易懂]很多人对小波多级分解的wavedec2总是迷惑,今天就详释她!wavedec2函数:1.功能:实现图像(即二维信号)的多层分解,多层,即多尺度.2.格式:[c,s]=wavedec2(X,N,’wname’)[c,s]=wavedec2(X,N,Lo_D,Hi_D)(我不讨论它)3.参数说明:对图像X用wname小波基函数实现N层分解,这里的小波基函数应该根据实际情况选择,具体选择办法可以搜之或者…

  • 开源 网管 工具_智和网管平台激活成功教程版

    开源 网管 工具_智和网管平台激活成功教程版http://network.51cto.com/art/200906/129490.htm 转载于:https://blog.51cto.com/zhengj3/168618

  • myeclipse免费版下载_Myeclipse

    myeclipse免费版下载_Myeclipse
     占奇发布于:2011年03月21日(40评)

    MyEclipse9.0经过M1,M2,终于出了正式版(MyEclipseForSpring还是8.6.1)。该版本集成了Eclipse3.6.1,支持HTML5和JavaEE6
    标准版各平台下载地址:官网支持功能自定义下载安装
     
    MyEclipse9.0forWindows下载
    MyEclipse9.0(Wind

  • 费马小定理(易懂)_四年rain的博客_什么易懂

    费马小定理(易懂)_四年rain的博客_什么易懂费马小定理:内容:若存在整数a,p且gcd(a,p)=1,即二者互为质数,则有a^(p-1)≡1(modp)。(这里的≡指的是恒等于,a^(p-1)≡1(modp)是指a的p-1次幂取模与1取模恒等)(不理解的话请留言)证明:这里证明较为复杂需要先引出两个定理:引理一:若a,b,c为任意3个整数,m为正整数,且(m,c)=1,则当a·c≡b·c(modm)时,有…

    2022年10月21日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号