<jupyter_start><jupyter_text>USING GPUS IF AVALIABLE<jupyter_code>def SetGPU():
    if torch.cuda.is_available():
        return torch.device('cuda')
    else:
        return torch.device('cpu')
device = SetGPU()
device
def to_device(data, device):
    if isinstance(data, (list,tuple)):
        return [to_device(x, device) for x in data]
    return data.to(device, non_blocking=True)
class DeviceDataLoader:
    def __init__(self, dl, device):
        self.dl = dl
        self.device = device

    def __iter__(self):
        for btch in self.dl:
            yield to_device(btch, self.device)
    def __len__(self):
        return len(dl)
train_dl = DeviceDataLoader(train_dl, device)
vdl = DeviceDataLoader(val_dl, device)
def evaluate(model,val_loader):
    outputs = [model.validation_step(btch) for btch in val_loader]
    return model.validation_epoch_end(outputs)
def fit(model, epochs, lr, train_loader,val_loader,opt_func=torch.optim.SGD):
    history = []
    optimizer = opt_func(model.parameters(), lr)
    for epoch in range(epochs):
        #training phase
        for btch in train_loader:
            loss = model.training_step(btch)
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()
        #validation phase
        result = evaluate(model, val_loader)
        model.epoch_end(epoch, result)
        history.append(result)
    return history
to_device(model, device)
history = [evaluate(model,vdl)]
history
history += fit(model, 30, 5e-2, train_dl, vdl)
history += fit(model, 30, 1e-3, train_dl, vdl)
history += fit(model, 30, 1e-4, train_dl, vdl)
import os
os.listdir()
# torch.save(model, 'drive/MyDrive/programming_project/digitLinearModel.pth')
torch.save(model.state_dict(), 'drive/MyDrive/programming_project/digit_state_dict.pth')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11578372474057892, 'fraction_numerical': 0.006553795740032769, 'mean_word_length': 3.5346534653465347, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '122759', 'license': 'MIT', 'n_tokens_mistral': 621, 'n_tokens_neox': 608, 'n_words': 134, 'path': 'backendModels/notebooks/digit_linear_Classifier_Pytorch.ipynb', 'repo_name': 'Yossef-Dawoad/pytorch-10-number-classifier-GUI-', 'size': '1831'}