【问题标题】:How to use gpu in .py files Google Colab?如何在 .py 文件 Google Colab 中使用 gpu?
【发布时间】:2020-08-28 11:42:49
【问题描述】:

我有一个配置 gpu 计算的 GC 笔记本。当我在这个笔记本上运行时:

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

我可以在设备中看到 GPU:

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 2881683908768157563
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 14433918882325490908
physical_device_desc: "device: XLA_CPU device"
, name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 8857856280193037152
physical_device_desc: "device: XLA_GPU device"
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 15701463552
locality {
  bus_id: 1
  links {
  }
}
incarnation: 13142570581108506915
physical_device_desc: "device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0"
]

但是当我将这段代码(没有别的)放在 .py 文件(在驱动器中)中时,我使用的是从 shell 运行的

!python3 file.py

我在设备中看不到 GPU:

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 5088676628756383267
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 550355900248427842
physical_device_desc: "device: XLA_CPU device"
]

如何解决? IE。如何在笔记本终端运行的 .py 文件中使用 GPU 计算?

【问题讨论】:

  • 使用魔法命令% 代替:%run file.py

标签: python tensorflow gpu google-colaboratory


【解决方案1】:

您可以使用@Anwarvic 的建议:

%run file.py

它非常适合我。

【讨论】:

    【解决方案2】:

    我实际上不确定为什么会发生这种情况,但我认为您正在运行的文件不需要使用 GPU,因此没有使用 GPU。如果我错了,请纠正我。

    【讨论】:

      猜你喜欢
      • 2019-06-27
      • 2020-04-14
      • 2018-07-19
      • 2020-06-25
      • 2019-10-27
      • 2018-11-06
      • 2021-04-12
      • 2020-12-04
      • 2018-11-10
      相关资源
      最近更新 更多