Table of Contents generated with DocToc
Getting Started With TensorFlow
Tensors
TensorFlow里的数据形式称为tensor, tensor的rank即为数据的维度。
3 # a rank 0 tensor; this is a scalar with shape []
[1. ,2., 3.] # a rank 1 tensor; this is a vector with shape [3]
[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]
TensorFlow Core tutorial
Importing TensorFlow
import tensorflow as tf
The Computational Graph
TensorFlow的过程可以分为两个部分:
- 构建Computational Graph;
- 运行Computational Graph。
一个Computational Graph由许多节点(node)组成。例如:
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)
# Output: Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)
上面就是构建的过程,下面是运行的过程, 运行前需要创建一个Session
对象,tensorflow所有的运行都是通过其中的run
函数来实现:
sess = tf.Session()
print(sess.run([node1, node2]))
# Output: [3.0, 4.0]
# 这里看出直接print node1输出的是node1的类型,如果print他的值,就需要经过sess.run函数
通过sess.run
来实现一个加法的操作:
node3 = tf.add(node1, node2)
print("node3: ", node3)
print("sess.run(node3): ",sess.run(node3))
# Output
# node3: Tensor("Add_2:0", shape=(), dtype=float32)
# sess.run(node3): 7.0
Computational Graph如下图所示,可以通过TensorBoard来可视化Graph: ~getting_started_add
上面的例子我们
- 用
tf.constant
来创建一个常量, 创建后不可被修改; - 那如果我们想创建一个变量,先不赋值,再
run
的过程在赋值传参,可以使用tf.placeholder
, - 那如果想创建一个变量,先赋值,然后在
run
的过程中可以重新赋值,可以使用tf.Variable
,重新赋值使用tf.assign
.
注意: 上面所说tf.Variable
可以赋值,但是需要显示调用tf.global_variables_initializer()
才会将值赋给变量,否则直接print会报错,具体怎么操作,见下面代码。
示例:
# ------ tf.placeholder ------
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b # + provides a shortcut for tf.add(a, b)
print(sess.run(adder_node, {a: 3, b:4.5}))
print(sess.run(adder_node, {a: [1,3], b: [2, 4]}))
add_and_triple = adder_node * 3.
print(sess.run(add_and_triple, {a: 3, b:4.5}))
# Output:
# 7.5
# [ 3. 7.]
# 22.5
# ------ tf.Variable ------
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
# print sess.run([W, b]) # error will occur if you uncomment this line
init = tf.global_variables_initializer()
sess.run(init)
print sess.run([W, b])
print(sess.run(linear_model, {x:[1,2,3,4]}))
# Output
# [array([ 0.30000001], dtype=float32), array([-0.30000001], dtype=float32)]
# [ 0. 0.30000001 0.60000002 0.90000004]
# ------ tf.assign ------
tf.assign(W, [-1.])
tf.assign(b, [1.])
print sess.run([W, b])
# Output
# [array([-1.], dtype=float32), array([ 1.], dtype=float32)]
小结:上面讲了几种赋值方式:tf.constant
, tf.Variable
, tf.placeholder
, tf.assign
。然后讲了tensorflow的运行机制,先构建好computational graph,然后放进sess.run
即可出结果。
tf.train API
一个训练的过程,示例如下:
import numpy as np
import tensorflow as tf
# Model parameters
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
sess.run(train, {x:x_train, y:y_train})
# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x:x_train, y:y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))
tf.contrib.learn API
tf.contrib.learn
是high-level的tensorflow库,包含很多机器学习算法,可以定义很多模型。
- 例如调用自带的现行回归模型:
import tensorflow as tf
import numpy as np
# Declare list of features. We only have one real-valued feature. There are many
# other types of columns that are more complicated and useful.
features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
# An estimator is the front end to invoke training (fitting) and evaluation
# (inference). There are many predefined types like linear regression,
# logistic regression, linear classification, logistic classification, and
# many neural network classifiers and regressors. The following code
# provides an estimator that does linear regression.
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)
# TensorFlow provides many helper methods to read and set up data sets.
# Here we use `numpy_input_fn`. We have to tell the function how many batches
# of data (num_epochs) we want and how big each batch should be.
x = np.array([1., 2., 3., 4.])
y = np.array([0., -1., -2., -3.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x}, y, batch_size=4,
num_epochs=1000)
# We can invoke 1000 training steps by invoking the `fit` method and passing the
# training data set.
estimator.fit(input_fn=input_fn, steps=1000)
# Here we evaluate how well our model did. In a real example, we would want
# to use a separate validation and testing data set to avoid overfitting.
estimator.evaluate(input_fn=input_fn)
# result: {'global_step': 1000, 'loss': 1.9650059e-11}
- 调用自己定义的模型
import numpy as np
import tensorflow as tf
# Declare list of features, we only have one real-valued feature
def model(features, labels, mode):
# Build a linear model and predict values
W = tf.get_variable("W", [1], dtype=tf.float64)
b = tf.get_variable("b", [1], dtype=tf.float64)
y = W*features['x'] + b
# Loss sub-graph
loss = tf.reduce_sum(tf.square(y - labels))
# Training sub-graph
global_step = tf.train.get_global_step()
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = tf.group(optimizer.minimize(loss),
tf.assign_add(global_step, 1))
# ModelFnOps connects subgraphs we built to the
# appropriate functionality.
return tf.contrib.learn.ModelFnOps(
mode=mode, predictions=y,
loss= loss,
train_op=train)
estimator = tf.contrib.learn.Estimator(model_fn=model)
# define our data set
x=np.array([1., 2., 3., 4.])
y=np.array([0., -1., -2., -3.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x": x}, y, 4, num_epochs=1000)
# train
estimator.fit(input_fn=input_fn, steps=1000)
# evaluate our model
print(estimator.evaluate(input_fn=input_fn, steps=10))
# result: {'loss': 5.9819476e-11, 'global_step': 1000}
其中一些函数还没弄明白怎么调用,先看完整个官方文档再回来纠缠细节。