1. # 查看主机的GPU
    2. gpus = tf.config.experimental.list_logical_devices(device_type="GPU")
    3. # 查看主机的CPU
    4. cpus = tf.config.experimental.list_logical_devices(device_type="CPU")
    5. # 设置当前程序可见的GPU范围
    6. gpus = tf.config.experimental.list_logical_devices(device_type="GPU")
    7. tf.config.set_visible_devices(devices=gpus[0:2], device_type="GPU")
    8. # 程序动态的获取显存
    9. gpus = tf.config.experimental.list_logical_devices(device_type="GPU")
    10. for gpu in gpus:
    11. tf.config.experimental.set_memory_growth(device=gpu, true)
    12. # 程序获取固定显存大小 1GB
    13. gpus = tf.config.experimental.list_logical_devices(device_type="GPU")
    14. tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])