『TensorFlow』读书笔记_AlexNet
网络结构

创新点
Relu激活函数:效果好于sigmoid,且解决了梯度弥散问题
Dropout层:Alexnet验证了dropout层的效果
重叠的最大池化:此前以平均池化为主,最大池化避免了平均池化的模糊效果;而重叠的池化步长和池化窗口大小设置增加了特征的丰富性
LRN层:局部响应归一化层,这个层能够使得响应大的神经元变得相对更大,其他的收到抑制,理论上增加了泛化能力,不过实际上在大大增加了训练时间的同时并没有对效果有明显的提升,故除了AlexNet外基本没有使用;提出原因在于Relu函数输出不想之前的tanh和sigmoid有一个有限值域,所以LRN加以限制,似乎作用并不明显
代码实现
# Author : Hellcat
# Time : 2017/12/8 import math
import time
import tensorflow as tf
from datetime import datetime batch_size = 32
num_batches = 100 def print_activations(t):
'''
查看tensor信息
:param t: tensor对象
:return: 隶属节点的名称,张量本身尺寸
'''
print(t.op.name, ' ', t.get_shape().as_list()) def inference(images):
parameters = [] with tf.name_scope('conv1') as scope:
kernel = tf.Variable(tf.truncated_normal([11,11,3,64],
dtype=tf.float32, stddev=1e-4),
name='weights')
conv = tf.nn.conv2d(images, kernel,[1,4,4,1],padding='SAME')
biases = tf.Variable(tf.constant(0.,shape=[64],dtype=tf.float32),
trainable=True,name='biases')
bias = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.relu(bias,name=scope)
print_activations(conv1)
parameters += [kernel,biases]
lrn1 = tf.nn.lrn(conv1,4,bias=1.,alpha=0.001/9,beta=0.75,name='lrn1')
pool1 = tf.nn.max_pool(lrn1,ksize=[1,3,3,1],strides=[1,2,2,1],
padding='VALID',name='pool1')
print_activations(pool1) with tf.name_scope('conv2') as scope:
kernel = tf.Variable(tf.truncated_normal([5,5,64,192],
dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(pool1,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.,shape=[192],dtype=tf.float32),
trainable=True,name='biases')
bias = tf.nn.bias_add(conv,biases)
conv2 = tf.nn.relu(bias,name=scope)
print_activations(conv2)
parameters += [kernel,biases]
lrn2 = tf.nn.lrn(conv2,4,bias=1.,alpha=0.001/9,beta=0.75,name='lrn2')
pool2 = tf.nn.max_pool(lrn2,ksize=[1,3,3,1],strides=[1,2,2,1],
padding='VALID',name='pool2')
print_activations(pool2) with tf.name_scope('conv3') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,192,384],
dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(pool2,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.,shape=[384],dtype=tf.float32),
trainable=True,name='biases')
bias = tf.nn.bias_add(conv,biases)
conv3 = tf.nn.relu(bias,name=scope)
parameters += [kernel,biases]
print_activations(conv3) with tf.name_scope('conv4') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,384,256],
dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(conv3,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.,shape=[256],dtype=tf.float32),
trainable=True,name='biases')
bias = tf.nn.bias_add(conv,biases)
conv4 = tf.nn.relu(bias,name=scope)
parameters += [kernel,biases]
print_activations(conv4) with tf.name_scope('conv5') as scope:
kernel = tf.Variable(tf.truncated_normal([3,3,256,256],
dtype=tf.float32,stddev=1e-1),name='weights')
conv = tf.nn.conv2d(conv4,kernel,[1,1,1,1],padding='SAME')
biases = tf.Variable(tf.constant(0.,shape=[256],dtype=tf.float32),
trainable=True,name='biases')
bias = tf.nn.bias_add(conv,biases)
conv5 = tf.nn.relu(bias,name=scope)
parameters += [kernel,biases]
print_activations(conv5)
pool5 = tf.nn.max_pool(conv5, ksize=[1,3,3,1],
strides=[1,2,2,1],padding='VALID',name='pool5')
print_activations(pool5) with tf.name_scope('fcl1') as scope:
reshape = tf.reshape(pool5,[batch_size,-1])
dim = reshape.get_shape()[1].value
weights = tf.Variable(tf.truncated_normal([dim,4096],stddev=0.001),dtype=tf.float32)
biases = tf.Variable(tf.constant(0.1,shape=[4096]),dtype=tf.float32)
fcl1 = tf.nn.relu(tf.nn.bias_add(tf.matmul(reshape,weights),biases))
fcl1 = tf.nn.dropout(fcl1,keep_prob=0.75)
parameters += [weights,biases]
print_activations(fcl1) with tf.name_scope('fcl2') as scope:
weights = tf.Variable(tf.truncated_normal([4096,4096],stddev=0.001),dtype=tf.float32)
biases = tf.Variable(tf.constant(0.1,shape=[4096]),dtype=tf.float32)
fcl2 = tf.nn.relu(tf.nn.bias_add(tf.matmul(fcl1,weights),biases))
fcl2 = tf.nn.dropout(fcl2,keep_prob=0.75)
parameters += [weights,biases]
print_activations(fcl2) with tf.name_scope('fcl3') as scope:
weights = tf.Variable(tf.truncated_normal([4096,10],stddev=0.001),dtype=tf.float32)
biases = tf.Variable(tf.constant(0.1,shape=[10]),dtype=tf.float32)
fcl3 = tf.nn.bias_add(tf.matmul(fcl2,weights),biases)
parameters += [weights,biases]
print_activations(fcl3) return fcl3, parameters def time_tensorflow_run(session, target, info_string):
'''
网路运行时间测试函数
:param session: 会话对象
:param target: 运行目标节点
:param info_string:提示字符
:return: None
'''
num_steps_burn_in = 10 # 预热轮数
total_duration = 0.0 # 总时间
total_duration_squared = 0.0 # 总时间平方和
for i in range(num_steps_burn_in + num_batches):
start_time = time.time()
_ = session.run(target)
duration = time.time() - start_time # 本轮时间
if i >= num_steps_burn_in:
if not i % 10:
print('%s: step %d, duration = %.3f' %
(datetime.now(),i-num_steps_burn_in,duration))
total_duration += duration
total_duration_squared += duration**2 mn = total_duration/num_batches # 平均耗时
vr = total_duration_squared/num_batches - mn**2
sd = math.sqrt(vr)
print('%s:%s across %d steps, %.3f +/- %.3f sec / batch' %
(datetime.now(), info_string, num_batches, mn, sd)) def run_benchmark():
with tf.Graph().as_default():
image_size = 224
images = tf.Variable(tf.random_normal([batch_size,image_size,image_size,3],
dtype=tf.float32,
stddev=1e-1))
target_layer,parameters = inference(images) init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) time_tensorflow_run(sess,target_layer,'Forward')
objective = tf.nn.l2_loss(target_layer) # 模拟目标函数
grad = tf.gradients(objective, parameters) # 梯度求解
time_tensorflow_run(sess, grad,'Forward-backward') # 反向传播
# 参数更新并未计入时耗中 if __name__=='__main__':
run_benchmark()
输出结果
conv1 [32, 56, 56, 64]
conv1/pool1 [32, 27, 27, 64]
conv2 [32, 27, 27, 192]
conv2/pool2 [32, 13, 13, 192]
conv3 [32, 13, 13, 384]
conv4 [32, 13, 13, 256]
conv5 [32, 13, 13, 256]
conv5/pool5 [32, 6, 6, 256]
fcl1/Relu [32, 4096]
fcl2/Relu [32, 4096]
fcl3/BiasAdd [32, 10]
2017-12-11 15:54:04.515831: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2017-12-11 15:54:25.862993: step 0, duration = 1.664
2017-12-11 15:54:42.982326: step 10, duration = 1.522
2017-12-11 15:54:59.837156: step 20, duration = 1.514
2017-12-11 15:55:14.968393: step 30, duration = 1.482
2017-12-11 15:55:29.901042: step 40, duration = 1.488
2017-12-11 15:55:44.832427: step 50, duration = 1.481
2017-12-11 15:55:59.978011: step 60, duration = 1.533
2017-12-11 15:56:15.705833: step 70, duration = 1.557
2017-12-11 15:56:32.164702: step 80, duration = 2.052
2017-12-11 15:56:49.980907: step 90, duration = 1.985
2017-12-11 15:57:06.628665:Forward across 100 steps, 0.163 +/- 0.492 sec / batch
2017-12-11 15:58:31.174363: step 0, duration = 7.852
2017-12-11 15:59:49.693544: step 10, duration = 7.901
2017-12-11 16:01:16.828232: step 20, duration = 8.463
2017-12-11 16:02:40.500340: step 30, duration = 6.796
2017-12-11 16:03:45.091418: step 40, duration = 6.690
2017-12-11 16:04:50.500747: step 50, duration = 5.797
2017-12-11 16:05:48.492420: step 60, duration = 5.754
2017-12-11 16:06:46.093358: step 70, duration = 5.737
2017-12-11 16:07:43.135421: step 80, duration = 5.704
2017-12-11 16:08:40.122760: step 90, duration = 5.626
2017-12-11 16:09:30.885931:Forward-backward across 100 steps, 0.663 +/- 2.016 sec / batch
『TensorFlow』读书笔记_AlexNet的更多相关文章
- 『TensorFlow』读书笔记_降噪自编码器
『TensorFlow』降噪自编码器设计 之前学习过的代码,又敲了一遍,新的收获也还是有的,因为这次注释写的比较详尽,所以再次记录一下,具体的相关知识查阅之前写的文章即可(见上面链接). # Aut ...
- 『TensorFlow』读书笔记_VGGNet
VGGNet网络介绍 VGG系列结构图, 『cs231n』卷积神经网络工程实践技巧_下 1,全部使用3*3的卷积核和2*2的池化核,通过不断加深网络结构来提升性能. 所有卷积层都是同样大小的filte ...
- 『TensorFlow』读书笔记_ResNet_V2
『PyTorch × TensorFlow』第十七弹_ResNet快速实现 要点 神经网络逐层加深有Degradiation问题,准确率先上升到饱和,再加深会下降,这不是过拟合,是测试集和训练集同时下 ...
- 『TensorFlow』读书笔记_SoftMax分类器
开坑之前 今年3.4月份的时候就买了这本书,同时还买了另外一本更为浅显的书,当时读不懂这本,所以一度以为这本书很一般,前些日子看见知乎有人推荐它,也就拿出来翻翻看,发现写的的确蛮好,只是稍微深一点,当 ...
- 『TensorFlow』读书笔记_多层感知机
多层感知机 输入->线性变换->Relu激活->线性变换->Softmax分类 多层感知机将mnist的结果提升到了98%左右的水平 知识点 过拟合:采用dropout解决,本 ...
- 『TensorFlow』读书笔记_简单卷积神经网络
如果你可视化CNN的各层级结构,你会发现里面的每一层神经元的激活态都对应了一种特定的信息,越是底层的,就越接近画面的纹理信息,如同物品的材质. 越是上层的,就越接近实际内容(能说出来是个什么东西的那些 ...
- 『TensorFlow』读书笔记_进阶卷积神经网络_分类cifar10_上
完整项目见:Github 完整项目中最终使用了ResNet进行分类,而卷积版本较本篇中结构为了提升训练效果也略有改动 本节主要介绍进阶的卷积神经网络设计相关,数据读入以及增强在下一节再与介绍 网络相关 ...
- 『TensorFlow』读书笔记_进阶卷积神经网络_分类cifar10_下
数据读取部分实现 文中采用了tensorflow的从文件直接读取数据的方式,逻辑流程如下, 实现如下, # Author : Hellcat # Time : 2017/12/9 import os ...
- 『TensorFlow』读书笔记_Inception_V3_下
极为庞大的网络结构,不过下一节的ResNet也不小 线性的组成,结构大体如下: 常规卷积部分->Inception模块组1->Inception模块组2->Inception模块组3 ...
随机推荐
- GDB查看堆栈局部变量
GDB查看堆栈局部变量 “参数从右到左入栈”,“局部变量在栈上分配空间”,听的耳朵都起茧子了.最近做项目涉及C和汇编互相调用,写代码的时候才发现没真正弄明白.自己写了个最简单的函数,用gdb跟踪了调用 ...
- MATLAB变量
序言 在Matlab中,变量名由A~Z.a~z.数字和下划线组成,且变量的第一个字符必须是字母. 尽管变量名可以是任意长度, 但是Matlab只识别名称的前N=namelengthmax个字符, 这里 ...
- python_高级特征
切片 Slice : 取一个tuple的前三个元素,传统做法如下 : for i in range(3): dataList.append(testTuple[i]) if i == 2: prin ...
- 20155228 2017-11-19 实现mypwd(选做,加分)
20155228 2017-11-19 实现mypwd(选做,加分) 题目和要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试mypwd ...
- [转载]Oracle Golden Gate - 概念和机制 (ogg)
出处:https://www.cnblogs.com/qiumingcheng/p/5435907.html Golden Gate(简称OGG)提供异构环境下交易数据的实时捕捉.变换.投递. OGG ...
- week_one-python基础 列表 增删改查
# Author:larlly #列表增删改查#定义列表name = ["wo","ni","ta","wo"] #定义 ...
- 复习-css列表和表格相关属性
css列表和表格相关属性 list-style:设置所有列表属性 list-style-image:将图像设置为列表项标记,主要有url值 list-style-position:设置列表项标记的放置 ...
- Python3 tkinter基础 Radiobutton indicatoron 改变按钮的外观 圆形/方形
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- python基础知识点四
网络编程(socket) 软件开发的架构: 两个程序之间通讯的应用大致通过从用户层面可以分为两种: 1是C/S,即客户端与服务端,为应用类的,比如微信,网盘等需要安装桌面应用的 2是B/S,即浏览器与 ...
- 解决 Command "python setup.py egg_info" failed with error code 1 问题
参考: "pip install unroll": "python setup.py egg_info" failed with error code 1 解决 ...