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

如何解决使用 TensorFlow 的梯度下降比基本的 Python 实现慢得多,为什么?

我正在学习机器学习课程。我有一个简单的线性回归 (LR) 问题来帮助我习惯 TensorFlow。 LR 问题是找到参数 ab,使得 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-