Keras specifying runtime graphics and limiting GPU usage

Source: Internet
Author: User
Tags keras

Keras in the use of the GPU when the feature is that the default is full of video memory. That way, if you have multiple models that need to run with a GPU, the restrictions are huge and a waste to the GPU. So when using Keras, you need to consciously set how much capacity you need to use the video card when you run it.

There are generally three situations in this setting:
1. Specify the video card
2. Limit GPU Usage
3. That is, specify the graphics card and limit the GPU Usage view GPU Usage statement (Linux)

# 1 seconds to refresh once
watch-n 1 nvidia-smi
first, the designated video card
Import os
os.environ["cuda_visible_devices"] = "2"

This specifies the use of the GPU number 2, you can specify the GPU used according to the needs and actual situation , limit GPU usage 1, set the percentage of use GPU

Import TensorFlow as TF
import keras.backend.tensorflow_backend as KTF

#进行配置, using 30% gpu
config = tf. Configproto ()
config.gpu_options.per_process_gpu_memory_fraction = 0.3
session = TF. Session (Config=config)

# Set Session
Ktf.set_session (session)
It is important to note that although the code or configuration level sets a percentage threshold for memory consumption, the threshold is exceeded if the program needs to do so if the threshold is reached in the actual operation. In other words, if you run on a big data set, you will still use more memory. The above video memory limit is just to avoid wasting memory when running a small data set.
2, GPU on-demand use
Import TensorFlow as TF
import keras.backend.tensorflow_backend as KTF

config = tf. Configproto ()  
config.gpu_options.allow_growth=true   #不全部占满显存, ON demand
session = TF. Session (Config=config)

# Set Session
Ktf.set_session (Sess)
Iii. specifying the GPU and limiting GPU usage

This is relatively simple, that is to say the above two cases can be connected ...

Import OS
import TensorFlow as tf
import keras.backend.tensorflow_backend as KTF

# Specifies that the first GPU is available 
os.environ["cuda_visible_devices"] = "0"

config = tf. Configproto ()  
config.gpu_options.allow_growth=true   #不全部占满显存, on demand
sess = tf. Session (config=config)

ktf.set_session (Sess)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.