博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tensorflow---alexnet training (tflearn)
阅读量:4638 次
发布时间:2019-06-09

本文共 4231 字,大约阅读时间需要 14 分钟。

 

# 输入数据import input_datamnist = input_data.read_data_sets("/tmp/data/", one_hot=True)import tensorflow as tf# 定义网络超参数learning_rate = 0.001training_iters = 200000batch_size = 64display_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)    # Dropout    norm1 = 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)    # Dropout    norm2 = 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)    # Dropout    norm3 = 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 iterations    while 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 += 1    print "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

你可能感兴趣的文章
printf的使用
查看>>
NLP Attention
查看>>
PHP 之数据类型判断
查看>>
第二次冲刺 站立会议3
查看>>
LA3029最大子矩阵
查看>>
万网域名MX解析设置方案[net.cn, ubuntu]
查看>>
403. Frog Jump
查看>>
C++学习之二分查找续
查看>>
Vue创建SPA那些事
查看>>
python基础学习1-列表推导式和字典推导式
查看>>
Linux下开发python django程序(模板设置和载入数据)
查看>>
mfc Radio Buttons
查看>>
Python【第三章】:python 面向对象 (new)
查看>>
redis学习总结
查看>>
css文字禁止选中
查看>>
[刘阳Java]_Java环境搭建_第2讲
查看>>
[JavaScript]父子窗口间参数传递
查看>>
Test Controller Tool
查看>>
86. Partition List
查看>>
[LintCode] 378 Convert Binary Search Tree to Doubly Linked List 解题报告
查看>>