Package: tortreinador
Torch TerinadorA trainer based on pytorch including a train loop for MDN (Mixture Density Network), a data loader, plot line chart and
a couple of techniques for avoid over fittingInstallationThis package needs Python>=3.7 and the version of Pytorch used in development is 1.13.1 and cuda11.2, considering the different version of cuda, the package will
not install Pytorch automatically. You should check your cuda's version, install the suitablepytorchfirst. Then, run the command below:pip install tortreinadorQuick Startfromtortreinadorimporttrainfromtortreinador.models.MDNimportmdn,Mixture,NLLLossimporttorchimportpandasaspddata=pd.read_excel('D:\\Resource\\Gas_Giants_Core_Earth20W.xlsx')data['M_total (M_E)']=data['Mcore (M_J/10^3)']+data['Menv (M_E)']# Support index, e.g input_parameters = [0, 1, 2]input_parameters=['Mass (M_J)','Radius (R_E)','T_sur (K)',]output_parameters=['M_total (M_E)','T_int (K)','P_CEB (Mbar)','T_CEB (K)']trainer=train.TorchTrainer()# Load Datat_loader,v_loader,test_x,test_y,s_x,s_y=trainer.load_data(data=data,input_parameters=input_parameters,output_parameters=output_parameters,if_normal=True,if_shuffle=True)# Modelmodel=mdn(len(input_parameters),len(output_parameters),10,256)# Losscriterion=NLLLoss()pdf=Mixture()# Optimizeroptim=torch.optim.Adam(trainer.xavier_init(model),lr=0.0001984,weight_decay=0.001)# Trainingt_l,v_l,val_r2,train_r2,mse=trainer.fit_for_MDN(t_loader,v_loader,criterion,model=model,mixture=pdf,model_save_path='D:\\Resource\\MDN\\',optim=optim,best_r2=0.5)# Plot line chartresult_pd=pd.DataFrame()result_pd['epoch']=range(150)result_pd['train_r2_avg']=train_r2result_pd['val_r2_avg']=val_r2trainer.plot_line_2(y_1='train_r2_avg',y_2='val_r2_avg',df=result_pd,fig_size=(10,6),output_path="your save path",dpi=300)FunctionsThis package just support MDN for now, but theload_datais suitable for every condition as long as the type of data is Dataframetortreinador.train.TorchTrainer():Parameters:batch_size: int = 512is_gpu: bool = Trueepoch: int = 150log_dir: Optional[str] = None,Specify a file path to start up tensorboardXFunctions:load_data()Describe:Processing Dataframe according to the input/output parameters and split size to train set, validation set and test set,
you can freely choose if normalization, if shuffle.Parameters:data: DataFrame,input_parameters: list,output_parameters: list,feature_range: Any = None,train_size: float = 0.8,val_size: float = 0.1,test_size: float = 0.1,if_normal: bool = True,if_shuffle: bool = True,n_workers: int = 8Return:DataLoaderDataLoaderNumpy arrayNumpy arrayMinMaxScalerMinMaxScalerplot_line_2()Describe:This function is usually used after training to compare the validation loss and train loss, validation R2 and train R2Parameters:y_1: stry_2: strdf: DataFrameoutput_path: strfig_size: tuple = (10, 6)dpi: int = 300xavier_init()Describe:A technique for prevent over fittingParameters:net: Module_calculate()Describe:A private method for calculate loss, it is able to overwrite(Testing)Parameters:model: Any,pdf: Any,x: Any,y: Any,criterion: Any,t: str = 'train'def_calculate(self,model,pdf,x,y,criterion,t='train'):pi,mu,sigma=model(x)mixture=pdf(pi,mu,sigma)y_pred=mixture.sample()ift=='train':returncriterion(pi,mu,sigma,y),y_pred.cpu().numpy(),y.cpu().numpy()else:returncriterion(pi,mu,sigma,y),self.mse(y_pred,y),y_pred.cpu().numpy(),y.cpu().numpy()fit_for_MDN()Describe:Train loop for MDN(Mixture Density Network)Parameters:t_l: Dataloaderv_l: Dataloadercriterion: Moduleoptim: Optimizermodel: Modulemodel_save_path: strThe model which has the best performance will save accordingmodel_save_pathmixture: ModuleThe sampling class inherited from nn.Modulewarmup_epoch: Optional[int] = NoneA technique for prevent over fitting, specify a number such as 5, then it will use warm up in the first 5 epochlr_milestones: Optional[list] = NoneDecrease learning rate according the input list and gamma, for example: lr_milestones=[10], gamma=0.7, then the learning rate will x0.7 at the 10 epochgamma: float = 0.7best_r2: float = 0.80Return:train loss: Listvalidation loss: listvalidation R2: listtrain R2: listvalidation mse: list