예제 #1
0
    void rateclass_t::learn(
            double & lg_L,
            double & aicc,
            vector< pair< double, double > > & params,
            const int nrestart
            ) const
    {
        params.clear();
        params.push_back( make_pair( 1.0, 0.5 ) );
        lg_L = EM( data, params );
        aicc = _aicc( 1, lg_L, data.size() / factor );

        for ( int i = 2; ; ++i ) {
            double old_lg_L, old_aicc;
            vector< pair< double, double > > old_params = params;

            old_params.push_back( make_pair( 1.0, 0.5 ) );

            initialize_params( old_params, 0 );
            old_lg_L = EM( data, old_params );

            for ( int j = 1; j < nrestart; ++j ) {
                double new_lg_L;
                vector< pair< double, double > > new_params = old_params;

                initialize_params( new_params, j );
                new_lg_L = EM( data, new_params );

                if ( new_lg_L > old_lg_L ) {
                    old_lg_L = new_lg_L;
                    old_params = new_params;
                }
            }

            old_aicc = _aicc( 2 * i, old_lg_L, data.size() / factor );

            // if our AICc doesn't improve, we're done
            if ( old_aicc >= aicc )
                break;

            aicc = old_aicc;
            lg_L = old_lg_L;
            params = old_params;
        }

        // we've actually rates corresponding to the majority,
        // but we really want the inverse
        for ( unsigned i = 0; i < params.size(); ++i )
            params[ i ].second = 1.0 - params[ i ].second;

        sort( params.begin(), params.end(), rate_cmp );
    }
예제 #2
0
파일: cparse.c 프로젝트: hsbt/racc
static VALUE
initialize(VALUE self) {
  volatile VALUE vparams;
  struct cparse_params *v;

  vparams = Data_Make_Struct(CparseParams, struct cparse_params,
                             cparse_params_mark, -1, v);
  rb_iv_set(self, "@vparams", vparams);

  rb_ivar_set(self, id_yydebug,        Qnil);
  rb_ivar_set(self, id_action_table,   Qnil);
  rb_ivar_set(self, id_action_check,   Qnil);
  rb_ivar_set(self, id_action_default, Qnil);
  rb_ivar_set(self, id_action_pointer, Qnil);
  rb_ivar_set(self, id_goto_table,     Qnil);
  rb_ivar_set(self, id_goto_check,     Qnil);
  rb_ivar_set(self, id_goto_default,   Qnil);
  rb_ivar_set(self, id_goto_pointer,   Qnil);
  rb_ivar_set(self, id_nt_base,        Qnil);
  rb_ivar_set(self, id_reduce_table,   Qnil);
  rb_ivar_set(self, id_token_table,    Qnil);
  rb_ivar_set(self, id_shift_n,        Qnil);
  rb_ivar_set(self, id_reduce_n,       Qnil);
  rb_ivar_set(self, id_use_result,     Qnil);

  D_puts("starting cparse");
  v->sys_debug = Qtrue;
  vparams = initialize_params(vparams, self, Qnil, Qnil);
  v->lex_is_iterator = Qfalse;

  return self;
}
예제 #3
0
파일: cparse.c 프로젝트: vasco/racc
static VALUE
racc_cparse(VALUE parser)
{
    volatile VALUE vparams;
    struct cparse_params *v;

    vparams = Data_Make_Struct(CparseParams, struct cparse_params,
                               cparse_params_mark, -1, v);
    D_puts("starting cparse");
    v->sys_debug = Qtrue;
    vparams = initialize_params(vparams, parser, Qnil, Qnil);
    v->lex_is_iterator = Qfalse;
    parse_main(v, Qnil, Qnil, 0);

    return v->retval;
}
예제 #4
0
파일: cparse.c 프로젝트: Danylyuk/first_app
static VALUE
racc_cparse(VALUE parser, VALUE arg, VALUE sysdebug)
{
    volatile VALUE vparams;
    struct cparse_params *v;

    vparams = Data_Make_Struct(CparseParams, struct cparse_params,
                               cparse_params_mark, -1, v);
    D_puts("starting cparse");
    v->sys_debug = RTEST(sysdebug);
    vparams = initialize_params(vparams, parser, arg, Qnil, Qnil);
    v->lex_is_iterator = FALSE;
    parse_main(v, Qnil, Qnil, 0);

    return v->retval;
}
예제 #5
0
파일: cparse.c 프로젝트: vasco/racc
static VALUE
racc_yyparse(VALUE parser, VALUE lexer, VALUE lexmid)
{
    volatile VALUE vparams;
    struct cparse_params *v;

    vparams = Data_Make_Struct(CparseParams, struct cparse_params,
                               cparse_params_mark, -1, v);
    v->sys_debug = Qfalse;
    D_puts("start C yyparse");
    vparams = initialize_params(vparams, parser, lexer, lexmid);
    v->lex_is_iterator = Qtrue;
    D_puts("params initialized");
    parse_main(v, Qnil, Qnil, 0);
    call_lexer(v);
    if (!v->fin) {
        rb_raise(rb_eArgError, "%s() is finished before EndOfToken",
                 rb_id2name(v->lexmid));
    }

    return v->retval;
}
예제 #6
0
파일: params.c 프로젝트: abannert/plethora
void parse_args(int argc, char *argv[])
{
    long l;
    int i;
    const char *progname = argv[0];
    struct headers *header;

    initialize_params();

    while ((i = getopt(argc, argv, opts)) > 0) {
        switch (i) {
        case 'H':
            header = parse_header_param(optarg);
            if (header == NULL) {
                fprintf(stderr, "error parsing -H parameter\n");
                print_help(stderr, progname);
                exit(-1);
            } else {
                overwrite_header(&config_opts.headers, header);
            }
            break;
        case 'C':
        {
            /* split on : if present
             * first part is config_opts.connect
             * second part is config_opts.connect_port
             */
            char *portstr;
            config_opts.connect = strdup(optarg);
            portstr = strchr(config_opts.connect, ':');
            if (portstr)
                *portstr++ = '\0'; /* terminate the host part */
            if (portstr && *portstr) {
                errno = 0;
                l = strtol(portstr, (char **)NULL, 10);
                if (errno) {
                    perror("invalid connect port (-C) value");
                    print_help(stderr, progname);
                    exit(-1);
                } else if (l < 0 || l >= USHRT_MAX) {
                    fprintf(stderr, "invalid connect port (-C): %ld\n",
                            l);
                    print_help(stderr, progname);
                    exit(-1);
                }
                config_opts.connect_port = (unsigned short)l;
            }
        }
        break;
        case 'c':
            errno = 0;
            l = strtol(optarg, (char **)NULL, 10);
            if (errno) {
                perror("invalid concurrency (-c) value");
                print_help(stderr, progname);
                exit(-1);
            } else if (l <= 0 || l == LONG_MAX) {
                fprintf(stderr, "invalid concurrency (-c): %ld\n", l);
                print_help(stderr, progname);
                exit(-1);
            }
            config_opts.concurrency = (int)l;
            break;
        case 'n':
            errno = 0;
            l = strtol(optarg, (char **)NULL, 10);
            if (errno) {
                perror("invalid request count (-n) value");
                print_help(stderr, progname);
                exit(-1);
            } else if (l <= 0 || l == LONG_MAX) {
                fprintf(stderr, "invalid request count (-n): %ld\n", l);
                print_help(stderr, progname);
                exit(-1);
            }
            config_opts.count = (int)l;
            break;
        case 'o':
            config_opts.halfopen = 1;
            break;
        case 'v':
            config_opts.verbose++;
            break;
        default:
            fprintf(stderr, "Unknown option: %o\n", i);
        case 'h':
            print_help(stderr, progname);
            exit(-1);
            break;
        }
    }
    argc -= optind;
    argv += optind;
    if (argc == 0) {
        fprintf(stderr, "At least one URL required...");
        print_help(stderr, progname);
        exit(-1);
    } else for (i = 0; i < argc; i++) {
            struct urls *url = malloc(sizeof(*url));
            RING_INIT(url);
            url->url = strdup(argv[i]);
            RING_APPEND(config_opts.urls, url);
        }
}
예제 #7
0
파일: em_ml.c 프로젝트: davidvaz/yap-cmake
int run_em(EM_ENG_PTR em_ptr)
{
    int     r, iterate, old_valid, converged, saved = 0;
    double  likelihood, log_prior;
    double  lambda, old_lambda = 0.0;

    config_em(em_ptr);

    for (r = 0; r < num_restart; r++) {
        SHOW_PROGRESS_HEAD("#em-iters", r);

        initialize_params();
        itemp = daem ? itemp_init : 1.0;
        iterate = 0;

        /* [21 Aug 2007, by yuizumi]
         * while-loop for inversed temperature (DAEM).  Note that this
         * loop is evaluated only once for EM without annealing, since
         * itemp initially set to 1.0 by the code above.
         */
        while (1) {
            if (daem) {
                SHOW_PROGRESS_TEMP(itemp);
            }
            old_valid = 0;

            while (1) {
                if (CTRLC_PRESSED) {
                    SHOW_PROGRESS_INTR();
                    RET_ERR(err_ctrl_c_pressed);
                }

                RET_ON_ERR(em_ptr->compute_inside());
                RET_ON_ERR(em_ptr->examine_inside());

                likelihood = em_ptr->compute_likelihood();
                log_prior  = em_ptr->smooth ? em_ptr->compute_log_prior() : 0.0;
                lambda = likelihood + log_prior;

                if (verb_em) {
                    if (em_ptr->smooth) {
                        prism_printf("Iteration #%d:\tlog_likelihood=%.9f\tlog_prior=%.9f\tlog_post=%.9f\n", iterate, likelihood, log_prior, lambda);
                    }
                    else {
                        prism_printf("Iteration #%d:\tlog_likelihood=%.9f\n", iterate, likelihood);
                    }
                }

                if (debug_level) {
                    prism_printf("After I-step[%d]:\n", iterate);
                    prism_printf("likelihood = %.9f\n", likelihood);
                    print_egraph(debug_level, PRINT_EM);
                }

                if (!isfinite(lambda)) {
                    emit_internal_error("invalid log likelihood or log post: %s (at iteration #%d)",
                                        isnan(lambda) ? "NaN" : "infinity", iterate);
                    RET_ERR(ierr_invalid_likelihood);
                }
                if (old_valid && old_lambda - lambda > prism_epsilon) {
                    emit_error("log likelihood or log post decreased [old: %.9f, new: %.9f] (at iteration #%d)",
                               old_lambda, lambda, iterate);
                    RET_ERR(err_invalid_likelihood);
                }
                if (itemp == 1.0 && likelihood > 0.0) {
                    emit_error("log likelihood greater than zero [value: %.9f] (at iteration #%d)",
                               likelihood, iterate);
                    RET_ERR(err_invalid_likelihood);
                }

                converged = (old_valid && lambda - old_lambda <= prism_epsilon);
                if (converged || REACHED_MAX_ITERATE(iterate)) {
                    break;
                }

                old_lambda = lambda;
                old_valid  = 1;

                RET_ON_ERR(em_ptr->compute_expectation());

                if (debug_level) {
                    prism_printf("After O-step[%d]:\n", iterate);
                    print_egraph(debug_level, PRINT_EM);
                }

                SHOW_PROGRESS(iterate);
                RET_ON_ERR(em_ptr->update_params());
                iterate++;
            }

            /* [21 Aug 2007, by yuizumi]
             * Note that 1.0 can be represented exactly in IEEE 754.
             */
            if (itemp == 1.0) {
                break;
            }
            itemp *= itemp_rate;
            if (itemp >= 1.0) {
                itemp = 1.0;
            }
        }

        SHOW_PROGRESS_TAIL(converged, iterate, lambda);

        if (r == 0 || lambda > em_ptr->lambda) {
            em_ptr->lambda     = lambda;
            em_ptr->likelihood = likelihood;
            em_ptr->iterate    = iterate;

            saved = (r < num_restart - 1);
            if (saved) {
                save_params();
            }
        }
    }

    if (saved) {
        restore_params();
    }

    em_ptr->bic = compute_bic(em_ptr->likelihood);
    em_ptr->cs  = em_ptr->smooth ? compute_cs(em_ptr->likelihood) : 0.0;

    return BP_TRUE;
}
예제 #8
0
파일: mp_em_ml.c 프로젝트: edechter/PRISM
int mpm_run_em(EM_ENG_PTR emptr) {
	int     r, iterate, old_valid, converged, saved=0;
	double  likelihood, log_prior;
	double  lambda, old_lambda=0.0;

	config_em(emptr);

	for (r = 0; r < num_restart; r++) {
		SHOW_PROGRESS_HEAD("#em-iters", r);

		initialize_params();
		mpm_bcast_inside();
		clear_sw_msg_send();

		itemp = daem ? itemp_init : 1.0;
		iterate = 0;

		while (1) {
			if (daem) {
				SHOW_PROGRESS_TEMP(itemp);
			}
			old_valid = 0;

			while (1) {
				if (CTRLC_PRESSED) {
					SHOW_PROGRESS_INTR();
					RET_ERR(err_ctrl_c_pressed);
				}

				if (failure_observed) {
					inside_failure = mp_sum_value(0.0);
				}

				log_prior  = emptr->smooth ? emptr->compute_log_prior() : 0.0;
				lambda = mp_sum_value(log_prior);
				likelihood = lambda - log_prior;

				mp_debug("local lambda = %.9f, lambda = %.9f", log_prior, lambda);

				if (verb_em) {
					if (emptr->smooth) {
						prism_printf("Iteration #%d:\tlog_likelihood=%.9f\tlog_prior=%.9f\tlog_post=%.9f\n", iterate, likelihood, log_prior, lambda);
					} else {
						prism_printf("Iteration #%d:\tlog_likelihood=%.9f\n", iterate, likelihood);
					}
				}

				if (!isfinite(lambda)) {
					emit_internal_error("invalid log likelihood or log post: %s (at iterateion #%d)",
					                    isnan(lambda) ? "NaN" : "infinity", iterate);
					RET_ERR(ierr_invalid_likelihood);
				}
				if (old_valid && old_lambda - lambda > prism_epsilon) {
					emit_error("log likelihood or log post decreased [old: %.9f, new: %.9f] (at iteration #%d)",
					           old_lambda, lambda, iterate);
					RET_ERR(err_invalid_likelihood);
				}
				if (itemp == 1.0 && likelihood > 0.0) {
					emit_error("log likelihood greater than zero [value: %.9f] (at iteration #%d)",
					           likelihood, iterate);
					RET_ERR(err_invalid_likelihood);
				}

				converged = (old_valid && lambda - old_lambda <= prism_epsilon);
				if (converged || REACHED_MAX_ITERATE(iterate)) {
					break;
				}

				old_lambda = lambda;
				old_valid  = 1;

				mpm_share_expectation();

				SHOW_PROGRESS(iterate);
				RET_ON_ERR(emptr->update_params());
				iterate++;
			}

			if (itemp == 1.0) {
				break;
			}
			itemp *= itemp_rate;
			if (itemp >= 1.0) {
				itemp = 1.0;
			}
		}

		SHOW_PROGRESS_TAIL(converged, iterate, lambda);

		if (r == 0 || lambda > emptr->lambda) {
			emptr->lambda     = lambda;
			emptr->likelihood = likelihood;
			emptr->iterate    = iterate;

			saved = (r < num_restart - 1);
			if (saved) {
				save_params();
			}
		}
	}

	if (saved) {
		restore_params();
	}

	emptr->bic = compute_bic(emptr->likelihood);
	emptr->cs  = emptr->smooth ? compute_cs(emptr->likelihood) : 0.0;

	return BP_TRUE;
}