TensorFlow 学习(2)

准备工作

决定玩玩 docker 版本的,工作环境为 Mac

  • 安装 VirtualBox 最新版本,docker 在 Mac 上不能直接使用(依赖 Linux kernel 的 cgroup 等特性)
  • 安装 docker toolbox,这是一些在 Mac 下使用 docker 的工具,装好后会自动配置一个叫 default 的虚拟机,这个虚拟机可以用 docker-machine 命令来操纵,这个虚拟机已经配置好了 ssh,因此可以直接 ssh 到虚拟机上想干嘛干嘛
  • 在 docker quickstart terminal 里面,PATH 已经设置好了,可以直接用 docker 等命令,更多可以参考这里
  • 安装 docker 镜像,docker run -it b.gcr.io/tensorflow/tensorflow
  • 测试 TF,参看这里

运行 code 如下

$ docker run -it b.gcr.io/tensorflow/tensorflow
Unable to find image 'b.gcr.io/tensorflow/tensorflow:latest' locally
latest: Pulling from tensorflow/tensorflow

0bf056161913: Pull complete
1796d1c62d0c: Pull complete
e24428725dd6: Pull complete
89d5d8e8bafb: Pull complete
91dc2e300ec6: Pull complete
f2b205f23258: Pull complete
33c0372bafe3: Pull complete
e58fef39e1c1: Pull complete
20bc72598f11: Pull complete
d1627c65b6cf: Pull complete
f44441b29ccc: Pull complete
2b13eefb13c7: Pull complete
151aba0ce7fe: Pull complete
4ea38d2f04f0: Pull complete
74cd363304ab: Pull complete
4ac133eed955: Pull complete
Digest: sha256:bed079b6881a45cd548c319991ef79dbc17b2ec1f6d5295ca4fd5c45b180ef5a
Status: Downloaded newer image for b.gcr.io/tensorflow/tensorflow:latest
root@55f407ea0937:~# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 1
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 1
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>>

Hello World

我们来试试个简单的 TF 程序,参看 mnist 的例子:

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

if __name__ == '__main__':
  mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
  x = tf.placeholder(tf.float32, [None, 784])
  W = tf.Variable(tf.zeros([784, 10]))
  b = tf.Variable(tf.zeros([10]))
  y = tf.nn.softmax(tf.matmul(x, W) + b)
  y_ = tf.placeholder(tf.float32, [None, 10])
  cross_entropy = -tf.reduce_sum(y_*tf.log(y))
  train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
  init = tf.initialize_all_variables()
  sess = tf.Session()
  sess.run(init)
  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})
  correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
  print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

执行之后

$ python mnist.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 1
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 1
0.917

看起来就这么简单。我们来想个复杂的问题试试?首先我们要理解这段程序

  • 数据用 placeholder 表示,这样 training 和 inference 阶段可以向里面填
  • 优化变量使用 variable 表示,这样 TF 在 training 的时候会使用自动求出来的导数来更新它们
  • 表达出来目标函数(cross_entropy)后我们需要通过一个 solver 来求解,那个 train_step 就是迭代的其中的一步
  • session 通过 run 方法执行一次 computation graph,这是 train_step 表示的一个更新优化变量的图
  • 后面计算 accuracy 也直接使用 TF 自己计算,然后把 test data 代入获得结果

是不是有点心动想找个实际问题来试试看了呢?

——————
And it came to pass at the end of two full years, that Pharaoh dreamed:and, behold, he stood by the river

Advertisements
TensorFlow 学习(2)

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s