提问人:Stefan 提问时间:12/29/2020 最后编辑:Stefan 更新时间:1/2/2021 访问量:738
使用 TensorFlow 的梯度下降比基本的 Python 实现慢得多,为什么?
Gradient descent using TensorFlow is much slower than a basic Python implementation, why?
问:
我正在学习机器学习课程。我有一个简单的线性回归 (LR) 问题来帮助我习惯 TensorFlow。LR 问题是找到近似点云的参数(为了简单起见,我自己生成了点云)。a
b
Y = a*X + b
(x, y)
我正在使用“固定步长梯度下降 (FSSGD)”来解决这个 LR 问题。我使用 TensorFlow 实现了它,它可以工作,但我注意到它在 GPU 和 CPU 上都非常慢。因为我很好奇,我自己在 Python/NumPy 中实现了 FSSGD,正如预期的那样,它运行得更快,关于:
- 比TF@CPU快 10 倍
- 比TF@GPU快 20 倍
如果 TensorFlow 这么慢,我无法想象会有这么多人在使用这个框架。所以我一定做错了什么。谁能帮我,这样我就可以加快我的 TensorFlow 实现速度。
我对 CPU 和 GPU 性能之间的差异不感兴趣。这两项绩效指标只是为了完整和说明而提供的。 我感兴趣的是为什么我的 TensorFlow 实现比原始 Python/NumPy 实现慢得多。
作为参考,我在下面添加我的代码。
- 剥离到一个最小的(但完全有效)的例子。
- 用。
Python v3.7.9 x64
- 目前使用(因为课程使用 TensorFlow v1)
tensorflow-gpu==1.15
- 经测试可在 Spyder 和 PyCharm 中运行。
我使用 TensorFlow 实现的 FSSGD(执行时间约为 40 秒@CPU @GPU 80 秒):
#%% General imports
import numpy as np
import timeit
import tensorflow.compat.v1 as tf
#%% Get input data
# Generate simulated input data
x_data_input = np.arange(100, step=0.1)
y_data_input = x_data_input + 20 * np.sin(x_data_input/10) + 15
#%% Define tensorflow model
# Define data size
n_samples = x_data_input.shape[0]
# Tensorflow is finicky about shapes, so resize
x_data = np.reshape(x_data_input, (n_samples, 1))
y_data = np.reshape(y_data_input, (n_samples, 1))
# Define placeholders for input
X = tf.placeholder(tf.float32, shape=(n_samples, 1), name="tf_x_data")
Y = tf.placeholder(tf.float32, shape=(n_samples, 1), name="tf_y_data")
# Define variables to be learned
with tf.variable_scope("linear-regression", reuse=tf.AUTO_REUSE): #reuse= True | False | tf.AUTO_REUSE
W = tf.get_variable("weights", (1, 1), initializer=tf.constant_initializer(0.0))
b = tf.get_variable("bias", (1,), initializer=tf.constant_initializer(0.0))
# Define loss function
Y_pred = tf.matmul(X, W) + b
loss = tf.reduce_sum((Y - Y_pred) ** 2 / n_samples) # Quadratic loss function
# %% Solve tensorflow model
#Define algorithm parameters
total_iterations = 1e5 # Defines total training iterations
#Construct TensorFlow optimizer
with tf.variable_scope("linear-regression", reuse=tf.AUTO_REUSE): #reuse= True | False | tf.AUTO_REUSE
opt = tf.train.GradientDescentOptimizer(learning_rate = 1e-4)
opt_operation = opt.minimize(loss, name="GDO")
#To measure execution time
time_start = timeit.default_timer()
with tf.Session() as sess:
#Initialize variables
sess.run(tf.global_variables_initializer())
#Train variables
for index in range(int(total_iterations)):
_, loss_val_tmp = sess.run([opt_operation, loss], feed_dict={X: x_data, Y: y_data})
#Get final values of variables
W_val, b_val, loss_val = sess.run([W, b, loss], feed_dict={X: x_data, Y: y_data})
#Print execution time
time_end = timeit.default_timer()
print('')
print("Time to execute code: {0:0.9f} sec.".format(time_end - time_start))
print('')
# %% Print results
print('')
print('Iteration = {0:0.3f}'.format(total_iterations))
print('W_val = {0:0.3f}'.format(W_val[0,0]))
print('b_val = {0:0.3f}'.format(b_val[0]))
print('')
我自己的python FSSGD实现(执行时间约4秒):
#%% General imports
import numpy as np
import timeit
#%% Get input data
# Define input data
x_data_input = np.arange(100, step=0.1)
y_data_input = x_data_input + 20 * np.sin(x_data_input/10) + 15
#%% Define Gradient Descent (GD) model
# Define data size
n_samples = x_data_input.shape[0]
#Initialize data
W = 0.0 # Initial condition
b = 0.0 # Initial condition
# Compute initial loss
y_gd_approx = W*x_data_input+b
loss = np.sum((y_data_input - y_gd_approx)**2)/n_samples # Quadratic loss function
#%% Execute Gradient Descent algorithm
#Define algorithm parameters
total_iterations = 1e5 # Defines total training iterations
GD_stepsize = 1e-4 # Gradient Descent fixed step size
#To measure execution time
time_start = timeit.default_timer()
for index in range(int(total_iterations)):
#Compute gradient (derived manually for the quadratic cost function)
loss_gradient_W = 2.0/n_samples*np.sum(-x_data_input*(y_data_input - y_gd_approx))
loss_gradient_b = 2.0/n_samples*np.sum(-1*(y_data_input - y_gd_approx))
#Update trainable variables using fixed step size gradient descent
W = W - GD_stepsize * loss_gradient_W
b = b - GD_stepsize * loss_gradient_b
#Compute loss
y_gd_approx = W*x_data_input+b
loss = np.sum((y_data_input - y_gd_approx)**2)/x_data_input.shape[0]
#Print execution time
time_end = timeit.default_timer()
print('')
print("Time to execute code: {0:0.9f} sec.".format(time_end - time_start))
print('')
# %% Print results
print('')
print('Iteration = {0:0.3f}'.format(total_iterations))
print('W_val = {0:0.3f}'.format(W))
print('b_val = {0:0.3f}'.format(b))
print('')
答:
我认为这是大迭代次数的结果。我已将迭代编号从 更改为 ,并将 x 从 更改为 .这样,我减少了迭代次数,但计算量增加了 10 倍。使用 np 可在 22 秒内完成,而在 tensorflow 中,则在 25 秒内完成。1e5
1e3
x_data_input = np.arange(100, step=0.1)
x_data_input = np.arange(100, step=0.0001)
我的猜测:tensorflow 在每次迭代中都有很多开销(给我们一个可以做很多事情的框架),但前向传递和后向传递速度还可以。
评论
BatchNormalization
我的问题的实际答案隐藏在各种评论中。对于未来的读者,我将在本回答中总结这些发现。
关于 TensorFlow 与原始 Python/NumPy 实现之间的速度差异
这部分答案其实很合乎逻辑。
每次迭代(= 每次调用)TensorFlow 都会执行计算。TensorFlow 在启动每次计算时都有很大的开销。在 GPU 上,这种开销甚至比 CPU 上还要糟糕。但是,TensorFlow 执行实际计算的效率非常高,并且比上述原始 Python/NumPy 实现更高效。Session.run()
因此,当数据点的数量增加时,以及每次迭代的计算次数增加时,您会看到 TensorFlow 和 Python/NumPy 之间的相对性能在 TensorFlow 的优势中发生了变化。反之亦然。
问题中描述的问题非常小,这意味着计算次数非常少,而迭代次数非常大。这就是 TensorFlow 表现如此糟糕的原因。这种类型的小问题并不是 TensorFlow 设计的典型用例。
减少执行时间
尽管如此,TensorFlow 脚本的执行时间仍可以减少很多!为了减少执行时间,必须减少迭代次数(无论问题的大小如何,这无论如何都是一个好目标)。
正如@amin所指出的,这是通过缩放输入数据来实现的。一个非常简短的解释为什么这样做:与要找到值的绝对值相比,梯度和变量更新的大小更加平衡。因此,需要更少的步骤(= 迭代)。
按照@amin的建议,我最终按如下方式缩放了我的 x 数据(重复了一些代码以使新代码的位置清晰):
# Tensorflow is finicky about shapes, so resize
x_data = np.reshape(x_data_input, (n_samples, 1))
y_data = np.reshape(y_data_input, (n_samples, 1))
### START NEW CODE ###
# Scale x_data
x_mean = np.mean(x_data)
x_std = np.std(x_data)
x_data = (x_data - x_mean) / x_std
### END NEW CODE ###
# Define placeholders for input
X = tf.placeholder(tf.float32, shape=(n_samples, 1), name="tf_x_data")
Y = tf.placeholder(tf.float32, shape=(n_samples, 1), name="tf_y_data")
缩放将收敛速度提高了 1000 倍。而不是 ,是必需的。这在一定程度上是因为可以使用最大值而不是 .1e5 iterations
1e2 iterations
step size of 1e-1
step size of 1e-4
请注意,发现的权重和偏差是不同的,从现在开始,您必须提供缩放数据。
或者,您可以选择取消缩放找到的权重和偏差,以便可以馈送未缩放的数据。取消缩放是使用以下代码完成的(放在代码末尾的某个位置):
#%% Unscaling
W_val_unscaled = W_val[0,0]/x_std
b_val_unscaled = b_val[0]-x_mean*W_val[0,0]/x_std
评论