From a29e3a9d4a930756de7175b1f1c1ca456bd85907 Mon Sep 17 00:00:00 2001 From: "B. Bogart" Date: Thu, 5 Mar 2009 16:53:03 +0000 Subject: Added rinit method to set neuron weights to time-seeded random values. svn path=/trunk/externals/ann/; revision=10837 --- src/ann_som.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/ann_som.c b/src/ann_som.c index 18aee95..4f9fef4 100644 --- a/src/ann_som.c +++ b/src/ann_som.c @@ -16,6 +16,10 @@ #include #include #include +#include + +#include + #ifdef linux #include #endif @@ -318,6 +322,39 @@ static void som_cinit(t_som *x, t_symbol *s, int argc, t_atom *argv){ } } +static void som_rinit(t_som *x, t_symbol *s, int argc, t_atom *argv) +{ /* initialize the neuron-weights to time-seeded random values*/ + int i, j; + float m,r; + struct timeb mytime; + + ftime(&mytime); // get current time + srand(mytime.time*1000+mytime.millitm); // Seed with time (in millisecs) + + switch (argc) { + case 0: + for (i=0; inum_neurons; i++) { + for (j=0; jnum_sensors; j++) { + r = (float)rand()/RAND_MAX; + x->weights[i][j]=r; + } + } + case 1: + m = atom_getfloat(argv); + for (i=0; inum_neurons; i++) { + for (j=0; jnum_sensors; j++) { + r = (float)rand()/RAND_MAX*m; + x->weights[i][j]=r; + } + } + break; + default: + if (argc > 1) { + error("som_rinit: Pass a single float (random value multiplier)."); + } + } +} + /* dump the weights of the queried neuron to the output */ static void som_dump(t_som *x, t_float nf){ int n=nf; @@ -743,6 +780,7 @@ static void som_setup(void) class_addmethod(som_class, (t_method)som_makenewsom, gensym("new"), A_GIMME, 0); class_addmethod(som_class, (t_method)som_init, gensym("init"), A_GIMME, 0); class_addmethod(som_class, (t_method)som_cinit, gensym("cinit"), A_GIMME, 0); + class_addmethod(som_class, (t_method)som_rinit, gensym("rinit"), A_GIMME, 0); class_addmethod(som_class, (t_method)som_learn, gensym("learn"), A_GIMME, 0); class_addmethod(som_class, (t_method)som_neighbour, gensym("neighbour"), A_GIMME, 0); -- cgit v1.2.1