首页 > 编程知识 正文

卷积神经网络纯python代码,mnist数据集怎么用

时间:2023-05-06 04:52:08 阅读:127468 作者:1086

1 .使用要点介绍: http://www.Sina.com/: sqdzdj http://www.Sina.com/: (op ),专门用于运算的操作节点,所有操作均为一个op http://www.ssww

获取调用:

(1) tf.get_default_graph ) ) ) )。

)2) op、sess或tensor的图形属性

3358www.Sina.com/:会话:提供运算程序图tensor占位符,run时为operation指定参数http://www.Sina.com 4 )通过直接修改2fzdhb列表graph:元中的数据http://www.Sina.com/http://www.Sina.com/:新的sqdzdj数据3358 www 不能跨维进行修改。 sqdzdj、初始形状、tf.Tensor.get_shape (获取静态形状)、tf.Tensor.set_shape:更新Tensor对象的静态形状3358www.Tensor )和Sinsor 可以创建新形状,以描述原始sqdzdj正在运行的形状(动态变化)。 tf.reshape:创建具有不同动态形状的新sqdzdj http://www.Sina.com/https://www.tensor flow.org/verg

卷积层、激活函数、池化层、全部连接层

部分大型网络包括session:提取特征映射时卷积核的行为placeholder

1.SAME )跨边缘采样,采样面积与输入图像像素宽度一致

2.VALID:不跨边缘采样,采样面积输入图像像素宽度feed_dict

卷积不是为了减少数据量,而是添加激活函数(sigmoid,relu )才进行过滤。 池化层用于减少参数量。

学习率不需要搜索,很可能引起梯度爆炸

增加激活函数:提高网络非线性分割能力

使用sigmoid等函数通过反向传播求出误差梯度时,计算首先,量相对较大,而使用Relu激活函数,可以大大节约整个过程的计算量

第二,在深层网络中,当sigmoid函数反向传播时,容易发生梯度爆炸Name

主要作用:特征提取通过去除特征图中不重要的样本来减少参数量

最常用的是maxpooling ()、

TF.nn.max_pool(value,ksize=,strides=,padding=,name=None ) )。

value:4-D Tensor形状[batch,height,width,channels]

ksize:池化窗口大小,[1,ksize,ksize,1]

strides:步长,[1,strides,strides,1]

padding:“SAME "、" VALID "、Shape

前卷积和池化相当于进行特征工程,后全连接相当于进行特征加权,最后全连接在整个卷积神经网络中充当“分类器”。reshape

importtensorflowastffromtensorflow.examples.tutorials.mnistimportinput _ datafromtensorflow _ core.python.trainining stddev=1.0,定义用于初始化权重的函数defweight_variables(shape ) :w=TF.compat.V1.variable ) TF.Random_normal_iii

t.v1.Variable(tf.constant(0.0, shape=shape)) return bdef model(): """自定义卷积模型 :return: """ #准备数据的占位符 x [None, 784] y_true [None,10] with tf.variable_scope("data"): x = tf.compat.v1.placeholder(tf.float32, [None, 784]) y_true = tf.compat.v1.placeholder(tf.int32, [None, 10]) #2.一卷积层 卷积:5*5*1,32个 strides=1 激活 tf.nn.relu 池化 with tf.variable_scope("conv1"): #随机初始化权重,偏置32 w_conv1= weight_variables([5,5,1,32]) b_conv1= bias_variables([32]) #对x进行形状的改变[None, 784] [None ,28,28,1] x_reshape = tf.reshape(x,[-1,28,28,1]) #[None,28,28,1]---->[None,28,28,32] x_relu1 = tf.nn.relu(tf.nn.conv2d(x_reshape, w_conv1,strides=[1,1,1,1],padding="SAME")+b_conv1) #池化 2*2,strides2 [None,28,28,32]--->[None,14,14,32] x_pool1 = tf.nn.max_pool(x_relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME" ) #3.二卷积层 5*5*32 64个filter,strides=1 激活:tf.nn.relu 池化 with tf.variable_scope("conv2"): #随机初始化权重,偏置 w_conv2 = weight_variables([5,5,32,64]) b_conv2 = bias_variables([64]) #卷积,激活,池化计算 #[None, 14, 14, 32]---->[None, 14, 14,64] x_relu2 = tf.nn.relu(tf.nn.conv2d(x_pool1, w_conv2 ,strides=[1,1,1,1],padding="SAME")+b_conv2) #池化 2*2,strides 2,【None,14,14,64】--》【None,7,7,64】 x_pool2 = tf.nn.max_pool(x_relu2,ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME") #4.全连接层[None,7,7,64]--->[None,7*7*64]*[7*7*64,10]+[10] = [None, 10] with tf.variable_scope("conv2"): #随机初始化权重和偏置 w_fc=weight_variables([7*7*64,10]) b_fc = bias_variables([10]) # 修改形状 【None,7,7,64】---》【None,7*7*64】 x_fc_reshape = tf.reshape[x_pool2,[-1, 7*7*64]] #进行矩阵运算得出每个样本的10个结果 y_predict = tf.matmul(x_fc_reshape, w_fc)+b_fc return x,y_true,y_predictdef conv_fc(): #获取真实的数据 mnist = input_data.read_data_sets("./data/mnist/input_data/", one_hot=True) #定义模型,得出输出 x,y_true, y_predict = model() #进行交叉熵损失计算,然后求平均值 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict)) #梯度下降求出损失 with tf.variable_scope("optimizer"): train_op = tf.compat.v1.train.GradientDesentOptimizer(0.0001).minimize(loss) #5.计算准确率 with tf.variable_scope("acc"): equal_list = tf.equal(tf.argmax(y_true,1), tf.argmax(y_predict,1)) #equal_list None个样本[1,0,1,0,1,1,.........] accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32)) #定义一个初始化变量的op init_op = tf.compat.v1.global_variables_initializer() #开启会话运行 with tf.compat.v1.Session() as sess: sess.run(init_op) #循环去训练 for i in range(1000): #取出真实存在的特征值和目标值 mnist_x,mnist_y = mnist.train.next_batch(50) #运行train_op训练 sess.run(train_op, feed_dict={x: mnist_x, y_true:mnist_y}) print("训练第%d步,准确率:%f"%(i, sess.run(accuracy, feed_dict={x:mnist_x, y_true:mnist_y})))if __name__=="__main__": full_connected()

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。