Package: lcurvetools
lcurvetoolsSimple tools for Python language to plot learning curves of a neural network model trained with the keras or scikit-learn framework.NOTE:All of the plotting examples below are forinteractive Python modein Jupyter-like environments. If you are in non-interactive mode you may need to explicitly callmatplotlib.pyplot.showto display the window with built plots on your screen.Thelcurves_by_historyfunction to plot learning curves by thehistoryattribute of the kerasHistoryobjectNeural network model training with keras is performed using thefitmethod. The method returns theHistoryobject with thehistoryattribute which is dictionary and contains keys with training and validation values of losses and metrics, as well as learning rate values at successive epochs. Thelcurves_by_historyfunction uses theHistory.historydictionary to plot the learning curves as the dependences of the above values on the epoch index.Usage schemeImport thekerasmodule and thelcurves_by_historyfunction:importkerasfromlcurvetoolsimportlcurves_by_historyCreate,compileandfitthe keras model:model=keras.Model(...)# or keras.Sequential(...)model.compile(...)hist=model.fit(...)Usehist.historydictionary to plot the learning curves as the dependences of values of all keys in the dictionary on an epoch index with automatic recognition of keys of losses, metrics and learning rate:lcurves_by_history(hist.history);Typical appearance of the output figureThe appearance of the output figure depends on the list of keys in thehist.historydictionary, which is determined by the parameters of thecompileandfitmethods of the model. For example, for a typical usage of these methods, the list of keys would be['loss', 'accuracy', 'val_loss', 'val_accuracy']and the output figure will contain 2 subplots with loss and metrics vertical axes and might look like this:model.compile(loss="categorical_crossentropy",metrics=["accuracy"])hist=model.fit(x_train,y_train,validation_split=0.1,epochs=50)lcurves_by_history(hist.history);Note:minimum values of loss curves and the maximum values of metric curves are marked by points.Of course, if themetricsparameter of thecompilemethod is not specified, then the output figure will not contain a metric subplot.Usage of callbacks for thefitmethod can add new keys to thehist.historydictionary. For example, theReduceLROnPlateaucallback adds thelrkey with learning rate values for successive epochs. In this case the output figure will contain additional subplot with learning rate vertical axis in a logarithmic scale and might look like this:hist=model.fit(x_train,y_train,validation_split=0.1,epochs=50,callbacks=keras.callbacks.ReduceLROnPlateau(),)lcurves_by_history(hist.history);Customizing appearance of the output figureThelcurves_by_historyfunction has optional parameters to customize the appearance of the output figure. For example, theepoch_range_to_scaleoption allows to specify the epoch index range within which the subplots of the losses and metrics are scaled.Ifepoch_range_to_scaleis a list or a tuple of two int values, then they specify the epoch index limits of the scaling range in the form[start, stop), i.e. as forsliceandrangeobjects.Ifepoch_range_to_scaleis an int value, then it specifies the lower epoch indexstartof the scaling range, and the losses and metrics subplots are scaled by epochs with indices fromstartto the last.So, you can exclude the first 5 epochs from the scaling range as follows:lcurves_by_history(hist.history,epoch_range_to_scale=5);For a description of other optional parameters of thelcurves_by_historyfunction to customize the appearance of the output figure, see its docstring.Thelcurves_by_historyfunction returns a numpy array or a list of thematplotlib.axes.Axesobjects corresponded to the built subplots from top to bottom. So, you can use the methods of these objects to customize the appearance of the output figure.axs=lcurves_by_history(history,initial_epoch=1,epoch_range_to_scale=6)axs[0].tick_params(axis="x",labeltop=True)axs[-1].set_xlabel('number of passed epochs')axs[-1].legend().remove()Thehistory_concatenatefunction to concatenate twoHistory.historydictionariesThis function is useful for combining histories of model fitting with two or more runs into a single history to plot full learning curves.Usage schemeImport thekerasmodule and thehistory_concatenate,lcurves_by_historyfunction:importkerasfromlcurvetoolsimporthistory_concatenate,lcurves_by_historyCreate,compileandfitthe keras model:model=keras.Model(...)# or keras.Sequential(...)model.compile(...)hist1=model.fit(...)Compile as needed and fit using possibly other parameter values:model.compile(...)# optionalhist2=model.fit(...)Concatenate the.historydictionaries into one:full_history=history_concatenate(hist1.history,hist2.history)Usefull_historydictionary to plot full learning curves:lcurves_by_history(full_history);Thelcurves_by_MLP_estimatorfunction to plot learning curves of the scikit-learn MLP estimatorThe scikit-learn library provides 2 classes for building multi-layer perceptron (MLP) models of classification and regression:MLPClassifierandMLPRegressor. After creation and fitting of these MLP estimators with usingearly_stopping=TruetheMLPClassifierandMLPRegressorobjects have theloss_curve_andvalidation_scores_attributes with train loss and validation score values at successive epochs. Thelcurves_by_historyfunction uses theloss_curve_andvalidation_scores_attributes to plot the learning curves as the dependences of the above values on the epoch index.Usage schemeImport theMLPClassifier(orMLPRegressor) class and thelcurves_by_MLP_estimatorfunction:fromsklearn.neural_networkimportMLPClassifierfromlcurvetoolsimportlcurves_by_MLP_estimatorCreateandfitthe scikit-learn MLP estimator:clf=MLPClassifier(...,early_stopping=True)clf.fit(...)Useclfobject withloss_curve_andvalidation_scores_attributes to plot the learning curves as the dependences of loss and validation score values on epoch index:lcurves_by_MLP_estimator(clf)Typical appearance of the output figureThelcurves_by_MLP_estimatorfunction with default value of the parameteron_separate_subplots=Falseshows the learning curves of loss and validation score on one plot with two vertical axes scaled independently. Loss values are plotted on the left axis and validation score values are plotted on the right axis. The output figure might look like this:Note:the minimum value of loss curve and the maximum value of validation score curve are marked by points.Customizing appearance of the output figureThelcurves_by_MLP_estimatorfunction has optional parameters to customize the appearance of the output figure. For example,thelcurves_by_MLP_estimatorfunction withon_separate_subplots=Trueshows the learning curves of loss and validation score on two separated subplots:lcurves_by_MLP_estimator(clf,on_separate_subplots=True)And theepoch_range_to_scaleoption allows to specify the epoch index range within which the subplots of the losses and metrics are scaled (see details about this option in the docstring of thelcurves_by_MLP_estimatorfunction).lcurves_by_MLP_estimator(clf,epoch_range_to_scale=10);For a description of other optional parameters of thelcurves_by_MLP_estimatorfunction to customize the appearance of the output figure, see its docstring.Thelcurves_by_MLP_estimatorfunction returns a numpy array or a list of thematplotlib.axes.Axesobjects of an output figure (see additional details in theReturnssection of thelcurves_by_MLP_estimatorfunction docstring). So, you can use the methods of these objects to customize the appearance of the output figure.axs=lcurves_by_MLP_estimator(clf,initial_epoch=1,epoch_range_to_scale=11);axs[0].grid(axis='y',visible=False)axs[1].grid(axis='y',visible=False)axs[0].set_xlabel('number of passed epochs')axs[1].set_ylabel('validation accuracy');CHANGELOGVersion 1.0.0Initial release.