TensorFlow(2): HelloWorld与基本流程

  |   0 评论   |   2,571 浏览

HelloWorld

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow!

>>> a = tf.constant(10)
>>> b = tf.constant(20)
>>> print sess.run(a+b)
30

基本概念

来源

Tensor

tensorflow的数据组成单元为tensor,一个tensor由任意维度的数组组成,数组是由最小集合变换而来。

   3 # a rank 0 tensor; a scalar with shape []
   [1., 2., 3.] # a rank 1 tensor; 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]

Computational Graph

计算图是将tensorflow的操作放入图的结点中。

最简单的图

创建一个最简单的图

import tensorflow as tf
node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)

输出

>>> node1 = tf.constant(3.0, dtype=tf.float32)

>>> node2 = tf.constant(4.0) # also tf.float32 implicitly

>>> print(node1, node2)
Tensor("Const 4:0", shape=(), dtype=float32) Tensor("Const 5:0", shape=(), dtype=float32)

运行上面的图

sess = tf.Session()
print(sess.run([node1, node2]))

输出

>>> sess = tf.Session()

>>> print(sess.run([node1, node2]))
[3.0, 4.0]

复杂一些的图

node3 = tf.add(node1, node2)
print("node3:", node3)
print("sess.run(node3):", sess.run(node3))

输出

>>> node3 = tf.add(node1, node2)

>>> print("node3:", node3)
('node3:', <tf.Tensor 'Add:0' shape=() dtype=float32>)

>>> print("sess.run(node3):", sess.run(node3))
('sess.run(node3):', 7.0)

可以通过 TensorBoard 来看到可视化的图

占位符

前面的图都是常量,通过占位符来实现变量的功能

声明占位符

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]}))

输出

>>> print(sess.run(adder_node, {a: 3, b: 4.5}))
7.5
>>> print(sess.run(adder_node, {a: [1, 3], b: [2, 4]}))
[ 3.  7.]

占位符复杂操作

构建一个稍微复杂一点的含有占位符的操作

add_and_triple = adder_node * 3.
print(sess.run(add_and_triple, {a: 3, b: 4.5}))

输出

>>> print(sess.run(add_and_triple, {a: 3, b: 4.5}))
22.5

可训练的变量

训练一个有变量的模型

W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W*x + b

初始化所有的变量

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

运行

print(sess.run(linear_model, {x: [1, 2, 3, 4]}))

输出

>>> print(sess.run(linear_model, {x: [1, 2, 3, 4]}))
[ 0.          0.30000001  0.60000002  0.90000004]

要评估模型的好坏,需要一个y变量来提供预期的值,然后编写损失函数loss

y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_deltas)
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))

输出

>>> print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))
23.66

这里W和b是初始化出来的不准确。为了降低loss值,我们手动指定正确的W和b的值。

fixW = tf.assign(W, [-1.])
fixb = tf.assign(b, [1.])
sess.run([fixW, fixb])
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))

输出

>>> sess.run([fixW, fixb])
[array([-1.], dtype=float32), array([ 1.], dtype=float32)]
>>> print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))
0.0

训练tf.train api

tensorflow提供优化算子,可以缓慢调整每一个变量的值,来最小化损失函数loss。

最简单的优化算子为梯度下降算子,对应的tf方法为tf.gradients。

示例

optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
sess.run(init) # reset values to incorrect defaults.
for i in range(1000):
  sess.run(train, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})

print(sess.run([W, b]))

输出

>>> print(sess.run([W, b]))
[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]

完整的代码

import tensorflow as tf

# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=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))

输出

W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11

tf.estimator

tf.estimator是高级的tf库,来简化机器学习操作,包括

  1. 进行训练
  2. 进行估算
  3. 管理数据

同时包含很多常用的模型

入门

官网上的代码

# NumPy is often used to load, manipulate and preprocess data.
import numpy as np
import tensorflow as tf

# Declare list of features. We only have one numeric feature. There are many
# other types of columns that are more complicated and useful.
feature_columns = [tf.feature_column.numeric_column("x", shape=[1])]

# An estimator is the front end to invoke training (fitting) and evaluation
# (inference). There are many predefined types like linear regression,
# linear classification, and many neural network classifiers and regressors.
# The following code provides an estimator that does linear regression.
estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns)

# TensorFlow provides many helper methods to read and set up data sets.
# Here we use two data sets: one for training and one for evaluation
# We have to tell the function how many batches
# of data (num_epochs) we want and how big each batch should be.
x_train = np.array([1., 2., 3., 4.])
y_train = np.array([0., -1., -2., -3.])
x_eval = np.array([2., 5., 8., 1.])
y_eval = np.array([-1.01, -4.1, -7, 0.])
input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_train}, y_train, batch_size=4, num_epochs=None, shuffle=True)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_train}, y_train, batch_size=4, num_epochs=1000, shuffle=False)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_eval}, y_eval, batch_size=4, num_epochs=1000, shuffle=False)

# We can invoke 1000 training steps by invoking the  method and passing the
# training data set.
estimator.train(input_fn=input_fn, steps=1000)

# Here we evaluate how well our model did.
train_metrics = estimator.evaluate(input_fn=train_input_fn)
eval_metrics = estimator.evaluate(input_fn=eval_input_fn)
print("train metrics: %r"% train_metrics)
print("eval metrics: %r"% eval_metrics)

估算子输出

>>> estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns)
INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmp4Qnyls
INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_task_type': 'worker', '_is_chief': True, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x10905c3d0>, '_save_checkpoints_steps': None, '_keep_checkpoint_every_n_hours': 10000, '_service': None, '_num_ps_replicas': 0, '_tf_random_seed': None, '_master': '', '_num_worker_replicas': 1, '_task_id': 0, '_log_step_count_steps': 100, '_model_dir': '/var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmp4Qnyls', '_save_summary_steps': 100}

训练输出

>>> estimator.train(input_fn=input_fn, steps=1000)
INFO:tensorflow:Create CheckpointSaverHook.
2017-12-10 10:55:36.373972: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
INFO:tensorflow:Saving checkpoints for 1 into /var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmp4Qnyls/model.ckpt.
INFO:tensorflow:loss = 14.0, step = 1
INFO:tensorflow:global_step/sec: 1063.92
INFO:tensorflow:loss = 0.269534, step = 101 (0.094 sec)
INFO:tensorflow:global_step/sec: 1066.04
INFO:tensorflow:loss = 0.0797396, step = 201 (0.094 sec)
INFO:tensorflow:global_step/sec: 1062.66
INFO:tensorflow:loss = 0.0232967, step = 301 (0.093 sec)
INFO:tensorflow:global_step/sec: 1237.36
INFO:tensorflow:loss = 0.00372493, step = 401 (0.081 sec)
INFO:tensorflow:global_step/sec: 1371.82
INFO:tensorflow:loss = 0.000646794, step = 501 (0.073 sec)
INFO:tensorflow:global_step/sec: 1410.69
INFO:tensorflow:loss = 0.000262144, step = 601 (0.071 sec)
INFO:tensorflow:global_step/sec: 1411.79
INFO:tensorflow:loss = 3.975e-05, step = 701 (0.071 sec)
INFO:tensorflow:global_step/sec: 1436.49
INFO:tensorflow:loss = 2.31565e-05, step = 801 (0.070 sec)
INFO:tensorflow:global_step/sec: 1358.66
INFO:tensorflow:loss = 2.64456e-06, step = 901 (0.074 sec)
INFO:tensorflow:Saving checkpoints for 1000 into /var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmp4Qnyls/model.ckpt.
INFO:tensorflow:Loss for final step: 3.15212e-07.
<tensorflow.python.estimator.canned.linear.LinearRegressor object at 0x100536a50>

训练集的metrics

>>> train_metrics = estimator.evaluate(input_fn=train_input_fn)
INFO:tensorflow:Starting evaluation at 2017-12-10-02:56:18
INFO:tensorflow:Restoring parameters from /var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmp4Qnyls/model.ckpt-1000
INFO:tensorflow:Finished evaluation at 2017-12-10-02:56:18
INFO:tensorflow:Saving dict for global step 1000: average_loss = 1.19704e-07, global_step = 1000, loss = 4.78816e-07

评估集的metrics

>>> eval_metrics = estimator.evaluate(input_fn=eval_input_fn)
INFO:tensorflow:Starting evaluation at 2017-12-10-02:56:39
INFO:tensorflow:Restoring parameters from /var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmp4Qnyls/model.ckpt-1000
INFO:tensorflow:Finished evaluation at 2017-12-10-02:56:40
INFO:tensorflow:Saving dict for global step 1000: average_loss = 0.00255552, global_step = 1000, loss = 0.0102221
>>> print("train metrics: %r"% train_metrics)
train metrics: {'average_loss': 1.1970396e-07, 'global_step': 1000, 'loss': 4.7881582e-07}
>>> print("eval metrics: %r"% eval_metrics)
eval metrics: {'average_loss': 0.0025555168, 'global_step': 1000, 'loss': 0.010222067}

自定义模型

tf.estimator 并没有限定只能使用内置的模型.

这里给一个使用自定义的LinearRegressor模型的例子,同时复用上面的主要逻辑.

使用自定义模型,关键在于tf.estimator.Estimator类的使用.

tf.estimator.LinearRegressor正是类tf.estimator.Estimator的一个子类

所以我们只需要自定义方法model_fn,来告诉tf.estimator如何评估预测,训练和损失函数. 示例如下

import numpy as np
import tensorflow as tf

# Declare list of features, we only have one real-valued feature
def model_fn(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))
  # EstimatorSpec connects subgraphs we built to the
  # appropriate functionality.
  return tf.estimator.EstimatorSpec(
      mode=mode,
      predictions=y,
      loss=loss,
      train_op=train)

estimator = tf.estimator.Estimator(model_fn=model_fn)
# define our data sets
x_train = np.array([1., 2., 3., 4.])
y_train = np.array([0., -1., -2., -3.])
x_eval = np.array([2., 5., 8., 1.])
y_eval = np.array([-1.01, -4.1, -7., 0.])
input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_train}, y_train, batch_size=4, num_epochs=None, shuffle=True)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_train}, y_train, batch_size=4, num_epochs=1000, shuffle=False)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_eval}, y_eval, batch_size=4, num_epochs=1000, shuffle=False)

# train
estimator.train(input_fn=input_fn, steps=1000)
# Here we evaluate how well our model did.
train_metrics = estimator.evaluate(input_fn=train_input_fn)
eval_metrics = estimator.evaluate(input_fn=eval_input_fn)
print("train metrics: %r"% train_metrics)
print("eval metrics: %r"% eval_metrics)

声明模型输出

>>> estimator = tf.estimator.Estimator(model_fn=model_fn)
INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmpzFr6BE
INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_task_type': 'worker', '_is_chief': True, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x10b3e5950>, '_save_checkpoints_steps': None, '_keep_checkpoint_every_n_hours': 10000, '_service': None, '_num_ps_replicas': 0, '_tf_random_seed': None, '_master': '', '_num_worker_replicas': 1, '_task_id': 0, '_log_step_count_steps': 100, '_model_dir': '/var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmpzFr6BE', '_save_summary_steps': 100}

训练输出

>>> estimator.train(input_fn=input_fn, steps=1000)
INFO:tensorflow:Create CheckpointSaverHook.

INFO:tensorflow:Saving checkpoints for 1 into /var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmpzFr6BE/model.ckpt.
INFO:tensorflow:loss = 238.858812705, step = 1
INFO:tensorflow:global_step/sec: 1083.23
INFO:tensorflow:loss = 0.021875679502, step = 101 (0.092 sec)
INFO:tensorflow:global_step/sec: 1324.24
INFO:tensorflow:loss = 0.00156988131769, step = 201 (0.076 sec)
INFO:tensorflow:global_step/sec: 1243.33
INFO:tensorflow:loss = 0.000146581933847, step = 301 (0.080 sec)
INFO:tensorflow:global_step/sec: 946.093
INFO:tensorflow:loss = 1.70844717209e-05, step = 401 (0.105 sec)
INFO:tensorflow:global_step/sec: 1142.11
INFO:tensorflow:loss = 1.4791159315e-06, step = 501 (0.087 sec)
INFO:tensorflow:global_step/sec: 1191.06
INFO:tensorflow:loss = 1.86484431302e-08, step = 601 (0.084 sec)
INFO:tensorflow:global_step/sec: 1416.25
INFO:tensorflow:loss = 6.86582490973e-09, step = 701 (0.071 sec)
INFO:tensorflow:global_step/sec: 1439.7
INFO:tensorflow:loss = 1.24622057759e-09, step = 801 (0.069 sec)
INFO:tensorflow:global_step/sec: 1402.27
INFO:tensorflow:loss = 4.39855319638e-11, step = 901 (0.071 sec)
INFO:tensorflow:Saving checkpoints for 1000 into /var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmpzFr6BE/model.ckpt.
INFO:tensorflow:Loss for final step: 9.47125781477e-12.
<tensorflow.python.estimator.estimator.Estimator object at 0x10b309e90>

估算输出

>>> train_metrics = estimator.evaluate(input_fn=train_input_fn)
INFO:tensorflow:Starting evaluation at 2017-12-10-03:23:53
INFO:tensorflow:Restoring parameters from /var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmpzFr6BE/model.ckpt-1000
INFO:tensorflow:Finished evaluation at 2017-12-10-03:23:53
INFO:tensorflow:Saving dict for global step 1000: global_step = 1000, loss = 7.43039e-12
>>> eval_metrics = estimator.evaluate(input_fn=eval_input_fn)
INFO:tensorflow:Starting evaluation at 2017-12-10-03:24:09
INFO:tensorflow:Restoring parameters from /var/folders/cs/dwhbchds5x37zkzz2lbnvt_c0000gn/T/tmpzFr6BE/model.ckpt-1000
INFO:tensorflow:Finished evaluation at 2017-12-10-03:24:09
INFO:tensorflow:Saving dict for global step 1000: global_step = 1000, loss = 0.0101005

结果输出

>>> print("train metrics: %r"% train_metrics)
train metrics: {'loss': 7.4303914e-12, 'global_step': 1000}
>>> print("eval metrics: %r"% eval_metrics)
eval metrics: {'loss': 0.010100459, 'global_step': 1000}

评论

发表评论

validate