一个计算机技术爱好者与学习者

0%

tensorflow入门

1. tensorflow简介

TensorFlow 是一个用于人工智能的开源神器。
TensorFlow 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。它灵活的架构让你可以在多种平台上展开计算,例如台式计算机中的一个或多个CPU(或GPU),服务器,移动设备等等。TensorFlow 最初由Google大脑小组(隶属于Google机器智能研究机构)的研究员和工程师们开发出来,用于机器学习和深度神经网络方面的研究,但这个系统的通用性使其也可广泛用于其他计算领域。

本文主要参考莫烦同学的教程,进行了少量修改。

2. 安装tensorflow

1、参考Installing TensorFlow或者下载与安装,安装TensorFlow。

2、假设我们的环境是anaconda,首先我们切换到3.6的环境,然后pip安装即可。

1
2
activate py3
pip install --upgrade tensorflow

3、测试

1
2
3
4
5
6
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

如果屏幕打印出“Hello, TensorFlow!”,则证明安装成功!

3. tensorflow基础

3.1. 处理结构

TensorFlow 让我们可以先绘制计算结构图,也可以称是一系列可人机交互的计算操作, 然后把编辑好的Python文件转换成更高效的 C++,并在后端进行计算。

TensorFlow 首先要定义神经网络的结构,然后再把数据放入结构当中去运算和training

下面动图展示了 TensorFlow 数据处理流程:
数据流图

因为TensorFlow是采用 数据流图(data flow graphs)来计算, 所以首先我们得创建一个数据流图,然后再将我们的数据(数据以 张量(tensor) 的形式存在)放到数据流图中计算。

图中的 节点(Nodes)一般用来表示施加的数学操作,但也可以表示数据输入(feed in)的起点/输出(push out)的终点,或者是读取/写入持久变量(persistent variable)的终点;线(edges)则表示在节点间相互联系的多维数据数组,即 张量(tensor),训练模型时,tensor 会不断的从数据流图中的一个节点 flow 到另一节点,这就是 TensorFlow 名字的由来。一旦输入端的所有张量准备好,节点将被分配到各种计算设备完成异步并行地执行运算。

它灵活的架构让你可以在多种平台上展开计算,例如台式计算机中的一个或多个CPU(或GPU),服务器,移动设备等等。

  • 使用图 (graph) 来表示计算任务
  • 在被称之为 会话 (Session) 的上下文 (context) 中执行图
  • 使用 tensor 表示数据
  • 通过 变量 (Variable) 维护状态
  • 使用 feed 和 fetch 可以为任意的操作(arbitrary operation) 赋值或者从其中获取数据

3.2. 线性预测demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

import tensorflow as tf
import numpy as np

# create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

### create tensorflow structure start ###
Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))

y = Weights*x_data + biases

loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

print(tf.__version__)
if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
init = tf.initialize_all_variables()
else:
init = tf.global_variables_initializer()

### create tensorflow structure end ###

sess = tf.Session()
sess.run(init)

for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(Weights), sess.run(biases))

x_data和y_data是真实数据,y是预测出的数据,loss代表预测误差,optimizer代表调优方法,train代表最小化误差,sess.run(train)代表执行一次调优。

3.3. Session

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import tensorflow as tf

m1 = tf.constant([[2, 2]])
m2 = tf.constant([[3],
[3]])
dot_operation = tf.matmul(m1, m2)

print(dot_operation) # wrong! no result

# method1 use session
sess = tf.Session()
result = sess.run(dot_operation)
print(result)
sess.close()

# method2 use session
with tf.Session() as sess:
result_ = sess.run(dot_operation)
print(result_)

从上面代码的执行结果可以看出,以tf开头的那些语句,并没有立即执行,而是在sess.run()的时候才会执行。

3.4. Variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import tensorflow as tf

var = tf.Variable(0) # our first variable in the "global_variable" set

add_operation = tf.add(var, 1)
update_operation = tf.assign(var, add_operation)

init = tf.global_variables_initializer()

with tf.Session() as sess:
# once define variables, you have to initialize them by doing this
sess.run(init)
for _ in range(3):
sess.run(update_operation)
print(sess.run(var))

如果定义了变量,一定要global_variables_initializer并且run。

3.5. placeholder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import tensorflow as tf

x1 = tf.placeholder(dtype=tf.float32, shape=None)
y1 = tf.placeholder(dtype=tf.float32, shape=None)
z1 = x1 + y1

x2 = tf.placeholder(dtype=tf.float32, shape=[2, 1])
y2 = tf.placeholder(dtype=tf.float32, shape=[1, 2])
z2 = tf.matmul(x2, y2)

with tf.Session() as sess:
# when only one operation to run
z1_value = sess.run(z1, feed_dict={x1: 1, y1: 2})

# when run multiple operations
z1_value, z2_value = sess.run(
[z1, z2], # run them together
feed_dict={
x1: 1, y1: 2,
x2: [[2], [2]], y2: [[3, 3]]
})
print(z1_value)
print(z2_value)

在计算时,给x1赋值1,给y1赋值2。

3.6. 激励函数

激励函数是用来激活神经元的函数。当你的神经网络层只有两三层,不是很多的时候, 对于隐藏层,使用任意的激励函数;在多层神经网络中,则要有所选择。在卷积神经网络中,一般使用relu;在循环神经网络中,一般使用relu或者tanh。

4. 神经网络

4.1. 添加层

1
2
3
4
5
6
7
8
9
10
11
12
import tensorflow as tf

def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs

4.2. 建造神经网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import tensorflow as tf
import numpy as np

def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs

# Make up some real data
x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)

# the error between prediction and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

# important step
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for i in range(1000):
# training
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
# to see the step improvement
print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))

4.3. 结果可视化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs

# Make up some real data
x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)

# the error between prediction and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

# important step
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data, y_data)
plt.ion()
plt.show()
# plt.show(block=False)

for i in range(1000):
# training
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
# to see the step improvement
# print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
try:
ax.lines.remove(lines[0])
except Exception:
pass
prediction_value = sess.run(prediction,feed_dict={xs: x_data})
lines = ax.plot(x_data, prediction_value, 'r-',lw=5)
plt.pause(0.1)

4.4. 加速神经网络训练

加速神经网络训练的方法:

  • Stochastic Gradient Descent (SGD)
  • Momentum
  • AdaGrad
  • RMSProp
  • Adam

具体参考《加速神经网络训练 (Speed Up Training)》

4.5. optimizer

相关资料:

  • TensorFlow可用的optimizer 链接
  • 各种Optimizer 的对比 链接

一般使用GradientDescentOptimizer就够了,往后学习的话,可以使用MomentumOptimizer、AdamOptimizer、RMSPropOptimizer。

5. 源码分享

https://github.com/voidking/Tensorflow-Tutorial.git

6. 书签

TensorFlow官网

Tensorflow游乐场

莫烦Tensorflow教程系列

TensorFlow 官方文档中文版

TensorFlow中文社区

TensorFlow入门

youtube CS 20SI: Tensorflow for Deep Learning Research

Tensorflow 的处理结构及基本使用

2016年不可错过的21个深度学习视频、教程和课程

  • 本文作者: 好好学习的郝
  • 原文链接: https://www.voidking.com/dev-tensorflow-start/
  • 版权声明: 本文采用 BY-NC-SA 许可协议,转载请注明出处!源站会即时更新知识点并修正错误,欢迎访问~
  • 微信公众号同步更新,欢迎关注~