tensorflow学习2-mnsit的nn案例
来源:互联网 发布:windows现在不能激活 编辑:程序博客网 时间:2024/06/06 01:01
来源与详解可看中文社区教程:中文社区mnist进阶教程
有几点我想说一说:
1.尺寸的变化。我觉得这个非常重要是操作的线索,刚学瞎比比,不正确的话还望指出来。
我一开始的理解是:图片输入28×28 卷积核5×5,步长5,得到24×24,池化得到12×12.再一次卷积得到8×8,池化后4×4
可是代码里明明是说7×7啊。
原来问题出在这儿:
tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')padding的设置:
SAME means that the output feature map has the same spatial dimensions as the input feature map. Zero padding is introduced to make the shapes match as needed, equally on every side of the input map.
VALID means no padding.
2.Dropout层,这个操作有些懵逼,只是了解了下:通过概率,防止过拟合。占坑以后有空学习了再补。
一般在训练时用,测试时不用,所以代码里是这样写的:
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print "test accuracy %g"%accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
3.闲来无事,想看看我这8G显存能跑多大数据。一点点,笑哭。一直在最后一步报memory out。
想起来是到test那一步才报错,train时是有batch的,而test是所有图一次进去,我也给test加了个batch ,好了menmory out解决。
for j in range(2): batch_t = mnist.test.next_batch(1000)print "test accuracy %g"%accuracy.eval(feed_dict={x:batch_t[0], y_: batch_t[1], keep_prob: 1.0})
好啦,上代码:
#! /usr/bin/env python# _*_ coding: utf-8 _*_import tensorflow as tf#下载安装数据集from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("workspace/mnist/data", one_hot=True)#运行tensorflow的InterractiveSessiongpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.90) sess = tf.InteractiveSession(config=tf.ConfigProto(gpu_options=gpu_options)) #占位符x = tf.placeholder("float", shape=[None, 784])y_ = tf.placeholder("float", shape=[None, 10])###构建一个多层卷积网络#权重初始化def weight_variable(shape):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):return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')def max_pool_2x2(x):return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')#第一层卷积w_conv1 = weight_variable([5, 5, 1, 32])b_conv1 = bias_variable([32])x_image = tf.reshape(x, [-1, 28, 28, 1])#-1是表示在那个维度的维度数目是根据计算自动调整的具体可看APIh_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)h_pool1 = max_pool_2x2(h_conv1)#第二层卷积W_conv2 = weight_variable([5, 5, 32, 64])b_conv2 = bias_variable([64])h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)h_pool2 = max_pool_2x2(h_conv2)#密集连接层W_fc1 = weight_variable([7 * 7 * 64, 1024])b_fc1 = bias_variable([1024])h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)#Dropoutkeep_prob = tf.placeholder("float")h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)#输出层W_fc2 = weight_variable([1024, 10])b_fc2 = bias_variable([10])y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)#训练和评估模型cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))#tf.case类型转换sess.run(tf.global_variables_initializer())for i in range(10000): batch = mnist.train.next_batch(100) if i%100 == 0: train_accuracy = accuracy.eval(feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}) print "step %d, training accuracy %g"%(i, train_accuracy) train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})print "test accuracy %g"%accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})运算的结果么:
阅读全文
0 0
- tensorflow学习2-mnsit的nn案例
- tensorflow学习1-mnsit的regression案例
- TensorFlow学习---tf.nn.softmax_cross_entropy_with_logits的用法
- tensorflow 构建CNN识别自己的数据(类似于MNSIT)
- Tensorflow学习---tf.nn.embedding_lookup
- TensorFlow学习笔记之tf.nn.softmax()与tf.nn.softmax_cross_entropy_with_logits的用法
- TensorFlow-MNSIT进阶篇代码
- 简单的Tensorflow实现NN
- tensorflow中nn的padding
- Tensorflow之nn 简单神经网络学习
- tensorflow学习:tf.nn.softmax_cross_entropy_with_logits()
- Tensorflow学习笔记(用哪学哪)tf.nn.dropout
- TensorFlow中nn.conv2d与nn.avg_pool的理解
- tensorflow 1.01 利用rnn计算mnsit分类遇到的巨坑
- 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法
- tensorflow 对Recursive NN的完美实现
- 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法
- TensorFlow 介绍 tf.nn.softmax_cross_entropy_with_logits 的用法
- 获取文件采用什么编码
- mybatis大于小于的转义
- ONE HOT 编码
- MAC下使用mysql workbench
- JavaScript 规范
- tensorflow学习2-mnsit的nn案例
- 12.Struts2_通配符映射
- Redis架构及分片管理
- DES/3DES/AES区别
- 在Ubuntu 14.04 64bit中永久添加DNS的方法
- 日常小结篇-java异常处理
- 曾鸣:新零售的未来 | 干货
- LintCode Find the Duplicate Number
- 维和步兵营~傻逼的戈瑞-冷酷的林皓南~装逼的海蓝