接上一篇博客[TensorFlow] TensorFlow Get Started (1)。
tf.train API
TensorFlow提供了optimizers,可以缓慢地更改每个变量,最大程度地最小化损失函数(loss function)。最简单的optimizer是gradient descent(梯度下降)。它根据损失函数的梯度方向改变变量的值。一般,人工计算梯度是复杂的而且容易出错,TensorFlow提供了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]))
得到结果:
[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]
实际上我们已经做了机器学习了hhh这就是一个线性回归,虽然这不需要太多TensorFlow的核心代码。
Complete program
下面给出线性回归的完整代码:
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))
得到结果:
W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11
通过TensorBoard可视化得到计算图:
图1 线性回归模型计算图
tf.contrib.learn
tf.contrib.learn是一个更高层次的TensorFlow库,简化了机器学习的机制。
Basic usage
使用tf.contrib.learn,线性回归可以变得更简单:
import tensorflow as tf
# NumPy is often used to load, manipulate and preprocess data.
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)
得到结果:
{'global_step': 1000, 'loss': 1.9650059e-11}
A custom model
tf.contrib.learn并不局限于预先定义好的模型。对于没有在TensorFlow中预定义的模型,我们也可以自己定义。
要定义能够与tf.contrib.learn一起使用的自定义模型,我们需要使用tf.contrib.learn.Estimator。 tf.contrib.learn.LinearRegressor实际上是一个tf.contrib.learn.Estimator的子类。 在这里,我们不创建新的Estimator子类,我们只是给Estimator提供了一个函数model_fn,它告诉tf.contrib.learn如何评估预测,训练和估计损失。代码如下:
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))
得到结果:
{'loss': 5.9819476e-11, 'global_step': 1000}
Next steps
TensorFlow官网上有更多教程:MNIST for beginners、Deep MNIST for experts。我也可能在之后的博客中继续介绍TensorFlow。