Package: torchplate
torchplate: Minimal Experiment Workflows in PyTorch(Github|PyPI|Documentation)Installation|Example|More examples|Starter project|ChangelogAn extremely minimal and simple experiment module for machine learning in PyTorch (PyTorch + boilerplate =torchplate).In addition to abstracting away the training loop, we provide several abstractions to improve the efficiency of machine learning workflows with PyTorch.Installation$ pip install torchplateExampleTo get started, create a child class oftorchplate.experiment.Experimentand provide several key, experiment-unique items: model, optimizer, and a training set dataloader. Then, provide an implementation of the abstract methodevaluate. This function takes in a batch from thetrainloaderand should return the loss (i.e., implement the forward pass + loss calculation). Add whatever custom methods you may want to this class. Then starting training! That's it!importtorchplatefromtorchplateimportexperimentfromtorchplateimportutilsimporttorchimporttorch.optimasoptimimporttorch.nnasnnimporttorch.nn.functionalasFimportrequestsimportcloudpickleascpfromurllib.requestimporturlopenclassNet(nn.Module):def__init__(self):super().__init__()self.fc1=nn.Linear(3*32*32,120)self.fc2=nn.Linear(120,84)self.fc3=nn.Linear(84,3)defforward(self,x):x=torch.flatten(x,1)x=F.relu(self.fc1(x))x=F.relu(self.fc2(x))x=self.fc3(x)returnxclassCifarExp(torchplate.experiment.Experiment):def__init__(self):self.model=Net()self.optimizer=optim.Adam(self.model.parameters(),lr=0.001)self.criterion=nn.CrossEntropyLoss()dataset=cp.load(urlopen("https://stanford.edu/~rsikand/assets/datasets/mini_cifar.pkl"))# use various torchplate.utils to improve efficiency of common workflowsself.trainloader,self.testloader=torchplate.utils.get_xy_loaders(dataset)# inherit from torchplate.experiment.Experiment and pass in# model, optimizer, and dataloadersuper().__init__(model=self.model,optimizer=self.optimizer,trainloader=self.trainloader,verbose=True)# provide this abstract method to calculate lossdefevaluate(self,batch):x,y=batchlogits=self.model(x)loss_val=self.criterion(logits,y)returnloss_valdeftest(self):accuracy_count=0forx,yinself.testloader:logits=self.model(x)pred=torch.argmax(F.softmax(logits,dim=1)).item()print(f"Prediction:{pred}, True:{y.item()}")ifpred==y:accuracy_count+=1print("Accuracy: ",accuracy_count/len(self.testloader))defon_epoch_end(self):# to illustrate the concept of callbacksprint("------------------ (Epoch end) --------------------")exp=CifarExp()exp.train(num_epochs=100)exp.test()More examplesSeeexamples/cifarfor another minimal example. Seeexamples/starterfor a full program example. To get started running your own experiments, you can useexamples/starteras a base (or use cookiecutter as shown below).Starter projectThestarterbranch holds the source for acookiecutterproject. This allows users to easily create projects from the starter code example by running a simple command. To get started,install cookiecutterand then type$ cookiecutter https://github.com/rosikand/torchplate.git --checkout starterwhich will generate the following structure for you to use as a base for your projects:torchplate_starter
├── datasets.py
├── experiments.py
├── models.py
└── runner.py