求和函数被定义为接口,以便能够替换神经元的计算策略:
import java.util.List; import edu.neuralnet.core.Connection; /** * Represents the inputs summing part of a neuron also called signal collector. * 神经元的求和部分,也可以称为信号收集器 */ public interface InputSummingFunction { /** * Performs calculations based on the output values of the input neurons. * 根据输入神经元的输出值执行计算 * @param inputConnections * neuron's input connections * @return total input for the neuron having the input connections * 总输入,具有输入连接的神经元 */ double collectOutput(List<Connection> inputConnections); } |
分别实现为:
import java.util.List; import edu.neuralnet.core.Connection; /** * Calculates the weighted sums of the input neurons' outputs. * 计算输入神经元输出的加权和 */ public final class WeightedSumFunction implements InputSummingFunction { /** * {@inheritDoc} */ @Override public double collectOutput(List<Connection> inputConnections) { double weightedSum = 0d; for (Connection connection : inputConnections) { weightedSum += connection.getWeightedInput(); } return weightedSum; } } |
激活函数的接口可以定义如下::
/** * Neural networks activation function interface. * 神经网络激活函数的接口 */ public interface ActivationFunction { /** * Performs calculation based on the sum of input neurons output. * 基于输入神经元输出的和来进行计算 * @param summedInput * neuron's sum of outputs respectively inputs for the connected * neuron * * @return Output's calculation based on the sum of inputs * 基于输入和来计算输出 */ double calculateOutput(double summedInput); } |
开始编写代码之前需要注意的最后一个问题是神经网络层。神经网络由几个链接层组成,形成所谓的多层网络。神经层可以分为三类:
输入层
隐藏层
输出层
在实践中,额外的神经层增加了另一个抽象层次的外部刺激,增强了神经网络认知更复杂知识的能力。
一个图层类可以被定义为一个有连接的神经元列表:
import java.util.ArrayList; import java.util.List; /** * Neural networks can be composed of several linked layers, forming the * so-called multilayer networks. A layer can be defined as a set of neurons * comprising a single neural net's layer. * 神经网络可以由多个连接层组成,形成所谓的多层网络, * 一层可以定义为一组包含神经网络层的神经元 */ public class NeuralNetLayer { /** * Layer's identifier * 层次标识符 */ private String id; /** * Collection of neurons in this layer * 该层神经元的集合 */ protected List<Neuron> neurons; /** * Creates an empty layer with an id. * 用ID创建一个空层 * @param id * layer's identifier */ public NeuralNetLayer(String id) { this.id = id; neurons = new ArrayList<>(); } /** * Creates a layer with a list of neurons and an id. * 创建一个包含神经元列表和id的层 * @param id * layer's identifier 层次标识符 * @param neurons * list of neurons to be added to the layer 添加到该层的神经元列表 */ public NeuralNetLayer(String id, List<Neuron> neurons) { this.id = id; this.neurons = neurons; } ... } |
最后,用Java创建一个简单的神经网络:
/** * Represents an artificial neural network with layers containing neurons. * 含有神经元层的人工神经网络 */ public class NeuralNet { /** * Neural network id * 神经网络ID */ private String id; /** * Neural network input layer * 神经网络的输入层 */ private NeuralNetLayer inputLayer; /** * Neural network hidden layers * 神经网络隐藏的层 */ private List<NeuralNetLayer> hiddenLayers; /** * Neural network output layer * 神经网络的输出层 */ private NeuralNetLayer outputLayer; /** * Constructs a neural net with all layers present. * 构造一个具有所有层的神经网络 * @param id * Neural network id to be set 设置神经网络标识 * @param inputLayer * Neural network input layer to be set 设置神经网络的输入层 * @param hiddenLayers * Neural network hidden layers to be set 设置神经网络隐藏的层 * @param outputLayer * Neural network output layer to be set 设置神经网络的输出层 */ public NeuralNet(String id, NeuralNetLayer inputLayer, List<NeuralNetLayer> hiddenLayers, NeuralNetLayer outputLayer) { this.id = id; this.inputLayer = inputLayer; this.hiddenLayers = hiddenLayers; this.outputLayer = outputLayer; } /** * Constructs a neural net without hidden layers. * 构造一个没有隐藏层的神经网络 * @param id * Neural network id to be set 设置神经网络标识 * @param inputLayer * Neural network input layer to be set 设置神经网络的输入层 * @param outputLayer * Neural network output layer to be set 设置神经网络隐藏的层 */ public NeuralNet(String id, NeuralNetLayer inputLayer, NeuralNetLayer outputLayer) { this.id = id; this.inputLayer = inputLayer; this.outputLayer = outputLayer; } ... } |
我们所得到的是一个基于Java的神经网络层次、神经元和连接的结构定义。我们也谈到了一些关于激活函数的内容,并为它们定义了一个接口。为简单起见,我们省略了各种激活函数的实现以及学习神经网络的基础知识。这两个主题将在本系列的后续文章中介绍。
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。