FeatureInitializerFactory.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.math3.ml.neuralnet;

  18. import org.apache.commons.math3.distribution.RealDistribution;
  19. import org.apache.commons.math3.distribution.UniformRealDistribution;
  20. import org.apache.commons.math3.analysis.UnivariateFunction;
  21. import org.apache.commons.math3.analysis.function.Constant;
  22. import org.apache.commons.math3.random.RandomGenerator;

  23. /**
  24.  * Creates functions that will select the initial values of a neuron's
  25.  * features.
  26.  *
  27.  * @since 3.3
  28.  */
  29. public class FeatureInitializerFactory {
  30.     /** Class contains only static methods. */
  31.     private FeatureInitializerFactory() {}

  32.     /**
  33.      * Uniform sampling of the given range.
  34.      *
  35.      * @param min Lower bound of the range.
  36.      * @param max Upper bound of the range.
  37.      * @param rng Random number generator used to draw samples from a
  38.      * uniform distribution.
  39.      * @return an initializer such that the features will be initialized with
  40.      * values within the given range.
  41.      * @throws org.apache.commons.math3.exception.NumberIsTooLargeException
  42.      * if {@code min >= max}.
  43.      */
  44.     public static FeatureInitializer uniform(final RandomGenerator rng,
  45.                                              final double min,
  46.                                              final double max) {
  47.         return randomize(new UniformRealDistribution(rng, min, max),
  48.                          function(new Constant(0), 0, 0));
  49.     }

  50.     /**
  51.      * Uniform sampling of the given range.
  52.      *
  53.      * @param min Lower bound of the range.
  54.      * @param max Upper bound of the range.
  55.      * @return an initializer such that the features will be initialized with
  56.      * values within the given range.
  57.      * @throws org.apache.commons.math3.exception.NumberIsTooLargeException
  58.      * if {@code min >= max}.
  59.      */
  60.     public static FeatureInitializer uniform(final double min,
  61.                                              final double max) {
  62.         return randomize(new UniformRealDistribution(min, max),
  63.                          function(new Constant(0), 0, 0));
  64.     }

  65.     /**
  66.      * Creates an initializer from a univariate function {@code f(x)}.
  67.      * The argument {@code x} is set to {@code init} at the first call
  68.      * and will be incremented at each call.
  69.      *
  70.      * @param f Function.
  71.      * @param init Initial value.
  72.      * @param inc Increment
  73.      * @return the initializer.
  74.      */
  75.     public static FeatureInitializer function(final UnivariateFunction f,
  76.                                               final double init,
  77.                                               final double inc) {
  78.         return new FeatureInitializer() {
  79.             /** Argument. */
  80.             private double arg = init;

  81.             /** {@inheritDoc} */
  82.             public double value() {
  83.                 final double result = f.value(arg);
  84.                 arg += inc;
  85.                 return result;
  86.             }
  87.         };
  88.     }

  89.     /**
  90.      * Adds some amount of random data to the given initializer.
  91.      *
  92.      * @param random Random variable distribution.
  93.      * @param orig Original initializer.
  94.      * @return an initializer whose {@link FeatureInitializer#value() value}
  95.      * method will return {@code orig.value() + random.sample()}.
  96.      */
  97.     public static FeatureInitializer randomize(final RealDistribution random,
  98.                                                final FeatureInitializer orig) {
  99.         return new FeatureInitializer() {
  100.             /** {@inheritDoc} */
  101.             public double value() {
  102.                 return orig.value() + random.sample();
  103.             }
  104.         };
  105.     }
  106. }