|
| 1 | +**TensorFlow**是Google维护的一个基于数据图的可扩展的机器学习框架,其项目地址位于 https://github.com/tensorflow/tensorflow ,我们可以从项目中找到官方的文档说明。 |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +TensorFlow提供了多层的API接口。**"TensorFlow Core"**为其最底层API接口,通过它,我们能够对开发流程进行更全面的把控,而上层的API接口都是基于"TensorFlow Core"进行封装的,这样我们能对"TensorFlow Core"更快上手和使用,从而使复杂流程变得简单高效。 |
| 6 | + |
| 7 | +## 什么是"Tensor" |
| 8 | + |
| 9 | +在TensorFlow中,数据的基本单位是**Tensor**,中文名叫**"张量"**,每个Tensor是由一个**多维数组**构成的,而Tensor的阶数,就是数据的维度。在Python语言中, 返回的Tensor是numpy ndarray对象。 |
| 10 | + |
| 11 | +比如说: |
| 12 | + |
| 13 | + 3 # 0阶Tensor,大小为[] |
| 14 | + [1., 2., 3.] # 1阶Tensor,大小为[3] |
| 15 | + [[1., 2., 3.], [4., 5., 6.]] # 2阶Tensor,大小为[2, 3] |
| 16 | + [[[1., 2., 3.]], [[7., 8., 9.]]] # 3阶Tensor,大小为[2, 1, 3] |
| 17 | + |
| 18 | + |
| 19 | +## 从"TensorFlow Core"入手 |
| 20 | + |
| 21 | +在TensorFlow中一系列操作可以由**"计算图"**表示,也就是一系列节点构成的图。程序通常被组织成一个构建阶段和一个执行阶段,在**构建阶段**,操作的执行步骤被描述成一个图,而在**执行阶段**, 使用会话执行执行图中的操作。 |
| 22 | + |
| 23 | +在图中,每个节点可以输入0或个多个Tensor,输出1个Tensor。 |
| 24 | + |
| 25 | +#### 导入tensorflow |
| 26 | + |
| 27 | + import tensorflow as tf |
| 28 | + |
| 29 | +#### Session会话 |
| 30 | + |
| 31 | +首先,创建一个session会话,我们必须在**会话**中执行图: |
| 32 | + |
| 33 | + # session |
| 34 | + sess = tf.Session() |
| 35 | + |
| 36 | +#### Constant常量与Variable变量 |
| 37 | + |
| 38 | +Constant和Variable是Tensor节点的两种类型,对应数据的常量与变量的赋值。对于变量来说,需要在会话中进行初始化才能真正赋值: |
| 39 | + |
| 40 | + # One type of the Tensor nodes is Constant |
| 41 | + node1 = tf.constant(3.0, dtype=tf.float32) |
| 42 | + node2 = tf.constant(4.0) # also tf.float32 implicitly |
| 43 | + print(sess.run([node1, node2])) # [3.0, 4.0] |
| 44 | + # One type of the Tensor nodes is Variable |
| 45 | + W = tf.Variable([.3], dtype=tf.float32) |
| 46 | + b = tf.Variable([-.3], dtype=tf.float32) |
| 47 | + init = tf.global_variables_initializer() |
| 48 | + sess.run(init) |
| 49 | + print(sess.run([W, b])) |
| 50 | + # [array([ 0.30000001], dtype=float32), array([-0.30000001], dtype=float32)] |
| 51 | + |
| 52 | +#### placeholder占位符 |
| 53 | + |
| 54 | +placeholder表示声明一个数据变量,而后可以定义对它的操作。在后面执行图的过程中,我们可以通过**字典**传参的方式将占位符赋值。 |
| 55 | + |
| 56 | + # placeholder |
| 57 | + # A placeholder is a promise to provide a value later. |
| 58 | + a = tf.placeholder(tf.float32) |
| 59 | + b = tf.placeholder(tf.float32) |
| 60 | + adder_node = a + b # + provides a shortcut for tf.add(a, b) |
| 61 | + print(sess.run(adder_node, {a: 3, b: 4.5})) # 7.5 |
| 62 | + print(sess.run(adder_node, {a: [1, 3], b: [2, 4]})) # [ 3. 7.] |
| 63 | + add_and_triple = adder_node * 3. |
| 64 | + print(sess.run(add_and_triple, {a: 3, b: 4.5})) # 22.5 |
| 65 | + |
| 66 | +#### 创建模型 |
| 67 | + |
| 68 | +我们可以定义一个模型,比如说线性模型,通过指定自变量的值,输出其对应的计算结果。 |
| 69 | + |
| 70 | + linear_model = W*x + b |
| 71 | + print(sess.run(linear_model, {x: [1, 2, 3, 4]})) |
| 72 | + # [ 0. 0.30000001 0.60000002 0.90000004] |
| 73 | + |
| 74 | +#### 评估模型 |
| 75 | + |
| 76 | +我们还可以定义一个损失函数,比如采用误差平方和(SSE)来定义损失函数,通过损失函数计算结果来判断模型的好坏。 |
| 77 | + |
| 78 | + # produce the loss value |
| 79 | + y = tf.placeholder(tf.float32) |
| 80 | + squared_deltas = tf.square(linear_model - y) |
| 81 | + loss = tf.reduce_sum(squared_deltas) |
| 82 | + print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})) # 23.66 |
| 83 | + |
| 84 | +#### 训练模型 |
| 85 | + |
| 86 | +TensorFlow中定义了一系列优化器,比如采用梯度下降法(Gradient Descent)来寻找损失函数最小的模型参数。 |
| 87 | + |
| 88 | + optimizer = tf.train.GradientDescentOptimizer(0.01) |
| 89 | + train = optimizer.minimize(loss) |
| 90 | + |
| 91 | + sess.run(init) # reset values to incorrect defaults. |
| 92 | + print(sess.run([W, b])) |
| 93 | + # [array([ 0.30000001], dtype=float32), array([-0.30000001], dtype=float32)] |
| 94 | + for i in range(1000): |
| 95 | + sess.run(train, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}) |
| 96 | + |
| 97 | + # produce the final model parameters |
| 98 | + print(sess.run([W, b])) |
| 99 | + # [array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)] |
| 100 | + |
| 101 | +#### TensorBoard |
| 102 | + |
| 103 | +整个机器学习的流程,可以通过TensorBoard可视化为下图表示。 |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +## 使用上层封装API |
| 108 | + |
| 109 | +上层封装的API能够大大简化机器学习流程,这里,我们使用**"tf.estimator"**来做程序示例。 |
| 110 | + |
| 111 | +#### 数据定义 |
| 112 | + |
| 113 | + x_train = np.array([1., 2., 3., 4.]) |
| 114 | + y_train = np.array([0., -1., -2., -3.]) |
| 115 | + x_eval = np.array([2., 5., 8., 1.]) |
| 116 | + y_eval = np.array([-1.01, -4.1, -7, 0.]) |
| 117 | + input_fn = tf.estimator.inputs.numpy_input_fn( |
| 118 | + {"x": x_train}, y_train, batch_size=4, num_epochs=None, shuffle=True) |
| 119 | + train_input_fn = tf.estimator.inputs.numpy_input_fn( |
| 120 | + {"x": x_train}, y_train, batch_size=4, num_epochs=1000, shuffle=False) |
| 121 | + eval_input_fn = tf.estimator.inputs.numpy_input_fn( |
| 122 | + {"x": x_eval}, y_eval, batch_size=4, num_epochs=1000, shuffle=False) |
| 123 | + |
| 124 | +#### 创建模型 |
| 125 | + |
| 126 | +创建基本的模型 |
| 127 | + |
| 128 | + feature_columns = [tf.feature_column.numeric_column("x", shape=[1])] |
| 129 | + estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns) |
| 130 | + |
| 131 | +创建定制化模型 |
| 132 | + |
| 133 | + def model_fn(features, labels, mode): |
| 134 | + # Build a linear model and predict values |
| 135 | + W = tf.get_variable("W", [1], dtype=tf.float64) |
| 136 | + b = tf.get_variable("b", [1], dtype=tf.float64) |
| 137 | + y = W*features['x'] + b |
| 138 | + # Loss sub-graph |
| 139 | + loss = tf.reduce_sum(tf.square(y - labels)) |
| 140 | + # Training sub-graph |
| 141 | + global_step = tf.train.get_global_step() |
| 142 | + optimizer = tf.train.GradientDescentOptimizer(0.01) |
| 143 | + train = tf.group(optimizer.minimize(loss), |
| 144 | + tf.assign_add(global_step, 1)) |
| 145 | + # EstimatorSpec connects subgraphs we built to the appropriate functionality. |
| 146 | + return tf.estimator.EstimatorSpec( |
| 147 | + mode=mode, |
| 148 | + predictions=y, |
| 149 | + loss=loss, |
| 150 | + train_op=train) |
| 151 | + estimator = tf.estimator.Estimator(model_fn=model_fn) |
| 152 | + |
| 153 | +#### 训练模型 |
| 154 | + |
| 155 | + estimator.train(input_fn=input_fn, steps=1000) |
| 156 | + |
| 157 | +#### 评估模型 |
| 158 | + |
| 159 | + train_metrics = estimator.evaluate(input_fn=train_input_fn) |
| 160 | + eval_metrics = estimator.evaluate(input_fn=eval_input_fn) |
| 161 | + print("train metrics: %r"% train_metrics) |
| 162 | + print("eval metrics: %r"% eval_metrics) |
| 163 | + |
0 commit comments