Package: train-pytorch
Train-PytorchSimlified pytorch training!PyPI project:https://pypi.org/project/train-pytorch/The package provide:A basicTrainerclass to facilidate pytorch model training.Some functions to compute common accuracy metrics including:binary_accuracymultiple_class_accuracyregression_r2You can also define your own function to input into theTrainerclass as long as your function can:take 2 inputs:logitsandlabelsperform all computation on:torch.tensorreturn a python value by:value.item()An example of our providedbinary_accuracyfunction is:defbinary_accuracy(logits,labels,cutoff=0):"""Compute binary classification accuracy score.return accuracy valueArgs:logits: logits - outputs of the modellabels: true labels of datacutoff: default is 0 - model outputs logitscan be set to 1 - if model outputs probabilities"""logits,labels=logits.cpu(),labels.cpu()predicts=(logits>cutoff).float()acc=(predicts==labels).float().mean()returnacc.item()1. InstallationFrom Github:git clone https://github.com/datngu/train_pytorchcd train_pytorchpip install .From PyPI:pip install train-pytorch1. Example on the MNIST dataset with multiple_class_accuracy1.1 Load your librariesimporttorchimporttorch.nnasnnimporttorch.optimasoptimfromtorchvisionimportdatasets,transformsfromtorch.utils.dataimportDataLoader## import train_pytorch packages and metric functionsfromtrain_pytorchimportTrainer,binary_accuracy,multiple_class_accuracy,regression_r21.2 Load your datasettransform=transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,),(0.5,))])train_dataset=datasets.MNIST(root='./data',train=True,transform=transform,download=True)test_dataset=datasets.MNIST(root='./data',train=False,transform=transform,download=True)train_loader=DataLoader(train_dataset,batch_size=512,shuffle=True)test_loader=DataLoader(test_dataset,batch_size=512,shuffle=False)1.3 Buid your modelclassCNNModel(nn.Module):def__init__(self):super(CNNModel,self).__init__()self.conv1=nn.Conv2d(1,32,kernel_size=3,stride=1,padding=1)self.conv2=nn.Conv2d(32,64,kernel_size=3,stride=1,padding=1)self.pool=nn.MaxPool2d(kernel_size=2,stride=2,padding=0)self.fc1=nn.Linear(64*7*7,128)self.fc2=nn.Linear(128,10)defforward(self,x):x=self.pool(torch.relu(self.conv1(x)))x=self.pool(torch.relu(self.conv2(x)))x=x.view(-1,64*7*7)x=torch.relu(self.fc1(x))x=self.fc2(x)returnx1.4 Let's train it!model=CNNModel()## GPU: optional#device = torch.device("cuda:0" if torch.cuda.is_available() else "mps")#model.to(device)criterion=nn.CrossEntropyLoss()optimizer=optim.Adam(model.parameters(),lr=0.001)trainer=Trainer(model,criterion,optimizer,multiple_class_accuracy,num_epochs=10,early_stoper=5)trainer.fit(train_loader,train_loader,'./output_dir')2. Example on the sklearn breast_cancer dataset with binary_accuracy2.1 Load your librariesimporttorchfromtorchimportnnfromtorch.nnimportfunctionalasFimportnumpyasnpimportmatplotlib.pyplotaspltfromsklearn.preprocessingimportStandardScalerfromsklearn.datasetsimportload_breast_cancerfromtorch.utils.dataimportDataset,DataLoader## import train_pytorch packages and metric functionsfromtrain_pytorchimportTrainer,binary_accuracy,multiple_class_accuracy,regression_r22.2 Load your datasetdata=load_breast_cancer()x=data['data']y=data['target']sc=StandardScaler()x=sc.fit_transform(x)## create dataset classclassdataset(Dataset):def__init__(self,x,y):self.x=torch.tensor(x,dtype=torch.float32)self.y=torch.tensor(y,dtype=torch.float32)self.length=self.x.shape[0]def__getitem__(self,idx):returnself.x[idx],self.y[idx]def__len__(self):returnself.length# a bit lazy to slipt train and test data, but it is okey for tutorial :Dtrain_data=dataset(x,y)val_data=dataset(x,y)train_loader=DataLoader(train_data,batch_size=64,shuffle=False)val_loader=DataLoader(val_data,batch_size=64,shuffle=False)2.3 Buid your modelclassNet(nn.Module):def__init__(self,input_shape):super(Net,self).__init__()self.fc1=nn.Linear(input_shape,32)self.fc2=nn.Linear(32,64)self.fc3=nn.Linear(64,1)defforward(self,x):x=torch.relu(self.fc1(x))x=torch.relu(self.fc2(x))x=self.fc3(x)returnx2.4 Let's train it!model=Net(input_shape=x.shape[1])## GPU: optional#device = torch.device("cuda:0" if torch.cuda.is_available() else "mps")#model.to(device)optimizer=torch.optim.SGD(model.parameters(),lr=0.1)loss_fn=nn.BCEWithLogitsLoss()trainer=Trainer(model,loss_fn,optimizer,binary_accuracy,num_epochs=10)trainer.fit(train_loader,val_loader,'./output_dir')