📄 snowparam.cpp
字号:
// Add a winnow algorithm to the network globalParams.algorithmSpecification += "w("; globalParams.algorithmSpecification += arg; globalParams.algorithmSpecification += "),"; i = 0; if (!globalParams.calculateExampleSize) { for (commas = 0; arg[i] && arg[i] != ':'; ++i) if (arg[i] == ',') ++commas; if (commas < 3) 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 Winnow.\n"; return false; } break; case 'w': // Set the winnow/perceptron smoothing globalParams.smoothing = atof(arg); break; case 'x': // Example globalParams.evalExample = arg; break; default: cerr << "ERROR: Unrecognized command parameter: " << param << endl; return false; } return true;}bool ParseParamFile( const char* paramFilename, GlobalParams & globalParams ){ bool result = true; char paramLine[256]; char *arg; ifstream paramStream(paramFilename); if (!paramStream) { cerr << "Error:\n"; cerr << "Failed to open architecture file: '" << paramFilename << "'. No parameters read.\n"; return false; } while (result && !paramStream.eof()) { //string paramLine; paramLine[0] = 0; paramStream.getline(paramLine, sizeof paramLine); if (strlen(paramLine)) { if (paramLine[0] == '-') { arg = paramLine + 2; while (isspace(*arg)) ++arg; result = ProcessParam(paramLine[1], arg, globalParams); } else if (paramLine[0] != '#') { cerr << "Error:\n"; cerr << "Each line in the parameter file must start with '-' " << "skipping " << paramLine << endl; } } } return result;}bool ValidateParams(GlobalParams & globalParams){ bool result = true; // Always must be a network file if (!globalParams.networkFile.length()) { cerr << "Fatal Error:\n"; cerr << "No network file specified (-F)\n\n"; result = false; } // If run mode is TRAIN or TEST, need an input file if ((globalParams.runMode == MODE_TRAIN || globalParams.runMode == MODE_TEST) && !globalParams.inputFile.length()) { cerr << "Fatal Error:\n"; cerr << "No input file specified (-I)\n"; result = false; }#ifdef SERVER_MODE_ // If run mode is SERVER, need a valid port number. if (globalParams.runMode == MODE_SERVER) { if (globalParams.outputFile.length() != 0) { cerr << "Warning: Results file has been specified in server mode (-R). " << " All server mode\n" << " output goes to STDOUT or to a client. -R will be " << "ignored.\n"; globalParams.outputFile = ""; } if (globalParams.serverPort < 1 || globalParams.serverPort > 65535) { cerr << "Fatal Error:\n"; cerr << " A valid port number must be supplied in server mode (1 - " << "65535)\n"; cerr << " -server <port>\n\n"; result = false; } if (globalParams.predictMethod == PREDICT_METHOD_UNSET) globalParams.predictMethod = WINNERS; } else#endif if (globalParams.predictMethod == PREDICT_METHOD_UNSET) globalParams.predictMethod = ACCURACY; // If run mode is Eval, need an example if (globalParams.runMode == MODE_EVAL && !globalParams.evalExample.length()) { cerr << "Fatal Error:\n"; cerr << "No example specified (-x)\n\n"; result = false; } // Error files can only be produced with the default output mode. if (globalParams.errorFile.length() > 0 && globalParams.predictMethod != ACCURACY) { cerr << "Warning:\n"; cerr << "Error files are not supported with any output mode other than\n"; cerr << " -o accuracy\n"; cerr << "(which is the default). No error file will be produced.\n"; globalParams.errorFile = ""; } // If run mode is not Eval, no example needed (used) if (globalParams.runMode != MODE_EVAL && globalParams.evalExample.length()) { cerr << "Warning:\n"; if (globalParams.runMode == MODE_TRAIN) cerr << "Training"; else if (globalParams.runMode == MODE_TEST) cerr << "Test"; else cerr << "Server"; cerr << " mode: Specified example will be ignored (-x)\n\n"; } // In TRAIN & INTERACTIVE mode, check for architecture definition if ((globalParams.runMode == MODE_TRAIN || globalParams.runMode == MODE_INTERACTIVE) && globalParams.targetIds.empty() && !globalParams.algorithmSpecification.length()) { globalParams.algorithmSpecification += "w(1.35,0.8,4.0,0.2:0-1),"; globalParams.targetIds.Parse("0-1"); cerr << "Warning: No architecture defined (defaulting to " << globalParams.algorithmSpecification << ")\n"; } if (globalParams.runMode != MODE_TRAIN && globalParams.generateConjunctions != CONJUNCTIONS_UNSET) { cerr << "Warning: The conjunction generation setting will be over-ridden " << "by the\n" << " network's setting, if it has one.\n"; } // If run mode is Train and online learning is specified if (globalParams.runMode == MODE_TRAIN && globalParams.onlineLearning && globalParams.testFile.length() == 0) { cerr << "Warning: Online learning doesn't make sense in training mode.\n" << " Online flag will be ignored.\n"; } // As of the date this code was written, constraint classification should // only be enabled for training mode, unless online learning is enabled. if (globalParams.runMode != MODE_TRAIN && globalParams.constraintClassification && !(globalParams.runMode == MODE_TEST && globalParams.onlineLearning)) { cerr << "Constraint classification is only valid in training or online " << "learning.\n" << "Constraint classification flag will be set to false.\n"; globalParams.constraintClassification = false; } // A curve interval only makes sense when a test file is specified in train // mode. if (globalParams.curveInterval) { if (globalParams.runMode != MODE_TRAIN) { cerr << "Warning: Curve interval is only valid in train mode.\n" << " Ignoring curve interval.\n"; globalParams.curveInterval = 0; } else if (globalParams.testFile.length() == 0) { cerr << "Warning: Curve interval specified without test file (-T)\n" << " No results will be output.\n"; } } if (globalParams.writePendingFeatures && !(globalParams.runMode == MODE_TRAIN || globalParams.onlineLearning)) { cerr << "Warning: Forcing all non-discarded features to be written to " << "the network\n" << " with '-a' has no effect when the network will not be " << "written.\n"; } if (globalParams.examplesInMemory && globalParams.runMode != MODE_TRAIN) { cerr << "Warning: Storing all examples in memory with the -M+ flag only " << "makes\n" << " sense in training mode.\n"; } if (globalParams.rawMode && !(globalParams.runMode == MODE_TRAIN || globalParams.runMode == MODE_TEST)) { cerr << "Warning: Enabling \"raw\" mode is only supported in '-train' " << "and '-test' modes.\n" << " '-z' will be disabled.\n"; } if (globalParams.gradientDescent && globalParams.constraintClassification) { cerr << "Warning: Gradient Descent and Constraint Classification cannot " << "be enabled\n" << " simultaneously. '-G' will be disabled.\n"; globalParams.gradientDescent = false; } if (globalParams.gradientDescent && globalParams.threshold_relative) { cerr << "Warning: Gradient Descent and threshold relative updating " << "cannot be enabled\n" << " simultaneously. '-t' will be disabled.\n"; globalParams.threshold_relative = false; } if (globalParams.targetOutputLimit < 1) { cerr << "Warning: Target output limit must be a positive integer.\n" << " -L will be set to 1.\n"; globalParams.targetOutputLimit = 1; } // If run mode is Interactive, labelsPresent can't be set if (globalParams.runMode == MODE_INTERACTIVE) { if (globalParams.labelsPresent) { cerr << "Warning: Interactive mode can't have labelsPresent.\n" << " -l+ flag will be ignored.\n"; globalParams.labelsPresent = false; } if (globalParams.eligibilityMethod != ELIGIBILITY_COUNT || globalParams.eligibilityThreshold > 1) { cerr << "Warning: in Interactive mode, eligibilityMethod must be 'count'\n" << " and the threshold should be larger than 1.\n"; globalParams.eligibilityMethod = ELIGIBILITY_COUNT; globalParams.eligibilityThreshold = 1; } globalParams.sparseNetwork = false; } if (globalParams.votedPerceptron && globalParams.constraintClassification) { cerr << "Warning: Averaged voted Perceptron and constraint classification\n" << " cannot be enabled at the same time.\n" << " Averaged voted Perceptron will be disabled.\n"; globalParams.votedPerceptron = false; } if (globalParams.votedPerceptron && globalParams.gradientDescent) { cerr << "Warning: Averaged voted Perceptron and gradient descent\n" << " cannot be enabled at the same time.\n" << " Averaged voted Perceptron will be disabled.\n"; globalParams.votedPerceptron = false; } if (globalParams.votedPerceptron && globalParams.runMode == MODE_INTERACTIVE) { cerr << "Warning: Averaged voted Perceptron cannot be used in interactive\n" << " mode. Averaged voted Perceptron will be disabled.\n"; globalParams.votedPerceptron = false; } return result;}bool ParseCmdLine( int argc, char* argv[], GlobalParams & globalParams ){ const bool DEBUG_PCL(false); bool result = true; bool cmdLineNetworks = false; // Must supply either -eval, -test, -train, or -server as first argument if (!strcmp(argv[1], "-test")) globalParams.runMode = MODE_TEST; else if (!strcmp(argv[1], "-eval") || !strcmp(argv[1], "-evaluate")) globalParams.runMode = MODE_EVAL; else if (!strcmp(argv[1], "-train")) { globalParams.runMode = MODE_TRAIN; if(DEBUG_PCL) cerr << "runMode set to MODE_TRAIN..." << endl; } else if (!strcmp(argv[1], "-interactive")) { globalParams.runMode = MODE_INTERACTIVE; globalParams.eligibilityMethod = ELIGIBILITY_COUNT; globalParams.eligibilityThreshold = 1; globalParams.labelsPresent = false; }#ifdef SERVER_MODE_ else if (!strcmp(argv[1], "-server")) { if (argc < 3) { cerr << "ERROR: The port number must be the next argument after " << "-server\n"; return false; } globalParams.runMode = MODE_SERVER; globalParams.serverPort = atoi(argv[2]); ++argv; // skip serverPort --argc; }#endif else { #ifdef SERVER_MODE_ cerr << "ERROR: First argument must be -train, -test, -evaluate, or " << "-server\n\n";#else cerr << "ERROR: First argument must be -train, -test, or -evaluate\n\n";#endif return false; } // command line arguments // skip mode argument (the program name has already been skipped, since the // getopt() function starts looking at argv from index 1) ++argv; --argc; // look for parameter file and raw mode flag first for (int i = 0; i < argc; ++i) { if (!strncmp(argv[i], "-A", 2)) { // Process the parameter file if (strlen(argv[i]) > 2) result = ParseParamFile(&argv[i][2], globalParams); else if (i + 1 < argc) result = ParseParamFile(argv[i + 1], globalParams); else return false; } else if (!strncmp(argv[i], "-z", 2)) { char* argument; if (strlen(argv[i]) > 2) argument = &argv[i][2]; else if (i + 1 < argc) argument = argv[i + 1]; else { cerr << "ERROR: The '-z' flag for enabling conventional (\"raw\") " << "mode requires an\n" << " argument: either '+' or '-'.\n"; return false; } if (*argument == '+') { globalParams.rawMode = true; switch (globalParams.runMode) { case MODE_TRAIN: globalParams.cycles = 1; globalParams.generateConjunctions = CONJUNCTIONS_OFF; // no break case MODE_TEST: globalParams.eligibilityThreshold = 1; globalParams.sparseNetwork = false; globalParams.fixedFeature = false; } } else if (*argument != '-') { cerr << "ERROR: The '-z' flag for enabling conventional (\"raw\") " << "mode is followed by\n" << " '" << argument << "' in the command line.\n" << " '+' and '-' are the only legal arguments for this " << "flag.\n"; return false; } } } // Read in input data char c; if(DEBUG_PCL) cerr << "##calling getOpt..." << endl; while ((c = getopt(argc, argv, "A:a:B:b:c:d:E:e:F:f:G:g:I:i:L:l:M:m:n:O:o:P:p:R:r:S:s:T:t:u:V:v:W:w:x:z:")) != (char)EOF && result) { // Ignore -A and -z, as they have already been processed above if (c != 'A' && c != 'z') { if(DEBUG_PCL) cerr << "##looping over cmd line args..." << endl; // If we get a network specification, let it override // whatever may have been in the param file if (!cmdLineNetworks && (c == 'B' || c == 'P' || c == 'W')) { globalParams.algorithmSpecification.empty(); cmdLineNetworks = true; } if(DEBUG_PCL) { cerr << "##calling ProcessParam() with c = " << c << ", optarg = " << optarg << "..." << endl; } result = ProcessParam(c, optarg, globalParams); } } return result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -