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

📄 neurallayer.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    neuralNodes[nnode] = node;
    neuralNodes[nnode].setNeuralLayer(this);
  }

  // -----------------------------------------------------------------------
  //  Calculation methods
  // -----------------------------------------------------------------------
  /**
   * Reset all internal calculation values of this layer by calling
   * the resetValue methods of all neural nodes. Note that this method
   * is recursive to the input side.
   */
  public void resetValues() {

    for (int i = 0; i < getNumberOfNodes(); i++)
      neuralNodes[i].resetValues();
  }

  /**
   * Calculates net input values of the nodes of the layer by
   * calling same method of all neural nodes. Note that this method
   * is recursive to the input side.
   *
   * @param newcalc calculate the input new
   * @return input values of nodes of layer, missing if not calculated
   * @exception MiningException error while calculating input values
   */
  public double[] inputValues(boolean newcalc) throws MiningException {

    int n = getNumberOfNodes();
    double[] inputs = new double[n];

    for (int i = 0; i < n; i++)
      inputs[i] = neuralNodes[i].inputValue(newcalc);

    return inputs;
  }

  /**
   * Calculates output values of the nodes of the layer by
   * calling same method of all neural nodes. Note that this method
   * is recursive to the input side.
   *
   * @param newcalc calculate the output new
   * @return output values of nodes of layer, missing if not calculated
   * @exception MiningException error while calculating output values
   */
  public double[] outputValues(boolean newcalc) throws MiningException {

    int n = getNumberOfNodes();
    double[] outputs = new double[n];

    for (int i = 0; i < n; i++)
      outputs[i] = neuralNodes[i].outputValue(newcalc);

    return outputs;
  }

  /**
   * Calculates error values of the nodes of this layer by
   * calling same method of all neural nodes. Note that this method
   * is recursive to the output side (backpropagation!).
   *
   * @param newcalc calculate the error new
   * @return error values of nodes of layer, missing if not calculated
   * @exception MiningException error while calculating error value
   */
  public double[] errorValues(boolean newcalc) throws MiningException {

    int n = getNumberOfNodes();
    double[] errors = new double[n];

    for (int i = 0; i < n; i++)
      errors[i] = neuralNodes[i].errorValue(newcalc);

    return errors;
  }

  /**
   * Updates the weights of the nodes of this layer by
   * calling same method of all neural nodes. Note that this method
   * is recursive to the input side.
   *
   * @param learningRate the learning rate
   * @param momentum the momentum
   * @exception MiningException cannot update weights
   */
  public void updateWeights(double learningRate, double momentum)
    throws MiningException {

    int n = getNumberOfNodes();
    for (int i = 0; i < n; i++)
      neuralNodes[i].updateWeights(learningRate, momentum);
  }

  // -----------------------------------------------------------------------
  //  Methods of PMML handling
  // -----------------------------------------------------------------------
  /**
   * Write neural layer to PMML element.
   *
   * @return PMML element of neural layer
   * @exception MiningException
   */
  public Object createPmmlObject() throws MiningException
  {
    if (layerType == NEURAL_INPUT) {
      com.prudsys.pdm.Adapters.PmmlVersion20.NeuralInputs inputs =
          new com.prudsys.pdm.Adapters.PmmlVersion20.NeuralInputs();

      // Get number of input neurons:
      int ninp = getNumberOfNodes();
      if (ninp == 0)
        throw new MiningException("no input neurons");
      com.prudsys.pdm.Adapters.PmmlVersion20.NeuralInput[] input =
          new com.prudsys.pdm.Adapters.PmmlVersion20.NeuralInput[ninp];

      // Set array of input neurons:
      for (int i = 0; i < ninp; i++)
        input[i] = (com.prudsys.pdm.Adapters.PmmlVersion20.NeuralInput) neuralNodes[i].createPmmlObject();

      inputs.setNeuralInput(input);
      return inputs;
    }
    else
    if (layerType == NEURON) {
      com.prudsys.pdm.Adapters.PmmlVersion20.NeuralLayer neurons =
        new com.prudsys.pdm.Adapters.PmmlVersion20.NeuralLayer();

      // Get number of neurons:
      int nneu = getNumberOfNodes();
      if (nneu == 0)
        throw new MiningException("no neurons");

      // Set number of neurons:
      neurons.setNumberOfNeurons( String.valueOf(nneu) );

      // Set activation function:
      if (activationFunction != null && neuralNetwork.getActivationFunction() == null) {
        String afname = ActivationFunction.convertTypeToPmml(activationFunction.
            getFunctionType());
        if (afname == null)
          afname = activationFunction.getFunctionType(); // use even if no equivalent PMML name exists
        neurons.setActivationFunction(afname);
      };

      // Set array of neurons:
      com.prudsys.pdm.Adapters.PmmlVersion20.Neuron[] neuron =
        new com.prudsys.pdm.Adapters.PmmlVersion20.Neuron[nneu];

      for (int i = 0; i < nneu; i++)
        neuron[i] = (com.prudsys.pdm.Adapters.PmmlVersion20.Neuron) neuralNodes[i].createPmmlObject();

      neurons.setNeuron(neuron);
      return neurons;
    }
    else
    if (layerType == NEURAL_OUTPUT) {
      com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutputs outputs =
          new com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutputs();

      // Get number of output neurons:
      int nout = getNumberOfNodes();
      if (nout == 0)
        throw new MiningException("no output neurons");

      // Set array of output neurons:
      com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutput[] output =
          new com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutput[nout];

      for (int i = 0; i < nout; i++)
        output[i] = (com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutput) neuralNodes[i].createPmmlObject();

      outputs.setNeuralOutput(output);
      return outputs;
    }

    return null;
  }

  /**
   * Read neural layer from PMML element.
   *
   * @param pmmlObject PMML element to read in
   * @exception MiningException always thrown
   */
  public void parsePmmlObject( Object pmmlObject ) throws MiningException
  {
    if (pmmlObject instanceof com.prudsys.pdm.Adapters.PmmlVersion20.NeuralInputs) {

      this.layerType = NEURAL_INPUT;
      com.prudsys.pdm.Adapters.PmmlVersion20.NeuralInputs inputs =
        (com.prudsys.pdm.Adapters.PmmlVersion20.NeuralInputs) pmmlObject;

      // Get number of input neurons:
      com.prudsys.pdm.Adapters.PmmlVersion20.NeuralInput[] input = inputs.getNeuralInput();
      if (input == null || input.length == 0)
        throw new MiningException("no input neurons");
      int ninp = input.length;

      // Get array of input neurons:
      neuralNodes = new NeuralInput[ninp];
      for (int i = 0; i < ninp; i++) {
        neuralNodes[i] = new NeuralInput();
        neuralNodes[i].parsePmmlObject(input[i]);
      };
      setNeuralNodes(neuralNodes);
    }
    else
    if (pmmlObject instanceof com.prudsys.pdm.Adapters.PmmlVersion20.NeuralLayer) {

      this.layerType = NEURON;
      com.prudsys.pdm.Adapters.PmmlVersion20.NeuralLayer neurons =
        (com.prudsys.pdm.Adapters.PmmlVersion20.NeuralLayer) pmmlObject;

      // Get number of neurons:
      com.prudsys.pdm.Adapters.PmmlVersion20.Neuron[] neuron = neurons.getNeuron();
      if (neuron == null || neuron.length == 0)
        throw new MiningException("no neurons");
      int nneu = neuron.length;

      // Get array of neurons:
      neuralNodes = new Neuron[nneu];
      for (int i = 0; i < nneu; i++) {
        neuralNodes[i] = new Neuron();
        neuralNodes[i].parsePmmlObject(neuron[i]);
      };
      setNeuralNodes(neuralNodes);

      // Get activation function:
      String afname = neurons.getActivationFunction();
      if (afname != null) {
        String aftype = ActivationFunction.convertPmmlToType(afname);
        if (afname == null)
          aftype = afname;   // try even if name is unknown in PMML standard
        ActivationFunction af = ActivationFunction.getInstance(aftype);
        if (af != null)
          setActivationFunction(af);
      }
    }
    else
    if (pmmlObject instanceof com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutputs) {

      this.layerType = NEURAL_OUTPUT;
      com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutputs outputs =
        (com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutputs) pmmlObject;

      // Get number of output neurons:
      com.prudsys.pdm.Adapters.PmmlVersion20.NeuralOutput[] output = outputs.getNeuralOutput();
      if (output == null || output.length == 0)
        throw new MiningException("no output neurons");
      int nout = output.length;

      // Get array of output neurons:
      neuralNodes = new NeuralOutput[nout];
      for (int i = 0; i < nout; i++) {
        neuralNodes[i] = new NeuralOutput();
        neuralNodes[i].parsePmmlObject(output[i]);

        // In PMML neural outputs have no ID, so we must create it:
        String oId = "_____o" + String.valueOf(i);
        neuralNodes[i].setId(oId);
      };
      setNeuralNodes(neuralNodes);
    }
    else
      throw new MiningException("unknown layer type");
  }

  // -----------------------------------------------------------------------
  //  Further methods
  // -----------------------------------------------------------------------
  /**
   * Delivers string representation of neural layer.
   *
   * @return string representation of neural layer
   */
  public String toString() {

    String s = "->Neural Layer, ";
    if (layerType == NEURAL_INPUT)
      s = s + " INPUT, ";
    else if (layerType == NEURON)
      s = s + " NEURON, ";
    else if (layerType == NEURAL_OUTPUT)
      s = s + " OUTPUT, ";
    else
      s = s + " UNDEFINED, ";
    s = s + "number of nodes: " + getNumberOfNodes() + "\n";
    for (int i = 0; i < getNumberOfNodes(); i++)
      s = s + "node " + i + " - " + neuralNodes[i].toString() + "\n";

    return s;
  }
}

⌨️ 快捷键说明

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