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. 概述 卷积神经网络是一种特殊的深层的神经网络模型,它的特殊性体现在两个方面,一方面它的神经元间的连接是非全连接的, 另一方面同一层中某些神经元之间的连接的权重是共享的(即相同的).它的非全连接和 ...
随机推荐
- gcc -o test test.c编译报错
报错内容 /tmp/cc7eQyD4.o: In function `main':test.c:(.text+0x51): undefined reference to `sqrt'collect2: ...
- iOS7,iOS8和iOS9的区别
iOS7,iOS8和iOS9的区别:iOS7.0 1.iOS 7是iOS面世以来在用户界面上做出改变最大的一个操作系统.iOS 7抛弃了以往的拟物化设计,而采用了扁平化设计. 苹果在重新思考 iOS ...
- day 2Linux软件从主机安装到服务器和安装JDK软件
软件安装 1.如何上传安装包到服务器**可以使用图形化工具,如: filezilla**可以使用sftp工具: alt+p 调出后,用put命令上传上传(如果不cd指定目录,则上传到当前用户的主目录) ...
- Jmeter参数跨线程组传递
1.利用BeanShell, 请求==>后置==>beanshellpostprocessorScripts内写:props.put("user_name"," ...
- git add -A、git add -u、git add .区别
git add各命令及缩写 git add各命令 缩写 git add --all git add -A git add --update git add -u git add . Git Versi ...
- uml 知识点
Unified Modeling Language (UML)又称统一建模语言或标准建模语言
- bzoj 3622 已经没有什么好害怕的了——二项式反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3622 令 f[i] 表示钦定 i 对 a[ ]>b[ ] 的关系的方案数:g[i] 表 ...
- Python——包
包 —— 把解决一类问题的模块放在同一个文件夹里 包的导入 import 和 from ... import 都行 导入之前:凡是带点的,点的左边都必须是包 导入之后:点的左边可以是包.模块.函数.类 ...
- 【jmeter】Jmeter启动GUI界面出错
今天要用Jmeter测试服务器性能,发现GUI界面总是有warning提示: WARNING: Could not open/create prefs root node Software\JavaS ...
- SQL的datetime类型数据转换为字符串格式大全
Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE( ...