Tensorflow 查看GPU数量并确认batch_size

def validate_batch_size_for_multi_gpu(batch_size):
    from tensorflow.python.client import device_lib
    local_device_protos = device_lib.list_local_devices()
    num_gpus = sum([1 for d in local_device_protos if d.device_type == 'GPU'])
    if not num_gpus:
        raise ValueError('no GPUs were found. please check')
    remainder = batch_size % num_gpus
    if remainder:
        err = ('When running with multiple GPUs, batch size '
               'must be a multiple of the number of available GPUs. '
               'Found {} GPUs with a batch size of {}; try --batch_size={} instead.'
               ).format(num_gpus, batch_size, batch_size - remainder)
        raise ValueError(err)
    return num_gpus
    ```

你可能感兴趣的:(tensorflow)