def validate(self, validX, validY): # Input # validX : Validation Input Data # validY : Validation Labels # Returns the validation accuracy evaluated over the current neural network model valActivations = self.feedforward(validX) pred = np.argmax(valActivations[-1], axis=1) validPred = oneHotEncodeY(pred, self.outputSize) validAcc = self.computeAccuracy(validY, validPred) return pred, validAcc
def train(self, trainX, trainY, printTrainStats=True, saveModel=False, modelName=None): for epoch in range(self.epochs): if printTrainStats: print("Epoch: ", epoch) X = np.asarray(trainX) Y = np.asarray(trainY) perm = np.arange(X.shape[0]) np.random.shuffle(perm) X = X[perm] Y = Y[perm] trainLoss = 0 numBatches = int(np.ceil(float(X.shape[0]) / self.batchSize)) for batchNum in range(numBatches): XBatch = np.asarray(X[batchNum*self.batchSize: (batchNum+1)*self.batchSize]) YBatch = np.asarray(Y[batchNum*self.batchSize: (batchNum+1)*self.batchSize]) activations = self.feedforward(XBatch) loss = self.computeLoss(YBatch, activations) trainLoss += loss predLabels = oneHotEncodeY(np.argmax(activations[-1], axis=1), self.out_nodes) acc = self.computeAccuracy(YBatch, predLabels) trainAcc += acc self.backpropagate(activations, YBatch) trainAcc /= numBatches if printTrainStats: print("Epoch ", epoch, " Training Loss=", loss, " Training Accuracy=", trainAcc) if saveModel: model = [] for l in self.layers: if type(l).__name__ != "FlattenLayer": model.append(l.weights) model.append(l.biases) np.save(modelName, model) print("Model Saved... ")
def train(self, trainX, trainY, validX=None, validY=None, printTrainStats=True, printValStats=True): # Method for training the Neural Network # Input # trainX - A list of training input data to the neural network # trainY - Corresponding list of training data labels # validX - A list of validation input data to the neural network # validY - Corresponding list of validation data labels # printTrainStats - Print training loss and accuracy for each epoch # printValStats - Prints validation set accuracy after each epoch of training # The methods trains the weights and baises using the training data(trainX, trainY) # and evaluates the validation set accuracy after each epoch of training for epoch in range(self.epochs): # A Training Epoch if printTrainStats or printValStats: print("Epoch: ", epoch) # Shuffle the training data for the current epoch X = np.asarray(trainX) Y = np.asarray(trainY) perm = np.arange(X.shape[0]) np.random.shuffle(perm) X = X[perm] Y = Y[perm] # Initializing training loss and accuracy trainLoss = 0 trainAcc = 0 # Divide the training data into mini-batches numBatches = int(np.ceil(float(X.shape[0]) / self.batchSize)) for batchNum in range(numBatches): XBatch = np.asarray(X[batchNum*self.batchSize: (batchNum+1)*self.batchSize]) YBatch = np.asarray(Y[batchNum*self.batchSize: (batchNum+1)*self.batchSize]) # Calculate the activations after the feedforward pass activations = self.feedforward(XBatch) # Compute the loss loss = self.computeLoss(YBatch, activations) trainLoss += loss # Estimate the one-hot encoded predicted labels after the feedword pass predLabels = oneHotEncodeY(np.argmax(activations[-1], axis=1), self.outputSize) # Calculate the training accuracy for the current batch acc = self.computeAccuracy(YBatch, predLabels) trainAcc += acc # Backpropagation Pass to adjust weights and biases of the neural network self.backpropagate(activations, YBatch) # Print Training loss and accuracy statistics trainAcc /= numBatches if printTrainStats: print("Epoch ", epoch, " Training Loss=", loss, " Training Accuracy=", trainAcc) # Estimate the prediction accuracy over validation data set if validX is not None and validY is not None and printValStats: _, validAcc = self.validate(validX, validY) print("Validation Set Accuracy: ", validAcc, "%")
def train(self, trainX, trainY, validX=None, validY=None, printTrainStats=True, printValStats=True, saveModel=False, loadModel=False, modelName=None): # Method for training the Neural Network # Input # trainX - A list of training input data to the neural network # trainY - Corresponding list of training data labels # validX - A list of validation input data to the neural network # validY - Corresponding list of validation data labels # printTrainStats - Print training loss and accuracy for each epoch # printValStats - Prints validation set accuracy after each epoch of training # saveModel - True -> Saves model in "modelName" file after each epoch of training # loadModel - True -> Loads model from "modelName" file before training # modelName - Name of the model from which the funtion loads and/or saves the neural net # The methods trains the weights and baises using the training data(trainX, trainY) # and evaluates the validation set accuracy after each epoch of training if loadModel: model = np.load(modelName) k,i = 0,0 for l in self.layers: if type(l).__name__ != "AvgPoolingLayer" and type(l).__name__ != "FlattenLayer": self.layers[i].weights = model[k] self.layers[i].biases = model[k+1] k+=2 i+=1 print("Model Loaded... ") for epoch in range(self.epochs): # A Training Epoch if printTrainStats or printValStats: print("Epoch: ", epoch) # Shuffle the training data for the current epoch X = np.asarray(trainX) Y = np.asarray(trainY) perm = np.arange(X.shape[0]) np.random.shuffle(perm) X = X[perm] Y = Y[perm] # Initializing training loss and accuracy trainLoss = 0 trainAcc = 0 # Divide the training data into mini-batches numBatches = int(np.ceil(float(X.shape[0]) / self.batchSize)) for batchNum in range(numBatches): # print(batchNum,"/",numBatches) # UNCOMMENT if model is taking too long to train XBatch = np.asarray(X[batchNum*self.batchSize: (batchNum+1)*self.batchSize]) YBatch = np.asarray(Y[batchNum*self.batchSize: (batchNum+1)*self.batchSize]) # Calculate the activations after the feedforward pass activations = self.feedforward(XBatch) # Compute the loss loss = self.computeLoss(YBatch, activations) trainLoss += loss # Estimate the one-hot encoded predicted labels after the feedword pass predLabels = oneHotEncodeY(np.argmax(activations[-1], axis=1), self.out_nodes) # Calculate the training accuracy for the current batch acc = self.computeAccuracy(YBatch, predLabels) trainAcc += acc # Backpropagation Pass to adjust weights and biases of the neural network self.backpropagate(activations, YBatch) # Print Training loss and accuracy statistics trainAcc /= numBatches if printTrainStats: print("Epoch ", epoch, " Training Loss=", loss, " Training Accuracy=", trainAcc) if saveModel: model = [] for l in self.layers: print(type(l).__name__) if type(l).__name__ != "AvgPoolingLayer" and type(l).__name__ != "FlattenLayer": model.append(l.weights) model.append(l.biases) np.save(modelName, model) print("Model Saved... ") # Estimate the prediction accuracy over validation data set if validX is not None and validY is not None and printValStats: _, validAcc = self.validate(validX, validY) print("Validation Set Accuracy: ", validAcc, "%")
def test(self, testX, testY): testActivations = self.feedforward(testX) pred = np.argmax(testActivations[-1], axis=1) testPred = oneHotEncodeY(pred, self.out_nodes) testAcc = self.computeAccuracy(testY, testPred) return testAcc