Package: nam
NAM: Neural Additive Models - Interpretable Machine Learning with Neural NetsOverview|Installation|PaperNAM is a library for generalized additive models research.
Neural Additive Models (NAMs) combine some of the expressivity of DNNs with the inherent intelligibility of generalized additive models. NAMs learn a linear combination of neural networks that each attend to a single input feature. These networks are trained jointly and can learn arbitrarily complex relationships between their input feature and the output.Overviewfromnam.configimportdefaultsfromnam.dataimportFoldedDataset,NAMDatasetfromnam.modelsimportNAM,get_num_unitsfromnam.trainerimportLitNAMfromnam.utilsimport*Define the experiments configurationsconfig=defaults()print(config)>>Config(activation='exu',batch_size=1024,cross_val=False,data_path='data/GALLUP.csv',decay_rate=0.995,device='cpu',dropout=0.5,early_stopping_patience=50,experiment_name='NAM',feature_dropout=0.5,fold_num=1,hidden_sizes=[64,32],l2_regularization=0.5,logdir='output',lr=0.0003,num_basis_functions=1000,num_epochs=1,num_folds=5,num_models=1,num_splits=3,num_workers=16,optimizer='adam',output_regularization=0.5,regression=False,save_model_frequency=2,save_top_k=3,seed=2021,shuffle=True,units_multiplier=2,use_dnn=False,wandb=True)Creating and preprocessing the datasetimportsklearnhousing=sklearn.datasets.fetch_california_housing()dataset=pd.DataFrame(data=housing.data,columns=housing.feature_names)dataset['target']=housing.targetconfig.regression=Truedataset=NAMDataset(config,data_path=dataset,features_columns=dataset.columns[:-1],targets_column=dataset.columns[-1])## Getting the training dataloadersdataloaders=dataset.train_dataloaders()Define NAM Modelmodel=NAM(config=config,name="NAM_GALLUP",num_inputs=len(dataset[0][0]),num_units=get_num_units(config,dataset.features),)model>>>NAM((feature_nns):ModuleList((0):FeatureNN((model):ModuleList((0):ExU(in_features=1,out_features=1000)(1):LinReLU(in_features=1000,out_features=64)(2):LinReLU(in_features=64,out_features=32)(3):Linear(in_features=32,out_features=1,bias=True)))(1):FeatureNN((model):ModuleList((0):ExU(in_features=1,out_features=4)(1):LinReLU(in_features=4,out_features=64)(2):LinReLU(in_features=64,out_features=32)(3):Linear(in_features=32,out_features=1,bias=True)))(2):FeatureNN((model):ModuleList((0):ExU(in_features=1,out_features=184)(1):LinReLU(in_features=184,out_features=64)(2):LinReLU(in_features=64,out_features=32)(3):Linear(in_features=32,out_features=1,bias=True)))))Training loopforfold,(trainloader,valloader)inenumerate(dataloaders):tb_logger=TensorBoardLogger(save_dir=config.logdir,name=f'{model.name}',version=f'fold_{fold+1}')checkpoint_callback=ModelCheckpoint(filename=tb_logger.log_dir+"/{epoch:02d}-{val_loss:.4f}",monitor='val_loss',save_top_k=config.save_top_k,mode='min')litmodel=LitNAM(config,model)trainer=pl.Trainer(logger=tb_logger,max_epochs=config.num_epochs,checkpoint_callback=checkpoint_callback)trainer.fit(litmodel,train_dataloader=trainloader,val_dataloaders=valloader)## Testing the trained modeltrainer.test(test_dataloaders=dataset.test_dataloaders())## Output>>>[{'test_loss':236.77108764648438,'test_loss_epoch':237.080322265625,'Accuracy_metric_epoch':0.6506563425064087,'Accuracy_metric':0.6458333134651184}]Nam Visualizationfig1 = plot_mean_feature_importance(litmodel.model, dataset)

fig2 = plot_nams(litmodel.model, dataset, num_cols= 3)Usage$pythonmain.py-h
usage:NeuralAdditiveModels[-h][--num_epochsNUM_EPOCHS][--learning_rateLEARNING_RATE][--batch_sizeBATCH_SIZE]--data_pathDATA_PATH--features_columnsFEATURES_COLUMNS[FEATURES_COLUMNS...]--targets_columnTARGETS_COLUMN[TARGETS_COLUMN...][--weights_columnWEIGHTS_COLUMN][--experiment_nameEXPERIMENT_NAME][--regressionREGRESSION][--logdirLOGDIR][--wandbWANDB][--hidden_sizesHIDDEN_SIZES[HIDDEN_SIZES...]][--activation{exu,relu}][--dropoutDROPOUT][--feature_dropoutFEATURE_DROPOUT][--decay_rateDECAY_RATE][--l2_regularizationL2_REGULARIZATION][--output_regularizationOUTPUT_REGULARIZATION][--dataset_nameDATASET_NAME][--seedSEED][--num_basis_functionsNUM_BASIS_FUNCTIONS][--units_multiplierUNITS_MULTIPLIER][--shuffleSHUFFLE][--cross_valCROSS_VAL][--num_foldsNUM_FOLDS][--num_splitsNUM_SPLITS][--fold_numFOLD_NUM][--num_modelsNUM_MODELS][--early_stopping_patienceEARLY_STOPPING_PATIENCE][--use_dnnUSE_DNN][--save_top_kSAVE_TOP_K]optionalarguments:-h,--helpshowthishelpmessageandexit--num_epochsNUM_EPOCHSThenumberofepochstoruntrainingfor.--learning_rateLEARNING_RATEHyperparameter:learningrate.--batch_sizeBATCH_SIZEHyperparameter:batchsize.--data_pathDATA_PATHThepathforthetrainingdata--features_columnsFEATURES_COLUMNS[FEATURES_COLUMNS...]Nameofthefeaturecolumnsinthedataset--targets_columnTARGETS_COLUMN[TARGETS_COLUMN...]Nameofthetargetcolumninthedataset--weights_columnWEIGHTS_COLUMNNameoftheweightscolumninthedataset--experiment_nameEXPERIMENT_NAMEThenamefortheexperiment--regressionREGRESSIONBooleanflagindicatingwhetherwearesolvingaregressiontaskoraclassificationtask.--logdirLOGDIRPathtodirwheretostoresummaries.--wandbWANDBUsingwandbforexperimentstrackingandlogging--hidden_sizesHIDDEN_SIZES[HIDDEN_SIZES...]FeatureNeuralNethiddensizes--activation{exu,relu}Activationfunctiontousedinthehiddenlayer.Possibleoptions:(1)relu,(2)exu--dropoutDROPOUTHyperparameter:Dropoutrate--feature_dropoutFEATURE_DROPOUTHyperparameter:Prob.withwhichfeaturesaredropped--decay_rateDECAY_RATEHyperparameter:Optimizerdecayrate--l2_regularizationL2_REGULARIZATIONHyperparameter:l2weightdecay--output_regularizationOUTPUT_REGULARIZATIONHyperparameter:featurereg--dataset_nameDATASET_NAMENameofthedatasettoloadfortraining.--seedSEEDseedfortorch--num_basis_functionsNUM_BASIS_FUNCTIONSNumberofbasisfunctionstouseinaFeatureNNforareal-valuedfeature.--units_multiplierUNITS_MULTIPLIERNumberofbasisfunctionsforacategoricalfeature--shuffleSHUFFLEShufflethetrainingdata--cross_valCROSS_VALBooleanflagindicatingwhethertoperformcrossvalidationornot.--num_foldsNUM_FOLDSNumberofNfolds--num_splitsNUM_SPLITSNumberofdatasplitstouse--fold_numFOLD_NUMIndexofthefoldtobeused--num_modelsNUM_MODELSthenumberofmodelstotrain.--early_stopping_patienceEARLY_STOPPING_PATIENCEEarlystoppingepochs--use_dnnUSE_DNNDeepNNbaseline.--save_top_kSAVE_TOP_KIndicatesthemaximumnumberofrecentcheckpointfilestokeep.Citing NAM@misc{kayid2020nams,title={Neural additive models Library},author={Kayid, Amr and Frosst, Nicholas and Hinton, Geoffrey E},year={2020}}@article{agarwal2020neural,title={Neural additive models: Interpretable machine learning with neural nets},author={Agarwal, Rishabh and Frosst, Nicholas and Zhang, Xuezhou and Caruana, Rich and Hinton, Geoffrey E},journal={arXiv preprint arXiv:2004.13912},year={2020}}