Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that the get_value of keras is running slower and slower?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to solve the get_value running slower and slower of keras". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to solve the problem of keras get_value running slower and slower" can help you solve the problem.

Problem description

As shown in the figure above, after time and memory consumption tracking tests, it is found that the program caused by the keras.backend.get_value () function is getting slower and slower, and seriously causes memory leaks.

Looking at the internal implementation of the function, it is found that a main core is x.eval (session=get_session ()), which may be the core statement that causes memory leaks and slows down; according to viewing some blog posts, it is getting slower and slower.

Reason: the x.eval function adds new nodes to the tf graph; this also causes the tf graph to become larger and larger and memory leaks

Solution import tensorflow.keras.backend as Kdef get_my_session (gpu_fraction=0.1):''Assume that you have 6GB of GPU memory and want to allocate ~ 2GB benchmark' num_threads = os.environ.get ('OMP_NUM_THREADS') gpu_options= tf.GPUOptions (per_process_gpu_memory_fraction=gpu_fraction) if num_threads: return tf.Session (config=tf.ConfigProto (gpu_options=gpu_options) Intra_op_parallelism_threads=num_threads)) else: return tf.Session (config=tf.ConfigProto (gpu_options=gpu_options)) K.set_session (get_my_session ())

As shown in the figure above, before I use tensorflow (that is, before the project file), I customize session, and then set keras.backend.set_session () with the custom session.

Then delete the get_value () function and directly use the execution statement x.eval (session=get_my_session ()) used in get_value (); so the core statement x.eval () that adds nodes that causes memory leaks uses the project to customize session uniformly, and then resets the graph with tf.reset_default_graph ()

That is, the problem code in the figure above is modified as follows: output = ctc_decode (yearly predcent) out = output [0] [0] out = output.eval (session=get_my_session ()) # delete K.get_value (out [0] [0]) tf.reset_default_graph () # and then reset the tf diagram.

This solves the problem of getting slower and slower caused by get_value ()

Personally, I think: this may not always add new nodes, causing the tf diagram to become infinitely larger; instead, reuse this custom node.

Add: solutions to get_session problems caused by version problems between tensorflow and keras

1. Import tensorflow.keras.backend as Kdef _ init__ (self, * * kwargs): self.__dict__.update (self._defaults) # set up default values self.__dict__.update (kwargs) # and update with user overrides self.class_names = self._get_class () self.anchors = self._get_anchors () self.sess = K.get_session ()

The error is as follows:

Get_session is not available when using TensorFlow 2.0.

It means tf2.0 doesn't have get_session.

two。 Solution 1import tensorflow.python.keras.backend as Ksess = K.get_session () 3. Solution 2import tensorflow as tfsess = tf.compat.v1.keras.backend.get_session ()

Previously, solution 1 has been used to solve the problem, which feels more convenient, but solution 1 has other attributes that will be lost.

For example, AttributeError: module 'keras.backend' has no attribute image_dim_ordering

Therefore, it is suggested that we adopt option 2.

This is the end of the content about "how to solve the problem that the get_value of keras is getting slower and slower". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report