入门资源:
tensorflow中文社区:http://www.tensorfly.cn/
谢小小XH的一系列博客:http://blog.csdn.net/xierhacker/article/category/6511974
然后自己手动尝试了一下官方给的mnist的例子:
# -*- coding: utf-8 -*-
import tensorflow as tf
#导入input_data用于自动下载和安装MNIST数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
#放置占位符,用于在计算时接收输入值
x = tf.placeholder("float", [None, 784])
#创建两个变量,分别用来存放权重值W和偏置值b
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
#使用Tensorflow提供的回归模型softmax,y代表输出
y = tf.nn.softmax(tf.matmul(x, W) + b)
#为了进行训练,需要把正确值一并传入网络
y_ = tf.placeholder("float", [None,10])
#计算交叉墒
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
#使用梯度下降算法以0.01的学习率最小化交叉墒
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
#初始化之前创建的变量的操作
init = tf.initialize_all_variables()
#启动初始化
sess = tf.Session()
sess.run(init)
#开始训练模型,循环1000次,每次都会随机抓取训练数据中的100条数据,然后作为参数替换之前的占位符来运行train_step
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
#评估模型,tf.argmax能给出某个tensor对象在某一维上数据最大值的索引。因为标签是由0,1组成了one-hot vector,返回的索引就是数值为1的位置
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
#计算正确预测项的比例,因为tf.equal返回的是布尔值,使用tf.cast可以把布尔值转换成浮点数,tf.reduce_mean是求平均值
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
#在session中启动accuracy,输入是MNIST中的测试集
print(sess.run(accuracy, feed_dict={x: mnist.test.images[], y_: mnist.test.labels[]}))
然后发现报错:
ResourceExhaustedError: OOM when allocating tensor with shape[10000]
[[Node: ArgMax_3 = ArgMax[T=DT_FLOAT, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"](_arg_Placeholder_3_0_1/_5, ArgMax_3/dimension)]]
[[Node: Mean_1/_9 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_27_Mean_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'ArgMax_3', defined at:
File "/home/john/anaconda3/lib/python3.5/site-packages/spyder/utils/ipython/start_kernel.py", line 223, in <module>
main()
File "/home/john/anaconda3/lib/python3.5/site-packages/spyder/utils/ipython/start_kernel.py", line 219, in main
kernel.start()
File "/home/john/anaconda3/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 474, in start
ioloop.IOLoop.instance().start()
File "/home/john/anaconda3/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 162, in start
super(ZMQIOLoop, self).start()
File "/home/john/anaconda3/lib/python3.5/site-packages/tornado/ioloop.py", line 887, in start
handler_func(fd_obj, events)
File "/home/john/anaconda3/lib/python3.5/site-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/home/john/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/home/john/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/home/john/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/home/john/anaconda3/lib/python3.5/site-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/home/john/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher
return self.dispatch_shell(stream, msg)
File "/home/john/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell
handler(stream, idents, msg)
File "/home/john/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 390, in execute_request
user_expressions, allow_stdin)
File "/home/john/anaconda3/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/home/john/anaconda3/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 501, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/home/john/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/home/john/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2827, in run_ast_nodes
if self.run_code(code, result):
File "/home/john/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-f51c0c864be9>", line 1, in <module>
runfile('/media/john/新加卷1/项目集合/兴趣项目/tensorflow入门/mnist_tf.py', wdir='/media/john/新加卷1/项目集合/兴趣项目/tensorflow入门')
File "/home/john/anaconda3/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/home/john/anaconda3/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/media/john/新加卷1/项目集合/兴趣项目/tensorflow入门/mnist_tf.py", line 41, in <module>
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
File "/home/john/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py", line 180, in argmax
return gen_math_ops.arg_max(input, axis, name)
File "/home/john/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/gen_math_ops.py", line 174, in arg_max
name=name)
File "/home/john/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/home/john/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/john/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
self._traceback = _extract_stack()
观察可以发现:
ResourceExhaustedError: OOM when allocating tensor with shape[10000]
是测试集规模太大,内存爆掉了,把最后一行改为:
print(sess.run(accuracy, feed_dict={x: mnist.test.images[:8000], y_: mnist.test.labels[:8000]}))
选用8000个即可,内存也不会爆掉
最后结果测试为:0.90250003
另:第一次使用GPU跑深度,速度快的真不是一丁半点