⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 neuron.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/**
 * Title: XELOPES Data Mining Library
 * Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
 * Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
 * Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
 * @author Michael Thess
 * @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
 * @version 1.2
 */

package com.prudsys.pdm.Models.Regression.NeuralNetwork;

import java.util.Random;

import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Models.Regression.NeuralNetwork.ActivationFunctions.Logistic;

/**
 * A Neuron of a the hidden layer of a Neural Network. <p>
 *
 * Corresponds to PMML element Neuron.
 *
 * @see com.prudsys.pdm.Adapters.PmmlVersion20.Neuron
 */
public class Neuron extends NeuralNode
{
    // -----------------------------------------------------------------------
    //  Constants of neuron connection type
    // -----------------------------------------------------------------------
    /** Disconnected type. */
    public static final int DISCONNECTED = 0;

    /** Input type. */
    public static final int INPUT = 1;

    /** Hidden type. */
    public static final int HIDDEN = 2;

    /** Output type. */
    public static final int OUTPUT = 3;

    // -----------------------------------------------------------------------
    //  Variables declarations
    // -----------------------------------------------------------------------
    /** Type within the neural layers. */
    protected int layerType = DISCONNECTED;

    /** Weights of inputs, can also include bias. */
    protected double[] weights = new double[1]; // for bias

    /** Change in the weights. */
    protected double[] weightsDelta = new double[1]; // for bias

    /** Reference of activation function of this neuron. */
    protected ActivationFunction activationFunction = new Logistic();

    /** Indicates whether a bias term is used in the neuron. */
    protected boolean useBias = true;

    /** Threshold value when no bias term is used. */
    protected double threshold = Category.MISSING_VALUE;

    /** Random generator. */
    protected Random random = new Random();

    // -----------------------------------------------------------------------
    //  Constructor
    // -----------------------------------------------------------------------
    /**
     * Empty constructor.
     */
    public Neuron()
    {
    }

    // -----------------------------------------------------------------------
    //  Getter and setter methods
    // -----------------------------------------------------------------------
    /**
     * Returns layer type (disconnected, input, hidden, output).
     *
     * @return layer type
     */
    public int getLayerType() {
      return layerType;
    }

    /**
     * Sets layer type (disconnected, input, hidden, output).
     *
     * @param layerType new layer type
     */
    public void setLayerType(int layerType) {
      this.layerType = layerType;
    }

    /**
     * Returns array of weights of the input connections.
     *
     * @return array of weights of the inputs
     */
    public double[] getWeights() {
      return weights;
    }

    /**
     * Sets array of weights of the input connections.
     *
     * @param weights new array of weights of the inputs
     */
    public void setWeights(double[] weights) {
      this.weights = weights;
    }

    /**
     * Returns change in weights.
     *
     * @return change in weights
     */
    public double[] getWeightsDelta() {
      return weightsDelta;
    }

    /**
     * Sets change in weights.
     *
     * @param weightsDelta new change in weights
     */
    public void setWeightsDelta(double[] weightsDelta) {
      this.weightsDelta = weightsDelta;
    }

    /**
     * Reference of activation function of this neuron. If activation function
     * is defined on level of NeuralLayer, this must be the same reference.
     *
     * @return activation function of neuron
     */
    public ActivationFunction getActivationFunction() {
      return activationFunction;
    }

    /**
     * Set activation function of this neuron. If activation function
     * is defined on level of NeuralLayer, this must be the same reference.
     *
     * @param activationFunction new activation function of this neuron
     */
    public void setActivationFunction(ActivationFunction activationFunction) {
      this.activationFunction = activationFunction;
    }

    /**
     * Returns true if a bias term is used in the neuron. A bias is equivalent
     * to an input connection set at a constant level. If a bias use is
     * prescribed by NeuralLayer, this bias use must also be true.
     *
     * @return true if bias term is used, false otherwise
     */
    public boolean isUseBias() {
      return useBias;
    }

    /**
     * Set bias term is used in the neuron. A bias is equivalent
     * to an input connection set at a constant level. If a bias use is
     * prescribed by NeuralLayer, this bias use must also be true.
     *
     * @param useBias set use bias term
     */
    public void setUseBias(boolean useBias) {
      this.useBias = useBias;
    }

    /**
     * Returns threshold value. Usually required, when no bias term
     * is used. If threshold value defined on NeuralNetwork level is
     * not missing, this must be the same value.
     *
     * @return threshold value
     */
    public double getThreshold() {
      return threshold;
    }

    /**
     * Sets new threshold value. Usually required, when no bias term
     * is used. If threshold value defined on NeuralNetwork level is
     * not missing, this must be the same value.
     *
     * @param threshold new threshold value
     */
    public void setThreshold(double threshold) {
      this.threshold = threshold;
    }

    /**
     * Overwrites same method of neural node in order to set
     * seed number of random generator depending on the ID.
     * Sets random number of bias (= weights[0]).
     *
     * @param id ID of neural node unique in Neural Network
     */
    public void setId(String id) {

      // Set ID:
      super.setId(id);

      // Set random value:
      byte[] b  = id.getBytes();
      long seed = 10000*b[0];
      int min   = Math.min(b.length, 10);  // use first 10 digits if long ID
      for (int i = 1; i < min; i++)
        seed = seed + 100*b[i];
      random.setSeed(seed);

      // Set bias to random value:
      weights[0] = random.nextDouble()*0.1 - 0.05;
    }

    /**
     * Returns number of weights.
     *
     * @return number of weights
     */
    public int getNumberOfWeights() {
      return ( weights != null ) ? weights.length : 0;
    }

    /**
     * Returns weight at specified index.
     *
     * @param index weight index
     * @return weight, missing if invalid index
     */
    public double getWeightAt(int index) {

      if (index < 0 || index >= getNumberOfWeights() )
        return Category.MISSING_VALUE;
      else
        return weights[index];
    }

    /**
     * Extends the weights and weightsDelts vectors by one. Set random
     * value for new weight.
     */
    public void addWeight() {

      // Weights:
      int nweights = getNumberOfWeights();
      double[] weights2 = new double[nweights];
      for (int i = 0; i < nweights; i++)
        weights2[i] = weights[i];
      weights = new double[nweights+1];
      for (int i = 0; i < nweights; i++)
        weights[i] = weights2[i];
      weights[nweights] = random.nextDouble()*0.1 - 0.05;

      // Weight deltas:
      for (int i = 0; i < nweights; i++)
        weights2[i] = weightsDelta[i];
      weightsDelta = new double[nweights+1];
      for (int i = 0; i < nweights; i++)
        weightsDelta[i] = weights2[i];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -