<jupyter_start><jupyter_text>Check if is on CPU or GPU```python Tensorprint(X.device) Modelprint(next(model.parameters()).is_cuda)```  Train data by GPU  Sample Dataset<jupyter_code>class RandomDataset(Dataset):
    def __init__(self, size, length):
        self.len = length
        self.data = torch.randn(length, size)

    def __getitem__(self, index):
        return self.data[index]

    def __len__(self):
        return self.len
# Create dataset: [100, 5]
N = 100
input_size = 5
random_dataset = RandomDataset(
    size=input_size,
    length=N)

# Dataloader: batch_size=30
batch_size = 30
rand_loader = DataLoader(
    dataset=random_dataset,
    batch_size=batch_size, shuffle=True)
print(f"Dataset shape: {random_dataset.data.size()}\n")
print(f"Dataloader length: {len(rand_loader)}")
for batch in rand_loader:
    print(batch.size())<jupyter_output>Dataset shape: torch.Size([100, 5])

Dataloader length: 4
torch.Size([30, 5])
torch.Size([30, 5])
torch.Size([30, 5])
torch.Size([10, 5])<jupyter_text>Sample model<jupyter_code>class Model(nn.Module):
    def __init__(self, input_size, output_size, use_GPU:bool=True, GPU_ids=[0]):
        super(Model, self).__init__()

        # Config
        self.use_GPU = use_GPU
        self.GPU_ids = GPU_ids

        # Layers
        self.fc = nn.Linear(input_size, output_size)

    def forward(self, input):
        ## Hidden tensor
        batch_size = input.size()[0]

        if self.use_GPU:
            hidden_tensor = torch.randn(batch_size, input_size).cuda()
        else:
            hidden_tensor = torch.randn(batch_size, input_size)

        ## FW
        input = input + hidden_tensor
            # (batch_size, input_size) 
        output = self.fc(input)
            # (batch_size, output_size)

        ## Display Info
        gpu_id = torch.cuda.current_device()
        gpu_name = torch.cuda.get_device_name()
        print(f"=== Using GPU[{gpu_id}] {gpu_name} ===")
        print(f"input on {input.device}")
        print(f"hidden_tensor on {hidden_tensor.device}")
        print(f"output on {output.device}")

        return output<jupyter_output><empty_output><jupyter_text>Load to cpu<jupyter_code>device = torch.device("cpu")
print(f"Use {device}")
# Load model
input_size = 5
output_size = 2
model = Model(input_size, output_size, use_GPU=False)
print(f"Is model using GPU: {next(model.parameters()).is_cuda}")
# Load data
for batch in rand_loader:
    # Load data
    Xb = batch

    # Train
    yb_ = model(Xb)
    print(f"Outside: Xb on {Xb.device}, yb_ on {yb_.device}\n")<jupyter_output>=== Using GPU[0] Tesla K40m ===
input on cpu
hidden_tensor on cpu
output on cpu
Outside: Xb on cpu, yb_ on cpu

=== Using GPU[0] Tesla K40m ===
input on cpu
hidden_tensor on cpu
output on cpu
Outside: Xb on cpu, yb_ on cpu

=== Using GPU[0] Tesla K40m ===
input on cpu
hidden_tensor on cpu
output on cpu
Outside: Xb on cpu, yb_ on cpu

=== Using GPU[0] Tesla K40m ===
input on cpu
hidden_tensor on cpu
output on cpu
Outside: Xb on cpu, yb_ on cpu<jupyter_text>Load to 1 GPU<jupyter_code>device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Use {device}")
print(f"Number of GPUs: {torch.cuda.device_count()}")
# Load model
input_size = 5
output_size = 2
model = Model(input_size, output_size, use_GPU=True)
model.to(device)
print(f"Is model using GPU: {next(model.parameters()).is_cuda}")
# Load data
for batch in rand_loader:
    # Load data
    Xb = batch.to(device)

    # Train
    yb_ = model(Xb)
    print(f"Outside: Xb on {Xb.device}, yb_ on {yb_.device}\n")<jupyter_output>=== Using GPU[0] Tesla K40m ===
input on cuda:0
hidden_tensor on cuda:0
output on cuda:0
Outside: Xb on cuda:0, yb_ on cuda:0

=== Using GPU[0] Tesla K40m ===
input on cuda:0
hidden_tensor on cuda:0
output on cuda:0
Outside: Xb on cuda:0, yb_ on cuda:0

=== Using GPU[0] Tesla K40m ===
input on cuda:0
hidden_tensor on cuda:0
output on cuda:0
Outside: Xb on cuda:0, yb_ on cuda:0

=== Using GPU[0] Tesla K40m ===
input on cuda:0
hidden_tensor on cuda:0
output on cuda:0
Outside: Xb on cuda:0, yb_ on cuda:0<jupyter_text>Save model on 1 GPU<jupyter_code># Check params
model.state_dict()
# Save model
torch.save(model.state_dict(), 'mymodel.pt')
# Load model
model_load = Model(input_size, output_size)
model_load.load_state_dict(torch.load('mymodel.pt'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1267767079321412, 'fraction_numerical': 0.018569463548830812, 'mean_word_length': 3.907761529808774, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '509488', 'license': 'Unlicense', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1593, 'n_words': 413, 'path': 'Pytorch/Notes/Notes_on_GPUs/CPU_vs_1GPU.ipynb', 'repo_name': 'dangkhoa1992/Advanced-Machine-Learning-Specialization', 'size': '4362'}