问题描述
我正在学习机器学习课程。我有一个简单的线性回归 (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-gpu==1.15
(因为课程使用 TensorFlow v1) - 经测试可在 Spyder 和 PyCharm 中运行。
我使用 TensorFlow 的 FSSGD 实现(执行时间大约 40 秒 @cpu 到 80 秒 @GPU):
#%% 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,1))
# Define placeholders for input
X = tf.placeholder(tf.float32,shape=(n_samples,1),name="tf_x_data")
Y = tf.placeholder(tf.float32,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,initializer=tf.constant_initializer(0.0))
b = tf.get_variable("bias",),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,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('')
解决方法
我认为这是大迭代次数的结果。我已将迭代编号从 1e5
更改为 1e3
,并将 x 从 x_data_input = np.arange(100,step=0.1)
更改为 x_data_input = np.arange(100,step=0.0001)
。通过这种方式,我减少了迭代次数,但将计算量增加了 10 倍。使用 np,它在 22 秒 内完成,在 tensorflow 中,它在 25 秒 内完成。
我的猜测:tensorflow 在每次迭代中都有很多开销(为我们提供了一个可以做很多事情的框架),但是正向传递和反向传递速度还可以。
,我的问题的实际答案隐藏在各种评论中。对于未来的读者,我将在此答案中总结这些发现。
关于 TensorFlow 和原始 Python/NumPy 实现之间的速度差异
这部分答案其实很合乎逻辑。
每次迭代(= Session.run()
的每次调用)TensorFlow 执行计算。 TensorFlow 启动每次计算的开销很大。在 GPU 上,这种开销甚至比在 CPU 上还要糟糕。但是,与上述原始 Python/NumPy 实现相比,TensorFlow 执行实际计算非常高效。
因此,当数据点的数量增加,因此每次迭代的计算数量增加时,您将看到 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,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,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