Tensorflow_GPU
来源:互联网 发布:java项目视频教程 编辑:程序博客网 时间:2024/06/06 03:05
Using GPUs
Supported devices
On a typical system, there are multiple computing devices. In TensorFlow, the supported device types are CPU
and GPU
. They are represented as strings
. For example:
"/cpu:0"
: The CPU of your machine."/gpu:0"
: The GPU of your machine, if you have one."/gpu:1"
: The second GPU of your machine, etc.
If a TensorFlow operation has both CPU and GPU implementations, the GPU devices will be given priority when the operation is assigned to a device. For example, matmul
has both CPU and GPU kernels. On a system with devices cpu:0
and gpu:0
, gpu:0
will be selected to run matmul
.
Logging Device placement
To find out which devices your operations and tensors are assigned to, create the session with log_device_placement
configuration option set to True
.
# Creates a graph.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)
You should see the following output:
Device mapping:/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci busid: 0000:05:00.0b: /job:localhost/replica:0/task:0/gpu:0a: /job:localhost/replica:0/task:0/gpu:0MatMul: /job:localhost/replica:0/task:0/gpu:0[[ 22. 28.] [ 49. 64.]]
Manual device placement
If you would like a particular operation to run on a device of your choice instead of what's automatically selected for you, you can use with tf.device
to create a device context such that all the operations within that context will have the same device assignment.
# Creates a graph.with tf.device('/cpu:0'): 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)
You will see that now a
and b
are assigned to cpu:0
.
Device mapping:/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci busid: 0000:05:00.0b: /job:localhost/replica:0/task:0/cpu:0a: /job:localhost/replica:0/task:0/cpu:0MatMul: /job:localhost/replica:0/task:0/gpu:0[[ 22. 28.] [ 49. 64.]]
Manual device placement
If you would like a particular operation to run on a device of your choice instead of what's automatically selected for you, you can use
with tf.device
to create a device context such that all the operations within that context will have the same device assignment.# Creates a graph.with tf.device('/cpu:0'): 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)
You will see that now
a
andb
are assigned tocpu:0
.Device mapping:/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci busid: 0000:05:00.0b: /job:localhost/replica:0/task:0/cpu:0a: /job:localhost/replica:0/task:0/cpu:0MatMul: /job:localhost/replica:0/task:0/gpu:0[[ 22. 28.] [ 49. 64.]]
Allowing GPU memory growth
By default, TensorFlow maps nearly all of the GPU memory of all GPUs (subject to
CUDA_VISIBLE_DEVICES
) visible to the process. This is done to more efficiently use the relatively precious GPU memory resources on the devices by reducing memory fragmentation.In some cases it is desirable for the process to only allocate a subset of the available memory, or to only grow the memory usage as is needed by the process. TensorFlow provides two Config options on the Session to control this.
The first is the
allow_growth
option, which attempts to allocate only as much GPU memory based on runtime allocations: it starts out allocating very little memory, and as Sessions get run and more GPU memory is needed, we extend the GPU memory region needed by the TensorFlow process. Note that we do not release memory, since that can lead to even worse memory fragmentation. To turn this option on, set the option in the ConfigProto by:config = tf.ConfigProto()config.gpu_options.allow_growth = Truesession = tf.Session(config=config, ...)
The second method is the
per_process_gpu_memory_fraction
option, which determines the fraction of the overall amount of memory that each visible GPU should be allocated. For example, you can tell TensorFlow to only allocate 40% of the total memory of each GPU by:config = tf.ConfigProto()config.gpu_options.per_process_gpu_memory_fraction = 0.4session = tf.Session(config=config, ...)
This is useful if you want to truly bound the amount of GPU memory available to the TensorFlow process.
Using a single GPU on a multi-GPU system
If you have more than one GPU in your system, the GPU with the lowest ID will be selected by default. If you would like to run on a different GPU, you will need to specify the preference explicitly:
# Creates a graph.with tf.device('/gpu:2'): 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)
If the device you have specified does not exist, you will get
InvalidArgumentError
:InvalidArgumentError: Invalid argument: Cannot assign a device to node 'b':Could not satisfy explicit device specification '/gpu:2' [[Node: b = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [3,2] values: 1 2 3...>, _device="/gpu:2"]()]]
If you would like TensorFlow to automatically choose an existing and supported device to run the operations in case the specified one doesn't exist, you can set
allow_soft_placement
toTrue
in the configuration option when creating the session.# Creates a graph.with tf.device('/gpu:2'): 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 allow_soft_placement and log_device_placement set# to True.sess = tf.Session(config=tf.ConfigProto( allow_soft_placement=True, log_device_placement=True))# Runs the op.print sess.run(c)
Using multiple GPUs
If you would like to run TensorFlow on multiple GPUs, you can construct your model in a multi-tower fashion where each tower is assigned to a different GPU. For example:
# Creates a graph.c = []for d in ['/gpu:2', '/gpu:3']: 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)
You will see the following output.
Device mapping:/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K20m, pci busid: 0000:02:00.0/job:localhost/replica:0/task:0/gpu:1 -> device: 1, name: Tesla K20m, pci busid: 0000:03:00.0/job:localhost/replica:0/task:0/gpu:2 -> device: 2, name: Tesla K20m, pci busid: 0000:83:00.0/job:localhost/replica:0/task:0/gpu:3 -> device: 3, name: Tesla K20m, pci busid: 0000:84:00.0Const_3: /job:localhost/replica:0/task:0/gpu:3Const_2: /job:localhost/replica:0/task:0/gpu:3MatMul_1: /job:localhost/replica:0/task:0/gpu:3Const_1: /job:localhost/replica:0/task:0/gpu:2Const: /job:localhost/replica:0/task:0/gpu:2MatMul: /job:localhost/replica:0/task:0/gpu:2AddN: /job:localhost/replica:0/task:0/cpu:0[[ 44. 56.] [ 98. 128.]]
- Tensorflow_GPU
- Windows10下tensorflow_gpu环境搭建
- win7跑tensorflow_gpu电脑卡机的解决办法
- tensorflow_gpu-1.2.1-cp35-cp35m-win_amd64.whl文件怎么安装?
- linux下xgboost、python版本、tensorflow_GPU的一些小事情
- ubuntu16.04下使用anaconda安装tensorflow_gpu版本以及object detection的过程
- TensorFlow_gpu版本 python库安装流程(包括驱动,cuda,cudnn)
- Win10下CUDA8.0+cuDNN6.0+python3.5+tensorflow_gpu的安装
- Ubuntu16.04LTS+CUDA8.0+cuDNN5.1+Anaconda3(python3.6)+tensorflow_gpu-1.2.1安装过程全记录
- Ubuntu14.04 + NVIDIA GTX1080Ti + cuda8.0 +cuDNN6.0 +python3.4 + tensorflow_gpu-1.4.0-cp34-cp34m-
- 人工智能学习tensorFlow_gpu-1.1.0图文详细安装教程(64位机win7旗舰sp1+Anaconda3-4.2.0+Pycharm2017.2.4)附所有软件下载地址
- Windows程序运行原理
- dos命令提示符
- Hive On Spark报错:Failed to execute spark task, org.apache.hadoop.hive.ql.metadata.HiveException
- 顺序栈-数组实现
- Java模仿实现Windows时钟日历
- Tensorflow_GPU
- MAC安装配置Tornado
- JavaScript客户端检测
- app:transformClassesWithDexForDebug错误记录
- Android Studio运行或Rebuild的时候报Error:Execution failed for task ':app:compileDebugNdk'.的解决办法
- 对字符编码的一些理解
- 数字签名(代码签名)流程和数字签名的验证
- 目录
- 读《JAVA并发实践》笔记---第一章(介绍),第二章(线程安全)