001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.math3.ml.neuralnet; 019 020import org.apache.commons.math3.distribution.RealDistribution; 021import org.apache.commons.math3.distribution.UniformRealDistribution; 022import org.apache.commons.math3.analysis.UnivariateFunction; 023import org.apache.commons.math3.analysis.function.Constant; 024import org.apache.commons.math3.random.RandomGenerator; 025 026/** 027 * Creates functions that will select the initial values of a neuron's 028 * features. 029 * 030 * @since 3.3 031 */ 032public class FeatureInitializerFactory { 033 /** Class contains only static methods. */ 034 private FeatureInitializerFactory() {} 035 036 /** 037 * Uniform sampling of the given range. 038 * 039 * @param min Lower bound of the range. 040 * @param max Upper bound of the range. 041 * @param rng Random number generator used to draw samples from a 042 * uniform distribution. 043 * @return an initializer such that the features will be initialized with 044 * values within the given range. 045 * @throws org.apache.commons.math3.exception.NumberIsTooLargeException 046 * if {@code min >= max}. 047 */ 048 public static FeatureInitializer uniform(final RandomGenerator rng, 049 final double min, 050 final double max) { 051 return randomize(new UniformRealDistribution(rng, min, max), 052 function(new Constant(0), 0, 0)); 053 } 054 055 /** 056 * Uniform sampling of the given range. 057 * 058 * @param min Lower bound of the range. 059 * @param max Upper bound of the range. 060 * @return an initializer such that the features will be initialized with 061 * values within the given range. 062 * @throws org.apache.commons.math3.exception.NumberIsTooLargeException 063 * if {@code min >= max}. 064 */ 065 public static FeatureInitializer uniform(final double min, 066 final double max) { 067 return randomize(new UniformRealDistribution(min, max), 068 function(new Constant(0), 0, 0)); 069 } 070 071 /** 072 * Creates an initializer from a univariate function {@code f(x)}. 073 * The argument {@code x} is set to {@code init} at the first call 074 * and will be incremented at each call. 075 * 076 * @param f Function. 077 * @param init Initial value. 078 * @param inc Increment 079 * @return the initializer. 080 */ 081 public static FeatureInitializer function(final UnivariateFunction f, 082 final double init, 083 final double inc) { 084 return new FeatureInitializer() { 085 /** Argument. */ 086 private double arg = init; 087 088 /** {@inheritDoc} */ 089 public double value() { 090 final double result = f.value(arg); 091 arg += inc; 092 return result; 093 } 094 }; 095 } 096 097 /** 098 * Adds some amount of random data to the given initializer. 099 * 100 * @param random Random variable distribution. 101 * @param orig Original initializer. 102 * @return an initializer whose {@link FeatureInitializer#value() value} 103 * method will return {@code orig.value() + random.sample()}. 104 */ 105 public static FeatureInitializer randomize(final RealDistribution random, 106 final FeatureInitializer orig) { 107 return new FeatureInitializer() { 108 /** {@inheritDoc} */ 109 public double value() { 110 return orig.value() + random.sample(); 111 } 112 }; 113 } 114}