import torch


def get_device(arguments):
    """
    Sets the Default GPU that the device will use for training and testing.
    :param arguments: A Dictionary of arguments.
    :return: A PyTorch device.
    """

    # If GPU is set to less than 0 then set device to use CPU.
    if arguments["gpu"] < 0 and not torch.cuda.is_available():
        return torch.device("cpu")

    # Sets the GPU device to preferred device and checks if available.
    else:
        if arguments["gpu"] > torch.cuda.device_count() - 1:
            return torch.device(f"cuda:{torch.cuda.device_count() - 1}")
        else:
            return torch.device(f"cuda:{arguments['gpu']}")