首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用TensorFlow的梯度下降要比基本的Python实现慢得多,为什么?

使用TensorFlow的梯度下降要比基本的Python实现慢得多,为什么?
EN

Stack Overflow用户
提问于 2020-12-29 12:49:11
回答 2查看 573关注 0票数 5

我在学习机器学习课程。我有一个简单的线性回归(LR)问题来帮助我适应TensorFlow。LR的问题是找到参数ab,以便Y = a*X + b近似于(x, y)点云(为了简单起见,我自己生成了它)。

我正在用固定步长梯度下降(FSSGD)来解决这个LR问题。我使用TensorFlow实现了它,但我注意到它在GPU和CPU上都非常慢。因为我很好奇,所以我自己在Python/NumPy中实现了FSSGD,正如预期的那样,运行速度要快得多,大约如下:

  • 10倍于TF@CPU
  • 20倍于TF@GPU

如果TensorFlow这么慢,我无法想象这么多人正在使用这个框架。所以我肯定做错什么了。有人能帮我吗,这样我就可以加速我的TensorFlow实现了。

我对CPU和GPU性能之间的差异不感兴趣。这两项业绩指标只是为了完整和说明而提供的。我感兴趣的是,为什么我的TensorFlow实现比原始的Python/NumPy实现慢得多。

作为参考,我在下面添加我的代码。

目前,example.

  • Using Python v3.7.9 x64.

  • Used tensorflow-gpu==1.15 (因为该课程使用TensorFlow v1)测试了在Spyder和PyCharm.

中运行的

我使用TensorFlow的FSSGD实现(执行时间约为40秒@CPU至80秒@GPU):

代码语言:javascript
复制
#%% 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秒):

代码语言:javascript
复制
#%% 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('')
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-31 10:35:36

我的问题的实际答案隐藏在各种评论中。对于未来的读者,我将在这个答案中总结这些发现。

关于TensorFlow与原始Python/NumPy实现之间的速度差异

答案的这一部分实际上是相当合乎逻辑的。

每次迭代(= Session.run()的每个调用) TensorFlow都执行计算。TensorFlow启动每一次计算有很大的开销。在GPU上,这种开销甚至比CPU上的还要糟糕。然而,TensorFlow执行实际的计算比上面的原始Python/NumPy实现更高效、更有效。

因此,当数据点的数量增加,因此每次迭代的计算次数增加时,您将看到TensorFlow和Python之间的相对性能改变了TensorFlow的优势。相反的情况也是如此。

问题中描述的问题很小,这意味着计算的次数很低,而迭代的次数却很大。这就是为什么TensorFlow表现如此糟糕的原因。这类小问题不是TensorFlow设计的典型用例。

减少执行时间

尽管如此,TensorFlow脚本的执行时间仍然可以大大缩短!为了减少执行时间,迭代次数必须减少(不管问题的大小,这无论如何都是一个好的目标)。

正如@amin所指出的,这是通过缩放输入数据来实现的。一个非常简单的解释为什么这是工作:梯度和变量更新的大小比绝对值要找到的值要更平衡。因此,需要更少的步骤(=迭代)。

以下是@amin的建议,最后我按如下方式缩放了x-数据(重复了一些代码以使新代码的位置变得清晰):

代码语言:javascript
复制
# 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

请注意,发现的重量和偏差是不同的,你必须从现在开始提供比例数据。

可选地,您可以选择不缩放发现的权重和偏差,这样您就可以输入未缩放的数据。取消缩放是使用以下代码完成的(放在代码末尾的某个位置):

代码语言:javascript
复制
#%% Unscaling
W_val_unscaled = W_val[0,0]/x_std
b_val_unscaled = b_val[0]-x_mean*W_val[0,0]/x_std
票数 1
EN

Stack Overflow用户

发布于 2020-12-29 14:12:44

我认为这是大迭代次数的结果。我将迭代次数从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在每一次迭代中都有很多开销(给我们一个可以做很多事情的框架),但是前进、传球和回传速度都是可以的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65492399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档