Core pytorch functions
Device Settings
These functions control what device is used for tensors.
to_device
will set a tensor or Pytorch model to the passed device. If no device is passed, the default device is used.
If Cuda is available, the device defined by torch.cuda.current_device()
. The current device can be set using Pytorch:
torch.cuda.set_device(1)
To disable GPU usage, set the use_cuda
environment variable to cpu
. os.environ['use_cuda'] = 'cpu'
rewards = torch.tensor([[0., 0., 0., 4.],
[0., 0., 0., 3.],
[0., 0., 0., 2.],])
gamma = 0.97
discounted = discount_rewards(rewards, gamma)
discounted
rewards = torch.tensor([4., 5., 6.]).float()
mask = torch.tensor([[True, True, True, False],
[True, True, False, False],
[True, True, True, True]])
scattered = scatter_rewards(rewards, mask)
scattered