TensorFlow实战中AlexNet卷积神经网络的训练

01 出错

TypeError: as_default() missing 1 required positional argument: 'self'

经过百度、谷歌的双重查找,没找到就具体原因。后面去TensorFlow官方文档中发现,tf.Graph的用法如下:

g = tf.Graph()
with g.as_default():
# Define operations and tensors in `g`.
c = tf.constant(30.0)
assert c.graph is g

因此,做了一点小改动。把语句:

with tf.Graph().as_default():

改成:

g = tf.Graph()
with g.as_default():

02 运行代码对比带LRN和不带

最后成功运行了第一个带有LRN的版本:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/11/20 10:42
# @Author : Chen Cjv
# @Site : http://www.cnblogs.com/cjvae/
# @File : AlexNet.py
# @Software: PyCharm
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
from datetime import datetime
import math
import time
import tensorflow as tf batch_size = 32
num_batches = 100 # 展示每一个卷积层或池化层输出的tensor的尺寸,接收一个tensor输入
def print_activation(t):
print(t.op.name, '', t.get_shape().as_list()) def inference(images):
# 训练的模型参数
parameters = [] # 1th CL starting
with tf.name_scope('conv1') as scope:
# 截断正态分布初始化卷积核参数
# 卷积核尺寸11 x 11 颜色3通道 卷积核64
kernel = tf.Variable(tf.truncated_normal([11, 11, 3, 64],
dtype=tf.float32, stddev=1e-1), name='weights')
# 实现卷积操作,步长4x4(在图像上每4x4区域取样一次,每次取样卷积核大小为11x11)
conv = tf.nn.conv2d(images, kernel, [1, 4, 4, 1], padding='SAME')
# 卷积偏置为0
biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32),
trainable=True, name='biases')
# 将卷积结果与偏置相加
bias = tf.nn.bias_add(conv, biases)
# 对结果非线性处理
conv1 = tf.nn.relu(bias, name=scope)
# 输出conv1的信息
print_activation(conv1)
# 添加参数
parameters += [kernel, biases]
# 1th CL ending # add 1th LRN layer and max-pooling layer starting
# depth_radius设为4,lrn可以选择不用,效果待测试
lrn1 = tf.nn.lrn(conv1, 4, bias=1.0, alpha=0.001/9, beta=0.75, name='lrn1')
# 池化:尺寸3x3(将3x3的大小的像素块降为1x1 步长为2x2 VALID表示取样不超过边框)
pool1 = tf.nn.max_pool(lrn1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool1')
print_activation(pool1)
# add 1th LRN layer and max-pooling layer ending # designing second Convolutional Layer starting
with tf.name_scope('conv2') as scope:
# 不同第一卷积层,这层卷积核尺寸5x5,通道为上层输出通道数(即卷积核数)64
# 卷积核数量为192
kernel = tf.Variable(tf.truncated_normal([5, 5, 64, 192],
dtype=tf.float32, stddev=1e-1), name='weights')
# 卷积步长为1,即扫描全部图像
conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[192],
dtype=tf.float32), trainable=True, name='biases')
bias = tf.nn.bias_add(conv, biases)
conv2 = tf.nn.relu(bias, name=scope)
parameters += [kernel, biases]
print_activation(conv2)
# designing 2th CL ending # add 2th LRN layer and max-pooling layer starting
lrn2 = tf.nn.lrn(conv2, 4, bias=1.0, 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_activation(pool2)
# add 2th LRN layer and max-pooling layer ending # designing 3th Convolutional Layer starting
with tf.name_scope('conv3') as scope:
# 卷积核尺寸3x3 通道数192 卷积核384
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.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_activation(conv3)
# designing 3th CL ending # designing 4th CL starting
with tf.name_scope('conv4') as scope:
# 卷积核尺寸3x3 通道数384 卷积核降为256
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.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_activation(conv4)
# designing fourth Convolutional Layer ending # designing fifth Convolutional Layer starting
with tf.name_scope('conv4') as scope:
# 卷积核尺寸3x3 通道数256 卷积核256
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.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_activation(conv5)
# designing fifth Convolutional Layer ending pool5 = tf.nn.max_pool(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding='VALID', name='pool5')
print_activation (pool5) return pool5, parameters # 评估每轮的计算时间
# session是训练句柄,target是训练算子,info_string是测试名称
def time_tensorflow_run(session, target, info_string):
# 只考虑预热轮数10轮之后的时间
num_steps_burn_in = 10
# 总时间
total_duration = 0.0
# 平方和
total_duration_squared = 0.0
for i in range(num_batches + num_steps_burn_in):
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 * duration # 计算平均耗时mn 标准差sd
mn = total_duration / num_batches
vr = total_duration_squared / num_batches - mn * mn
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():
g = tf.Graph ()
# 定义默认Graph
with g.as_default():
# 构造随机数据
image_size = 224
images = tf.Variable(tf.random_normal(
[batch_size, image_size, image_size, 3],
dtype=tf.float32, stddev=1e-1 )) pool5, parameters = inference(images) init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # 统计运行时间
time_tensorflow_run(sess, pool5, "Forward") objective = tf.nn.l2_loss(pool5)
grad = tf.gradients(objective, parameters)
time_tensorflow_run(sess, grad, "Forward-backward") # 执行主函数
run_benchmark()

下面是我的运行结果:

然后是不带LRN的版本:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/11/20 10:42
# @Author : Chen Cjv
# @Site : http://www.cnblogs.com/cjvae/
# @File : AlexNet.py
# @Software: PyCharm
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
from datetime import datetime
import math
import time
import tensorflow as tf batch_size = 32
num_batches = 100 # 展示每一个卷积层或池化层输出的tensor的尺寸,接收一个tensor输入
def print_activation(t):
print(t.op.name, '', t.get_shape().as_list()) def inference(images):
# 训练的模型参数
parameters = [] # 1th CL starting
with tf.name_scope('conv1') as scope:
# 截断正态分布初始化卷积核参数
# 卷积核尺寸11 x 11 颜色3通道 卷积核64
kernel = tf.Variable(tf.truncated_normal([11, 11, 3, 64],
dtype=tf.float32, stddev=1e-1), name='weights')
# 实现卷积操作,步长4x4(在图像上每4x4区域取样一次,每次取样卷积核大小为11x11)
conv = tf.nn.conv2d(images, kernel, [1, 4, 4, 1], padding='SAME')
# 卷积偏置为0
biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32),
trainable=True, name='biases')
# 将卷积结果与偏置相加
bias = tf.nn.bias_add(conv, biases)
# 对结果非线性处理
conv1 = tf.nn.relu(bias, name=scope)
# 输出conv1的信息
print_activation(conv1)
# 添加参数
parameters += [kernel, biases]
# 1th CL ending # add 1th max-pooling layer starting
# 池化:尺寸3x3(将3x3的大小的像素块降为1x1 步长为2x2 VALID表示取样不超过边框)
pool1 = tf.nn.max_pool (conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding='VALID', name='pool1')
print_activation(pool1)
# add max-pooling layer ending # designing second Convolutional Layer starting
with tf.name_scope('conv2') as scope:
# 不同第一卷积层,这层卷积核尺寸5x5,通道为上层输出通道数(即卷积核数)64
# 卷积核数量为192
kernel = tf.Variable(tf.truncated_normal([5, 5, 64, 192],
dtype=tf.float32, stddev=1e-1), name='weights')
# 卷积步长为1,即扫描全部图像
conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape=[192],
dtype=tf.float32), trainable=True, name='biases')
bias = tf.nn.bias_add(conv, biases)
conv2 = tf.nn.relu(bias, name=scope)
parameters += [kernel, biases]
print_activation(conv2)
# designing 2th CL ending # add 2th max-pooling layer starting
pool2 = tf.nn.max_pool (conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding='VALID', name='pool2')
print_activation(pool2)
# add 2th max-pooling layer ending # designing 3th Convolutional Layer starting
with tf.name_scope('conv3') as scope:
# 卷积核尺寸3x3 通道数192 卷积核384
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.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_activation(conv3)
# designing 3th CL ending # designing 4th CL starting
with tf.name_scope('conv4') as scope:
# 卷积核尺寸3x3 通道数384 卷积核降为256
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.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_activation(conv4)
# designing fourth Convolutional Layer ending # designing fifth Convolutional Layer starting
with tf.name_scope('conv4') as scope:
# 卷积核尺寸3x3 通道数256 卷积核256
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.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_activation(conv5)
# designing fifth Convolutional Layer ending pool5 = tf.nn.max_pool(conv5, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding='VALID', name='pool5')
print_activation (pool5) return pool5, parameters # 评估每轮的计算时间
# session是训练句柄,target是训练算子,info_string是测试名称
def time_tensorflow_run(session, target, info_string):
# 只考虑预热轮数10轮之后的时间
num_steps_burn_in = 10
# 总时间
total_duration = 0.0
# 平方和
total_duration_squared = 0.0
for i in range(num_batches + num_steps_burn_in):
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 * duration # 计算平均耗时mn 标准差sd
mn = total_duration / num_batches
vr = total_duration_squared / num_batches - mn * mn
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():
g = tf.Graph ()
# 定义默认Graph
with g.as_default():
# 构造随机数据
image_size = 224
images = tf.Variable(tf.random_normal(
[batch_size, image_size, image_size, 3],
dtype=tf.float32, stddev=1e-1 )) pool5, parameters = inference(images) init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # 统计运行时间
time_tensorflow_run(sess, pool5, "Forward") objective = tf.nn.l2_loss(pool5)
grad = tf.gradients(objective, parameters)
time_tensorflow_run(sess, grad, "Forward-backward") # 执行主函数
run_benchmark()

运行结果:

从两个版本可见,带有LRN层的AlexNet训练时间比较长,据说效果有待商榷。

《TensorFlow实战》中AlexNet卷积神经网络的训练中的更多相关文章

  1. TensorFlow 实战之实现卷积神经网络

    本文根据最近学习TensorFlow书籍网络文章的情况,特将一些学习心得做了总结,详情如下.如有不当之处,请各位大拿多多指点,在此谢过. 一.相关性概念 1.卷积神经网络(ConvolutionNeu ...

  2. TensorFlow 2.0 深度学习实战 —— 浅谈卷积神经网络 CNN

    前言 上一章为大家介绍过深度学习的基础和多层感知机 MLP 的应用,本章开始将深入讲解卷积神经网络的实用场景.卷积神经网络 CNN(Convolutional Neural Networks,Conv ...

  3. 使用TensorFlow v2.0构建卷积神经网络

    使用TensorFlow v2.0构建卷积神经网络. 这个例子使用低级方法来更好地理解构建卷积神经网络和训练过程背后的所有机制. CNN 概述 MNIST 数据集概述 此示例使用手写数字的MNIST数 ...

  4. 斯坦福NLP课程 | 第11讲 - NLP中的卷积神经网络

    作者:韩信子@ShowMeAI,路遥@ShowMeAI,奇异果@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/36 本文地址:http://www. ...

  5. 理解NLP中的卷积神经网络(CNN)

    此篇文章是Denny Britz关于CNN在NLP中应用的理解,他本人也曾在Google Brain项目中参与多项关于NLP的项目. · 翻译不周到的地方请大家见谅. 阅读完本文大概需要7分钟左右的时 ...

  6. TensorFlow深度学习实战---图像识别与卷积神经网络

    全连接层网络结构:神经网络每两层之间的所有结点都是有边相连的. 卷积神经网络:1.输入层 2.卷积层:将神经网络中的每一个小块进行更加深入地分析从而得到抽象程度更高的特征. 3 池化层:可以认为将一张 ...

  7. TensorFlow 深度学习笔记 卷积神经网络

    Convolutional Networks 转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Is ...

  8. 【深度学习与TensorFlow 2.0】卷积神经网络(CNN)

    注:在很长一段时间,MNIST数据集都是机器学习界很多分类算法的benchmark.初学深度学习,在这个数据集上训练一个有效的卷积神经网络就相当于学习编程的时候打印出一行“Hello World!”. ...

  9. 机器学习与Tensorflow(4)——卷积神经网络与tensorflow实现

    1.标准卷积神经网络 标准的卷积神经网络由输入层.卷积层(convolutional layer).下采样层(downsampling layer).全连接层(fully—connected laye ...

随机推荐

  1. jsonp——使用公共接口获取数据

    demo: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8 ...

  2. 一个简单的CI分页类

    [php] view plaincopy <span style="font-size:16px;">/** * * 关于 页码有效性的判断需要加在 控制器中判断,即当 ...

  3. Python3学习实例上手(1)-----模拟登录认证

    要求:输入用户名.密码进行认证,认证成功则欢迎,若输错三次会锁定最后一次输入的用户 知识点归纳:字典操作(items()方法).字符串操作(如strip().split()等).文件读写(for li ...

  4. 常见的SQL错误和解决方法

    前言 今天你会看到每个人——从新手到专家——在使用SQL时犯的各种常见错误.你不能永远避免犯任何错误,但是熟悉广泛的错误将帮助你在尽可能短的时间内解决这些错误. 注:在我们的例子中我们使用的是Orac ...

  5. mysql报错this is incompatible with sql_mode=only_full_group_by

    1.报错信息 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: In aggregated query without GROUP ...

  6. mysql数据库忘记密码时如何修改(一)

    方法/步骤 打开mysql.exe和mysqld.exe所在的文件夹,复制路径地址 打开cmd命令提示符,进入上一步mysql.exe所在的文件夹. 输入命令  mysqld --skip-grant ...

  7. mysql必知必会学习笔记(1)

    chapter 13 13.1 数据分组 如果要将数据分为多个逻辑组怎么办?? 要用数据分组 13.2 创建分组 使用group by子句.group by会指示对mysql进行分组,然后对每个组进行 ...

  8. Android Service创建USB HOST通信

    之前做了一个关于Android USB通信的Case,通过Android的USB总线给Zigbee供电,和板载的Zigbee(基于Zigbee的自组网)进行通信.要使用Android的USB Host ...

  9. rem媒体查询

    @media only screen and (min-width: 1080px), only screen and (min-device-width:1080px) { html,body { ...

  10. Consider everything deeply but still remain fearless.

    Consider everything deeply but still remain fearless.愿你能深思熟虑,但始终无所畏惧.