def learnParametersClosdFormSolution(inputData, outputData, numberOfClusters, lambdaRegularization): # Calculate the design matrix. clusteredDataInfo = re.calculateM(inputData, numberOfClusters) clusteredData = re.createDataClusters(inputData, clusteredDataInfo, numberOfClusters) covarianceMatrixClusters = re.covariancePerCluster(clusteredData) designMatrix = re.calculateDesignMatrix(inputData, clusteredDataInfo, covarianceMatrixClusters, numberOfClusters) Wml = computeClosedFormSolution(lambdaRegularization, designMatrix, outputData) # Return the parameters. return Wml
def learnParameterSGD(inputData, inputValidationData, outputData, outputValidationData, batchSize, numberOfClusters, lambdaRegularization): # Learning rate fixed. learningRate = 1 # Cluster the input data to decide the number of basis functions. clusteredDataInfo = re.calculateM(inputData, numberOfClusters) clusteredData = re.createDataClusters(inputData, clusteredDataInfo, numberOfClusters) covarianceMatrixClusters = re.covariancePerCluster(clusteredData) designMatrix = re.calculateDesignMatrix(inputData, clusteredDataInfo, covarianceMatrixClusters, numberOfClusters) # Choose a random w. randomW = np.random.rand(1, numberOfClusters + 1) # Run the update on parameters using the stochastic gradient update. W = stochasticUpdate(inputData, inputValidationData, outputData, outputValidationData, designMatrix, randomW, learningRate, batchSize, numberOfClusters, lambdaRegularization) return W
def computeTargetSGD(inputData, numberOfClusters, W): # Compute the design matrix for the input. clusteredDataInfo = re.calculateM(inputData, numberOfClusters) clusteredData = re.createDataClusters(inputData, clusteredDataInfo, numberOfClusters) covarianceMatrixClusters = re.covariancePerCluster(clusteredData) designMatrix = re.calculateDesignMatrix(inputData, clusteredDataInfo, covarianceMatrixClusters, numberOfClusters) # Target matrix to be returned. target = np.empty(( len(inputData), 1, )) target[:] = np.NAN target = np.asmatrix(target) if (W.shape[1] == 1): # Ensure that the dot product is performed without any errors. W = W.T for i in range(0, len(designMatrix)): target[i][0] = np.matmul(W, designMatrix[i].T) return target
def stochasticUpdate(inputData, inputValidationData, outputData, outputValidationData, designMatrix, currentW, learningRate, batchSize, numberOfClusters, lambdaRegularization): idealW = currentW # Initial minimum error set to a large value. rmsErrorMin = 9999 # Calculate design matrix for the validation input set. clusteredDataInfo = re.calculateM(inputValidationData, numberOfClusters) clusteredData = re.createDataClusters(inputValidationData, clusteredDataInfo, numberOfClusters) covarianceMatrixClusters = re.covariancePerCluster(clusteredData) designMatrixValidation = re.calculateDesignMatrix( inputValidationData, clusteredDataInfo, covarianceMatrixClusters, numberOfClusters) # Run the update step once for each batch. index = len(inputData) // batchSize for i in range(0, index): # Calculate the batch change in E for the batch. deltaE = calculateBatchDeltaE(designMatrix, outputData, i * batchSize, (i + 1) * batchSize, currentW, lambdaRegularization) # Update the parameters usind the batch gradient and the learning rate. currentW = np.subtract(currentW, learningRate * deltaE) # Calculate the new target values with the updated parameters. predictedTarget = computeTargetValuesSGD(inputValidationData, designMatrixValidation, numberOfClusters, currentW) # Calculate the rms error for the updated parameters. rmsError = re.calculateRootMeanSquaredError(outputValidationData, predictedTarget) if (rmsError < rmsErrorMin): # Got a new minimum rms error. # Save the parameters. idealW = currentW # Save the minimum rms error till now. rmsErrorMin = rmsError return idealW
def computeTargetValuesClosedFormSolution(inputData, numberOfClusters, Wml): # Calculate the design matrix. clusteredDataInfo = re.calculateM(inputData, numberOfClusters) clusteredData = re.createDataClusters(inputData, clusteredDataInfo, numberOfClusters) covarianceMatrixClusters = re.covariancePerCluster(clusteredData) designMatrix = re.calculateDesignMatrix(inputData, clusteredDataInfo, covarianceMatrixClusters, numberOfClusters) # Initialise the output matrix for the number of row vectors in the input. target = np.empty(( len(inputData), 1, )) target[:] = np.NAN target = np.asmatrix(target) if (Wml.shape[1] == 1): Wml = Wml.T # Calculate the output matrix using the learned parameters. for i in range(0, len(designMatrix)): target[i][0] = np.matmul(Wml, designMatrix[i].T) # Return the output matrix. return target