我爱编程

Tensorflow的基本使用 以线性回归为例

2017-03-02  本文已影响0人  嗯我是一个胖子

参考来源:博客链接

使用线性模型来对数据点进行建模。线性模型的数学表示是:

  y = W.x + b
   Where:
   x: house size, in sqm
   y: predicted house price, in $

(1) Tensors
TensorFlow中使用tensor数据结构(实际上就是一个多维数据)表示所有的数据,并在图计算中的节点之间传递数据一个tensor具有固定的类型、级别和大小,更加深入理解这些概念可参考Rank, Shape, and Type

(2) 在线性模型中,tensorflow 的基本组件是:

0.00001 是我们每次进行训练时在最陡的梯度方向上所采取的「步」长;它也被称作学习率(learning rate).

(3)训练模型
训练包含以预先确定好的次数执行梯度下降,或者是直到成本函数低于某个预先确定的临界值为止。TensorFlow的使用,所有变量都需要在训练开始时进行初始化,否则它们可能会带有之前执行过程中的残余值。

    init = tf.initialize_all_variables()  

虽然 TensorFlow 是一个 Python 库,Python 是一种解释性的语言,但是默认情况下不把 TensorFlow 运算用作解释性能的原因,因此不执行上面的 init 。相反 TensorFlow 是在一个会话中进行;创建一个会话 (sess) 然后使用 sess.run() 去执行.

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

完整代码

import numpy as np
import tensorflow as tf

# Model linear regression y = Wx + b
x = tf.placeholder(tf.float32, [None, 1])
W = tf.Variable(tf.zeros([1,1]))
b = tf.Variable(tf.zeros([1]))
product = tf.matmul(x,W)
y = product + b
y_ = tf.placeholder(tf.float32, [None, 1])

# Cost function sum((y_-y)**2)
cost = tf.reduce_mean(tf.square(y_-y))

# Training using Gradient Descent to minimize cost
train_step = tf.train.GradientDescentOptimizer(0.0000001).minimize(cost)

sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)
steps = 1000
for i in range(steps):
 # Create fake data for y = W.x + b where W = 2, b = 0
 xs = np.array([[i]])
 ys = np.array([[2*i]])
 # Train
 feed = { x: xs, y_: ys }
 sess.run(train_step, feed_dict=feed)
 print("After %d iteration:" % i)
 print("W: %f" % sess.run(W))
 print("b: %f" % sess.run(b))
 # Suggested by @jihobak
 print("cost: %f" % sess.run(cost, feed_dict=feed))

# NOTE: W should be close to 2, and b should be close to 0

在上面的训练中,我们在每个 epoch 送入单个数据点,这被称为随机梯度下降(stochastic gradient descent)。我们也可以在每个 epoch 送入一堆数据点,这被称为 mini-batch 梯度下降,或者甚至在一个 epoch 一次性送入所有的数据点,这被称为 batch 梯度下降。
请看下图的比较,注意这 3 张图的 2 处不同:

# * all_xs: 所有的特征值
# * all_ys: 所有的输出值
# datapoint_size: all_xs/all_ys 中点/项的数量
# batch_size: 配置如下:
#             1: 随机模型
#            integer < datapoint_size: mini-batch模式
#             datapoint_size: batch模式
# i: 当前epoch数量

if datapoint_size == batch_size:
   Batch 模式,所以选择所有数据点从 index 0 开始
  batch_start_idx = 0
elif datapoint_size < batch_size:
   不可能
  raise ValueError(“datapoint_size: %d, must be greater than         
                    batch_size: %d” % (datapoint_size, batch_size))
else:
   随机/mini-batch模式: 从所有可能的数据点中分批选择数据点
  batch_start_idx = (i * batch_size) % (datapoint_size — batch_size)
  batch_end_idx = batch_start_idx + batch_size
  batch_xs = all_xs[batch_start_idx:batch_end_idx]
  batch_ys = all_ys[batch_start_idx:batch_end_idx]

 将分批的数据点定义为xs, ys, 它们会被送入 'train_step'训练步骤
xs = np.array(batch_xs)
ys = np.array(batch_ys)
上一篇下一篇

猜你喜欢

热点阅读