Source for de.webdings.jannis.neuralnet.WeightRandomizer

   1: /* WeightRandomizer.java - Copyright (c) 2005 by Stefan Thesing
   2:  <p>This file is part of Jannis.</p>
   3:  <p>Jannis is free software; you can redistribute it and/or modify
   4:  it under the terms of the GNU General Public License as published by
   5:  the Free Software Foundation; either version 2 of the License, or
   6:  (at your option) any later version.</p>
   7: <p>Jannis is distributed in the hope that it will be useful,
   8: but WITHOUT ANY WARRANTY; without even the implied warranty of
   9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10: GNU General Public License for more details.</p>
  11: <p>You should have received a copy of the GNU General Public License
  12: along with Jannis; if not, write to the<br>
  13: Free Software Foundation, Inc.,<br>
  14: 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA<br>
  15: */
  16: package de.webdings.jannis.neuralnet;
  17: 
  18: import java.util.Random;
  19: 
  20: /**
  21:  * WeightRandomizer is used to generate random synapse weights.
  22:  * These are often needed for freshly defined neural nets 
  23:  * that will be trained.</p>
  24:  * <p>By standard, it generates weights ranging from -0.2 
  25:  * and +0.2, yet different ranges can be specified.</p>
  26:  * <p>It uses the {@link java.util.Random} class. 
  27:  * 
  28:  * 
  29:  * @author Stefan Thesing<br>
  30:  * Website: <a href="http://www.webdings.de">http://www.webdings.de</a>
  31:  * @version 0.1, 11.08.2005
  32:  * @see Synapse
  33:  * @see java.util.Random
  34:  */
  35: public class WeightRandomizer {
  36:     //ATTRIBUTES
  37:     private int i=0;
  38:     //METHODS
  39:     /**
  40:      * @return a random weight between -0.2 and +0.2
  41:      */
  42:     public float generateRandomWeight() {
  43:         return generateRandomWeight(2);
  44:     }
  45:     
  46:     /**
  47:      * @param maxDigitAfterDot
  48:      * @return a random weight between -0.x and +0.x, where
  49:      * x is the specified maximum digit after the dot
  50:      */
  51:     public float generateRandomWeight(int maxDigitAfterDot) {
  52:         return generateRandomWeight(0, maxDigitAfterDot);
  53:     }
  54:     
  55:     /**
  56:      * @param maxDigitBeforeDot
  57:      * @param maxDigitAfterDot
  58:      * @return a random weight between -x.y and +x.y, where
  59:      * x is the specified maximum number before the dot and
  60:      * y is the specified maximum digit after the dot
  61:      */
  62:     public float generateRandomWeight(int maxDigitBeforeDot, int maxDigitAfterDot) {
  63:       Random generator = new Random(System.currentTimeMillis()+i);
  64:       int digitBeforeDot = generator.nextInt(maxDigitBeforeDot+1);
  65:       int digitAfterDot = generator.nextInt(maxDigitAfterDot+1);
  66:       String s = digitBeforeDot+ "." + digitAfterDot;
  67:       Float gewicht = new Float(s);
  68:       if(generator.nextBoolean()) {
  69:         gewicht = new Float(gewicht.floatValue() * -1);
  70:       }
  71:       ++i;
  72:       return gewicht.floatValue();
  73:     }
  74: }

© 2005 by Stefan Thesing;
Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.