<jupyter_start><jupyter_text>**PyTorch with GPU in the Cloud**To enable GPU usage go to Edit -> Notebook settings -> Hardware accelerator and select GPU<jupyter_code>import torch

if torch.cuda.is_available(): 
    device = torch.device('cuda') 
    print("GPU device is available")
else:                                                   
    device = torch.device('cpu') 
    print("GPU device is NOT available")

# create PyTorch tensor
x = torch.rand(5, 5)

# attach tensor to GPU device
x = x.to(device)
print("Is tensor x on the GPU: {} ".format(x.is_cuda))
print(x)
# can also attach PyTorch Neural Network modules from torch.nn to GPU

import torch.nn as nn
import torch.nn.functional as F

class NeuralNetwork(nn.Module):
    def __init__(self):
      super(NeuralNetwork, self).__init__()
      self.dense_layer_1 = nn.Linear(4, 32)
      self.dense_layer_2 = nn.Linear(32, 2)

    def forward(self, x):
      x = F.relu(self.dense_layer_1(x))
      return F.relu(self.dense_layer_2(x))
    
neural_net = NeuralNetwork().to(device)

print("Is neural network on the GPU: {} ".format(next(neural_net.parameters()).is_cuda))
print(neural_net)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11789652247667515, 'fraction_numerical': 0.010178117048346057, 'mean_word_length': 3.4029850746268657, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '464265', 'license': 'MIT', 'n_tokens_mistral': 402, 'n_tokens_neox': 381, 'n_words': 105, 'path': 'Section 1/Hands on RL with PyTorch_ GPU in the Cloud.ipynb', 'repo_name': 'PacktPublishing/Hands-on-Reinforcement-Learning-with-PyTorch', 'size': '1179'}