for g in optimizer.param_groups: g['lr'] = new_lr return new_lr if __name__ == '__main__': use_cuda = len(sys.argv) > 1 and sys.argv[1] == 'cuda' num_epochs = 80 # get cifar 10 data trainloader, testloader = get_dataset() benchmark, debug = False, True resnet = Resnet(n=2,dbg=debug) resnet.train() if use_cuda: resnet = resnet.cuda() for block in resnet.residual_blocks: block.cuda() current_lr = 1e-4 # optimizer = optim.SGD(resnet.parameters(), lr=current_lr, weight_decay=0.0001, momentum=0.9) optimizer = optim.Adam(resnet.parameters(), lr=1e-4, weight_decay=0.0001) train_accs, test_accs = [], [] gradient_norms = [] def train_model(): current_lr=1e-4 stopping_threshold, current_count = 3, 0 n_iters = 0 for e in range(num_epochs): # modify learning rate at for i, data in enumerate(trainloader, 0): x, y = data
if __name__ == '__main__': transform = transforms.Compose([transforms.RandomResizedCrop(224),transforms.ToTensor() ,transforms.Normalize((0.5 , 0.5 , 0.5) , (0.5 , 0.5 , 0.5))]) # trainset = torchvision.datasets.CIFAR10(root = './data' , train = True , download = True , transform = transform) # trainloader = torch.utils.data.DataLoader(trainset , batch_size = 256 , shuffle = True , num_workers =2) testset = torchvision.datasets.CIFAR10(root = './data' , train = False , download = True , transform = transform) testloader = torch.utils.data.DataLoader(testset , batch_size = 9 , shuffle = False , num_workers = 2) # classes = ('plane' , 'car' , 'bird' , 'cat' , 'deer' , 'dog' , 'frog' , 'horse' , 'ship' , 'truck') os.environ['CUDA_VISIBLE_DEVICES'] = "3" # # resnet = Resnet() device_ids=[0] resnet = resnet.cuda(device_ids[0]) net=torch.nn.DataParallel(resnet,device_ids=device_ids) # # read model print('===> Try load checkpoint') if os.path.isdir('checkpoint'): try: checkpoint = torch.load('./checkpoint/resnet_final.t7') net.load_state_dict(checkpoint['state']) start_epoch = checkpoint['epoch'] print('===> Load last checkpoint data') except FileNotFoundError: start_epoch = 0 print('Can\'t found resnet_final.t7') else: start_epoch = 0