int main(int argc, char **argv) { int i; int errors; DataSet *train_set; DataSet *test_set; Network *adultnn = create_network(14); double e; double acc; Class predicted, desired; // training train_set = read_dataset("adult.train"); if (train_set == NULL) { fprintf(stderr, "Error reading training set\n"); exit(1); } add_layer(adultnn, 28); // hidden layer add_layer(adultnn, 2); // output layer initialize_weights(adultnn, SEED); print_network_structure(adultnn); printf("Training network with %d epochs...\n", EPOCHS); e = batch_train(adultnn, train_set, LEARNING_RATE, EPOCHS, sigmoid, dsigmoid); printf("Training finished, approximate final SSE: %f\n", e); print_network_structure(adultnn); // testing test_set = read_dataset("adult.test"); if (test_set == NULL) { fprintf(stderr, "Error reading test set\n"); exit(1); } errors = 0; printf("Testing with %d cases...\n", test_set->n_cases); for (i = 0; i < test_set->n_cases; ++i) { predicted = predict_class(adultnn, test_set->input[i]); desired = output_to_class(test_set->output[i]); if (predicted != desired) ++errors; printf("Case %d | predicted: %s, desired: %s, outputs: %4.3f %4.3f \n", i, class_to_string(predicted), class_to_string(desired), adultnn->output_layer->y[0], adultnn->output_layer->y[1]); } acc = 100.0 - (100.0 * errors / test_set->n_cases); printf("Testing accuracy: %f\n", acc); printf("Total classificarion errors: %d\n", errors); destroy_dataset(train_set); destroy_dataset(test_set); return 0; }
int read_network(NETWORK *network, FILE *stream) { fill_buffer(stream); create_network(network); get_degrees(network); read_edges(network); free_buffer(); return 0; }
int main() { int structure[3] = {2, 2, 1}; float input[2] = {1,2}; float output[1]; struct network *net = create_network(3, structure); feedforward(net, input, output); vprint_float(1, output); network_save_to_file(net, "mynet.net"); srand(time(NULL)); network_set_random_weights_biases(net, -1, 1); network_load_from_file(net, "mynet.net"); feedforward(net, input, output); vprint_float(1, output); }
int create_datacenter(provider *pv, int type, int nnodes){ int i; pv->datacenters = malloc(sizeof(datacenter)*pv->ndc); for(i=0;i<pv->ndc;i++){ pv->datacenters[i].type = type; pv->datacenters[i].nnodes = nnodes; pv->datacenters[i].allocation_policy = allocation_policy; pv->datacenters[i].optimization_alg = optimization_alg; pv->datacenters[i].alloc_apps = malloc(sizeof(allocated_applications)); pv->datacenters[i].alloc_apps->num_alloc_apps = 0; pv->datacenters[i].alloc_apps->first_app = NULL; pv->datacenters[i].alloc_apps->last_app = NULL; create_network(&pv->datacenters[i],type,nnodes); } return(1); }
void main(int argc,char **argv) { FILE *fp; printf( "\nNetwork Generator for n-Component Kohonen Networks\n" ); control_file=argv[1]; if(argc==2) if((fp=fopen(control_file,"r"))!=NULL){ control(fp); /* get information from control file */ if ((IUnits <= 0) || (OUnits < 0) || (HUnits <= 0)) exit( 1 ); create_network(weight_file); create_patterns(pattern_file,no_of_exemplars); /* if any */ quit(); } else printf("\nControl file %s could not be opened!",argv[1]); else printf("\nUsage: convert2snns <control file>"); printf("\n"); } /* main */
void init(const char* datafile) { DBG("INIT"); DBGV(NBTHREADS); sys("rm -rf data/*"); sys("rm -rf plots/*"); if(datafile[0]=='*') { generateData(&datafile[1]); } else X.load(datafile); if(LIMIT_NDATA!=-1 && X.height > LIMIT_NDATA) X.height = LIMIT_NDATA; n = X.height; D = X.width; create_network(); int ndo = 0; for(int i=0; i<N-1; i++) { node[i].init(X, ndo, n/N); ndo += n/N; } node[N-1].init(X,ndo, n-ndo); // mat_plotall.create((D+1)*(int)(sqrt(N)+1),(D+1)*(int)(sqrt(N)+1)); DBGV(LIMIT_NDATA); DBGV(N); DBGV(D); DBGV(n); if(USE_ENERGY) { DBGV(KEEP_ENERGY); } else { DBGV(q); } }
/* Function to be called from R */ void R_epidemics(int *seqLength, double *mutRate, int *npop, int *nHostPerPop, double *beta, int *nStart, int *t1, int *t2, int *Nsample, int *Tsample, int *duration, int *nbnb, int *listnb, double *pdisp){ int i, nstep, counter_sample = 0, tabidx; /* Initialize random number generator */ int j; time_t t; t = time(NULL); // time in seconds, used to change the seed of the random generator gsl_rng * rng; const gsl_rng_type *typ; gsl_rng_env_setup(); typ=gsl_rng_default; rng=gsl_rng_alloc(typ); gsl_rng_set(rng,t); // changes the seed of the random generator /* transfer simulation parameters */ struct param * par; par = (struct param *) malloc(sizeof(struct param)); par->L = *seqLength; par->mu = *mutRate; par->muL = par->mu * par->L; par->rng = rng; par->npop = *npop; par->popsizes = nHostPerPop; par->beta = *beta; par->nstart = *nStart; par->t1 = *t1; par->t2 = *t2; par->t_sample = Tsample; par->n_sample = *Nsample; par->duration = *duration; par->cn_nb_nb = nbnb; par->cn_list_nb = listnb; par->cn_weights = pdisp; /* check/print parameters */ check_param(par); print_param(par); /* dispersal matrix */ struct network *cn = create_network(par); /* print_network(cn, TRUE); */ /* group sizes */ struct ts_groupsizes * grpsizes = create_ts_groupsizes(par); /* initiate population */ struct metapopulation * metapop; metapop = create_metapopulation(par); /* get sampling schemes (timestep+effectives) */ translate_dates(par); struct table_int *tabdates = get_table_int(par->t_sample, par->n_sample); printf("\n\nsampling at timesteps:"); print_table_int(tabdates); /* create sample */ struct sample ** samplist = (struct sample **) malloc(tabdates->n * sizeof(struct sample *)); struct sample *samp; /* MAKE METAPOPULATION EVOLVE */ nstep = 0; while(get_total_nsus(metapop)>0 && (get_total_ninf(metapop)+get_total_nexp(metapop))>0 && nstep<par->duration){ nstep++; /* age metapopulation */ age_metapopulation(metapop, par); /* process infections */ for(j=0;j<get_npop(metapop);j++){ process_infections(get_populations(metapop)[j], metapop, cn, par); } /* draw samples */ if((tabidx = int_in_vec(nstep, tabdates->items, tabdates->n)) > -1){ /* TRUE if step must be sampled */ samplist[counter_sample++] = draw_sample(metapop, tabdates->times[tabidx], par); } fill_ts_groupsizes(grpsizes, metapop, nstep); } /* we stopped after 'nstep' steps */ if(nstep < par->duration){ printf("\nEpidemics ended at time %d, before last sampling time (%d).\n", nstep, par->duration); } else { /* printf("\n\n-- FINAL METAPOPULATION --"); */ /* print_metapopulation(metapop, FALSE); */ /* merge samples */ samp = merge_samples(samplist, tabdates->n, par); /* write sample to file */ printf("\n\nWriting sample to file 'out-sample.txt'\n"); write_sample(samp); /* free memory */ free_sample(samp); } /* write group sizes to file */ printf("\n\nPrinting group sizes to file 'out-popsize.txt'\n"); write_ts_groupsizes(grpsizes); /* free memory */ free_metapopulation(metapop); free_param(par); for(i=0;i<counter_sample;i++) free_sample(samplist[i]); free(samplist); free_table_int(tabdates); free_network(cn); free_ts_groupsizes(grpsizes); }
/* all-in-one function testing epidemics growth, summary statistics, etc. */ void test_epidemics(int seqLength, double mutRate, int npop, int *nHostPerPop, double beta, int nStart, int t1, int t2, int Nsample, int *Tsample, int duration, int *nbnb, int *listnb, double *pdisp){ int i, j, nstep=0, tabidx, counter_sample = 0; /* Initialize random number generator */ time_t t; t = time(NULL); // time in seconds, used to change the seed of the random generator gsl_rng * rng; const gsl_rng_type *typ; gsl_rng_env_setup(); typ=gsl_rng_default; rng=gsl_rng_alloc(typ); gsl_rng_set(rng,t); // changes the seed of the random generator /* transfer simulation parameters */ struct param * par; par = (struct param *) malloc(sizeof(struct param)); par->L = seqLength; par->mu = mutRate; par->muL = par->mu * par->L; par->rng = rng; par->npop = npop; par->npop = npop; par->popsizes = nHostPerPop; par->beta = beta; par->nstart = nStart; par->t1 = t1; par->t2 = t2; par->t_sample = Tsample; par->n_sample = Nsample; par->duration = duration; par->cn_nb_nb = nbnb; par->cn_list_nb = listnb; par->cn_weights = pdisp; /* check/print parameters */ check_param(par); print_param(par); /* dispersal matrix */ struct network *cn = create_network(par); /* group sizes */ struct ts_groupsizes * grpsizes = create_ts_groupsizes(par); /* initiate population */ struct metapopulation * metapop; metapop = create_metapopulation(par); /* get sampling schemes (timestep+effectives) */ translate_dates(par); struct table_int *tabdates = get_table_int(par->t_sample, par->n_sample); printf("\n\nsampling at timesteps:"); print_table_int(tabdates); /* create sample */ struct sample ** samplist = (struct sample **) malloc(tabdates->n * sizeof(struct sample *)); struct sample *samp; /* MAKE METAPOPULATION EVOLVE */ nstep = 0; while(get_total_nsus(metapop)>0 && (get_total_ninf(metapop)+get_total_nexp(metapop))>0 && nstep<par->duration){ nstep++; /* age metapopulation */ age_metapopulation(metapop, par); /* process infections */ for(j=0;j<get_npop(metapop);j++){ process_infections(get_populations(metapop)[j], metapop, cn, par); } /* draw samples */ if((tabidx = int_in_vec(nstep, tabdates->items, tabdates->n)) > -1){ /* TRUE if step must be sampled */ samplist[counter_sample++] = draw_sample(metapop, tabdates->times[tabidx], par); } fill_ts_groupsizes(grpsizes, metapop, nstep); } /* we stopped after 'nstep' steps */ if(nstep < par->duration){ printf("\nEpidemics ended at time %d, before last sampling time (%d).\n", nstep, par->duration); } else { printf("\n\n-- FINAL METAPOPULATION --"); print_metapopulation(metapop, TRUE); /* test samples */ for(i=0;i<tabdates->n;i++) { printf("\nsample %d\n", i); print_sample(samplist[i], TRUE); } samp = merge_samples(samplist, tabdates->n, par) ; print_sample(samp, TRUE); /* test allele listing */ struct snplist *snpbilan; snpbilan = list_snps(samp, par); print_snplist(snpbilan); /* test allele frequencies */ struct allfreq *freq; freq = get_frequencies(samp, par); print_allfreq(freq); /* test Hs*/ double Hs = hs(samp,par); printf("\nHs = %0.3f\n", Hs); /* test Hs full genome */ Hs = hs_full_genome(samp,par); printf("\nHs (full genome) = %0.5f\n", Hs); /* test nb of snps */ int nball = nb_snps(samp,par); printf("\nnumber of SNPs = %d\n", nball); /* test mean nb of snps */ double temp = mean_nb_snps(samp); printf("\nmean number of SNPs = %.2f\n", temp); /* test var nb of snps */ temp = var_nb_snps(samp); printf("\nvariance of number of alleles = %.2f\n", temp); /* test pairwise distances */ struct distmat_int *mat = pairwise_dist(samp, par); print_distmat_int(mat); /* test mean pairwise distances */ temp = mean_pairwise_dist(samp,par); printf("\nmean pairwise distance: %.2f", temp); /* test variance of pairwise distances */ temp = var_pairwise_dist(samp,par); printf("\nvar pairwise distance: %.2f", temp); /* test Fst */ temp = fst(samp,par); printf("\nfst: %.2f", temp); printf("\n\n"); /* free memory */ free_sample(samp); free_snplist(snpbilan); free_allfreq(freq); free_distmat_int(mat); } /* write group sizes to file */ printf("\n\nPrinting group sizes to file 'out-popsize.txt'"); write_ts_groupsizes(grpsizes); /* free memory */ free_metapopulation(metapop); free_param(par); for(i=0;i<counter_sample;i++) free_sample(samplist[i]); free(samplist); free_table_int(tabdates); free_network(cn); free_ts_groupsizes(grpsizes); }
/* Function to be called from R */ void R_monitor_epidemics(int *seqLength, double *mutRate, int *npop, int *nHostPerPop, double *beta, int *nStart, int *t1, int *t2, int *Nsample, int *Tsample, int *duration, int *nbnb, int *listnb, double *pdisp, int *minSize){ int nstep; /* Initialize random number generator */ int j; time_t t; t = time(NULL); // time in seconds, used to change the seed of the random generator gsl_rng * rng; const gsl_rng_type *typ; gsl_rng_env_setup(); typ=gsl_rng_default; rng=gsl_rng_alloc(typ); gsl_rng_set(rng,t); // changes the seed of the random generator /* transfer simulation parameters */ struct param * par; par = (struct param *) malloc(sizeof(struct param)); par->L = *seqLength; par->mu = *mutRate; par->muL = par->mu * par->L; par->rng = rng; par->npop = *npop; par->popsizes = nHostPerPop; par->beta = *beta; par->nstart = *nStart; par->t1 = *t1; par->t2 = *t2; par->t_sample = Tsample; par->n_sample = *Nsample; par->duration = *duration; par->cn_nb_nb = nbnb; par->cn_list_nb = listnb; par->cn_weights = pdisp; /* check/print parameters */ check_param(par); print_param(par); /* dispersal matrix */ struct network *cn = create_network(par); /* print_network(cn, TRUE); */ /* group sizes */ struct ts_groupsizes * grpsizes = create_ts_groupsizes(par); struct ts_sumstat * sumstats = create_ts_sumstat(par); /* initiate population */ struct metapopulation * metapop; metapop = create_metapopulation(par); /* get sampling schemes (timestep+effectives) */ translate_dates(par); struct table_int *tabdates = get_table_int(par->t_sample, par->n_sample); printf("\n\nsampling at timesteps:"); print_table_int(tabdates); /* create sample */ struct sample *samp; /* MAKE METAPOPULATION EVOLVE */ nstep = 0; while(get_total_nsus(metapop)>0 && (get_total_ninf(metapop)+get_total_nexp(metapop))>0 && nstep<par->duration){ nstep++; /* age metapopulation */ age_metapopulation(metapop, par); /* process infections */ for(j=0;j<get_npop(metapop);j++){ process_infections(get_populations(metapop)[j], metapop, cn, par); } /* draw sample */ samp = draw_sample(metapop, par->n_sample, par); /* compute statistics */ if(get_total_ninf(metapop)> *minSize) fill_ts_sumstat(sumstats, samp, nstep, par); /* get group sizes */ fill_ts_groupsizes(grpsizes, metapop, nstep); } /* write group sizes to file */ printf("\n\nWriting results to file..."); write_ts_groupsizes(grpsizes); write_ts_sumstat(sumstats); printf("done.\n\n"); /* free memory */ free_sample(samp); free_metapopulation(metapop); free_param(par); free_network(cn); free_ts_groupsizes(grpsizes); free_ts_sumstat(sumstats); }
NN::NN(vector<int> layers) { create_network(layers); }
int main(int argc, char *argv[]) { unsigned int moment; unsigned int node_index; unsigned int moment_index; node_t *node; Display *display; GC gc; int screen_number; int window_x; int window_y; unsigned int each_x; unsigned int each_y; unsigned int window_border_width; unsigned int window_height; unsigned int window_width; unsigned long gc_value_mask; unsigned long window_background_color; unsigned long window_border_color; Window root_window; Window window; XGCValues gc_values; Visual* default_visual; Colormap colormap; XColor system_color; XColor exact_color; unsigned int window_display_x_modulus; unsigned int window_display_y_modulus; unsigned int window_x_pixel; unsigned int window_y_pixel; display = XOpenDisplay(NULL); screen_number = DefaultScreen(display); root_window = RootWindow(display, screen_number); window_x = 0; window_y = 0; window_width = XWINDOW_WIDTH; window_height = XWINDOW_HEIGHT; window_border_width = 0; window_border_color = BlackPixel(display, screen_number); window_background_color = WhitePixel(display, screen_number); window = XCreateSimpleWindow(display, root_window, window_x, window_y, window_width, window_height, window_border_width, window_border_color, window_background_color); XMapWindow(display, window); XFlush(display); gc_value_mask = 0; gc = XCreateGC(display, window, gc_value_mask, &gc_values); default_visual = DefaultVisual(display, DefaultScreen(display)); colormap = XCreateColormap(display, window, default_visual, AllocNone); XSync(display, False); window_display_x_modulus = NODES / XWINDOW_WIDTH; window_display_y_modulus = TIME / XWINDOW_HEIGHT; srandom(SEED); /* init_action_tables(); */ network = create_network(); window_y_pixel = 0; for (moment = 0; moment < TIME; moment++) { if (0 == (moment % window_display_y_modulus)) { window_x_pixel = 0; for (node_index = 0; node_index < NODES; node_index++) { if (0 != (node_index % window_display_x_modulus)) { continue; } node = network->nodes_display_order[node_index]; system_color.red = 0; if (0 == node->value) { system_color.green = USHRT_MAX / 4; system_color.blue = USHRT_MAX; } else if (1 == node->value) { system_color.green = USHRT_MAX; system_color.blue = USHRT_MAX / 4; } else { exit(100); } /* system_color.blue = 0; */ if (2 == node->link_count) { system_color.green /= 2; system_color.blue /= 2; } /* system_color.green = 0; */ XAllocColor(display, colormap, &system_color); XSetForeground(display, gc, system_color.pixel); XDrawPoint(display, window, gc, window_x_pixel, window_y_pixel); window_x_pixel++; } XFlush(display); window_y_pixel++; } iterate_network(network); } while (1) { usleep(SLEEP); } destroy_network(network); XUnmapWindow(display, window); XDestroyWindow(display, window); XCloseDisplay(display); return 0; }