博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MXNet动手学深度学习笔记:卷积神经网络实现
阅读量:6672 次
发布时间:2019-06-25

本文共 4059 字,大约阅读时间需要 13 分钟。

  hot3.png

#coding:utf-8'''卷积神经网络'''import mxnet as mxfrom mxnet.gluon import nn from mxnet import ndarray as ndfrom mxnet import gluontry:    ctx = mx.gpu()    _ = nd.zeros((1,),ctx = ctx)except:    ctx = mx.cpu()def transform(data,label):    return data.astype('float32') / 255,label.astype('float32')mnist_train = gluon.data.vision.FashionMNIST(train=True,transform=transform)mnist_test = gluon.data.vision.FashionMNIST(train=False,transform=transform)# 显示样本的形状和标号data,label = mnist_train[0]print('shape:',data.shape,',label:',label)# def show_images(images):#     n = images.shape[0]#     _,figs = plt.subplots(1,n,figsize=(15,15))#     for i in range(n):#         figs[i].imshow(images[i].reshape((28,28)).asnumpy())#         figs[i].axes.get_xaxis().set_visible(False)#         figs[i].axes.get_yaxis().set_visible(False)#     plt.show()def get_text_labels(label):    text_labels = [        't-shirt','trouser','pullover','dress','coat',        'sandal','shirt','sneaker','bag','ankle boot'    ]    return [text_labels[int(i)] for i in label]# 显示数据# data,label = mnist_train[0:9]# show_images(data)# print(get_text_labels(label))# 读取数据batch_size = 64train_data = gluon.data.DataLoader(mnist_train,batch_size,shuffle=True)test_data = gluon.data.DataLoader(mnist_test,batch_size,shuffle=False)print('use contex:',ctx)weight_scale = 0.01# 输出通道 = 20,卷积核 kernel=(5,5)W1 = nd.random_normal(shape=(20,1,5,5),scale=weight_scale,ctx=ctx)b1 = nd.zeros(W1.shape[0],ctx=ctx)# 输出通道=50,卷积核 kernel=(3,3)W2 = nd.random_normal(shape=(50,20,3,3),scale=weight_scale,ctx=ctx)b2 = nd.zeros(W2.shape[0],ctx=ctx)# 输出维度=128W3 = nd.random_normal(shape=(1250,128),scale=weight_scale,ctx=ctx)b3 = nd.zeros(W3.shape[1],ctx=ctx)# 输出维度=10W4 = nd.random_normal(shape=(W3.shape[1],10),scale=weight_scale,ctx=ctx)b4 = nd.zeros(W4.shape[1],ctx=ctx)params = [W1,b1,W2,b2,W3,b3,W4,b4]for param in params:    param.attach_grad()# 定义卷积神经网络模型# 卷积层-激活层-池化层,然后转换成2D矩阵输出给后面的全连接层def net(X,verbose=False):    X = X.as_in_context(W1.context)    # 第一层卷积    h1_conv = nd.Convolution(data=X,weight=W1,bias=b1,                kernel=W1.shape[2:],num_filter=W1.shape[0])    # 第一层激活函数    h1_activation = nd.relu(h1_conv)    # 第一层池化层    h1 = nd.Pooling(data=h1_activation,pool_type='max',kernel=(2,2),stride=(2,2))    # 第二层卷积层    h2_conv = nd.Convolution(data=h1,weight=W2,bias=b2,                kernel=W2.shape[2:],num_filter=W2.shape[0])    h2_activation = nd.relu(h2_conv)    h2 = nd.Pooling(data=h2_activation,pool_type='max',kernel=(2,2))    h2 = nd.flatten(h2)    # 第一层全连接    h3_linear = nd.dot(h2,W3) + b3    h3 = nd.relu(h3_linear)    # 第二层全连接    h4_linear = nd.dot(h3,W4) + b4    if verbose:        print('1st conv block:', h1.shape)        print('2nd conv block:', h2.shape)        print('1st dense:', h3.shape)        print('2nd dense:', h4_linear.shape)        print('output:', h4_linear)     return h4_linear# 定义精度计算def accuracy(output,label):    return nd.mean(output.argmax(axis=1) == label).asscalar()# 估计模型精度def evaluate_accuracy(data_iterator,net,ctx):    acc = 0    for data,label in data_iterator:        label = label.as_in_context(ctx)        output = net(data)        acc += accuracy(output,label)        return acc / len(data_iterator)# 优化器def SGD(params,lr):    for param in params:        param[:] = param - lr * param.grad# 训练softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()learning_rate = .2epochs = 5for epoch in range(epochs):    train_loss = 0.0    train_acc = 0.0    for data,label in train_data:        # 复制到GPU        label = label.as_in_context(ctx)        with mx.autograd.record():            output = net(data)            loss = softmax_cross_entropy(output,label)        loss.backward()        SGD(params,learning_rate / batch_size)        train_loss += nd.mean(loss).asscalar()        train_acc += accuracy(output,label)    test_acc = evaluate_accuracy(test_data, net, ctx)    print("Epoch %d. Loss: %f, Train acc %f, Test acc %f" % (epoch, train_loss/len(train_data),            train_acc/len(train_data), test_acc))

 

转载于:https://my.oschina.net/wujux/blog/1809882

你可能感兴趣的文章
移动终端开发_高端课程
查看>>
我的友情链接
查看>>
提交服务器汉字乱码解决方法
查看>>
Android SDK:构建一个购物中心搜索的应用(二)-Points of Interest
查看>>
关于android使用自己的launcher替换默认launcher的方法
查看>>
ASP.NET 4.5 MVC实战教程 MVC视频教程
查看>>
Excel编辑模块openpyxl的常用功能介绍
查看>>
通过TFTP服务器拷贝路由器的配置文件到本地
查看>>
南方电网广东公司荣获“IT用户最佳实践案例奖”
查看>>
Yesod - 数据库 (9)
查看>>
Java并发 wait()、notify()和notifyAll()
查看>>
生命在于折腾 ---- 写一个编辑器
查看>>
Mysql数据迁移至Redis
查看>>
java cpu监控
查看>>
Lync 小技巧-2-解决每次出现安装进度条的方法
查看>>
《企业云桌面实施》-小技巧-01-规划注意事项
查看>>
zabbix生产场景配置发送报警邮件
查看>>
passive-interface的含义
查看>>
监听独立于数据库服务器的配置,解决ORA-12520及ORA-12545错误
查看>>
[每日一题] OCP1z0-047 :2013-08-05 SELECT语句――列的表达式.....................................25...
查看>>