📄 snowparam.cpp
字号:
//===========================================================//= University of Illinois at Urbana-Champaign =//= Department of Computer Science =//= Dr. Dan Roth - Cognitive Computation Group =//= =//= Project: SNoW =//= =//= Module: SnowParam.cpp =//= Version: 3.2.0 =//= Authors: Jeff Rosen, Andrew Carlson, Nick Rizzolo =//= Date: xx/xx/99 = //= =//= Comments: =//===========================================================#include "GlobalParams.h"#include "SnowParam.h"#include <stdlib.h>#include <iostream>#include <fstream>#if defined(WIN32) || defined(LINUX)extern "C"{ extern int getopt(int, char* const *, const char*); extern char *optarg;}#endif//extern GlobalParams globalParams;bool ProcessParam( char param, const char* arg, GlobalParams & globalParams ){ int i, commas; switch (param) { case 'a': if (!(globalParams.writePendingFeatures = *arg == '+') && *arg != '-') { cerr << "ERROR: The '-a' flag for forcing all non-discarded features " << "to be written\n" << " to the network is followed by '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } break; case 'B': // Add a naive bayes algorithm to the network globalParams.algorithmSpecification += "b("; globalParams.algorithmSpecification += arg; globalParams.algorithmSpecification += "),"; for (i = 0; arg[i] && arg[i] != ':'; ++i); if (arg[i] == ':') globalParams.targetIds.Parse(&arg[i + 1]); else { cerr << "Error: No target IDs specified for Perceptron.\n"; return false; } break; case 'b': globalParams.bayesSmoothing = atof(arg); break; case 'c': // interval for learning curve globalParams.curveInterval = atoi(arg); break; case 'd': // discarding if (!strcmp(arg, "none")) globalParams.discardMethod = DISCARD_NONE; else if (!strncmp(arg, "abs", 3)) { globalParams.discardMethod = DISCARD_ABS; if (arg[3] == ':') globalParams.discardThreshold = atof(&arg[4]); else { cerr << "ERROR: no discard threshold given for absolute discard " << "mode\n"; return false; } } else if (!strncmp(arg, "rel", 3)) globalParams.discardMethod = DISCARD_REL; else { cerr << "ERROR: discard flag -d requires parameter abs:# or rel\n"; return false; } break; case 'E': // Error report file globalParams.errorFile = arg; break; case 'e': // eligibility if (!strncmp(arg, "count", 5)) { globalParams.eligibilityMethod = ELIGIBILITY_COUNT; if (arg[5] == ':') { globalParams.eligibilityPercentage = 1.0; globalParams.eligibilityThreshold = atoi(&arg[6]); } else { cerr << "ERROR: no eligiiblity threshold given for eligibility " << "count mode\n"; return false; } } else if (!strncmp(arg, "percent", 7)) { globalParams.eligibilityMethod = ELIGIBILITY_PERCENT; if (arg[7] == ':') { globalParams.eligibilityThreshold = 1; globalParams.eligibilityPercentage = atof(&arg[8]); } else { cerr << "ERROR: no eligibility threshold given for eligibility " << "count mode\n"; return false; } } else if (isdigit(*arg)) { globalParams.eligibilityPercentage = 1.0; globalParams.eligibilityThreshold = atoi(arg); } else { cerr << "ERROR: eligibility flag -e requires parameter count:# or " << "percent:#\n"; return false; } break; case 'F': // Network file globalParams.networkFile = arg; break; case 'f': // The fixed feature is a like a dynamic threshold for each target. if (!(globalParams.fixedFeature = *arg == '+') && *arg != '-') { cerr << "ERROR: The '-f' flag for enabling the fixed feature is " << "followed by\n" << " '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } break; case 'G': if (!(globalParams.gradientDescent = *arg == '+') && *arg != '-') { cerr << "ERROR: The '-G' flag for enabling gradient descent is " << "followed by\n" << " '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } break; case 'g': // feature space "growing" - generate conjunctions if (arg[0] == '+') globalParams.generateConjunctions = CONJUNCTIONS_ON; else if (arg[0] == '-') globalParams.generateConjunctions = CONJUNCTIONS_OFF; else { cerr << "ERROR: The '-g' flag for automatic conjunction generation " << "is followed by\n" << " '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } if (arg[1] == ',') { if (!(globalParams.writeConjunctions = arg[2] == '+') && arg[2] != '-') { cerr << "ERROR: The second argument to the '-g' flag for writing " << "conjunctions is\n" << " specified as '" << &arg[2] << "' in the command " << "line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } } break; case 'I': // Input file globalParams.inputFile = arg; break; case 'i': // Online learning during test mode means the network is also trained // with the test examples. if (!(globalParams.onlineLearning = *arg == '+') && *arg != '-') { cerr << "ERROR: The '-i' flag for \"incremental\" or \"online\" " << "learning is followed\n" << " by '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } break; case 'L': // Limit the number of targets printed with -o globalParams.targetOutputLimit = atol(arg); break; case 'l': // Labeled examples if (!(globalParams.labelsPresent = *arg == '+') && *arg != '-') { cerr << "ERROR: The '-l' flag for notifying SNoW that labels are " << "present in each\n" << " example is followed by '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } break; case 'M': if (!(globalParams.examplesInMemory = *arg == '+') && *arg != '-') { cerr << "ERROR: The '-M' flag for holding all examples in memory is " << "followed by\n" << " '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } break; case 'm': // More than one target per example if (!(globalParams.multipleLabels = *arg == '+') && *arg != '-') { cerr << "ERROR: The '-m' flag for allowing more than one target per " << "example is\n" << " followed by '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } break; case 'n': //'Update existing network' mode. Snow looks for an existing network //with the name specified with the -F parameter, and trains on it. //The original file is overwritten. Users should be aware that weights //learned by one algorithm may need preprocessing before another //type of learner is applied -- e.g. NB will give negative weights, //which are not immediately compatible with Winnow. if (*arg != '+' && *arg != '-') { cerr << "ERROR: The '-n' flag for enabling constraint classification " << "is followed by\n" << " '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } else if( *arg == '+') globalParams.updateExistingNetwork = true; break; case 'O': // Ordered mode. A better name would have been "constraint // classification mode." I won't bore you with the confusing and, in // retrospect, stupid reasons why 'O' was chosen to enable this option. if (!(globalParams.constraintClassification = *arg == '+') && *arg != '-') { cerr << "ERROR: The '-O' flag for enabling constraint classification " << "is followed by\n" << " '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } if (arg[0] == '+' && arg[1] == ',') { if (!(globalParams.conservativeCC = arg[2] == '+') && arg[2] != '-') { cerr << "ERROR: The second argument to the '-O' flag for setting " << "conservative CC mode\n" << " was specified as '" << &arg[2] << "' in the command" << " line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } } break; case 'o': // Output mode if (strcmp(arg, "accuracy") == 0) globalParams.predictMethod = ACCURACY; else if (strcmp(arg, "winners") == 0) globalParams.predictMethod = WINNERS; else if (strcmp(arg, "softmax") == 0) globalParams.predictMethod = SOFTMAX; else if (strcmp(arg, "allactivations") == 0) globalParams.predictMethod = ALL_ACTIVATIONS; else if (strcmp(arg, "allpredictions") == 0) globalParams.predictMethod = ALL_PREDICTIONS; else if (strcmp(arg, "allboth") == 0) globalParams.predictMethod = ALL_BOTH; else { cerr << "ERROR: The '-o' flag for changing the output mode is " << "followed by\n" << " '" << arg << "' in the command line.\n" << " Legal arguments for this flag are:\n" << " -o accuracy\n" << " -o winners\n" << " -o softmax\n" << " -o allactivations\n" << " -o allpredictions\n" << " -o allboth\n"; return false; } break; case 'P': // Add a perceptron algorithm to the network globalParams.algorithmSpecification += "p("; globalParams.algorithmSpecification += arg; globalParams.algorithmSpecification += "),"; i = 0; if (!globalParams.calculateExampleSize) { for (commas = 0; arg[i] && arg[i] != ':'; ++i) if (arg[i] == ',') ++commas; if (commas < 2) globalParams.calculateExampleSize = true; } for (; arg[i] && arg[i] != ':'; ++i); if (arg[i] == ':') globalParams.targetIds.Parse(&arg[i + 1]); else { cerr << "Error: No target IDs specified for Perceptron.\n"; return false; } break; case 'p': globalParams.predictionThreshold = atof(arg); break; case 'R': // results file for Test mode globalParams.outputFile = arg; break; case 'r': // number of learning cycles (repetitions) globalParams.cycles = atoi(arg); break; case 'S': // set separator thickness globalParams.thickSeparator.positive = atof(arg); for (i = 0; arg[i] && arg[i] != ','; ++i); globalParams.thickSeparator.negative = (arg[i]) ? atof(&arg[i + 1]) : globalParams.thickSeparator.positive; globalParams.thickSeparator.negative *= -1; break; case 's': // sparse network if (!(globalParams.sparseNetwork = *arg == 's') && *arg != 'f') { cerr << "ERROR: The '-s' flag for making the network sparse or full " << "is followed by\n" << " '" << arg << "' in the command line.\n" << " 's' for sparse and 'f' for full are the only legal " << "arguments for\n" << " this flag.\n"; return false; } break; case 'T': // testing file for Train/Test mode globalParams.testFile = arg; break; case 't': // For winnow; makes alpha = c * threshold / activation if (!(globalParams.threshold_relative = *arg == '+') && *arg != '-') { cerr << "ERROR: The '-t' flag for enabling threshold relative " << "updating is followed by\n" << " '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } break; case 'u': // '+' means there will be updates during the first cycle if (!(globalParams.noFirstCycleUpdate = *arg == '-') && *arg != '+') { cerr << "ERROR: The '-u' flag for enabling first cycle updates is " << "followed by\n" << " '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } break; case 'V': // '+' means averaged voted Perceptron is enabled if (!(globalParams.votedPerceptron = *arg == '+') && *arg != '-') { cerr << "ERROR: The '-V' flag for enabling averaged voted Perceptron " << "is followed by\n" << " '" << arg << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } break; case 'v': // Verbosity level if (!strcmp(arg, "off")) globalParams.verbosity = VERBOSE_QUIET; else if (!strcmp(arg, "med")) globalParams.verbosity = VERBOSE_MED; else if (!strcmp(arg, "max")) globalParams.verbosity = VERBOSE_MAX; else if (!strcmp(arg, "min")) globalParams.verbosity = VERBOSE_MIN; else { cerr << "ERROR: The '-v' flag for enabling first cycle updates is " << "followed by\n" << " '" << arg << "' in the command line.\n" << " Legal arguments: 'off', 'min', 'med', 'max'\n" << " Defaulting to 'min'.\n"; globalParams.verbosity = VERBOSE_MIN; } break; case 'W':
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -