tensorflow__简单实现小批量梯度下降

TensorFlow 程序通常分为两部分:
第一部分构建计算图谱(这称为构造阶段),
第二部分运行它(这是执行阶段)。

建设阶段通常构建一个表示ML模型的计算图谱,然后对其进行训练计算。执行
阶段通常运行循环,重复地求出训练步骤,逐渐改进模型参数

0、加载包和数据

## 加载包
import numpy as np 
from sklearn.datasets import fetch_california_housing
import tensorflow as tf 
from sklearn.preprocessing import StandardScaler

## 加载数据
housing = fetch_california_housing()
m, n = housing.data.shape
scl = StandardScaler()
housing_scl = scl.fit_transform(housing.data)
housing_sc_bias = np.c_[np.ones((m, 1)), housing_scl]

1、构建计算图谱

# 用占位符,不对x的行数做限制
x = tf.placeholder(tf.float32, shape = (None, n + 1), name = 'x')
y = tf.placeholder(tf.float32, shape = (None,  1), name = 'y')
# 给theta 随机初始值
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0, seed = 42), name='theta')

# 计算误差
y_prd = tf.matmul(x, theta, name = 'predictions')
error = y_prd - y
# 类似 from functools import reduce
#reduce(lambda x1, x2: x1 + x2 ,[1,2,3])
mse = tf.reduce_mean(tf.square(error), name = 'mse')
# 梯度下降优化器
learn_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learn_rate)
training_op = optimizer.minimize(mse)

2、执行阶段

init = tf.global_variables_initializer() # 初始化变量
n_epochs = 10   
batch_size = 100 
n_batches = np.int(np.ceil(m / batch_size))

def fetch_batch(epoch, batch_index, batch_size):
    # 随机获取小批量数据
    np.random.seed(epoch * n_batches + batch_index) 
    indices = np.random.randint(m, size = batch_size)
    return housing_sc_bias[indices] , housing.target.reshape(-1, 1)[indices] 

with tf.Session() as sess:
    # 初始化变量
    sess.run(init)
    for epoch in range(n_epochs):  # 总共循环次数
        for batch_index in range(n_batches):
            x_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
            # 数据导入 类似于 sklearn.class.fit
            sess.run(training_op, feed_dict = {x : x_batch, y : y_batch})
    best_theta = theta.eval()

print(best_theta)

你可能感兴趣的:(机器学习实战,Python,算法)