mercredi 11 juillet 2018

Python: Multiple runs of neural network are only a little random

I'm using the MLPClassifier from sklearn.neural_network. The results of the training can be different for a data set depending on how the network is initialized and trained so I'm running it multiple times to get a sense of the 'average' run. When I do this manually I get different results as expected. When I do this in a loop, most of the results are the same and very few are different.

I've been trying to jostle the random number generator in an attempt to get more randomness, but it doesn't seem to have an effect. There is also a random seed parameter in the MLPclassifier. Here's the code I'm using:

import pandas as pd
FileIn = pd.read_csv(r"C:\Redacted\Arithmatic10.csv", names = ["Value", "A","B","C"])

#Split the important column into it's own frame
x = FileIn.drop('Value',axis=1)
y = FileIn['Value']

# Split the data set into training and test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y)
X_test_copy=X_test

# 'Scale' the data to allow for quicker learning
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
# Fit only to the training data
scaler.fit(X_train)

# Now apply the transformations to the data:
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report,confusion_matrix
import numpy

i=1
while True:
    if i>5:
        break
    else:
        numpy.random.seed(None)
        i=i+1
        Brains = [(10,10)]  #,(15,15,15),(20,20,20,20),(35,35,35,35,35)]
        for b in Brains:
            mlp = MLPClassifier(hidden_layer_sizes=(b),max_iter=1500,random_state=i)
            mlp.fit(X_train,y_train)
            #Try NN on the test set
            predictions = mlp.predict(X_test)
            n=confusion_matrix(y_test,predictions)
            print(n[0][1]+n[1][0]) #The total errors
            mlp=None

    #print the full results
    #print(confusion_matrix(y_test,predictions))
    #print(classification_report(y_test,predictions))




Aucun commentaire:

Enregistrer un commentaire