docstr: Sets the device to use for the backend.
  
  Allows the device used by the framework to be set using string, integer or device object directly. This is so
  users can follow the pattern recommended in
  https://pytorch.org/blog/pytorch-0_4_0-migration-guide/#writing-device-agnostic-code for writing
  device-agnostic code.
docstr_tok: ["Sets","the","device","to","use","for","the","backend",".","Allows","the","device","used","by","the","framework","to","be","set","using","string",",","integer","or","device","object","directly",".","This","is","so","users","can","follow","the","pattern","recommended","in","https",":","\/\/pytorch.org\/blog\/pytorch-0_4_0-migration-guide\/","#","writing-device-agnostic-code","for","writing","device-agnostic","code","."]
code: def set_device(
      device: Union[str, int, torch.device, None] = None
  ) -> None:
      """Sets the device to use for the backend.
  
      Allows the device used by the framework to be set using string, integer or device object directly. This is so
      users can follow the pattern recommended in
      https://pytorch.org/blog/pytorch-0_4_0-migration-guide/#writing-device-agnostic-code for writing
      device-agnostic code.
      """
      if isinstance(device, (int, str)):
          _PytorchBackend.device = torch.device(device)
      elif isinstance(device, torch.device):
          _PytorchBackend.device = device
      elif device is not None:
          raise TypeError(
              (
                  "`device` must be a ``None``, `string`, `integer` or "
                  f"`torch.device` object. Got {type(device)} instead."
              )
          )
  
file_path: alibi/alibi/explainers/similarity/backends/pytorch/base.py
imports: from typing import Any, Callable, List, Optional, Union
  import numpy
  import torch
  import torch.nn
label_desc: Use image node_id 4 for calling the _PytorchBackend obj's underlying member method code with example usage: obj.set_device(device) without return types