您的位置:首页 > 其它

【深度学习笔记】(二)Hello, Tensorflow!

2017-11-27 18:50 651 查看

【深度学习笔记】(二)Hello, Tensorflow!

一、安装

官方安装的方式很多种,本文采用Docker方式。Docker的深入使用文案很长很多,但我们都不需要,我们的主要目的还是Tensorflow,所以只需要基本的使用即可。PS:打开Tensorflow官网是需要翻墙的,所以上面的一些链接不能翻墙的情况下是打不开的,然鹅!Docker不需要翻墙就能打开,所以用Docker来安装Tensorflow就是为了绕墙而走。

1、Docker安装

既然不用翻墙,首先就是点我下载安装包,打开页面看到很多开发平台的版本,选择匹配自己开发平台的包下载安装即可,安装的过程就是一直点“下一步”。。。over。

安装成功后,会有两个入口:



第一个既然看不清名称就不用官他,也可以看得清也不要管他,因为我们只需要用第二个:Kitmatic,我们仅用用Kitmatic来操作Docker来提供Tensorfow的,不需要要学习第一个命令行的操作方式。

2、Kitmatic安装Tensorflow环境

点击第1步中的第二个图标启动Kitmatic;点击左上角的”NEW”按钮:



进入下图,在输入框中输入Tensorflow搜索,在Docker Hub搜索中选择一个,一般是第一个,点击“CREATE”按钮下载安装。

其他Jupyter NotebookTensorboard都可以在这里找到下载安装。



二、Hello, Tensorflow

1、编程步骤:

定义数据

定义计算图与变量

定义会话

进行计算

2、基于MNIST数据集的手写数字识别

这是很多教科书上的入门例子,但是没有说明其中代码依赖导致运行不起来。所以首先作为Tensorflow的入门例子,应该是包括在Tensorflow代码里的,先要把它clone下来:

git clone https://github.com/tensorflow/tensorflow

这时候可能因为权限问题无法拉下来,先去fork一份到自己名下就行了,或者直接打包下载。

接下来就能把代码跑起来了,写一遍感受一下

# 1、load data set
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# 2、see data set:
# train - test - validation

# train data set
# print(mnist.train.images.shape,mnist.train.labels.shape)

# test data set
# print(mnist.test.images.shape,mnist.test.labels.shape)

# validation data set
# print(mnist.validation.images.shape,mnist.validation.labels.shape)

# 3、开启tensorflow session
import tensorflow as tf
sess = tf.InteractiveSession()

# 4、define softmax regression
# x
x = tf.placeholder(tf.float32,[None,784])
# W
W = tf.Variable(tf.zeros([784,10]))
# b
b = tf.Variable(tf.zeros(10))
# y
y = tf.nn.softmax(tf.matmul(x,W) + b)
# y_
y_ = tf.placeholder(tf.float32,[None,10])
# loss
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),reduction_indices=[1]))
# SGD
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# init
tf.global_variables_initializer().run()

# 5、trainning starts
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x:batch_xs, y_:batch_ys})
# trainning ends

# correct predictiong
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
# accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
# evalue
print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))


三、参考

https://www.tensorflow.org

https://github.com/tensorflow/tensorflow

https://www.docker.com/

http://jupyter.org/

Tensorflow实战.黄文坚
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: