当前位置:网站首页>在Tensorflow中把Tensor转换为ndarray时,循环中不断调用run或者eval函数,代码运行越来越慢!
在Tensorflow中把Tensor转换为ndarray时,循环中不断调用run或者eval函数,代码运行越来越慢!
2022-06-12 08:28:00 【躁动的风儿】
问题
我有一个这样的需求:我目前有一个已经训练好的encoder模型,它的输出是Tensor类型,我想把它转换成ndarray类型。通过查询资料,我发现可以利用sess.run()把Tensor转换为ndarray,于是在我的代码里调用sess.run()成功转换了数据类型。
但是,我这个数据转换在每一次的循环中都会调用,也就是循环中一直调用sess.run(),于是问题来了,每循环一次,sess.run的用时都比上一次要久,导致后面训练越来越慢。从第一次调用用时0.17s到后面第100次调用时0.27s,而且这才是100次,如果训练10000次,那不知道要等多久,所以这个问题必须解决!
问题原因
如果在某一个循环里不断建立tensorflow图节点再运行的话,会导致tensorflow运行越来越慢。具体问题请看代码注释,没有注释的代码行可以不用关注,问题代码如下:
import gym
from gym.spaces import Box
import numpy as np
from tensorflow import keras
import tensorflow as tf
import time
class MyWrapper(gym.ObservationWrapper):
def __init__(self, env, encoder, latent_dim = 2):
super().__init__(env)
self._observation_space = Box(-np.inf, np.inf, shape=(7 + latent_dim,), dtype=np.float32)
self.observation_space = self._observation_space
self.encoder = encoder # 这是我已经提前训练好的模型
tf.InteractiveSession()
self.sess = tf.get_default_session()
self.sess.run(tf.global_variables_initializer())
def observation(self, obs):
obs = np.reshape(obs, (1, -1))
latent_z_tensor = self.encoder(obs)[2] # 问题就在与这里,这行代码在调用run时,会不断的创建图节点,所以越来越慢
t=time.time() # 测试运行用时
latent_z_arr = sels.sess.run(latent_z_tensor) # 每次run时,就会把上面的图重新构建一次
print(time.time()-t) # 测试运行用时
obs = np.reshape(obs, (-1,))
latent_z_arr = np.reshape(latent_z_arr, (-1,))
obs = obs.tolist()
obs.extend(latent_z_arr.tolist())
obs = np.array(obs)
return obs
解决思路
在初始化时,就建立好图结构,使用tf.placeholder占位符表示obs这个变量,具体方案示例如下(可以只关注带有注释的行):
import gym
from gym.spaces import Box
import numpy as np
from tensorflow import keras
import tensorflow as tf
import time
class MyWrapper(gym.ObservationWrapper):
def __init__(self, env, encoder, latent_dim = 2):
super().__init__(env)
self._observation_space = Box(-np.inf, np.inf, shape=(7 + latent_dim,), dtype=np.float32)
self.observation_space = self._observation_space
self.encoder = encoder
tf.InteractiveSession()
self.sess = tf.get_default_session()
self.obs=tf.placeholder(dtype=tf.float32,shape=(1,7)) # 重点在于这两行代码,初始化时先构建好图,先用占位符表示obs,实际运行时只需喂数据obs就好了
self.latent_z_tensor = self.encoder(self.obs)[2] # 在初始化时构建图
self.sess.run(tf.global_variables_initializer())
def observation(self, obs):
obs = np.reshape(obs, (1, -1))
t=time.time() # 测试运行用时
latent_z_arr = self.sess.run(self.latent_z_tensor, feed_dict={
self.obs:obs}) # 这里只需喂数据,不会重新构建图了。
print(time.time()-t) # 测试运行用时
obs = np.reshape(obs, (-1,))
latent_z_arr = np.reshape(latent_z_arr, (-1,))
obs = obs.tolist()
obs.extend(latent_z_arr.tolist())
obs = np.array(obs)
return obs
现在,数据类型转换完成,代码运行慢也解决了!
这个问题至此解决完成,查了四五天的资料终于搞定,这一刻,解决问题带来的快乐把前两天失恋的阴霾都冲散了不少,真是太开心了。
边栏推荐
- vscode 下载慢解决办法
- MYSQL中的调用存储过程,变量的定义,
- Error: what if the folder cannot be deleted when it is opened in another program
- JVM learning notes: garbage collection mechanism
- JVM学习笔记:三 本地方法接口、执行引擎
- (p21-p24) unified data initialization method: List initialization, initializing objects of non aggregate type with initialization list, initializer_ Use of Lisy template class
- MES系统质量追溯功能,到底在追什么?
- JVM learning notes: three local method interfaces and execution engines
- ctfshow web 1-2
- Hands on deep learning 18 -- model selection + over fitting and under fitting and code implementation
猜你喜欢

Figure neural network makes Google maps more intelligent

MATLAB image processing - Otsu threshold segmentation (with code)

Prediction of COVID-19 by RNN network

Hands on deep learning -- image classification dataset fashion MNIST

What is an extension method- What are Extension Methods?

APS究竟是什么系统呢?看完文章你就知道了

【数据存储】浮点型数据在内存中的存储

【进阶指针一】字符数组&数组指针&指针数组

Convolutional neural network CNN based cat dog battle picture classification (tf2.1 py3.6)

Scope of bean
随机推荐
A brief summary of C language printf output integer formatter
报错:文件夹在另一个程序中打开无法删除怎么办
Only use MES as a tool? Looks like you missed the most important thing
What should be paid attention to when establishing MES system? What benefits can it bring to the enterprise?
What kind of sparks will be generated when the remote sensing satellite meets the Beidou navigation satellite?
安科瑞电动机保护器具有过载反时限、过载定时限、接地、起动超时、漏电、欠载、断相、堵转等功能
Hands on learning and deep learning -- Realization of linear regression from scratch
Easyexcel exports excel tables to the browser, and exports excel through postman test [introductory case]
牛客网的项目梳理
What scheduling rules does the APS software have? What is the exception handling scheme?
MYSQL中的查询
电气火灾探测器对各用电回路进行实时监控
Webrtc adding third-party libraries
(p15-p16) optimization of the right angle bracket of the template and the default template parameters of the function template
智能制造的时代,企业如何进行数字化转型
What is the quality traceability function of MES system pursuing?
Special notes on using NAT mode in VM virtual machine
js中的正则表达式
三国杀周边--------猪国杀题解
Detailed explanation of private, public and interface attributes in cmake