Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot training and validation loss curves?

I am working with the yeast dataset available at:

http://archive.ics.uci.edu/ml/datasets/yeast

and I want to make a neural network classifier model and plot the learning curves. So, I have used the model_selection of Scikit twice; one for making the training and testing set and once more for selecting the validation set. From these two sets I would like to plot the learning curves, my code is the following:

import numpy as np
import pandas as pd
from sklearn import model_selection, linear_model
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.neural_network import MLPClassifier
import matplotlib.pyplot as plt

def readFile(file):
    head=["seq_n","mcg","gvh","alm","mit","erl","pox","vac","nuc","site"]
    f=pd.read_csv(file,delimiter=r"\s+")
    f.columns=head
    return f

def NeuralClass(X,y):
    X_train,X_test,y_train,y_test=model_selection.train_test_split(X,y,test_size=0.2)
    X_tr,X_val,y_tr,y_val=model_selection.train_test_split(X_train,y_train,test_size=0.2)
    mlp=MLPClassifier(activation="relu",max_iter=3000)
    mlp.fit(X_train,y_train)
    print (mlp.score(X_train,y_train))
    plt.plot(mlp.loss_curve_)
    mlp.fit(X_val,y_val)
    plt.plot(mlp.loss_curve_)

def main():
    f=readFile("yeast.data")
    list=["seq_n","site"]
    X=f.drop(list,1)
    y=f["site"]
    NeuralClass(X,y)

if __name__=="__main__":
    main()

I have obtained a graph like the following, and I do not know if it's correct:

loss

The question is if this would be the correct way to plot the validation curve or if the method I followed is the right one.

like image 427
Little Avatar asked Sep 11 '26 08:09

Little


1 Answers

Didn't test it, but should be something like this:

def NeuralClass(X,y):
    X_train,X_test,y_train,y_test = model_selection.train_test_split(
        X,y,test_size=0.2)
    mlp=MLPClassifier(
        activation="relu",
        max_iter=3000, 
        validation_fraction=0.2, 
        early_stopping=True)
    mlp.fit(X_train,y_train)
    print (mlp.score(X_train,y_train))
    plt.plot(mlp.loss_curve_)
    plt.plot(mlp.validation_scores_)
like image 127
Marat Avatar answered Sep 12 '26 22:09

Marat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!