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 configure TensorFlow and keras using gpu

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to configure TensorFlow and keras using gpu". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to configure TensorFlow and keras using gpu.

1. The training runtime specifies the GPU

Add a line of code at run time:

CUDA_VISIBLE_DEVICES=1 python train.py2. Assign GPU on demand or quantitatively during operation

Tensorflow just adds a few lines of code when opening Session, while Keras specifies GPU and limits on demand, which is not the same as TensorFlow, because keras training is packaged and is not good for Session operation. Here are two corresponding operations.

Operation in keras: import osimport tensorflow as tffrom keras.backend.tensorflow_backend import set_session # specifies the first block of GPU available os.environ ["CUDA_VISIBLE_DEVICES"] = "0" # the second way to specify GPU config = tf.ConfigProto () config.gpu_options.allocator_type = 'BFC' # A "Best-fit with coalescing" algorithm Simplified from a version of dlmalloc.config.gpu_options.per_process_gpu_memory_fraction = 0.3 # quantitative config.gpu_options.allow_growth = True # Operation in on-demand set_session (tf.Session (config=config)) TensorFlow: # specify GPUimport osos.environ ["CUDA_VISIBLE_DEVICES"] = "0" # set GPU quantitative allocation config= tf.ConfigProto () config.gpu_options.per_process_gpu_memory_fraction = 0.9 # occupancy GPU90% 's video memory session = tf.Session (config=config) # set GPU to allocate on demand config= tf.ConfigProto () config.gpu_options.allow_growth = True session = tf.Session (config=config)

Add: Keras and Tensorflow force CPU,GPU

If Keras uses Theano backend, it should automatically use CPU instead of GPU. Just start GPU and use Theano internal commands.

Keras and Tensorflow on the Tensorflow backend will automatically use the visible GPU, which I need to run only on CPU. There are three methods found on the Internet, and the last one is useful to me, but it also records all three as follows:

Use tensorflow's with tf.device ('/ cpu:0'): function. The simple operation is to put all the commands in the domain described earlier.

Parameters when declaring Session using tensorflow: for some of the parameter settings in Session in tensorflow, and how Keras sets the Session of the Tensorflow it calls, see Keras setting GPU usage memory size (Tensorflow backend).

For Tensorflow, you can add device_count= {'gpu':0} when declaring Session, as follows:

Import tensorflow as tf sess = tf.Session (config=tf.ConfigProto (device_count= {'gpu':0}))

For Keras, call the backend function and set it to use the Session defined above. The code is as follows:

Import tensorflow as tfimport keras.backend.tensorflow_backend as KTF KTF.set_session (tf.Session (config=tf.ConfigProto (device_count= {'gpu':0})

For multithreading and GPU memory settings, see Keras setting GPU usage memory size (Tensorflow backend).

3. The third is to use the CUDA_VISIBLE_DEVICES command line argument, and the code is as follows:

CUDA_VISIBLE_DEVICES= "0" python3 train.py so far, I believe you have a deeper understanding of "how to configure TensorFlow and keras using gpu". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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