convolution-卷积神经网络
训练mnist数据集
结构组成:
input_image --> convolution1 --> pool1 --> convolution2 --> pool2 --> full_connecion1 --> full_connection2
# 卷积
import tensorflow as tf import input_data # 加载mnist数据集
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) # 构建多层卷积网络
# 权重及偏置初始化, ReLU神经元 用一个较小的正数来初始化偏置项来打破对称性以及避免0梯度
def weight_variable(shape):
"""
:param shape:二维tensor,第一个维度代表层中权重变量所连接(connect from)的单元数目,
第二个维度代表层中权重变量所连接(connect to)到的单元数量
:return: W
"""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial) def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial) # 卷积及池化
def conv2d(x, W):
"""
卷积
:param x:
:param W:
:return:
"""
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME") def max_pool_2x2(x):
"""
最大池化
:param x:
:return:
"""
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") def compute_accuracy(v_xs, v_ys):
""" 计算的准确率 """
global prediction # prediction value
y_pre = sess.run(prediction, feed_dict={xs: v_xs})
# 与期望的值比较 bool
correct_pre = tf.equal(tf.argmax(y_pre, 1), tf.argmax(ys, 1))
# 将bools转化为数字
accuracy = tf.reduce_mean(tf.cast(correct_pre, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})
return result # 数据图片
xs = tf.placeholder("float", shape=[None, 784]) # size 28 * 28 =784
# 预期概率
ys = tf.placeholder("float", shape=[None, 10]) # 10: 矩阵维度(分类)
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs, [-1, 28, 28, 1]) # -1: 任意数量的图片; 28*28:图片的长宽; 1:灰色图片为1 # layer1
W_conv1 = weight_variable([5, 5, 1, 32]) # 5*5:patch过滤长宽, 1:起始输入一张图片, 32:out_size
b_conv1 = bias_variable([32]) # 32:上层输入的out_size
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # output_size=28*28*32
h_pool1 = max_pool_2x2(h_conv1) # output_size=14*14*32 pool_strdes=2 # layer2
W_conv2 = weight_variable([5, 5, 32, 64]) # 64是训练中不断增加的高度,自定义
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # output_size=14*14*64
h_pool2 = max_pool_2x2(h_conv2) # output_size=7*7*64 池化的步长为[2,2] # func1 layer
W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) # 将pool2铺平为7*7*64
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # 矩阵相乘
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # 防止过拟合 # func2 layer
W_fc2 = weight_variable([1024, 10]) # 传入的1024, 判断0-9的数字one-hot,10来代表每个数字
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) # softmax分类 # the loss between prediction and really
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1]))
tf.summary.scalar('loss', cross_entropy) # 字符串类型的标量张量,包含一个Summaryprotobuf 1.1记录标量
# training
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) # AdamOptimizer使用复杂模型 sess = tf.Session()
sess.run(tf.initialize_all_variables()) # start training
for i in range(1000):
batch_x, batch_y = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_x, ys: batch_y, keep_prob: 0.1})
if i % 50 == 0:
print(compute_accuracy(mnist.test.images, mnist.test.labels))
print("Training Finished !!!")
convolution-卷积神经网络的更多相关文章
- Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1
3.Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1 http://blog.csdn.net/sunbow0 ...
- 深度学习:卷积神经网络(convolution neural network)
(一)卷积神经网络 卷积神经网络最早是由Lecun在1998年提出的. 卷积神经网络通畅使用的三个基本概念为: 1.局部视觉域: 2.权值共享: 3.池化操作. 在卷积神经网络中,局部接受域表明输入图 ...
- 深度学习方法(十一):卷积神经网络结构变化——Google Inception V1-V4,Xception(depthwise convolution)
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.机器学习技术感兴趣的同学加入. 上一篇讲了深度学习方法(十) ...
- Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.2
3.Spark MLlib Deep Learning Convolution Neural Network(深度学习-卷积神经网络)3.2 http://blog.csdn.net/sunbow0 ...
- Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.3
3.Spark MLlib Deep Learning Convolution Neural Network(深度学习-卷积神经网络)3.3 http://blog.csdn.net/sunbow0 ...
- Deep Learning模型之:CNN卷积神经网络(一)深度解析CNN
http://m.blog.csdn.net/blog/wu010555688/24487301 本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep le ...
- Caffe(卷积神经网络框架)介绍
Caffe(卷积神经网络框架)Caffe,全称Convolution Architecture For Feature Extraction caffe是一个清晰,可读性高,快速的深度学习框架.作者是 ...
- Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现(转)
Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现 zouxy09@qq.com http://blog.csdn.net/zouxy09 自己平时看了一些论文, ...
- 卷积神经网络CNN介绍:结构框架,源码理解【转】
1. 卷积神经网络结构 卷积神经网络是一个多层的神经网络,每层都是一个变换(映射),常用卷积convention变换和pooling池化变换,每种变换都是对输入数据的一种处理,是输入特征的另一种特征表 ...
- 卷积神经网络(CNN)
1. 概述 卷积神经网络是一种特殊的深层的神经网络模型,它的特殊性体现在两个方面,一方面它的神经元间的连接是非全连接的, 另一方面同一层中某些神经元之间的连接的权重是共享的(即相同的).它的非全连接和 ...
随机推荐
- [转载]redis持久化的两种操作RDB和AOF
Redis 持久化: 提供了多种不同级别的持久化方式:一种是RDB,另一种是AOF. RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AO ...
- QQ在线客服的使用
<a target="_blank" href="http://wpa.qq.com/msgrd?v=1&uin=2804010556&site=a ...
- (精)AVL树旋转共8种情况(涵盖所有考研的范围)
- BMP、GIF、JPEG、PNG以及其它图片格式简单介绍
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/songjinshi/article/details/37516649 BMP格式 BMP是英文Bit ...
- hadoop之 reduce个数控制
1.参数变更1.x 参数名 2.x 参数名 mapred.tasktracker.red ...
- 51nod 1934 受限制的排列——笛卡尔树
题目:http://www.51nod.com/Challenge/Problem.html#!#problemId=1934 根据给出的信息,可以递归地把笛卡尔树建出来.一个点只应该有 0/1/2 ...
- 打开Visual Studio 2012的解决方案 连接 Dynamics CRM 2011 的Connect to Dynamics CRM Server 在其工具下没有显示
一.使用TFS 代码管理,发现Visual Studio 2012 菜单栏 工具下的Connect to Dynamics CRM Server 没有显示. 平常打开VS下的工具都会出现Connect ...
- Mono.Cecil 修改目标.NET的IL代码保存时报异常的处理。
使用Mono.Cecil对目标.NET的DLL程序进行IL修改后保存时报“Failed to resolve assembly: ' xxxxxx, version=xxxxx,Culture=xxx ...
- 如何更改tomcat7及以上版本内存设置
http://jingyan.baidu.com/article/295430f1c22a940c7e0050fb.html?qq-pf-to=pcqq.c2c 当在tomcat的webapps文件夹 ...
- 禁止Grid、TreeGrid列排序和列菜单
Ext的Grid和Treegrid默认提供列菜单的功能,在列菜单中可以进行排序以及控制列显示状态. 在实际项目中,往往有些列是不需要用户看到的,因此就必须屏蔽列菜单的功能. 1.屏蔽Grid,包括Ed ...