如何在张量流中实现多元线性随机梯度下降算法?
How to implement multivariate linear stochastic gradient descent algorithm in tensorflow?
我从单变量线性梯度下降的简单实现开始,但不知道如何将其扩展到多变量随机梯度下降算法?
单变量线性回归
import tensorflow as tf
import numpy as np
# create random data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.5
# Find values for W that compute y_data = W * x_data
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
y = W * x_data
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# Before starting, initialize the variables
init = tf.initialize_all_variables()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
for step in xrange(2001):
sess.run(train)
if step % 200 == 0:
print(step, sess.run(W))
你的问题分为两部分:
- 如何把这个问题换到更高维度space。
- 如何从批量梯度下降变为随机梯度下降。
要获得更高维度的设置,您可以定义线性问题y = <x, w>
。然后,您只需要更改变量 W
的维度以匹配 w
的维度,并将乘法 W*x_data
替换为标量积 tf.matmul(x_data, W)
并且您的代码应该 运行就好了。
要将学习方法更改为随机梯度下降,您需要使用 tf.placeholder
.
抽象成本函数的输入
一旦定义了 X
和 y_
来保存每一步的输入,您就可以构建相同的成本函数。然后,您需要通过提供适当的小批量数据来调用您的步骤。
这是一个如何实现此类行为的示例,它应该表明 W
快速收敛到 w
。
import tensorflow as tf
import numpy as np
# Define dimensions
d = 10 # Size of the parameter space
N = 1000 # Number of data sample
# create random data
w = .5*np.ones(d)
x_data = np.random.random((N, d)).astype(np.float32)
y_data = x_data.dot(w).reshape((-1, 1))
# Define placeholders to feed mini_batches
X = tf.placeholder(tf.float32, shape=[None, d], name='X')
y_ = tf.placeholder(tf.float32, shape=[None, 1], name='y')
# Find values for W that compute y_data = <x, W>
W = tf.Variable(tf.random_uniform([d, 1], -1.0, 1.0))
y = tf.matmul(X, W, name='y_pred')
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y_ - y))
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# Before starting, initialize the variables
init = tf.initialize_all_variables()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
mini_batch_size = 100
n_batch = N // mini_batch_size + (N % mini_batch_size != 0)
for step in range(2001):
i_batch = (step % n_batch)*mini_batch_size
batch = x_data[i_batch:i_batch+mini_batch_size], y_data[i_batch:i_batch+mini_batch_size]
sess.run(train, feed_dict={X: batch[0], y_: batch[1]})
if step % 200 == 0:
print(step, sess.run(W))
两个旁注:
下面的实现称为小批量梯度下降,因为在每个步骤中,梯度是使用大小为 mini_batch_size
的数据子集计算的。这是随机梯度下降的变体,通常用于稳定每一步的梯度估计。随机梯度下降可以通过设置mini_batch_size = 1
.
得到
可以在每个时期对数据集进行洗牌,以使实现更接近理论考虑。最近的一些工作还考虑只使用一次遍历数据集,因为它可以防止过度拟合。更数学更详细的解释,可以看Bottou12。这可以根据您的问题设置和您正在寻找的统计信息 属性 轻松更改。
我从单变量线性梯度下降的简单实现开始,但不知道如何将其扩展到多变量随机梯度下降算法?
单变量线性回归
import tensorflow as tf
import numpy as np
# create random data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.5
# Find values for W that compute y_data = W * x_data
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
y = W * x_data
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# Before starting, initialize the variables
init = tf.initialize_all_variables()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
for step in xrange(2001):
sess.run(train)
if step % 200 == 0:
print(step, sess.run(W))
你的问题分为两部分:
- 如何把这个问题换到更高维度space。
- 如何从批量梯度下降变为随机梯度下降。
要获得更高维度的设置,您可以定义线性问题y = <x, w>
。然后,您只需要更改变量 W
的维度以匹配 w
的维度,并将乘法 W*x_data
替换为标量积 tf.matmul(x_data, W)
并且您的代码应该 运行就好了。
要将学习方法更改为随机梯度下降,您需要使用 tf.placeholder
.
抽象成本函数的输入
一旦定义了 X
和 y_
来保存每一步的输入,您就可以构建相同的成本函数。然后,您需要通过提供适当的小批量数据来调用您的步骤。
这是一个如何实现此类行为的示例,它应该表明 W
快速收敛到 w
。
import tensorflow as tf
import numpy as np
# Define dimensions
d = 10 # Size of the parameter space
N = 1000 # Number of data sample
# create random data
w = .5*np.ones(d)
x_data = np.random.random((N, d)).astype(np.float32)
y_data = x_data.dot(w).reshape((-1, 1))
# Define placeholders to feed mini_batches
X = tf.placeholder(tf.float32, shape=[None, d], name='X')
y_ = tf.placeholder(tf.float32, shape=[None, 1], name='y')
# Find values for W that compute y_data = <x, W>
W = tf.Variable(tf.random_uniform([d, 1], -1.0, 1.0))
y = tf.matmul(X, W, name='y_pred')
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y_ - y))
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# Before starting, initialize the variables
init = tf.initialize_all_variables()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
mini_batch_size = 100
n_batch = N // mini_batch_size + (N % mini_batch_size != 0)
for step in range(2001):
i_batch = (step % n_batch)*mini_batch_size
batch = x_data[i_batch:i_batch+mini_batch_size], y_data[i_batch:i_batch+mini_batch_size]
sess.run(train, feed_dict={X: batch[0], y_: batch[1]})
if step % 200 == 0:
print(step, sess.run(W))
两个旁注:
下面的实现称为小批量梯度下降,因为在每个步骤中,梯度是使用大小为
mini_batch_size
的数据子集计算的。这是随机梯度下降的变体,通常用于稳定每一步的梯度估计。随机梯度下降可以通过设置mini_batch_size = 1
. 得到
可以在每个时期对数据集进行洗牌,以使实现更接近理论考虑。最近的一些工作还考虑只使用一次遍历数据集,因为它可以防止过度拟合。更数学更详细的解释,可以看Bottou12。这可以根据您的问题设置和您正在寻找的统计信息 属性 轻松更改。