當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.config.set_visible_devices用法及代碼示例

設置可見設備列表。

用法

tf.config.set_visible_devices(
    devices, device_type=None
)

參數

  • devices 要顯示的PhysicalDevice 列表
  • device_type (可選)僅配置匹配此設備類型的設備。例如 "CPU" 或 "GPU"。其他設備將保持不變。

拋出

  • ValueError 如果參數驗證失敗。
  • RuntimeError 運行時已經初始化。

指定哪些PhysicalDevice 對象對運行時可見。 TensorFlow 隻會在可見的物理設備上分配內存和放置操作,否則不會在它們上創建 LogicalDevice。默認情況下,所有發現的設備都標記為可見。

以下示例演示了禁用機器上的第一個 GPU。

physical_devices = tf.config.list_physical_devices('GPU')
try:
  # Disable first GPU
  tf.config.set_visible_devices(physical_devices[1:], 'GPU')
  logical_devices = tf.config.list_logical_devices('GPU')
  # Logical device was not created for first GPU
  assert len(logical_devices) == len(physical_devices) - 1
except:
  # Invalid device or cannot modify virtual devices once initialized.
  pass

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.config.set_visible_devices。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。