#NEWLINE# @ PluggableDecorator ( 'fit_decorators' ) #NEWLINE# @ args_from_config #NEWLINE# def fit( dataset_loader_train , model , model_persister , persist = True , activate = True , dataset_loader_test = None , evaluate = False , persist_if_better_than = None ) : #NEWLINE# #INDENT# if ( persist_if_better_than is not None ) : #NEWLINE# #INDENT# evaluate = True #NEWLINE# if ( dataset_loader_test is None ) : #NEWLINE# #INDENT# raise ValueError ( "When using 'persist_if_better_than', make sure you also provide a 'dataset_loader_test'." ) #NEWLINE# #UNINDENT# #UNINDENT# with timer ( logger . info , 'Loading data' ) : #NEWLINE# #INDENT# ( X , y ) = dataset_loader_train ( ) #NEWLINE# #UNINDENT# with timer ( logger . info , 'Fitting model' ) : #NEWLINE# #INDENT# model . fit ( X , y ) #NEWLINE# #UNINDENT# if evaluate : #NEWLINE# #INDENT# with timer ( logger . debug , 'Evaluating model on train set' ) : #NEWLINE# #INDENT# score_train = model . score ( X , y ) #NEWLINE# annotate ( model , { #NEWLINE# #INDENT# 'score_train' : score_train , #NEWLINE# #UNINDENT# } ) #NEWLINE# logger . info ( 'Train score: {}' . format ( score_train ) ) #NEWLINE# #UNINDENT# #UNINDENT# score_test = None #NEWLINE# if ( evaluate and ( dataset_loader_test is not None ) ) : #NEWLINE# #INDENT# ( X_test , y_test ) = dataset_loader_test ( ) #NEWLINE# with timer ( logger . debug , 'Evaluating model on test set' ) : #NEWLINE# #INDENT# score_test = model . score ( X_test , y_test ) #NEWLINE# annotate ( model , { #NEWLINE# #INDENT# 'score_test' : score_test , #NEWLINE# #UNINDENT# } ) #NEWLINE# logger . info ( 'Test score:  {}' . format ( score_test ) ) #NEWLINE# #UNINDENT# #UNINDENT# if persist : #NEWLINE# #INDENT# if ( ( persist_if_better_than is not None ) and ( score_test < persist_if_better_than ) ) : #NEWLINE# #INDENT# logger . info ( 'Not persisting model that has a test score {} < {}' . format ( score_test , persist_if_better_than ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# annotate ( model , { #NEWLINE# #INDENT# 'train_timestamp' : datetime . now ( ) . isoformat ( ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# with timer ( logger . info , 'Writing model' ) : #NEWLINE# #INDENT# version = model_persister . write ( model ) #NEWLINE# #UNINDENT# logger . info ( 'Wrote model with version {}.' . format ( version ) ) #NEWLINE# if activate : #NEWLINE# #INDENT# model_persister . activate ( version ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return model