This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from torch.optim.optimizer import Optimizer, required | |
| import re | |
| EETA_DEFAULT = 0.001 | |
| class LARS(Optimizer): | |
| """ | |
| Layer-wise Adaptive Rate Scaling for large batch training. | |
| Introduced by "Large Batch Training of Convolutional Networks" by Y. You, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SimCLR_Loss(nn.Module): | |
| def __init__(self, batch_size, temperature): | |
| super().__init__() | |
| self.batch_size = batch_size | |
| self.temperature = temperature | |
| self.mask = self.mask_correlated_samples(batch_size) | |
| self.criterion = nn.CrossEntropyLoss(reduction="sum") | |
| self.similarity_f = nn.CosineSimilarity(dim=2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Identity(nn.Module): | |
| def __init__(self): | |
| super(Identity, self).__init__() | |
| def forward(self, x): | |
| return x | |
| class LinearLayer(nn.Module): | |
| def __init__(self, | |
| in_features, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| transform = transforms.Compose( | |
| [transforms.ToTensor(), | |
| transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) | |
| batch_size = 4 | |
| trainset = torchvision.datasets.CIFAR10(root='./data', train=True, | |
| download=True, transform=transform) | |
| trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, | |
| shuffle=True, num_workers=2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dg = C10DataGen('train',trimages) | |
| dl = DataLoader(dg,batch_size = 128,drop_last=True) | |
| vdg = C10DataGen('valid',valimages) | |
| vdl = DataLoader(vdg,batch_size = 128,drop_last=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class C10DataGen(Dataset): | |
| def __init__(self,phase,imgarr,s = 0.5): | |
| self.phase = phase | |
| self.imgarr = imgarr | |
| self.s = s | |
| self.transforms = transforms.Compose([transforms.RandomHorizontalFlip(0.5), | |
| transforms.RandomResizedCrop(32,(0.8,1.0)), | |
| transforms.Compose([transforms.RandomApply([transforms.ColorJitter(0.8*self.s, | |
| 0.8*self.s, | |
| 0.8*self.s, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pickle | |
| def unpickle(file): | |
| with open(file, 'rb') as fo: | |
| dict = pickle.load(fo, encoding='bytes') | |
| return dict | |
| train_files = ['data_batch_1','data_batch_2','data_batch_3','data_batch_4','data_batch_5'] | |
| images = np.array([],dtype=np.uint8).reshape((0,3072)) | |
| labels = np.array([]) | |
| for tf in train_files: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !wget https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz | |
| !tar -xf /content/cifar-10-python.tar.gz |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def set_seed(seed = 16): | |
| np.random.seed(seed) | |
| torch.manual_seed(seed) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import pandas as pd | |
| import shutil, time, os, requests, random, copy | |
| import torch | |
| import torch.nn as nn | |
| import torch.optim as optim | |
| from torch.utils.data import Dataset, DataLoader | |
| from torchvision import datasets, transforms, models |