Package: potatorch
PotaTorch is a lightweight PyTorch framework specifically designed to run on hardware with limited resources.InstallationPotaTorch is published on PyPI, you can install it through pip:pipinstallpotatorchor you can install it from sources:gitclone--single-branch-bmainhttps://github.com/crybot/potatorch
pipinstall-epotatorchMinimal Working ExampleYou can run the following example directly fromexamples/mlp.pyif you already have pytorch installed, or you can run it with docker through the provided scripts:./build.sh&&./run.shThe example trains a feed forward network on a toy problem:importtorchfromtorchimportnnfrompotatorch.trainingimportTrainingLoop,make_optimizerfrompotatorch.callbacksimportProgressbarCallbackfromtorch.utils.dataimportTensorDataset# Fix a seed for TrainingLoop to make non-deterministic operations such as# shuffling reproducibleSEED=42device='cuda'epochs=100lr=1e-4# Define your model as a pytorch Modulemodel=nn.Sequential(nn.Linear(1,128),nn.ReLU(),nn.Linear(128,128),nn.ReLU(),nn.Linear(128,1))# Create your dataset as a torch.data.Datasetdataset=TensorDataset(torch.arange(1000).view(1000,1),torch.sin(torch.arange(1000)))# Provide a loss function and an optimizerloss_fn=torch.nn.MSELoss()optimizer=make_optimizer(torch.optim.Adam,lr=lr)# Construct a TrainingLoop object.# TrainingLoop handles the initialization of dataloaders, dataset splitting,# shuffling, mixed precision training, etc.# You can provide callback handles through the `callbacks` argument.training_loop=TrainingLoop(dataset,loss_fn,optimizer,train_p=0.8,val_p=0.1,test_p=0.1,random_split=False,batch_size=None,shuffle=False,device=device,num_workers=0,seed=SEED,val_metrics={'l1':nn.L1Loss(),'mse':nn.MSELoss()},callbacks=[ProgressbarCallback(epochs=epochs,width=20),])# Run the training loopmodel=training_loop.run(model,epochs=epochs)Automatic Hyperparameters OptimizationPotaTorch provides a basic set of utilities to perform hyperparameters optimization. You can choose amonggrid search,random searchandbayesian search. All of them are provided bypotatorch.optimization.tuning.HyperOptimizer. The following is a working example of a simple grid search on a toy problem. You can find the full script underexamples/grid_search.pydeftrain(dataset,device,config):""" Your usual training function that runs a TrainingLoop instance """SEED=42# `epochs` is a fixed hyperparameter; it won't change among runsepochs=config['epochs']# Define your model as a pytorch Modulemodel=nn.Sequential(nn.Linear(1,128),nn.ReLU(),nn.Linear(128,128),nn.ReLU(),nn.Linear(128,1))loss_fn=torch.nn.MSELoss()# `lr` is a dynamic hyperparameter; it will change among runsoptimizer=make_optimizer(torch.optim.Adam,lr=config['lr'])training_loop=TrainingLoop(dataset,loss_fn,optimizer,train_p=0.8,val_p=0.1,test_p=0.1,random_split=False,batch_size=None,shuffle=False,device=device,num_workers=0,seed=SEED,val_metrics={'l1':nn.L1Loss(),'mse':nn.MSELoss()},callbacks=[ProgressbarCallback(epochs=epochs,width=20),])model=training_loop.run(model,epochs=epochs,verbose=1)# Return a dictionary containing the training and validation metrics# calculated during the last epoch of the loopreturntraining_loop.get_last_metrics()# Define your search configurationsearch_config={'method':'grid',# which search method to use: ['grid', 'bayes', 'random']'metric':{'name':'val_loss',# the metric you're optimizing'goal':'minimize'# whether you want to minimize or maximize it},'parameters':{# the set of hyperparameters you want to optimize'lr':{'values':[1e-2,1e-3,1e-4]# a range of values for the grid search to try}},'fixed':{# fixed hyperparameters that won't change among runs'epochs':200}}defmain():device='cuda'dataset=TensorDataset(torch.arange(1000).view(1000,1),torch.sin(torch.arange(1000)))# Apply additional parameters to the train function to have f(config) -> {}score_function=partial(train,dataset,device)# Construct the hyperparameters optimizerhyperoptimizer=HyperOptimizer(search_config)# Run the optimization over the hyperparameters spaceconfig,error=hyperoptimizer.optimize(score_function,return_error=True)print('Best configuration found:{}\nwith error:{}'.format(config,error))