# 输入数据
import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)import tensorflow as tf# 定义网络超参数
learning_rate = 0.001
training_iters = 200000
batch_size = 64
display_step = 20# 定义网络参数
n_input = 784 # 输入的维度
n_classes = 10 # 标签的维度
dropout = 0.8 # Dropout 的概率# 占位符输入
x = tf.placeholder(tf.types.float32, [None, n_input])
y = tf.placeholder(tf.types.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.types.float32)# 卷积操作
def conv2d(name, l_input, w, b):return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(l_input, w, strides=[1, 1, 1, 1], padding='SAME'),b), name=name)# 最大下采样操作
def max_pool(name, l_input, k):return tf.nn.max_pool(l_input, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME', name=name)# 归一化操作
def norm(name, l_input, lsize=4):return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name=name)# 定义整个网络
def alex_net(_X, _weights, _biases, _dropout):# 向量转为矩阵_X = tf.reshape(_X, shape=[-1, 28, 28, 1])# 卷积层conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1'])# 下采样层pool1 = max_pool('pool1', conv1, k=2)# 归一化层norm1 = norm('norm1', pool1, lsize=4)# Dropoutnorm1 = tf.nn.dropout(norm1, _dropout)# 卷积conv2 = conv2d('conv2', norm1, _weights['wc2'], _biases['bc2'])# 下采样pool2 = max_pool('pool2', conv2, k=2)# 归一化norm2 = norm('norm2', pool2, lsize=4)# Dropoutnorm2 = tf.nn.dropout(norm2, _dropout)# 卷积conv3 = conv2d('conv3', norm2, _weights['wc3'], _biases['bc3'])# 下采样pool3 = max_pool('pool3', conv3, k=2)# 归一化norm3 = norm('norm3', pool3, lsize=4)# Dropoutnorm3 = tf.nn.dropout(norm3, _dropout)# 全连接层,先把特征图转为向量dense1 = tf.reshape(norm3, [-1, _weights['wd1'].get_shape().as_list()[0]]) dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1') # 全连接层dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2') # Relu activation# 网络输出层out = tf.matmul(dense2, _weights['out']) + _biases['out']return out# 存储所有的网络参数
weights = {'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64])),'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128])),'wc3': tf.Variable(tf.random_normal([3, 3, 128, 256])),'wd1': tf.Variable(tf.random_normal([4\*4\*256, 1024])),'wd2': tf.Variable(tf.random_normal([1024, 1024])),'out': tf.Variable(tf.random_normal([1024, 10]))
}
biases = {'bc1': tf.Variable(tf.random_normal([64])),'bc2': tf.Variable(tf.random_normal([128])),'bc3': tf.Variable(tf.random_normal([256])),'bd1': tf.Variable(tf.random_normal([1024])),'bd2': tf.Variable(tf.random_normal([1024])),'out': tf.Variable(tf.random_normal([n_classes]))
}# 构建模型
pred = alex_net(x, weights, biases, keep_prob)# 定义损失函数和学习步骤
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)# 测试网络
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))# 初始化所有的共享变量
init = tf.initialize_all_variables()# 开启一个训练
with tf.Session() as sess:sess.run(init)step = 1# Keep training until reach max iterationswhile step \* batch_size < training_iters:batch_xs, batch_ys = mnist.train.next_batch(batch_size)# 获取批数据sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout})if step % display_step == 0:# 计算精度acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})# 计算损失值loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})print "Iter " + str(step\*batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc)step += 1print "Optimization Finished!"# 计算测试精度print "Testing Accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.})

  

tensorflow 是强大的分布式跨平台深度学习框架

keras,TensorLayer,Tflearn 都是基于tensorflow 开发的库(提供傻瓜式编程)

知识点:

from __future__ import print_function   : 为了老版本的python 兼顾新特性 (from __future import *)

转载于:https://www.cnblogs.com/fanhaha/p/7645326.html

tensorflow---alexnet training (tflearn)相关推荐

  1. 成功解决AttributeError: module 'tensorflow.python.training.training' has no attribute 'SummaryWriter'

    成功解决AttributeError: module 'tensorflow.python.training.training' has no attribute 'SummaryWriter' 目录 ...

  2. ‘tensorflow.python.training.experimental.mixed_precision‘ has no attribute ‘_register_wrapper_optimi

    报错 AttributeError: module 'tensorflow.python.training.experimental.mixed_precision' has no attribute ...

  3. TensorFlow alexnet在华为Mate10上运行方法

    我使用的caffe模型:https://github.com/BVLC/caffe/tree/ea455eb29393ebe6de9f14e88bfce9eae74edf6d/models/bvlc_ ...

  4. TensorFlow AlexNet

    主要用于图像分类 参考: 卷积神经网络之AlexNet 深入了解AlexNet网络 深度学习笔记:CNN经典论文研读之AlexNet及其Tensorflow实现

  5. tflearn教程_TFLearn:为TensorFlow提供更高级别的API 的深度学习库

    TFlearn是一个基于Tensorflow构建的模块化透明深度学习库.它旨在为TensorFlow提供更高级别的API,以促进和加速实验,同时保持完全透明并与之兼容. TFLearn功能包括: 通过 ...

  6. TensorFlow实现案例汇集:代码+笔记

    这是使用 TensorFlow 实现流行的机器学习算法的教程汇集.本汇集的目标是让读者可以轻松通过案例深入 TensorFlow. 这些案例适合那些想要清晰简明的 TensorFlow 实现案例的初学 ...

  7. [机器学习收藏] TensorFlow初学者必须了解的55个经典案例

    TensorFlow初学者必须了解的55个经典案例 2017-06-191024深度学习1024深度学习 导语:本文是TensorFlow实现流行机器学习算法的教程汇集,目标是让读者可以轻松通过清晰简 ...

  8. 数十种TensorFlow实现案例汇集:代码+笔记

    这是使用 TensorFlow 实现流行的机器学习算法的教程汇集.本汇集的目标是让读者可以轻松通过案例深入 TensorFlow. 这些案例适合那些想要清晰简明的 TensorFlow 实现案例的初学 ...

  9. 深度学习TensorFlow的55个经典案例

             人工智能大数据与深度学习  公众号: weic2c 本文是TensorFlow实现流行机器学习算法的教程汇集,目标是让读者可以轻松通过清晰简明的案例深入了解 TensorFlow.这 ...

最新文章

  1. html中引入ifrim视频,Making Of Rallypoint - Outer Rim
  2. 田志刚:人跟人差距怎么那么大呢?
  3. python 柱形图 负值_Highcharts 使用负值的反向柱形图
  4. BZOJ 2734 [HNOI2012]集合选数 (状压DP、时间复杂度分析)
  5. ubuntu 配置url地址重定向协议
  6. 数据库外键约束的几种方法及区别
  7. PL/SQL 08 异常 exception
  8. 解决datepicker设置选中日期setDate不生效问题
  9. Mapv 是地理信息可视化开源库
  10. NDK-JNI实战教程(一) 在Android Studio运行第一个NDK程序
  11. 工作中,什么情况下应该负责到底?
  12. Windows资源监控工具汇总
  13. js 中try catch用法
  14. 智能语音机器人语音识别系统
  15. Android SDK各个版本API的特性及兼容性(Dalvik/ART)
  16. 机械革命z2黑苹果改造计划第三番-macOS键盘快捷键Win键盘适配
  17. 深度优先搜索与宽度优先搜索
  18. Python「PIL」:调整图片大小
  19. 网易北京研发中心-网易传媒部门深度学习算法实习生面试总结
  20. 智能家居有必要HomeKit吗?

热门文章

  1. 正式压力测试:locust进阶,超简单搭建生产级locust集群
  2. STL库(C++11)提供的异步执行方法的方式
  3. 【微服务架构】SpringCloud之路由网关(zuul)
  4. 将request中的所有参数存放到自定义的map中
  5. bert推理速度太慢如何解决
  6. oracle sqlserver 查看指定用户下面的表
  7. Python多线程(3)——Queue模块
  8. runtime系统的Cello
  9. 快速人体姿态估计:CVPR2019论文阅读
  10. 2021年大数据Hadoop(八):HDFS的Shell命令行使用