tensoflow练习3:卷积神经网络用于分类
来源:互联网 发布:易中天品三国 知乎 编辑:程序博客网 时间:2024/06/05 07:26
再次利用卷积神经网络对手写体进行识别;卷积神经网络是一种非常强大的神经网络模型(可应用于图像识别,语音处理等领域)。下面将给出源码:
#coding = utf-8import tensorflow as tfimport numpy as npfrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets('MNIST_data/',one_hot=True)n_output_layer = 10#定义待训练的神经网络def convoluional_neural_network(data): weights = {'w_conv1':tf.Variable(tf.random_normal([5,5,1,32])), 'w_conv2':tf.Variable(tf.random_normal([5,5,32,64])), 'w_fc':tf.Variable(tf.random_normal([7*7*64,1024])), 'out':tf.Variable(tf.random_normal([1024,n_output_layer])), } biases = { 'b_conv1':tf.Variable(tf.random_normal([32])), 'b_conv2':tf.Variable(tf.random_normal([64])), 'b_fc':tf.Variable(tf.random_normal([1024])), 'out':tf.Variable(tf.random_normal([n_output_layer])) } data = tf.reshape(data,[-1,28,28,1]) conv1 = tf.nn.relu(tf.add(tf.nn.conv2d(data,weights['w_conv1'],strides=[1,1,1,1],padding='SAME'),biases['b_conv1'])) #池化 conv1 = tf.nn.max_pool(conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') #第二层 conv2 = tf.nn.relu(tf.add(tf.nn.conv2d(conv1,weights['w_conv2'],strides=[1,1,1,1],padding='SAME'),biases['b_conv2'])) conv2 = tf.nn.max_pool(conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') #全连接层 fc = tf.reshape(conv2,[-1,7*7*64]) fc = tf.nn.relu(tf.add(tf.matmul(fc,weights['w_fc']),biases['b_fc']))#输出节点为1024 #dropout 可选 output = tf.add(tf.matmul(fc,weights['out']),biases['out']) return outputbatch_size = 100X= tf.placeholder('float',[None,28*28])Y= tf.placeholder('float')def train_neural_network(X, Y): predict = convoluional_neural_network(X) cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(predict,Y)) optimizer = tf.train.AdamOptimizer().minimize(cost_func) epochs = 13 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) epoch_loss = 0 print('training begins:') for epoch in range(epochs): for i in range(int(mnist.train.num_examples/batch_size)): x, y = mnist.train.next_batch(batch_size) _, c = sess.run([optimizer,cost_func],feed_dict={X:x,Y:y}) epoch_loss += c print(epoch, ' : ', epoch_loss) correct = tf.equal(tf.argmax(predict,1),tf.argmax(Y,1)) accuracy = tf.reduce_mean(tf.cast(correct,'float')) print('精确率:', accuracy.eval({X:mnist.test.images,Y:mnist.test.labels}))train_neural_network(X,Y)
相同部分就不加描述了,这里主要讲一下卷积过程;
(1)第一次卷积与池化:(特征个数从1变为32)
conv1 = tf.nn.relu(tf.add(tf.nn.conv2d(data,weights['w_conv1'],strides=[1,1,1,1],padding='SAME'),biases['b_conv1'])) #池化 conv1 = tf.nn.max_pool(conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
padding=’SAME’时,表示0边距;strides=[1,1,1,1]:表示1步长;卷积后尺寸不变;然后池化操作变为14*14;
(2)第二次卷积与池化将尺寸变为7*7;(32变为64)
#第二层 conv2 = tf.nn.relu(tf.add(tf.nn.conv2d(conv1,weights['w_conv2'],strides=[1,1,1,1],padding='SAME'),biases['b_conv2'])) conv2 = tf.nn.max_pool(conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding
(3)全连接
fc = tf.reshape(conv2,[-1,7*7*64])fc=tf.nn.relu(tf.add(tf.matmul(fc,weights['w_fc']),biases['b_fc']))#输出节点为1024
结果:
阅读全文
0 0
- tensoflow练习3:卷积神经网络用于分类
- 用于高光谱图像分类的深度卷积神经网络
- 使用卷积神经网络进行图片分类 3
- 卷积神经网络用于图像识别
- 3用于MNIST的卷积神经网络-3.3简单卷积神经网络的训练和评估会话
- tensoflow练习2:利用Recurrent Neural Network 进行分类
- 一个缩减版的inception卷积神经网络用于mnist数据集分类
- TensorFlow实现用于图像分类的卷积神经网络(代码详细注释)
- 卷积神经网络CNN-目标分类
- Matlab:BP神经网络,用于分类
- pytorch创建神经网络用于分类
- 用于视觉识别的卷积神经网络
- tensorflow笔记:卷积神经网络用于MNIST识别
- 用于图像处理的卷积神经网络
- 3用于MNIST的卷积神经网络-3.1/3.2简单卷积神经网络的计算图设计(上/下)
- 4用于cifar10的卷积神经网络-4.1用于cifar10的卷积神经网络结构设计
- 从线性分类器到卷积神经网络
- 使用卷积神经网络进行图片分类 1
- android scrollview 滚动到指定位置
- java集合类内容总结
- HTML入门笔记21-HTML定义列表
- Notice: Undefined variable: xxxxx.php on line nn [php]
- Aladdin and the Flying Carpet Light OJ 1341
- tensoflow练习3:卷积神经网络用于分类
- 图解修改Maven仓库下载到本地jar包默认存储位置
- usaco 3.2 Magic Squares
- linux驱动复习之阻塞与非阻塞的IO操作 [一]
- 图论 邻接表建图+dfs
- [leetcode]211. Add and Search Word
- 并查集初步
- Unity3D之UI按键绑定事件(二)
- H