/* * File: NetworkTester.java * Author: Justin Basilico * Course: PO CS 152: Neural Networks * Assignment: Final Project * Updated: 2001.12.18 * Created: 2001.12.18 * * Description: * This file contains the NetworkTester abstract class, which just contains a * main method that reads in a network and a dataset of double arrays from * files and tests the network on the dataset. * * Copyright: Justin Basilico (2001). */ /** * NetworkTester abstract class * * This abstract class just implements a program for testing a * FeedForwardNetwork against a dataset of double array values. * * The program is run by just doing: * java NetworkTester * where the are the following: * * -n * This parameter specifies the file to read the FeedForwardNetwork from. It * is required to run the program. * * -d * Use the specified file that contains a dataset of an array of double input * and output values. This is the dataset to test the network against. This * parameter is required to run the program. * * -S * This parameter means that the output and target output values for the * network will not shown for each input, only the mean squared error. If it * is not given, then they will be output. * * -N * This parameter means that the network will not be printed at the end of the * execution. If it is not given, then the network will be output. * * -h * This parameter just outputs a usage message for this program. * * The program reads in the given network file that contains a * FeedForwardNetwork and then tests the network on the given dataset, which * it also reads in from a file. For each input, it will print the mean * squared error along with the values of the output and target units of the * network for that input (unless it has been specified not to do so). Then * the average of the mean squared error for all inputs and then outputs the * network (unless it has been specified not to do so). * * @author Justin Basilico * @version 2001.12.18 * @see NetworkEvolver * @see DatasetCreator * @see FeedForwardNetwork */ public abstract class NetworkTester extends Object { /** * main * * This is the main method where execution begins. It parses the command- * line arguments and then reads a FeedForwardNetwork from a specified * file, then a dataset of NetworkInputs that are double values and tests * the network on those inputs, outputting the error for each one. * * @param args The String array of command-line arguments. */ public static void main( String[] args) // Command-line arguments. { // These are the variables used for the testing. FeedForwardNetwork network = null; String networkFile = null; NetworkInput[] dataset = null; String datasetFile = null; boolean showAll = true; boolean showNetwork = true; boolean runProgram = true; // Parse the command-line arguments. for (int i = 0; i < args.length && runProgram; i++) { if ( args[i].equalsIgnoreCase("-n") ) networkFile = args[++i]; else if ( args[i].equalsIgnoreCase("-d") ) datasetFile = args[++i]; else if ( args[i].equalsIgnoreCase("-S") ) showAll = false; else if ( args[i].equalsIgnoreCase("-N") ) showNetwork = false; else if ( args[i].equalsIgnoreCase("-h") ) runProgram = false; else { // Error: Bad parameter. System.err.println("Unknown option: " + args[i]); runProgram = false; } } if ( !runProgram ) { printUsage(); return; } if ( networkFile == null ) { // Error: No network file specified. System.err.println("You must specify a network file."); runProgram = false; } else if ( !Utilities.fileExists(networkFile) ) { // Error: No such network file. System.out.println("No such network file exists: " + networkFile); runProgram = false; } else { // Read the network from the given file. NeuralNetwork nnet = NeuralNetwork.readNetworkFromFile(networkFile); if ( nnet == null ) { // Error: Problem reading in the network. System.err.println("Error reading network."); runProgram = false; } else if ( !(nnet instanceof FeedForwardNetwork) ) { // Error: The NeuralNetwork is not a FeedForwardNetwork. System.err.println("That is not a valid FeedForwardNetwork."); runProgram = false; } else // Turn the NeuralNetwork into a FeedForwardNetwork. network = (FeedForwardNetwork) nnet; } if ( !runProgram ) { printUsage(); return; } if ( datasetFile == null ) { // Error: No dataset file specified. System.err.println("You must specify a testing set."); runProgram = false; } else if ( !Utilities.fileExists(datasetFile) ) { // Error: Dataset file does not exist. System.err.println("No such dataset file exists: " + datasetFile); runProgram = false; } else { System.out.print("Reading dataset from file " + datasetFile + "..."); System.out.flush(); // Read in the dataset file. dataset = DatasetReader.readDoubleDataset(datasetFile); if ( dataset == null ) { // Error: Problem reading in the dataset. System.err.println(" Error reading dataset file. " + "Aborting program."); runProgram = false; } else // We succeeded in reading in the file. System.out.println(" Done."); } if ( !runProgram ) { printUsage(); return; } // These are used for outputting the output values. double[] output = new double[network.getOutputSize() + 1]; double[] target = new double[network.getOutputSize() + 1]; double errorSum = 0.0; for (int i = 0; i < dataset.length; i++) { // Test the network on the ith input. double error = network.testOnInput(dataset[i]); errorSum += error; // Print the error for this input. System.out.print("Input " + (i + 1) + ": Error " + Utilities.NF7.format(error) + "."); if ( showAll ) { // Show the output and target output units. network.getOutputs(output); network.getTarget(target); System.out.print(" Output: "); Utilities.printArray(output, Utilities.NF7); System.out.print(". Target: "); Utilities.printArray(target, Utilities.NF7); } System.out.println(); } // Output the average error. System.out.println("Average error: " + Utilities.NF7.format(errorSum / (double) dataset.length)); if ( showNetwork ) // Print the network. network.print(); } /** * printUsage * * This method just prints the usage of this program to System.out. */ public static void printUsage() { System.out.println("Usage: java NetworkTester "); System.out.println("Parameters:"); System.out.println(" -n "); System.out.println(" -d "); System.out.println(" -S (don't show all output)"); System.out.println(" -N (don't show the network)"); System.out.println(" -h (print help file)"); } }