如何基于TensorFlow实现指定GPU运行及优化GPU资源分配策略?

2026-06-09 23:241阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计456个文字,预计阅读时间需要2分钟。

如何基于TensorFlow实现指定GPU运行及优化GPU资源分配策略?

1. 在终端执行时设置使用哪种GPU(两种方式): - 方式一:通过export语句执行一次即可,之后运行代码不再执行(export命令执行一次后,后续代码运行不再需要执行)。 - 方式二:直接在代码中指定。

2. 代码中指定(两种方式): - 方式一:import os, sys - 方式二:import osos.environ[CUDA_VISIBLE_DEVICES]

1. 在终端执行时设置使用哪些GPU(两种方式)

(1) 如下(export 语句执行一次就行了,以后再运行代码不用执行)

(2) 如下

2. 代码中指定(两种方式)

(1)

import os os.environ["CUDA_VISIBLE_DEVICES"] = "1"

(2)

# Creates a graph. with tf.device('/gpu:1'): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b') c = tf.matmul(a, b) # Creates a session with log_device_placement set to True. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # Runs the op. print sess.run(c)

若想使用多个GPU,如下

c = [] for d in ['/gpu:0', '/gpu:1']: with tf.device(d): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3]) b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2]) c.append(tf.matmul(a, b)) with tf.device('/cpu:0'): sum = tf.add_n(c) # Creates a session with log_device_placement set to True. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # Runs the op. print sess.run(sum)

3.GPU资源分配

如何基于TensorFlow实现指定GPU运行及优化GPU资源分配策略?

(1) 设置允许GPU增长

config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=config, ...)

(2) 设置每个GPU内存使用多少

config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.4 session = tf.Session(config=config, ...)

以上这篇基于tensorflow指定GPU运行及GPU资源分配的几种方式小结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计456个文字,预计阅读时间需要2分钟。

如何基于TensorFlow实现指定GPU运行及优化GPU资源分配策略?

1. 在终端执行时设置使用哪种GPU(两种方式): - 方式一:通过export语句执行一次即可,之后运行代码不再执行(export命令执行一次后,后续代码运行不再需要执行)。 - 方式二:直接在代码中指定。

2. 代码中指定(两种方式): - 方式一:import os, sys - 方式二:import osos.environ[CUDA_VISIBLE_DEVICES]

1. 在终端执行时设置使用哪些GPU(两种方式)

(1) 如下(export 语句执行一次就行了,以后再运行代码不用执行)

(2) 如下

2. 代码中指定(两种方式)

(1)

import os os.environ["CUDA_VISIBLE_DEVICES"] = "1"

(2)

# Creates a graph. with tf.device('/gpu:1'): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b') c = tf.matmul(a, b) # Creates a session with log_device_placement set to True. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # Runs the op. print sess.run(c)

若想使用多个GPU,如下

c = [] for d in ['/gpu:0', '/gpu:1']: with tf.device(d): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3]) b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2]) c.append(tf.matmul(a, b)) with tf.device('/cpu:0'): sum = tf.add_n(c) # Creates a session with log_device_placement set to True. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # Runs the op. print sess.run(sum)

3.GPU资源分配

如何基于TensorFlow实现指定GPU运行及优化GPU资源分配策略?

(1) 设置允许GPU增长

config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=config, ...)

(2) 设置每个GPU内存使用多少

config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.4 session = tf.Session(config=config, ...)

以上这篇基于tensorflow指定GPU运行及GPU资源分配的几种方式小结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。