Rya*_*an 93 python jupyter keras tensorflow
我正在运行Keras模型,提交截止日期为36小时,如果我在cpu上训练我的模型需要大约50个小时,有没有办法在gpu上运行Keras?
我正在使用Tensorflow后端并在我的Jupyter笔记本上运行它,没有安装anaconda.
Vik*_*ngh 140
是的,你可以在GPU上运行keras模型.你必须先检查几件事.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
要么
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
Run Code Online (Sandbox Code Playgroud)
输出将是这样的:
[
name: "/cpu:0"device_type: "CPU",
name: "/gpu:0"device_type: "GPU"
]
Run Code Online (Sandbox Code Playgroud)
完成所有这些后,您的模型将在GPU上运行:
要检查keras(> = 2.1.1)是否正在使用GPU:
from keras import backend as K
K.tensorflow_backend._get_available_gpus()
Run Code Online (Sandbox Code Playgroud)
祝一切顺利.
Ten*_*ort 21
2.0 兼容答案:虽然上面提到的答案详细解释了如何在 Keras 模型上使用 GPU,但我想解释如何为Tensorflow Version 2.0.
要知道有多少 GPU 可用,我们可以使用以下代码:
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
Run Code Online (Sandbox Code Playgroud)
要找出您的操作和张量分配给哪些设备,请将其tf.debugging.set_log_device_placement(True)作为程序的第一条语句。
启用设备放置日志会导致打印任何张量分配或操作。例如,运行以下代码:
tf.debugging.set_log_device_placement(True)
# Create some tensors
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
c = tf.matmul(a, b)
print(c)
Run Code Online (Sandbox Code Playgroud)
给出如下所示的输出:
在设备 /job:localhost/replica:0/task:0/device:GPU:0 tf.Tensor( [[22. 28.] [49. 64.]], shape=(2, 2), dtype=float32)
有关更多信息,请参阅此链接
小智 17
当然.我想你已经为GPU安装了TensorFlow.
导入keras后需要添加以下块.我正在一台拥有56个核心cpu和一个gpu的机器上工作.
import keras
import tensorflow as tf
config = tf.ConfigProto( device_count = {'GPU': 1 , 'CPU': 56} )
sess = tf.Session(config=config)
keras.backend.set_session(sess)
Run Code Online (Sandbox Code Playgroud)
当然,这种用法强制我的机器最大限制.您可以减少cpu和gpu消耗值.
| 归档时间: |
|
| 查看次数: |
146482 次 |
| 最近记录: |