卷积神经网络lenet-5详解,卷积神经网络alexnet

  卷积神经网络lenet-5详解,卷积神经网络alexnet

  该程序采用张量流一维卷积神经网络实现手写数字识别数据集的数字识别,图像适合二维卷积。这里只是想试试一维卷积的操作方法。

  # 2021年11月8日张量流一维卷积神经网络实现了手写数字识别数据集的数字识别,只是想尝试一维卷积的操作方法#mnist到样本来自_ _ futuuure python3的函数importtensorflowastfimportmatplotlib。pyplotaspltimportnumpyasnp #矩阵操作库#是打印_功能#也可以在python2中使用importmnistdatafromtensorflow。例子。教程。mnistimportinput _ datamnist=input _ data。阅读_ DDD从训练集中选择前数字个数据def train _ size(num):print( totaltrainingimageindataset= str)mnist。train)es[333666]3360)print(x _ trainingimageindataset=(str)x _ train。shape))y _ train=mnist。火车。labels 3360)print(y _ trainingimageindataset= str)y _ train。shape))print)“”)return x _ train,y _火车#从测试集中开始的第一个数字个数据def test _ size(num):print( totaltestexamplesindataset= str)mnist。测试。图像。美国东部时间。图像[:non]3360)打印(x _ test examples loaded=(str)x _ test。shape))y _ test=Mn ist。测试。)3360)print(y _ test examples loaded= str)y _ test。形状))返回x _测试,y _测试#表示训练集中的第数字个数据,def display _ digit(num):print(y _ train[num])label=y _ train[num显示为图像28)工厂名称(示例:% d标签:% d)(编号,标签) )随机读取的图像的编号PLT.imshow)图像,cmap=PLT。getcmap(gray _ r))PLT。显示)#指示训练集中第一个开始到第一个停止的数据,每一列都是示例defdisplay_mult_flat stop ) : #)拼接images=NP.concatenate ) (images,x_train[I].整形)(1,784 cmap=PLT。get _ cmap)y _ train=train _ size)55000)选择55000个训练集数据x_train.shape[0]张照片display_mult_flat(0为随机400)参数learning _ rate=0.001 training _ iters=500 #训练次数batch_size=256 #批量训练样本大小display _ sters 28)n _ classes=10 # mnisttotalclasses(0-9数字)压差=0.85 #压差,比例输出

  =tf.placeholder(tf.float32,[None,n _ classes])keep _ prob=TF。占位符(TF。浮动32)#定义一个输入为x,权值为w,偏置为b,给定步幅的卷积层,激活函数是填充设为萨姆姆模式def conv2d(x,w,b,跨距=1): x=tf.nn.conv2d(x,w,跨距=[1,跨距,跨距,1],padding= SAME )x=TF。nn。bias _ add(x,b) return tf.nn.relu(x)#定义一个输入是x的最大池层,卷积核为ksize并且填料为SAMEdef maxpool2d(x,k=2): return tf.nn.max_pool(x,ksize=[1,k,k,1],strides=[1,k,k,1],padding=SAME)#定义卷积神经网络,其构成是两个卷积层,一个德鲁普层,最后是输出层def conv_net(x,权重,偏差,辍学):#重塑输入图片x=TF。shape(x,shape=[-1,784,1,1])#因为采用一维卷积,因此x的形状是向量形式#第一卷积层conv1=conv2d(x,权重[wc1],偏置[ bc1 ])#用于降采样的最大池conv1=maxpool2d(conv1,k=2)#第二卷积层conv2=conv2d(conv1,权重[wc2],偏置[ bc2 ])#用于降采样的最大池conv2=maxpool2d(conv2,k=2)#整形conv2输出以匹配全连接层fc1=tf的输入。整形(conv2,[-1,权重[wd1]).get_shape().as _ list()[0]])#全连接层fc1=tf.add(tf.matmul(fc1,weights[wd1]),bias[ bd1 ])fc1=TF。nn。relu(fc1)#全连接层fc2=tf.add(tf.matmul(fc1,weights[wd2]),bias[ bd2 ])fc2=TF。nn。relu(fc2)# Dropout # fc1=TF。nn。nn。辍学(fc1定义网络层的权重和偏置,第一个卷积神经层有一个5*5的卷积核,一个输入和32个输出。第二个#conv层有一个5*5的卷积核,32个输入和64个输出。全连接层有1024个输入和10个输出对应于最后#的数字数目。所有的权重和偏置用随机正常分布完成初始化:权重={ #5*5 conv,1个输入,32个输出wc1:tf .变量(tf.random_normal([5,1,1,32]),#5*5 conv,32个输入和64个输出 wc2:tf .变量(tf.random_normal([5,1,32,64])),#全连接,7*7*64个输入,1024个输出wd1:tf .变量(tf.random_normal([196*64,1024]),“wd2”:TF .变量(tf.random_normal([1024,1024])),# 1024个输入,10个用于类数字"出去"的输出:tf .变量(TF。random _ normal([1024,n _ classes])} bias={ bc1 :TF .变量(tf.random_normal([32])),“bc2”:TF .变量(tf.random_normal([64])), bd1:tf .变量(tf.random_normal([1024])), bd2:tf .变量(tf.random_normal([1024])), out:tf .变量(TF。random _ normal([n _ classes])} #建立一个给定权重和偏置的convnet。定义基于交叉熵与逻辑的损失函数,并用圣经》和《古兰经》传统中)亚当(人类第一人的名字优化器进行损失最小化。

  #优化后,计算精度:pred=conv _网(x,权重,偏差,keep_prob)成本=TF。reduce _均值(TF。nn。软max _ cross _ entropy _ with _ logits(logits=pred,labels=y))优化器=TF。火车。adamoptimizer(learning _ rate=learning _ rate).最小化(成本)正确_预测=TF。相等(TF。arg max(pred,1),tf.argmax(y,1))accu ray=TF。reduce _均值(TF。cast(正确_预测,TF。float 32))init=TF。全局变量初始值设定项()#启动计算图,并迭代train _ iterats次,其中每次输入批量大小个数据进行优化,请注意,用从手写数字识别数据集分离出的#mnist.train数据进行训练,每进行显示_步骤次迭代,会计算当前的精度,最后,在2048个测试图片上计算精度,#此时无辍学train _ loss=[]train _ ACC=[]test _ ACC=[]带tf .sess:sess。run(init)step=1,而step=training _ iters:batch _ x,batch _ y=mnist。火车。下一个批处理(批处理大小)会话。如果step % display _ step==0:Loss _ train,acc_train=sess.run([cost,accuray),feed_dict={x: batch_x,y:batch_y,keep}) print(Iter str(step),Minibatch Loss= {:2f} .format(loss_train) \ ,训练精度={:2f} .格式(ACC _ train))#计算2048张测试图像的精度。#请注意,在这种情况下,没有丢失acc_test=sess.run(accuray,feed_dict={x: mnist.test.images,y: mnist.test.labels,keep_prob: 1 .})打印(测试精度:{:2f} .格式(ACC _ Test))train_loss。追加(Loss _ Train)Train _ ACC。附加(ACC _ Train)测试_ ACC。append(ACC _ Test)step=1 eval _ indexes=range(0,training_iters,display_step)#Plot随时间的损失PLT。Plot(eval _ indexes,Train _ Loss, k-)plt.title(每次迭代的软最大损失)plt.xlabel(迭代)plt.ylabel(软最大损失)plt.show()#Plot训练和测试精度plt .绘图

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

相关文章阅读

  • python 残差,残差神经网络是cnn
  • python 残差,残差神经网络是cnn,Python深度学习神经网络残差块
  • 卷积神经网络的基本结构,卷积神经网络的结构
  • 基于图神经网络,dqn神经网络
  • 神经网络心得体会,简单循环神经网络
  • 深度卷积神经网络与卷积神经网络的区别,卷积神经网络简单理解
  • 基于空间的图卷积神经网络,卷积神经网络每一层通过数据变换实现图像的
  • java神经网络框架,神经网络模型有哪几种,java神经网络框架,神经网络模型有哪几种形式
  • 人工神经网络算法与机器算法,人工神经网络算法的应用
  • 怎样做神经网络分类器检测,神经网络 分类器
  • 卷积神经网络是一种常用来处理,卷积神经网络的基本概念
  • 社交网络分析模型,python建立神经网络模型
  • 神经网络 dnn,神经网络的梯度
  • bp网络神经预测模型,bp神经网络实例分析
  • 神经网络反向传播,反向传播神经网络基本原理
  • 留言与评论(共有 条评论)
       
    验证码: