void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // arguments const_marray mX(prhs[0]); const_marray mY(prhs[1]); const_marray mLam(prhs[2]); const_marray mAug(prhs[3]); const_marray mT0(prhs[4]); const_marray mSkip(prhs[5]); marray mW = duplicate(prhs[6]); marray mW0 = duplicate(prhs[7]); // take inputs cmat_t X = view2d<double>(mX); cvec_t Y = view_as_col<double>(mY); double lambda = mLam.get_scalar(); double aug = mAug.get_scalar(); double t0 = mT0.get_scalar(); index_t skip = (index_t)mSkip.get_scalar(); const index_t d = mX.nrows(); // main if (aug == 0) { vec_t w(mW.ptr_real(), d, 1); SGD_QN trainer(w, lambda, skip, t0); run_sgdx(trainer, X, Y, 1); } else { vec_t w(mW.ptr_real(), d, 1); double& w0 = *(mW0.ptr_real()); SGD_QN_A trainer(w, w0, lambda, aug, skip, t0); run_sgdx(trainer, X, Y, 1); } // return plhs[0] = mW; plhs[1] = mW0; }
double train_denoising(NIterator noisy_it, NIterator noisy_end, CIterator clean_it, CIterator clean_end, std::size_t max_epochs, Args... args) { dll::rbm_trainer<parent_t, EnableWatcher, RW, true> trainer(args...); return trainer.train(as_derived(), noisy_it, noisy_end, clean_it, clean_end, max_epochs); }
int main(int argc, char** argv) { if (argc < 2) { printf("provide training file dir\n"); std::exit(1); } std::string treeDir(argv[1]); std::string trainPath = treeDir + "/train.txt"; std::string devPath = treeDir + "/dev.txt"; // model parameters int wordDim = 32; int numClasses = 5; // training parameters sOptions_t options = { trainPath, devPath, 25, 2, 0.01, 0.0001, 0.001, 0.001, 0.0001 }; SentimentTraining trainer(options, wordDim, numClasses); trainer.train(); return 0; }
bool makeBOWModel(std::vector<BOWImg> &images, Mat &vocabulary, Mat &trainData, Mat &response) { // Load training images std::cout<<"--->Loading training images ... "<<std::endl; int numImages = imgRead(images); if(numImages < 0) return false; std::cout<<" "<<numImages<<" images loaded."<<std::endl; // Random shuffle samples std::random_shuffle (images.begin(), images.end()); /* Get the ROI of images. Feature detect and extract descriptors. */ printf("--->Extracting %s features ...\n", conf.extractor.c_str()); features(images, conf.extractor, conf.detector); BOWKMeansTrainer trainer(conf.numClusters,TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,10,1.0)); for(std::vector<BOWImg>::iterator iter = images.begin();iter != images.end(); iter++) { Mat tmp = iter->descriptor; trainer.add(tmp); } std::cout<<"--->Constructing vocabulary list ..."<<std::endl; vocabulary = trainer.cluster(); std::cout<<"--->Extracting BOW features ..."<<std::endl; bowFeatures(images, vocabulary, conf.extractor); // Prepare traning data. Mat rawData; for(std::vector<BOWImg>::iterator iter = images.begin();iter != images.end(); iter++) { rawData.push_back(iter->BOWDescriptor); response.push_back(iter->label); } // PCA #ifdef _USE_PCA_ float factor = 1; int maxComponentsNum = static_cast<float>(conf.numClusters) * factor; PCA pca(rawData, Mat(),CV_PCA_DATA_AS_ROW, maxComponentsNum); Mat pcaData; for(int i = 0;i<rawData.rows;i++) { Mat vec = rawData.row(i); Mat coeffs = pca.project(vec); pcaData.push_back(coeffs); } trainData = pcaData; #else trainData = rawData; #endif return true; }
void Base::RunTrainer() { INFO0; if (Exists(Tagger().WeightFileName())) return; PrepareFiles(Stage::trainer); std::ofstream cout_file(Tagger().FolderName() + ".txt"); auto cout = std::cout.rdbuf(); std::cout.rdbuf(cout_file.rdbuf()); Trainer trainer(Tagger()); std::cout.rdbuf(cout); }
void TrainDocWordVectors() { int vec_dim = 50; int num_threads = 4; int num_rounds = 20; int num_negative_samples = 10; float starting_alpha = 0.06f; const char *doc_words_file_name = "e:/dc/el/tac/2010/train/dw.bin"; const char *word_cnts_file = "e:/dc/el/wiki/word_cnts.bin"; const char *word_vecs_file_name = "e:/dc/el/vecs/word_vecs_3.bin"; const char *dst_vec_file_name = "e:/dc/el/vecs/2010/train_3_dw_vecs.bin"; EADocVecTrainer trainer(num_rounds, num_threads, num_negative_samples, starting_alpha); trainer.TrainDocWordFixedWordVecs(doc_words_file_name, word_cnts_file, word_vecs_file_name, vec_dim, dst_vec_file_name); }
int main(int argc, const char *argv[]) { if (argc==3) { DepthmapLoaderT depthmapLoader; LabelsLoaderT labelsLoader(RGB2LABEL, N_LABELS); SamplerT sampler(N_SAMPLES, atoi(argv[1])); TrainingSetT trainingSet(argv[2], "_depth.png", "_labels.png", N_LABELS, depthmapLoader, labelsLoader, sampler); TreeT tree(atoi(argv[1]), TREE_DEPTH); TreeTrainerT trainer(".", USE_CPU); TreeTrainerParametersT params; params.nFeatures = N_FEATURES; params.nThresholds = N_THRESHOLDS; params.computeFRange = false; params.featLowBounds[0] = -60; params.featLowBounds[1] = -60; params.featUpBounds[0] = 60; params.featUpBounds[1] = 60; params.thrLowBound = -200; params.thrUpBound = 200; params.randomThrSampling = true; params.perLeafSamplesThr = static_cast<float>(N_SAMPLES)/N_LABELS; //params.perLeafSamplesThr = 1; try { trainer.train(tree, trainingSet, params, 1, TRAIN_DEPTH); std::stringstream treeName; treeName << "tree" << argv[1] << ".xml"; tree.save(treeName.str()); } catch (cl::Error err) { std::cerr << err.what() << ": " << err.err() << std::endl; } return 0; } return 1; }
/* use training data to build a codebook/vocabulary */ int trainVocabulary(std::string vocabPath, std::string vocabTrainDataPath, double clusterRadius) { //ensure not overwriting a vocabulary std::ifstream checker; checker.open(vocabPath.c_str()); if(checker.is_open()) { std::cerr << vocabPath << ": Vocabulary already present" << std::endl; checker.close(); return -1; } std::cout << "Loading vocabulary training data" << std::endl; cv::FileStorage fs; //load in vocab training data fs.open(vocabTrainDataPath, cv::FileStorage::READ); cv::Mat vocabTrainData; fs["VocabTrainData"] >> vocabTrainData; if (vocabTrainData.empty()) { std::cerr << vocabTrainDataPath << ": Training Data not found" << std::endl; return -1; } fs.release(); std::cout << "Performing clustering" << std::endl; //uses Modified Sequential Clustering to train a vocabulary of2::BOWMSCTrainer trainer(clusterRadius); trainer.add(vocabTrainData); cv::Mat vocab = trainer.cluster(); //save the vocabulary std::cout << "Saving vocabulary" << std::endl; fs.open(vocabPath, cv::FileStorage::WRITE); fs << "Vocabulary" << vocab; fs.release(); return 0; }
static bool localTest2Layer( void ) { log_info( "Test 2 Layer - Start" ); Dnn dnn; DnnBuilder builder( dnn, ACTIVATION_RELU ); builder.addLayerInput( 1, 1, 2 ); builder.addLayerFullyConnected( 20 ); builder.addLayerSoftmax( 2 ); dnn.initialize(); // Randomize the weights. DNN_NUMERIC *input = dnn.getDataInput(); DNN_NUMERIC *output = dnn.getDataOutput(); if( input == 0 || output == 0 ) { return log_error( "bad pointers from dnn" ); } *input++ = 0.3; // Send a data point (0.3,-0.5) forward through the network. *input = -0.5; if( !dnn.predict()) { log_error( "Problem with forward propagation through the network." ); } DNN_NUMERIC probability1 = *output; DnnTrainerSGD trainer( &dnn ); trainer.learningRate( 0.01 ); trainer.l2Decay( 0.001 ); trainer.train( 0 ); dnn.predict(); DNN_NUMERIC probability2 = *output; if( probability2 <= probability1 ) { return log_error( "Probability did not change for the better: %f to %f", probability1, probability2 ); } log_info( "Test 2 Layer - End" ); return true; }
int main(int argc, char* argv[]) { std::vector<Mat<float> > mse; float lr = 1e-1f; //Neural Networks settings : Topology topo; unsigned int nbrneurons = 25; unsigned int nbrlayer = 1; unsigned int nbrinput = width*height; unsigned int nbroutput = 10; //topo.push_back(nbrinput,NTNONE); //input layer topo.push_back(nbrinput,NTSIGMOID); //input layer //topo.push_back(nbrneurons, NTSIGMOID); topo.push_back(15, NTSIGMOID); //topo.push_back(nbroutput, NTSOFTMAX); //linear output topo.push_back(nbroutput, NTSIGMOID); //linear output NN<float> nn(topo, lr); NNTrainer<float> trainer(&nn); //------------------------------ /* //DATASET SETTINGS : report.open(report_fn.c_str(), ios::out); image.open(training_image_fn.c_str(), ios::in | ios::binary); // Binary image file label.open(training_label_fn.c_str(), ios::in | ios::binary ); // Binary label file // Reading file headers char number; for (int i = 1; i <= 16; ++i) { image.read(&number, sizeof(char)); } for (int i = 1; i <= 8; ++i) { label.read(&number, sizeof(char)); } //------------------------------ //checking rotation : Mat<float> im1(8,8, (char)1); Mat<float> im2( rotate(im1,PI/2.0f) ); im1.afficher(); im2.afficher(); //-------------------------------- //checking arctan !!! float y = -4.0f; float x = 4.0f; std::cout << arctan(y,x)*180.0f/(PI) << std::endl; //-------------------------------------------------- //checking reading : char labelval = 0; float theta = PI/2; im1 = inputMNIST(labelval); im2 = rotate(im1,theta); im2.afficher(); std::cout << "Rotation of : " << theta*180.0f/PI << std::endl; report.close(); label.close(); image.close(); */ char labelval = 0; //--------------------------------------------------- int nbrTimes = 1; int iteration = 50000; int offx = 2; int offy = 2; int size = 28; int countSuccess = 0; for(int k=1;k<=nbrTimes;k++) { //---------------------------------------- //---------------------------------------- //---------------------------------------- //DATASET SETTINGS : report.open(report_fn.c_str(), ios::out); image.open(training_image_fn.c_str(), ios::in | ios::binary); // Binary image file label.open(training_label_fn.c_str(), ios::in | ios::binary ); // Binary label file // Reading file headers char number; for (int i = 1; i <= 16; ++i) { image.read(&number, sizeof(char)); } for (int i = 1; i <= 8; ++i) { label.read(&number, sizeof(char)); } //---------------------------------------- //---------------------------------------- //---------------------------------------- int batchSize = 100; while( iteration) { Mat<float> inputOriginal( inputMNIST(labelval) ); //let us choose the rotation : float theta = ((float)(rand()%2))*PI; //let us apply it : Mat<float> rotatedinput(extract( rotate(inputOriginal, theta), offx,offy, offx+(size-1), offy+(size-1) )); Mat<float> inputR( reshapeV( rotatedinput ) ); Mat<float> inputO( reshapeV( inputOriginal) ); Mat<float> target( 0.0f, 10,1); target.set( 1.0f, labelval+1, 1); if(labelval < 10) { /* Mat<float> output( nn.feedForward( inputR) ); int idmax = idmin( (-1.0f)*output).get(1,1); transpose( operatorL(target,output) ).afficher(); std::cout << " LEARNING ITERATION : " << iteration << " ; IDMAX = " << idmax << std::endl; rotatedinput.afficher(); nn.backPropBATCH(target,batchSize); //nn.backPropCrossEntropy(target); */ //---------------------------------------- //NNTRAINER TEST : Mat<float> output( nn.feedForward( inputO) ); int idmax = idmin( (-1.0f)*output).get(1,1); transpose( operatorL(target,output) ).afficher(); std::cout << " LEARNING ITERATION : " << iteration << " ; IDMAX = " << idmax << std::endl; inputOriginal.afficher(); trainer.accumulateGradient( inputO, (output - target) ); float momentum = 1.0f; trainer.update(momentum); //---------------------------------------- //counting : if(idmax == labelval+1) { countSuccess++; } //original input : /* output = nn.feedForward( inputO); idmax = idmin( (-1.0f)*output).get(1,1); transpose( operatorL(target,output) ).afficher(); std::cout << " LEARNING ITERATION : " << iteration << " ; IDMAX = " << idmax << std::endl; //nn.backProp(target); nn.backPropBATCH(target, batchSize); //nn.backPropCrossEntropy(target); //counting : if(idmax == labelval+1) { countSuccess++; } */ //------------------- if( iteration % 1000 == 0) { std::cout << " TEST : " << countSuccess << " / " << 1000 << std::endl; mse.push_back(Mat<float>((float)countSuccess,1,1)); writeInFile(std::string("./mse.txt"), mse); countSuccess = 0; } iteration--; } } if(k!=nbrTimes) { report.close(); label.close(); image.close(); iteration = 50000; } } std::cout << " VALIDATION TEST : in progress.." << std::endl; iteration = 1000; int success = 0; while( iteration) { Mat<float> rotatedinput( inputMNIST(labelval) ); //let us choose the rotation : //float theta = rand()%360; float theta = ((float)(rand()%2))*PI; //let us apply it : rotatedinput = extract( rotate(rotatedinput, theta), offx,offy, offx+(size-1), offy+(size-1) ); Mat<float> input( reshapeV( rotatedinput ) ); Mat<float> target( 0.0f, 10,1); target.set( 1.0f, labelval+1, 1); if(labelval < 10) { Mat<float> output( nn.feedForward( input)); int idmax = idmin( (-1.0f)*output).get(1,1); transpose(output).afficher(); std::cout << " ITERATION : " << iteration << " ; IDMAX = " << idmax << std::endl; if(idmax == labelval+1) { success++; } iteration--; } } std::cout << "VALIDATION TEST : " << success << " / 1000." << std::endl; report.close(); label.close(); image.close(); //nn.save(std::string("neuralnetworksDIGITROTATEDPI4")); return 0; }
int main(int argc, char** argv) { // parse directory option int getopt_res; std::string model_directory = ""; std::string cad120_directory = ""; bool cross_validation = false; bool grid_search = false; bool dfound = false; bool cfound = false; while ((getopt_res = getopt(argc, argv, "d:c:vg")) != -1) { switch (getopt_res) { case 'd': dfound = true; model_directory = optarg; break; case 'c': cfound = true; cad120_directory = optarg; break; case 'v': cross_validation = true; break; case 'g': grid_search = true; break; default: fprintf(stderr, "Usage: train_cad120 -c CAD120_DIRECTORY_PATH -d MODEL_DIRECTORY_PATH [-v] [-g]\n"); fflush(stderr); return 1; } } if (!dfound || !cfound) { fprintf(stderr, "Usage: train_cad120 -c CAD120_DIRECTORY_PATH -d MODEL_DIRECTORY_PATH [-v] [-g]\n"); fflush(stderr); return 1; } pcml::TrainFutureMotion trainer(model_directory); trainer.loadConfig(); const std::vector<std::string>& joint_names = trainer.jointNames(); // add input motions to trainer pcml::CAD120Reader reader(cad120_directory); for (int subject=0; subject < reader.numSubjects(); subject++) { for (int action=0; action < reader.numActions(subject); action++) { for (int video=0; video < reader.numVideos(subject, action); video++) { // print subject/action/video printf("Parsing [subject %d, action %d, video %d]\n", subject, action, video); fflush(stdout); std::vector<Eigen::VectorXd> motion_list; std::vector<int> action_labels_list; // reading a demonstration reader.startReadFrames(subject, action, video); int frame_idx = 0; while (reader.readNextFrame()) { // retrieve motion Eigen::VectorXd current_motion( joint_names.size() * 3 ); for (int i=0; i<joint_names.size(); i++) { Eigen::Vector3d position; reader.getJointPosition(joint_names[i], position); current_motion.block( i*3, 0, 3, 1 ) = position; } motion_list.push_back(current_motion); // retrieve action label action_labels_list.push_back( reader.getSubActivityIndex() ); /*// print sub-activity per frame printf("sub-activity at %04d: %s\n", frame_idx, reader.getSubActivity().c_str()); fflush(stdout); */ frame_idx++; } // conversion from stl vector to eigen matrix/vector Eigen::MatrixXd motion( motion_list[0].rows(), motion_list.size() ); Eigen::VectorXi action_labels( action_labels_list.size() ); for (int i=0; i<motion_list.size(); i++) { motion.col(i) = motion_list[i]; action_labels(i) = action_labels_list[i]; } trainer.addMotion(motion, action_labels); } } } if (cross_validation) { printf("Doing cross validation takes a while...\n"); fflush(stdout); trainer.crossValidationSVMs(); printf("Cross validation complete\n"); fflush(stdout); } if (grid_search) { printf("Doing grid search takes a while...\n"); fflush(stdout); trainer.gridSearchSVMHyperparameters(); printf("Grid search complete\n"); fflush(stdout); } if (!cross_validation && !grid_search) { // training printf("Training takes a while...\n"); fflush(stdout); trainer.train(); printf("Training complete\n"); fflush(stdout); // saving the trained model printf("Saving the trained model\n"); fflush(stdout); trainer.saveTrainedModel(); } return 0; }
int main(int argc, char* argv[]) { if (argc != 2 && argc != 3) { cout << "usage rnnlib [-s | --save] config_file" << endl; exit(0); } bool autosave = false; string configFilename; if (argc == 3) { string saveFlag(argv[1]); autosave = (saveFlag == "-s" || saveFlag == "--save"); configFilename = argv[2]; } else { configFilename = argv[1]; } ConfigFile conf(configFilename); #ifdef FAST_LOGISTIC Logistic::fill_lookup(); #endif string task = conf.get<string>("task"); if (task == "transcription" && conf.has("dictionary")) { task = conf.set<string>("task", "dictionary_transcription"); } bool display = conf.get<bool>("display", false); vector<int> jacobianCoords = conf.get_list<int>("jacobianCoords"); bool gradCheck = conf.get<bool>("gradCheck", false); verbose = conf.get<bool>("verbose", false); int displaySequence = conf.get<int>("sequence", 0); string dataset = conf.get<string>("dataset", "train"); check(in(validDatasets, dataset), dataset + " given as 'dataset' parameter in config file '" + configFilename + "'\nmust be one of '" + str(validDatasets) + "'"); string dataFileString = dataset + "File"; string saveName = ""; ofstream* logout = 0; TeeDev* tdev = 0; TeeStream* tout = 0; string displayPath = ""; string logname = ""; if (display || jacobianCoords.size()) { displayPath = conf.get<string>("displayPath"); logname = displayPath + "log"; } else if (autosave) { if (in (conf.filename, '@')) { saveName = conf.filename.substr(0, conf.filename.find('@')); } else { saveName = conf.filename.substr(0, conf.filename.rfind('.')); } saveName += "@" + time_stamp(); logname = saveName + ".log"; } if (autosave || display || jacobianCoords.size()) { logout = new ofstream(logname.c_str()); check(logout->is_open(), "can't open log file " + logname); tdev = new TeeDev(cout, *logout); tout = new TeeStream(*tdev); cout << "writing to log file " << logname << endl; } ostream& out = tout ? *tout : cout; vector<string> dataFiles = conf.get_list<string>(dataFileString); int dataFileNum = conf.get<int>("dataFileNum", 0); check(dataFiles.size() > dataFileNum, "no " + ordinal(dataFileNum) + " file in size " + str(dataFiles.size()) + " file list " + dataFileString + " in " + configFilename); DataHeader header(dataFiles[dataFileNum], task, 1); DataSequence* testSeq = 0; if (display || gradCheck || jacobianCoords.size()) { NetcdfDataset* data = new NetcdfDataset(dataFiles[dataFileNum], task, displaySequence); testSeq = new DataSequence((*data)[0]); delete data; } Mdrnn *net; if (task == "code") { net = new CodeNet(out, conf, header); } else if (task == "memory") { net = new MemoryNet(out, conf, header); } else if (task == "pm") { net = new PmNet(out, conf, header); } else { net = new MultilayerNet(out, conf, header); } out << endl << "network:" << endl; PRINT(task, out); out << *net; //build weight container after net is created WeightContainer::instance().build(); int numWeights = WeightContainer::instance().weights.size(); out << numWeights << " weights" << endl << endl; //build the network after the weight container net->build(); //only construct optimiser after weight container is built Optimiser* opt; if (conf.get<string>("optimiser", "steepest") == "rprop") { opt = new Rprop(out); } else { opt = new SteepestDescent(out, conf.get<double>("learnRate", 1e-4), conf.get<double>("momentum", 0.9)); } Trainer trainer(out, net, opt, conf); out << "setting random seed to " << Random::set_seed(conf.get<unsigned long int>("randSeed", 0)) << endl << endl; if (conf.get<bool>("loadWeights", false)) { out << "loading dynamic data from " << conf.filename << endl; DataExportHandler::instance().load(conf, out); out << "epoch = " << trainer.epoch << endl << endl; } double initWeightRange = conf.get<double>("initWeightRange", 0.1); out << "randomising uninitialised weights with mean 0 std. dev. " << initWeightRange << endl << endl; WeightContainer::instance().randomise(initWeightRange); out << "optimiser:" << endl << *opt << endl; if (gradCheck) { out << "data header:" << endl << header << endl; out << "running gradient check for sequence " << displaySequence << endl; out << *testSeq; prt_line(out); GradientCheck(out, net, *testSeq, conf.get<int>("sigFigs", 6), conf.get<double>("pert", 1e-5), conf.get<bool>("verbose", false)); } else if (jacobianCoords.size()) { out << "data header:" << endl << header << endl; out << "calculating Jacobian for sequence " << displaySequence << " at coords " << jacobianCoords << endl; out << *testSeq; out << "output path: " << endl << displayPath << endl; net->feed_forward(*testSeq); net->print_output_shape(out); net->outputLayer->outputErrors.get(jacobianCoords) = net->outputLayer->outputActivations.get(jacobianCoords); net->feed_back(); DataExportHandler::instance().display(displayPath); } else if (display) { out << "data header:" << endl << header << endl; out << "displaying sequence " << displaySequence << endl; out << *testSeq; out << "output path: " << endl << displayPath << endl; net->train(*testSeq); net->print_output_shape(out); out << "errors:" << endl << net->outputLayer->errorMap; DataExportHandler::instance().display(displayPath); } else if (conf.get<bool>("errorTest", false)) { trainer.calculate_all_errors(); } else { out << "trainer:" << endl; trainer.train(saveName); } if (logout) { delete logout; } if (tdev) { delete tdev; } // if (tout) // { // delete tout; // } delete net; delete opt; }
double train(Iterator&& first, Iterator&& last, std::size_t max_epochs, Args... args) { dll::rbm_trainer<parent_t, EnableWatcher, RW, false> trainer(args...); return trainer.train(as_derived(), std::forward<Iterator>(first), std::forward<Iterator>(last), max_epochs); }
double train_denoising(const Samples& noisy, const Samples& clean, std::size_t max_epochs, Args... args) { dll::rbm_trainer<parent_t, EnableWatcher, RW, true> trainer(args...); return trainer.train(as_derived(), noisy.begin(), noisy.end(), clean.begin(), clean.end(), max_epochs); }
void ClassifySvmSharedCommand::processSharedAndDesignData(vector<SharedRAbundVector*> lookup) { try { OutputFilter outputFilter(verbosity); LabeledObservationVector labeledObservationVector; FeatureVector featureVector; readSharedRAbundVectors(lookup, designMap, labeledObservationVector, featureVector); // optionally remove features with low standard deviation if ( stdthreshold > 0.0 ) { FeatureVector removedFeatureVector = applyStdThreshold(stdthreshold, labeledObservationVector, featureVector); if (removedFeatureVector.size() > 0) { std::cout << removedFeatureVector.size() << " OTUs were below the stdthreshold of " << stdthreshold << " and were removed" << std::endl; if ( outputFilter.debug() ) { std::cout << "the following OTUs were below the standard deviation threshold of " << stdthreshold << std::endl; for (FeatureVector::iterator i = removedFeatureVector.begin(); i != removedFeatureVector.end(); i++) { std::cout << " " << i->getFeatureLabel() << std::endl; } } } } // apply [0,1] standardization if ( transformName == "zeroone") { std::cout << "transforming data to lie within range [0,1]" << std::endl; transformZeroOne(labeledObservationVector); } else { std::cout << "transforming data to have zero mean and unit variance" << std::endl; transformZeroMeanUnitVariance(labeledObservationVector); } SvmDataset svmDataset(labeledObservationVector, featureVector); OneVsOneMultiClassSvmTrainer trainer(svmDataset, evaluationFoldCount, trainingFoldCount, *this, outputFilter); if ( mode == "rfe" ) { SvmRfe svmRfe; ParameterRange& linearKernelConstantRange = kernelParameterRangeMap["linear"]["constant"]; ParameterRange& linearKernelSmoCRange = kernelParameterRangeMap["linear"]["smoc"]; RankedFeatureList rankedFeatureList = svmRfe.getOrderedFeatureList(svmDataset, trainer, linearKernelConstantRange, linearKernelSmoCRange); map<string, string> variables; variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile)); variables["[distance]"] = lookup[0]->getLabel(); string filename = getOutputFileName("summary", variables); outputNames.push_back(filename); outputTypes["summary"].push_back(filename); m->mothurOutEndLine(); std::ofstream outputFile(filename.c_str()); int n = 0; int rfeRoundCount = rankedFeatureList.front().getRank(); std::cout << "ordered features:" << std::endl; std::cout << setw(5) << "index" << setw(12) << "OTU" << setw(5) << "rank" << std::endl; outputFile << setw(5) << "index" << setw(12) << "OTU" << setw(5) << "rank" << std::endl; for (RankedFeatureList::iterator i = rankedFeatureList.begin(); i != rankedFeatureList.end(); i++) { n++; int rank = rfeRoundCount - i->getRank() + 1; outputFile << setw(5) << n << setw(12) << i->getFeature().getFeatureLabel() << setw(5) << rank << std::endl; if ( n <= 20 ) { std::cout << setw(5) << n << setw(12) << i->getFeature().getFeatureLabel() << setw(5) << rank << std::endl; } } outputFile.close(); } else { MultiClassSVM* mcsvm = trainer.train(kernelParameterRangeMap); map<string, string> variables; variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile)); variables["[distance]"] = lookup[0]->getLabel(); string filename = getOutputFileName("summary", variables); outputNames.push_back(filename); outputTypes["summary"].push_back(filename); m->mothurOutEndLine(); std::ofstream outputFile(filename.c_str()); printPerformanceSummary(mcsvm, std::cout); printPerformanceSummary(mcsvm, outputFile); outputFile << "actual predicted" << std::endl; for ( LabeledObservationVector::const_iterator i = labeledObservationVector.begin(); i != labeledObservationVector.end(); i++ ) { Label actualLabel = i->getLabel(); outputFile << i->getDatasetIndex() << " " << actualLabel << " "; try { Label predictedLabel = mcsvm->classify(*(i->getObservation())); outputFile << predictedLabel << std::endl; } catch ( MultiClassSvmClassificationTie& m ) { outputFile << "tie" << std::endl; std::cout << "classification tie for observation " << i->datasetIndex << " with label " << i->first << std::endl; } } outputFile.close(); delete mcsvm; } } catch (exception& e) { m->errorOut(e, "ClassifySvmSharedCommand", "processSharedAndDesignData"); exit(1); } }
double train(const Samples& training_data, std::size_t max_epochs, Args... args) { dll::rbm_trainer<parent_t, EnableWatcher, RW, false> trainer(args...); return trainer.train(as_derived(), training_data.begin(), training_data.end(), max_epochs); }
// Apart from command-line flags, input is a collection of lstmf files, that // were previously created using tesseract with the lstm.train config file. // The program iterates over the inputs, feeding the data to the network, // until the error rate reaches a specified target or max_iterations is reached. int main(int argc, char **argv) { ParseArguments(&argc, &argv); // Purify the model name in case it is based on the network string. if (FLAGS_model_output.empty()) { tprintf("Must provide a --model_output!\n"); return 1; } STRING model_output = FLAGS_model_output.c_str(); for (int i = 0; i < model_output.length(); ++i) { if (model_output[i] == '[' || model_output[i] == ']') model_output[i] = '-'; if (model_output[i] == '(' || model_output[i] == ')') model_output[i] = '_'; } // Setup the trainer. STRING checkpoint_file = FLAGS_model_output.c_str(); checkpoint_file += "_checkpoint"; STRING checkpoint_bak = checkpoint_file + ".bak"; tesseract::LSTMTrainer trainer( NULL, NULL, NULL, NULL, FLAGS_model_output.c_str(), checkpoint_file.c_str(), FLAGS_debug_interval, static_cast<inT64>(FLAGS_max_image_MB) * 1048576); // Reading something from an existing model doesn't require many flags, // so do it now and exit. if (FLAGS_stop_training || FLAGS_debug_network) { if (!trainer.TryLoadingCheckpoint(FLAGS_continue_from.c_str())) { tprintf("Failed to read continue from: %s\n", FLAGS_continue_from.c_str()); return 1; } if (FLAGS_debug_network) { trainer.DebugNetwork(); } else { if (FLAGS_train_mode & tesseract::TF_INT_MODE) trainer.ConvertToInt(); GenericVector<char> recognizer_data; trainer.SaveRecognitionDump(&recognizer_data); if (!tesseract::SaveDataToFile(recognizer_data, FLAGS_model_output.c_str())) { tprintf("Failed to write recognition model : %s\n", FLAGS_model_output.c_str()); } } return 0; } // Get the list of files to process. if (FLAGS_train_listfile.empty()) { tprintf("Must supply a list of training filenames! --train_listfile\n"); return 1; } GenericVector<STRING> filenames; if (!tesseract::LoadFileLinesToStrings(FLAGS_train_listfile.c_str(), &filenames)) { tprintf("Failed to load list of training filenames from %s\n", FLAGS_train_listfile.c_str()); return 1; } UNICHARSET unicharset; // Checkpoints always take priority if they are available. if (trainer.TryLoadingCheckpoint(checkpoint_file.string()) || trainer.TryLoadingCheckpoint(checkpoint_bak.string())) { tprintf("Successfully restored trainer from %s\n", checkpoint_file.string()); } else { if (!FLAGS_continue_from.empty()) { // Load a past model file to improve upon. if (!trainer.TryLoadingCheckpoint(FLAGS_continue_from.c_str())) { tprintf("Failed to continue from: %s\n", FLAGS_continue_from.c_str()); return 1; } tprintf("Continuing from %s\n", FLAGS_continue_from.c_str()); trainer.InitIterations(); } if (FLAGS_continue_from.empty() || FLAGS_append_index >= 0) { // We need a unicharset to start from scratch or append. string unicharset_str; // Character coding to be used by the classifier. if (!unicharset.load_from_file(FLAGS_U.c_str())) { tprintf("Error: must provide a -U unicharset!\n"); return 1; } tesseract::SetupBasicProperties(true, &unicharset); if (FLAGS_append_index >= 0) { tprintf("Appending a new network to an old one!!"); if (FLAGS_continue_from.empty()) { tprintf("Must set --continue_from for appending!\n"); return 1; } } // We are initializing from scratch. trainer.InitCharSet(unicharset, FLAGS_script_dir.c_str(), FLAGS_train_mode); if (!trainer.InitNetwork(FLAGS_net_spec.c_str(), FLAGS_append_index, FLAGS_net_mode, FLAGS_weight_range, FLAGS_learning_rate, FLAGS_momentum)) { tprintf("Failed to create network from spec: %s\n", FLAGS_net_spec.c_str()); return 1; } trainer.set_perfect_delay(FLAGS_perfect_sample_delay); } } if (!trainer.LoadAllTrainingData(filenames)) { tprintf("Load of images failed!!\n"); return 1; } bool best_dumped = true; char* best_model_dump = NULL; size_t best_model_size = 0; STRING best_model_name; tesseract::LSTMTester tester(static_cast<inT64>(FLAGS_max_image_MB) * 1048576); tesseract::TestCallback tester_callback = nullptr; if (!FLAGS_eval_listfile.empty()) { if (!tester.LoadAllEvalData(FLAGS_eval_listfile.c_str())) { tprintf("Failed to load eval data from: %s\n", FLAGS_eval_listfile.c_str()); return 1; } tester_callback = NewPermanentTessCallback(&tester, &tesseract::LSTMTester::RunEvalAsync); } do { // Train a few. int iteration = trainer.training_iteration(); for (int target_iteration = iteration + kNumPagesPerBatch; iteration < target_iteration; iteration = trainer.training_iteration()) { trainer.TrainOnLine(&trainer, false); } STRING log_str; trainer.MaintainCheckpoints(tester_callback, &log_str); tprintf("%s\n", log_str.string()); } while (trainer.best_error_rate() > FLAGS_target_error_rate && (trainer.training_iteration() < FLAGS_max_iterations || FLAGS_max_iterations == 0)); delete tester_callback; tprintf("Finished! Error rate = %g\n", trainer.best_error_rate()); return 0; } /* main */
int main(int argc, const char* argv[]) { try { // Parse command line arguments. TCLAP::CmdLine cmd("Depth RF trainer", ' ', "0.3"); TCLAP::ValueArg<std::string> image_list_file_arg("f", "image-list-file", "File containing the names of image files", true, "", "string", cmd); TCLAP::ValueArg<int> num_of_classes_arg("n", "num-of-classes", "Number of classes in the data", true, 1, "int", cmd); TCLAP::SwitchArg print_confusion_matrix_switch("m", "conf-matrix", "Print confusion matrix", cmd, true); TCLAP::ValueArg<int> background_label_arg("l", "background-label", "Lower bound of background labels to be ignored", false, -1, "int", cmd); TCLAP::ValueArg<std::string> json_forest_file_arg("j", "json-forest-file", "JSON file where the trained forest should be saved", false, "forest.json", "string"); TCLAP::ValueArg<std::string> binary_forest_file_arg("b", "binary-forest-file", "Binary file where the trained forest should be saved", false, "forest.bin", "string"); TCLAP::ValueArg<std::string> config_file_arg("c", "config", "YAML file with training parameters", false, "", "string", cmd); #if AIT_MULTI_THREADING TCLAP::ValueArg<int> num_of_threads_arg("t", "threads", "Number of threads to use", false, -1, "int", cmd); #endif cmd.xorAdd(json_forest_file_arg, binary_forest_file_arg); cmd.parse(argc, argv); const int num_of_classes = num_of_classes_arg.getValue(); bool print_confusion_matrix = print_confusion_matrix_switch.getValue(); const std::string image_list_file = image_list_file_arg.getValue(); // Initialize training and weak-learner parameters to defaults or load from file ForestTrainerT::ParametersT training_parameters; WeakLearnerT::ParametersT weak_learner_parameters; if (config_file_arg.isSet()) { ait::log_info(false) << "Reading config file " << config_file_arg.getValue() << "... " << std::flush; std::ifstream ifile_config(config_file_arg.getValue()); cereal::JSONInputArchive iarchive(ifile_config); iarchive(cereal::make_nvp("training_parameters", training_parameters)); iarchive(cereal::make_nvp("weak_learner_parameters", weak_learner_parameters)); ait::log_info(false) << " Done." << std::endl; } #if AIT_MULTI_THREADING if (num_of_threads_arg.isSet()) { training_parameters.num_of_threads = num_of_threads_arg.getValue(); } #endif // Read image file list ait::log_info(false) << "Reading image list ... " << std::flush; std::vector<std::tuple<std::string, std::string>> image_list; std::ifstream ifile(image_list_file); if (!ifile.good()) { throw std::runtime_error("Unable to open image list file"); } ait::CSVReader<std::string> csv_reader(ifile); for (auto it = csv_reader.begin(); it != csv_reader.end(); ++it) { if (it->size() != 2) { cmd.getOutput()->usage(cmd); ait::log_error() << "Image list file should contain two columns with the data and label filenames."; exit(-1); } const std::string& data_filename = (*it)[0]; const std::string& label_filename = (*it)[1]; boost::filesystem::path data_path = boost::filesystem::path(data_filename); boost::filesystem::path label_path = boost::filesystem::path(label_filename); if (!data_path.is_absolute()) { data_path = boost::filesystem::path(image_list_file).parent_path(); data_path /= data_filename; } if (!label_path.is_absolute()) { label_path = boost::filesystem::path(image_list_file).parent_path(); label_path /= label_filename; } image_list.push_back(std::make_tuple(data_path.string(), label_path.string())); } ait::log_info(false) << " Done." << std::endl; // TODO: Ensure that label images do not contain values > num_of_classes except for background pixels. Other approach: Test samples directly below. // Set lower bound for background pixel lables ait::label_type background_label; if (background_label_arg.isSet()) { background_label = background_label_arg.getValue(); } else { background_label = num_of_classes; } weak_learner_parameters.background_label = background_label; // Create weak learner and trainer. StatisticsT::Factory statistics_factory(num_of_classes); WeakLearnerT iwl(weak_learner_parameters, statistics_factory); ForestTrainerT trainer(iwl, training_parameters); SampleProviderT sample_provider(image_list, weak_learner_parameters); BaggingWrapperT bagging_wrapper(trainer, sample_provider); #ifdef AIT_TESTING RandomEngineT rnd_engine(11); #else std::random_device rnd_device; ait::log_info() << "rnd(): " << rnd_device(); RandomEngineT rnd_engine(rnd_device()); #endif // Train a forest and time it. auto start_time = std::chrono::high_resolution_clock::now(); // TODO // ForestTrainerT::ForestT forest = bagging_wrapper.train_forest(rnd_engine); // TODO: Testing all samples for comparison with depth_trainer sample_provider.clear_samples(); for (int i = 0; i < image_list.size(); ++i) { sample_provider.load_samples_from_image(i, rnd_engine); } SampleIteratorT samples_start = sample_provider.get_samples_begin(); SampleIteratorT samples_end = sample_provider.get_samples_end(); ait::log_info() << "Starting training ..."; ForestTrainerT::ForestT forest = trainer.train_forest(samples_start, samples_end, rnd_engine); auto stop_time = std::chrono::high_resolution_clock::now(); auto duration = stop_time - start_time; auto period = std::chrono::high_resolution_clock::period(); double elapsed_seconds = duration.count() * period.num / static_cast<double>(period.den); ait::log_info() << "Done."; ait::log_info() << "Running time: " << elapsed_seconds; // Optionally: Serialize forest to JSON file. if (json_forest_file_arg.isSet()) { { ait::log_info(false) << "Writing json forest file " << json_forest_file_arg.getValue() << "... " << std::flush; std::ofstream ofile(json_forest_file_arg.getValue()); cereal::JSONOutputArchive oarchive(ofile); oarchive(cereal::make_nvp("forest", forest)); ait::log_info(false) << " Done." << std::endl; } // Optionally: Serialize forest to binary file. } else if (binary_forest_file_arg.isSet()) { { ait::log_info(false) << "Writing binary forest file " << binary_forest_file_arg.getValue() << "... " << std::flush; std::ofstream ofile(binary_forest_file_arg.getValue(), std::ios_base::binary); cereal::BinaryOutputArchive oarchive(ofile); oarchive(cereal::make_nvp("forest", forest)); ait::log_info(false) << " Done." << std::endl; } } else { throw("This should never happen. Either a JSON or a binary forest file have to be specified!"); } // Optionally: Compute some stats and print them. if (print_confusion_matrix) { ait::log_info(false) << "Creating samples for testing ... " << std::flush; sample_provider.clear_samples(); for (int i = 0; i < image_list.size(); ++i) { sample_provider.load_samples_from_image(i, rnd_engine); } SampleIteratorT samples_start = sample_provider.get_samples_begin(); SampleIteratorT samples_end = sample_provider.get_samples_end(); ait::log_info(false) << " Done." << std::endl; std::vector<ait::size_type> sample_counts(num_of_classes, 0); for (auto sample_it = samples_start; sample_it != samples_end; sample_it++) { ++sample_counts[sample_it->get_label()]; } auto logger = ait::log_info(true); logger << "Sample counts>> "; for (int c = 0; c < num_of_classes; ++c) { if (c > 0) { logger << ", "; } logger << "class " << c << ": " << sample_counts[c]; } logger.close(); // For each tree extract leaf node indices for each sample. std::vector<std::vector<ait::size_type>> forest_leaf_indices = forest.evaluate(samples_start, samples_end); // Compute number of prediction matches based on a majority vote among the forest. int match = 0; int no_match = 0; for (auto tree_it = forest.cbegin(); tree_it != forest.cend(); ++tree_it) { for (auto sample_it = samples_start; sample_it != samples_end; sample_it++) { const auto &node_it = tree_it->cbegin() + (forest_leaf_indices[tree_it - forest.cbegin()][sample_it - samples_start]); const auto &statistics = node_it->get_statistics(); auto max_it = std::max_element(statistics.get_histogram().cbegin(), statistics.get_histogram().cend()); auto label = max_it - statistics.get_histogram().cbegin(); if (label == sample_it->get_label()) { match++; } else { no_match++; } } } ait::log_info() << "Match: " << match << ", no match: " << no_match; // Compute confusion matrix. auto forest_utils = ait::make_forest_utils(forest); auto confusion_matrix = forest_utils.compute_confusion_matrix(samples_start, samples_end); ait::log_info() << "Confusion matrix:" << std::endl << confusion_matrix; auto norm_confusion_matrix = ait::EvaluationUtils::normalize_confusion_matrix(confusion_matrix); ait::log_info() << "Normalized confusion matrix:" << std::endl << norm_confusion_matrix; ait::log_info() << "Diagonal of normalized confusion matrix:" << std::endl << norm_confusion_matrix.diagonal(); // Computing per-frame confusion matrix ait::log_info() << "Computing per-frame confusion matrix."; using ConfusionMatrixType = typename decltype(forest_utils)::MatrixType; ConfusionMatrixType per_frame_confusion_matrix(num_of_classes, num_of_classes); per_frame_confusion_matrix.setZero(); WeakLearnerT::ParametersT full_parameters(weak_learner_parameters); // Modify parameters to retrieve all pixels per sample full_parameters.samples_per_image_fraction = 1.0; SampleProviderT full_sample_provider(image_list, full_parameters); for (int i = 0; i < image_list.size(); ++i) { full_sample_provider.clear_samples(); full_sample_provider.load_samples_from_image(i, rnd_engine); samples_start = full_sample_provider.get_samples_begin(); samples_end = full_sample_provider.get_samples_end(); forest_utils.update_confusion_matrix(per_frame_confusion_matrix, samples_start, samples_end); } ait::log_info() << "Per-frame confusion matrix:" << std::endl << per_frame_confusion_matrix; ConfusionMatrixType per_frame_norm_confusion_matrix = ait::EvaluationUtils::normalize_confusion_matrix(per_frame_confusion_matrix); ait::log_info() << "Normalized per-frame confusion matrix:" << std::endl << per_frame_norm_confusion_matrix; ait::log_info() << "Diagonal of normalized per-frame confusion matrix:" << std::endl << per_frame_norm_confusion_matrix.diagonal(); ait::log_info() << "Mean of diagonal of normalized per-frame confusion matrix:" << std::endl << per_frame_norm_confusion_matrix.diagonal().mean(); } } catch (const std::runtime_error& error) { std::cerr << "Runtime exception occured" << std::endl; std::cerr << error.what() << std::endl; } return 0; }
double SVM::train() { Array X(input, nInstances, nVars); Array y(response, nInstances); Array K; if(opt->kernel == SVM_RBF) { K = kernelRBF(X, X, opt->sigma); } else if(opt->kernel == SVM_LINEAR) { K = kernelLinear(X, X); } else{ exit(-1); } // K must be a symmetric matrix assert(K.rows == K.cols); double *_K = K.getData(); Array A(K.rows, K.cols); double *_A = A.getData(); double *_y = y.getData(); int step = A.cols; for(int i = 0;i < K.rows; i++) { for(int j = 0;j < K.cols;j++) _A[i * step + j] = _y[i] * _y[j] * _K[i * step + j]; } Array LB(nInstances); Array UB(nInstances); double *_LB = LB.getData(); double *_UB = UB.getData(); for(int i = 0;i < UB.size;i++) _UB[i] = opt->C; for(int i = 0;i < LB.size;i++) _LB[i] = 0; Array x(nInstances); x.setZeros(); #ifdef DEBUG opt->minconf_opt.verbose = 3; #endif minConf_TMP trainer(x, LB, UB, &(opt->minconf_opt)); trainer.loadData(nInstances, nInstances, _A, _y); trainer.process(); trainErr = getTrainErr(x, y, K); alpha = x.getData(); // Release memory. A.release(); K.release(); LB.release(); UB.release(); return trainErr; }
void Gym::startTraining() { // Imposto la cancellazione asincrona del thread pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL ); // Prendo il controllo sulle FLTK Fl::lock(); // Aggiorno il log di lavoro this->log_buffer->append( "\n\nInizio l'addestramento." ); this->log_display->insert_position( this->log_buffer->length() ); this->log_display->show_insert_position(); // Rimuovo le vecchie informazioni this->error_data.clear(); // Modifico il colore della casella dell'errore massimo this->error_box->color( FL_RED ); this->error_box->redraw(); // Rilascio il controllo sulle FLTK Fl::unlock(); // Cancello la vecchia rete neurale if ( this->neural_network != NULL ) delete this->neural_network; // Ricavo il numero degli strati const size_t n_layers = (size_t) this->n_layers_input->value(); // Ricavo il numero dei neuroni degli strati nascosti const size_t hidden_neurons_0 = (size_t) this->hidden_neurons_input0->value(); const size_t hidden_neurons_1 = (size_t) this->hidden_neurons_input1->value(); const size_t hidden_neurons_2 = (size_t) this->hidden_neurons_input2->value(); // Creo la nuova rete neurale if ( n_layers == 2 ) this->neural_network = new Network( 2, this->input_size, this->output_size ); else if ( n_layers == 3 ) this->neural_network = new Network( 3, this->input_size, hidden_neurons_0, this->output_size ); else if ( n_layers == 4 ) this->neural_network = new Network( 4, this->input_size, hidden_neurons_0, hidden_neurons_1, this->output_size ); else if ( n_layers == 5 ) this->neural_network = new Network( 5, this->input_size, hidden_neurons_0, hidden_neurons_1, hidden_neurons_2, this->output_size ); // Ricavo l'algoritmo di addestramento const size_t train_algorithm = this->train_algorithm_input->value(); // Ricavo l'errore desiderato const T_Precision desired_error = (T_Precision) this->desired_error_input->value(); // Ricavo le epoche massime const size_t max_epochs = (size_t) this->max_epochs_input->value(); // Ricavo la frequenza di report const size_t report_frequency = (size_t) this->report_frequency_input->value(); // Creo l'addestratore della rete neurale Trainer trainer( this->neural_network ); // Imposto la funzione di report dell'addestramento trainer.setReportFun( Gym::static_update_plot ); trainer.setReportFunData( (void *) this ); // Controllo l'algoritmo di addestramento della rete neurale switch ( train_algorithm ) { // BATCH case TRAIN_BATCH: { // Ricavo il tasso di apprendimento const T_Precision eps = (T_Precision) this->learning_rate_input->value(); // Ricavo il momentum const T_Precision momentum = (T_Precision) this->momentum_input->value(); // Imposto i parametri dell'apprendimento trainer.setParams( eps, momentum ); // Addestro la rete neurale trainer.trainOnFile< algorithms::Batch >( this->train_set_path.c_str(), desired_error, max_epochs, report_frequency ); break; } // RPROP case TRAIN_RPROP: { // Ricavo il fatto di incremento const T_Precision increase_factor = (T_Precision) this->increase_factor_input->value(); // Ricavo il fatto di decremento const T_Precision decrease_factor = (T_Precision) this->decrease_factor_input->value(); // Imposto i parametri dell'apprendimento trainer.setParams( decrease_factor, increase_factor ); // Addestro la rete neurale trainer.trainOnFile< algorithms::Rprop >( this->train_set_path.c_str(), desired_error, max_epochs, report_frequency ); break; } // RPROP+ case TRAIN_RPROP_PLUS: { // Ricavo il fatto di incremento const T_Precision increase_factor = (T_Precision) this->increase_factor_input->value(); // Ricavo il fatto di decremento const T_Precision decrease_factor = (T_Precision) this->decrease_factor_input->value(); // Imposto i parametri dell'apprendimento trainer.setParams( decrease_factor, increase_factor ); // Addestro la rete neurale trainer.trainOnFile< algorithms::RpropPlus >( this->train_set_path.c_str(), desired_error, max_epochs, report_frequency ); break; } // RPROP- case TRAIN_RPROP_MINUS: { // Ricavo il fatto di incremento const T_Precision increase_factor = (T_Precision) this->increase_factor_input->value(); // Ricavo il fatto di decremento const T_Precision decrease_factor = (T_Precision) this->decrease_factor_input->value(); // Imposto i parametri dell'apprendimento trainer.setParams( decrease_factor, increase_factor ); // Addestro la rete neurale trainer.trainOnFile< algorithms::RpropMinus >( this->train_set_path.c_str(), desired_error, max_epochs, report_frequency ); break; } // IRPROP+ case TRAIN_IRPROP_PLUS: { // Ricavo il fatto di incremento const T_Precision increase_factor = (T_Precision) this->increase_factor_input->value(); // Ricavo il fatto di decremento const T_Precision decrease_factor = (T_Precision) this->decrease_factor_input->value(); // Imposto i parametri dell'apprendimento trainer.setParams( decrease_factor, increase_factor ); // Addestro la rete neurale trainer.trainOnFile< algorithms::IRpropPlus >( this->train_set_path.c_str(), desired_error, max_epochs, report_frequency ); break; } // IRPROP- case TRAIN_IRPROP_MINUS: { // Ricavo il fatto di incremento const T_Precision increase_factor = (T_Precision) this->increase_factor_input->value(); // Ricavo il fatto di decremento const T_Precision decrease_factor = (T_Precision) this->decrease_factor_input->value(); // Imposto i parametri dell'apprendimento trainer.setParams( decrease_factor, increase_factor ); // Addestro la rete neurale trainer.trainOnFile< algorithms::IRpropMinus >( this->train_set_path.c_str(), desired_error, max_epochs, report_frequency ); break; } default: break; } // Prendo il controllo sulle FLTK Fl::lock(); // Imposto il passo dell'asse delle ascisse del grafico dell'errore this->error_plot->SetXRulerStep( (float) report_frequency ); // Modifico il colore della casella dell'errore massimo this->error_box->color( FL_GREEN ); this->error_box->redraw(); // Modifico l'etichetta del pulsante this->training_button->label( "Addestra" ); // Memorizzo la fine del thread this->training_flag = false; // Aggiorno il log di lavoro this->log_buffer->append( "\nAddestramento terminato con successo." ); this->log_display->insert_position( this->log_buffer->length() ); this->log_display->show_insert_position(); // Rilascio il controllo sulle FLTK Fl::unlock(); }
void Tasty::_init() { #ifdef QT_DEBUG static bool inited = false; Q_ASSERT(!inited); inited = true; auto now = QDateTime::currentMSecsSinceEpoch(); #endif Bayes::instance(this); _engine = new QQmlApplicationEngine(this); _settings = new Settings(this); _manager = _engine->networkAccessManager(); _pusher = new PusherClient(this); _dataCache = new TastyDataCache; _entryImageWidth = _settings->maxImageWidth(); _commentImageWidth = _entryImageWidth; Q_TEST(connect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(_saveOrReconnect(Qt::ApplicationState)))); Q_TEST(connect(_manager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(_showNetAccessibility(QNetworkAccessManager::NetworkAccessibility)))); Q_TEST(connect(_pusher, SIGNAL(unreadChats(int)), this, SLOT(_setUnreadChats(int)))); Q_TEST(connect(_pusher, SIGNAL(unreadNotifications(int)), this, SLOT(_setUnreadNotifications(int)))); Q_TEST(connect(_pusher, SIGNAL(unreadFriendsEntry(int)), this, SLOT(_incUnreadFriendsEntries()))); QQuickStyle::setStyle("Material"); qmlRegisterType<FeedModel> ("org.binque.taaasty", 1, 0, "FeedModel"); qmlRegisterType<CalendarModel> ("org.binque.taaasty", 1, 0, "CalendarModel"); qmlRegisterType<CommentsModel> ("org.binque.taaasty", 1, 0, "CommentsModel"); qmlRegisterType<AttachedImagesModel>("org.binque.taaasty", 1, 0, "AttachedImagesModel"); qmlRegisterType<UsersModel> ("org.binque.taaasty", 1, 0, "UsersModel"); qmlRegisterType<MessagesModel> ("org.binque.taaasty", 1, 0, "MessagesModel"); qmlRegisterType<FlowsModel> ("org.binque.taaasty", 1, 0, "FlowsModel"); qmlRegisterType<TagsModel> ("org.binque.taaasty", 1, 0, "TagsModel"); qmlRegisterType<Entry> ("org.binque.taaasty", 1, 0, "TlogEntry"); qmlRegisterType<CalendarEntry> ("org.binque.taaasty", 1, 0, "CalendarEntry"); qmlRegisterType<Comment> ("org.binque.taaasty", 1, 0, "Comment"); qmlRegisterType<User> ("org.binque.taaasty", 1, 0, "User"); qmlRegisterType<Author> ("org.binque.taaasty", 1, 0, "Author"); qmlRegisterType<Tlog> ("org.binque.taaasty", 1, 0, "Tlog"); qmlRegisterType<Rating> ("org.binque.taaasty", 1, 0, "Rating"); qmlRegisterType<AttachedImage> ("org.binque.taaasty", 1, 0, "AttachedImage"); qmlRegisterType<Media> ("org.binque.taaasty", 1, 0, "Media"); qmlRegisterType<Notification> ("org.binque.taaasty", 1, 0, "Notification"); qmlRegisterType<Message> ("org.binque.taaasty", 1, 0, "Message"); qmlRegisterType<MessageBase> ("org.binque.taaasty", 1, 0, "MessageBase"); qmlRegisterType<Conversation> ("org.binque.taaasty", 1, 0, "Chat"); qmlRegisterType<Flow> ("org.binque.taaasty", 1, 0, "Flow"); qmlRegisterType<TextHandler> ("org.binque.taaasty", 1, 0, "TextHandler"); qmlRegisterType<Poster> ("org.binque.taaasty", 1, 0, "Poster"); qmlRegisterType<CachedImage>("ImageCache", 2, 0, "CachedImage"); auto root = _engine->rootContext(); root->setContextProperty("Tasty", this); root->setContextProperty("Settings", _settings); auto notifs = NotificationsModel::instance(this); root->setContextProperty("NotifsModel", notifs); auto friendActivity = NotificationsModel::friendActivity(this); root->setContextProperty("FriendActivityModel", friendActivity); auto chats = ChatsModel::instance(this); root->setContextProperty("ChatsModel", chats); auto cache = CacheManager::instance(_manager); cache->setMaxWidth(_settings->maxImageWidth()); cache->setAutoloadOverWifi(_settings->loadImagesOverWifi()); cache->setMaxLoadSize(_settings->maxLoadImageSize()); root->setContextProperty("Cache", cache); Q_TEST(QObject::connect(_settings, SIGNAL(loadImagesOverWifiChanged(bool)), cache, SLOT(setAutoloadOverWifi(bool)))); Q_TEST(QObject::connect(_settings, SIGNAL(maxLoadImageSizeChanged(int)), cache, SLOT(setMaxLoadSize(int)))); auto bayes = Bayes::instance(); root->setContextProperty("Bayes", bayes); auto trainer = bayes->trainer(); root->setContextProperty("Trainer", trainer); #ifdef Q_OS_ANDROID float density = 160; #else float density = 267; // test #endif double scale = density < 180 ? 1 : density < 270 ? 1.5 : density < 360 ? 2 : 3; root->setContextProperty("mm", density / 25.4); // N900: 1 mm = 10.5 px; Q10: 12.9 root->setContextProperty("pt", 1); root->setContextProperty("dp", scale); // N900: 1.5; Q10: 2 root->setContextProperty("sp", density / 160); // scaleable pixels root->setContextProperty("builtAt", QString::fromLatin1(__DATE__)); _engine->setBaseUrl(QStringLiteral("qrc:/qml/")); _engine->load(QUrl(QStringLiteral("main.qml"))); #ifdef QT_DEBUG auto ms = QDateTime::currentMSecsSinceEpoch() - now; qDebug() << "Started in" << ms << "ms"; #endif }