python 识别数字模块_Python使用gluon/mxnet模块实现的mnist手写数字识别功能完整示例…

  • Post author:
  • Post category:python


本文实例讲述了Python使用gluon/mxnet模块实现的mnist手写数字识别功能。分享给大家供大家参考,具体如下:

import gluonbook as gb

from mxnet import autograd,nd,init,gluon

from mxnet.gluon import loss as gloss,data as gdata,nn,utils as gutils

import mxnet as mx

net = nn.Sequential()

with net.name_scope():

net.add(

nn.Conv2D(channels=32, kernel_size=5, activation=’relu’),

nn.MaxPool2D(pool_size=2, strides=2),

nn.Flatten(),

nn.Dense(128, activation=’sigmoid’),

nn.Dense(10, activation=’sigmoid’)

)

lr = 0.5

batch_size=256

ctx = mx.gpu()

net.initialize(init=init.Xavier(), ctx=ctx)

train_data, test_data = gb.load_data_fashion_mnist(batch_size)

trainer = gluon.Trainer(net.collect_params(),’sgd’,{‘learning_rate’ : lr})

loss = gloss.SoftmaxCrossEntropyLoss()

num_epochs = 30

def train(train_data, test_data, net, loss, trainer,num_epochs):

for epoch in range(num_epochs):

total_loss = 0

for x,y in train_data:

with autograd.record():

x = x.as_in_context(ctx)

y = y.as_in_context(ctx)

y_hat=net(x)

l = loss(y_hat,y)

l.backward()

total_loss += l

trainer.step(batch_size)

mx.nd.waitall()

print(“Epoch [{}]: Loss {}”.format(epoch, total_loss.sum().asnumpy()[0]/(batch_size*len(train_data))))

if __name__ == ‘__main__’:

try:

ctx = mx.gpu()

_ = nd.zeros((1,), ctx=ctx)

except:

ctx = mx.cpu()

ctx

gb.train(train_data,test_data,net,loss,trainer,ctx,num_epochs)

希望本文所述对大家Python程序设计有所帮助。



版权声明:本文为weixin_35948624原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。