aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB. Bogart <bbogart@users.sourceforge.net>2009-03-05 16:53:03 +0000
committerB. Bogart <bbogart@users.sourceforge.net>2009-03-05 16:53:03 +0000
commita29e3a9d4a930756de7175b1f1c1ca456bd85907 (patch)
tree8a06454d1c357035b4217c6afa8b453f62dd9277
parent642ec2eac3ff73872903621462484715c95f8192 (diff)
Added rinit method to set neuron weights to time-seeded random
values. svn path=/trunk/externals/ann/; revision=10837
-rw-r--r--src/ann_som.c38
1 files changed, 38 insertions, 0 deletions
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 <stdio.h>
#include <fcntl.h>
#include <string.h>
+#include <stdlib.h>
+
+#include <sys/timeb.h>
+
#ifdef linux
#include <unistd.h>
#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; i<x->num_neurons; i++) {
+ for (j=0; j<x->num_sensors; j++) {
+ r = (float)rand()/RAND_MAX;
+ x->weights[i][j]=r;
+ }
+ }
+ case 1:
+ m = atom_getfloat(argv);
+ for (i=0; i<x->num_neurons; i++) {
+ for (j=0; j<x->num_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);