// -*- C++ -*- // Copyright 2006-2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz // or its licensors, as applicable. // // You may not use this file except under the terms of the accompanying license. // // Licensed under the Apache License, Version 2.0 (the "License"); you // may not use this file except in compliance with the License. You may // obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Project: ocr-bpnet - neural network classifier // File: bpnet.cc // Purpose: neural network classifier // Responsible: Hagen Kaprykowsky (kapry@iupr.net) // Reviewer: Yves Rangoni (rangoni@iupr.net) // Primary Repository: // Web Sites: www.iupr.org, www.dfki.de #include #include "bpnet.h" #include "ocr-utils.h" #include "narray-ops.h" #include "logger.h" using namespace ocropus; using namespace colib; using namespace narray_ops; namespace iupr_bpnet { #define MIN_SCORE 1e-6 #define epsilon_stdev 1e-04 // log the confusion matrix for the training set for each epoch Logger logger_confusion_train("bpnet.conf_train"); // log the confusion matrix for the testing set for each epoch Logger logger_confusion_test("bpnet.conf_test"); // log error rates for each epoch Logger logger_errors("bpnet.error_rates"); param_int freq_error_rates("freq_error_rates",1,"frequency of error_rates graph plotting"); param_float graph_scale("graph_scale",1.,"scale factor for graph ploting if error_rates is enabled"); param_bool merge_lr("merge_lr",true,"plot learning rate with error rates, otherwise in another plot"); template bool valid(T &v) { for(int i=0;i &v,intarray &c) { floatarray v_tmp; v_tmp.resize(v[0].length()); int c_tmp; int n = v.length(); for(int i=0;i=SIGMOID_RANGE) { abs_result = 1.0; } else { index = int(NSIGMOID*abs_x/SIGMOID_RANGE); abs_result = (index>=NSIGMOID) ? 1.0 : sigmoid_table(index); } return (x<0.0) ? 1.0-abs_result:abs_result; } // float random number between low and high static float random_range(float low,float high) { //drand48 is obsolete, replaced by rand(): float rnd = float(rand())/float(RAND_MAX); return (rnd * (high-low) + low); } // push activations one layer up using 2d weight matrix void bp_propagate_activations(floatarray &activations_input,int ninput, floatarray &activations_output,int noutput, floatarray &weights,floatarray &offsets, bool &sigmoid_inited) { float total; for(int i=0;i