def trainNNs(X, T, trainFraction, hiddenLayerStructures, numberRepetitions, numberIterations, classify=False): import neuralnetworks as nn import mlutils as ml import numpy as np import time result = [] for structure in hiddenLayerStructures: trainedResult = [] testResult = [] t0 = time.time() for n in range(0, numberRepetitions): Xtrain,Ttrain,Xtest,Ttest = ml.partition(X,T,(trainFraction, 1-trainFraction),classify) if classify: nnet = nn.NeuralNetworkClassifier(X.shape[1], structure, len(np.unique(T))) nnet.train(Xtrain, Ttrain, numberIterations, errorPrecision=1.e-8) trainedResult.append(np.sum(nnet.use(Xtrain)==Ttrain)/len(Ttrain)) testResult.append(np.sum(nnet.use(Xtest)==Ttest)/len(Ttest)) else: nnet = nn.NeuralNetwork(X.shape[1], structure, T.shape[1]) nnet.train(Xtrain, Ttrain, numberIterations) trainedResult.append(np.sqrt(np.mean(((nnet.use(Xtrain)-Ttrain)**2)))) testResult.append(np.sqrt(np.mean(((nnet.use(Xtest)-Ttest)**2)))) result.append([structure, trainedResult, testResult, time.time() - t0]) return result
def trainNNs(X, T, trainFraction, hiddenLayerStructures, numberRepetitions, numberIterations, classify=False): """ Trains neural networks repeatedly. :param X: Data to partition and train :param T: Target values :param trainFraction: What percent of the data should be used for training :param hiddenLayerStructures: Number of hidden layer structures while training :param numberRepetitions: Number of times to run train :param numberIterations: Iterations within Neural Network :param classify: Classification or Regression :return: List containing the hidden layer structure, the training error and testing error, and the elapsed time. """ import numpy as np import neuralnetworks as nn import time import mlutils as ml results = [] global resultErrors resultErrors = [] # debugging verbose = True for structure in hiddenLayerStructures: trainList = [] testList = [] t0 = time.time() for i in range(numberRepetitions): Xtrain, Ttrain, Xtest, Ttest = ml.partition(X, T, (trainFraction, 1 - trainFraction), classification=classify) if classify: nnet = nn.NeuralNetworkClassifier(X.shape[1],structure,len(np.unique(T))) else: nnet = nn.NeuralNetwork(X.shape[1],structure,T.shape[1])
def performanceC(X, T, trainFraction, hidden, numberRepetitions, numberIterations): # Make the lists for train and test data performance trainP = [] testP = [] # For numberRepetitions for rep in range(numberRepetitions): # Use ml.partition to randomly partition X and T into training and testing sets. Xtrain, Ttrain, Xtest, Ttest = ml.partition( X, T, (trainFraction, 1 - trainFraction), classification=True) # Create a neural network of the given structure nnet = nn.NeuralNetworkClassifier(X.shape[1], hidden, len(np.unique(T))) # Train it for numberIterations # nnet.train(X, T, numberIterations) nnet.train(Xtrain, Ttrain, numberIterations) # Use the trained network to produce outputs for the training and for the testing sets Ytrain = nnet.use(Xtrain) Ytest = nnet.use(Xtest) # Calculate the fraction of samples incorrectly classified for training and testing sets trainFrac = np.sum(Ytrain != Ttrain) / Ttrain.shape[0] testFrac = np.sum(Ytest != Ttest) / Ttest.shape[0] # Add the training and testing performance to a collection (such as a list) for this network structure trainP.append(trainFrac) testP.append(testFrac) # Return trainP and testP return trainP, testP
def trainNNs(X, T, trainFraction, hiddenLayerStructures, numberRepetitions, numberIterations, classify=False): results = [] for structure in hiddenLayerStructures: print(structure, end=" ") #time each hidden layer structure start_time = time.time() structureData = [structure] trainDataResults = [] testDataResults = [] for i in range(0, numberRepetitions): #partition data Xtrain, Ttrain, Xtest, Ttest = ml.partition( X, T, (trainFraction, 1 - trainFraction), classification=classify) if not classify: #create/train network nnet = nn.NeuralNetwork(Xtrain.shape[1], structure, Ttrain.shape[1]) nnet.train(Xtrain, Ttrain, nIterations=numberIterations) #test netork Ytrain = nnet.use(Xtrain) Ytest = nnet.use(Xtest) #add error for testing and traing data trainDataResults.append(np.sqrt(np.mean((Ytrain - Ttrain)**2))) testDataResults.append(np.sqrt(np.mean((Ytest - Ttest)**2))) else: #create/train network nnet = nn.NeuralNetworkClassifier(Xtrain.shape[1], structure, np.unique(Ttrain).size) nnet.train(Xtrain, Ttrain, nIterations=numberIterations) #test netork Ptrain = nnet.use(Xtrain) Ptest = nnet.use(Xtest) #add error for testing and traing data trainDataResults.append(1 - (np.sum(Ptrain == Ttrain) / len(Ttrain))) testDataResults.append(1 - (np.sum(Ptest == Ttest) / len(Ttest))) structureData.append(trainDataResults) structureData.append(testDataResults) structureData.append(time.time() - start_time) results.append(structureData) print("done") return results
def trainNNs(X, T, trainFraction, hiddenLayerStructures, numberRepetitions, numberIterations, classify): results = [] # Do tasks here for h_layer in hiddenLayerStructures: start = time.time() train_rmse = [] test_rmse = [] for repetition in range(numberRepetitions): Xtrain, Ttrain, Xtest, Ttest = ml.partition( X, T, (trainFraction, 1 - trainFraction), classification=classify) if classify: nnet = nn.NeuralNetworkClassifier(X.shape[1], h_layer, T.shape[1]) nnet.train(Xtrain, Ttrain, numberIterations) predTest, probsTest, _ = nnet.use( Xtest, allOutputs=True) # discard hidden unit outputs ml.percentCorrect(predTest, Ttest) else: nnet = nn.NeuralNetwork(X.shape[1], h_layer, T.shape[1]) nnet.train(Xtrain, Ttrain, numberIterations) Ytrain = nnet.use(Xtrain) Ytest = nnet.use(Xtest) trn_rmse = np.sqrt(np.mean((Ytrain - Ttrain)**2)) tst_rmse = np.sqrt(np.mean((Ytest - Ttest)**2)) train_rmse.append(trn_rmse) test_rmse.append(tst_rmse) if repetition == (numberRepetitions - 1): total_time = time.time() - start results.append([h_layer, train_rmse, test_rmse, total_time]) # End tasks # print(results) return results
result.append([structure, trainedResult, testResult, time.time() - t0]) return result def summarize(results): import numpy as np summaryResults = [] for result in results: summaryResults.append([result[0], np.mean(result[1]), np.mean(result[2]), result[3]]) return summaryResults def bestNetwork(summary): best = min(summary, key=lambda l: l[2]) return best data = pd.read_csv("templates/data1Normed.csv") names = list(data) data["signcode"] = data["sign"].astype('category').cat.codes data = data.values Xhands = data[:, 0:63] Xhands = Xhands.astype(np.float64) Tsign = data[:, 64:65] Tsign = Tsign.astype(np.int32) #run best on Xtrain,Ttrain,Xtest,Ttest = ml.partition(Xhands,Tsign,(0.8, 0.2),True) nnet = nn.NeuralNetworkClassifier(Xtrain.shape[1], bestNet, len(np.unique(Ttrain))) nnet.train(Xtrain, Ttrain, 200) result = nnet.use(Xtest)