创建神经网络对象
本主题是神经网络设计的工作流中所述的设计工作流的一部分。
创建神经网络的最简单方法是使用一个网络创建函数。为了研究如何做到这一点,您可以使用命令 feedforwardnet
创建一个简单的两层前馈网络:
net = feedforwardnet
net = Neural Network name: 'Feed-Forward Neural Network' userdata: (your custom info) dimensions: numInputs: 1 numLayers: 2 numOutputs: 1 numInputDelays: 0 numLayerDelays: 0 numFeedbackDelays: 0 numWeightElements: 10 sampleTime: 1 connections: biasConnect: [1; 1] inputConnect: [1; 0] layerConnect: [0 0; 1 0] outputConnect: [0 1] subobjects: inputs: {1x1 cell array of 1 input} layers: {2x1 cell array of 2 layers} outputs: {1x2 cell array of 1 output} biases: {2x1 cell array of 2 biases} inputWeights: {2x1 cell array of 1 weight} layerWeights: {2x2 cell array of 1 weight} functions: adaptFcn: 'adaptwb' adaptParam: (none) derivFcn: 'defaultderiv' divideFcn: 'dividerand' divideParam: .trainRatio, .valRatio, .testRatio divideMode: 'sample' initFcn: 'initlay' performFcn: 'mse' performParam: .regularization, .normalization plotFcns: {'plotperform', plottrainstate, ploterrhist, plotregression} plotParams: {1x4 cell array of 4 params} trainFcn: 'trainlm' trainParam: .showWindow, .showCommandLine, .show, .epochs, .time, .goal, .min_grad, .max_fail, .mu, .mu_dec, .mu_inc, .mu_max weight and bias values: IW: {2x1 cell} containing 1 input weight matrix LW: {2x2 cell} containing 1 layer weight matrix b: {2x1 cell} containing 2 bias vectors methods: adapt: Learn while in continuous use configure: Configure inputs & outputs gensim: Generate Simulink model init: Initialize weights & biases perform: Calculate performance sim: Evaluate network outputs given inputs train: Train network with examples view: View diagram unconfigure: Unconfigure inputs & outputs evaluate: outputs = net(inputs)
此处显示的内容简要说明了网络对象,它用于存储定义神经网络的所有信息。此处有很多细节,但有几个关键部分可以帮助您了解网络对象是如何组织的。
维度部分存储网络的整体结构。在此处,您可以看到一个网络输入(尽管一个输入可以是包含许多元素的向量)、一个网络输出和两个层。
连接部分存储网络组件之间的连接。例如,每一层都连接了一个偏置,输入连接到层 1,输出来自层 2。您还可以看到层 1 连接到层 2。(net.layerConnect
的行表示目标层,列表示源层。此矩阵中的 1 表示有连接,0 表示无连接。对于此示例,矩阵的元素 2,1 处有一个 1。)
网络对象的关键子对象包括 inputs
、layers
、outputs
、biases
、inputWeights
和 layerWeights
。使用以下命令查看第一层的 layers
子对象
net.layers{1}
Neural Network Layer name: 'Hidden' dimensions: 10 distanceFcn: (none) distanceParam: (none) distances: [] initFcn: 'initnw' netInputFcn: 'netsum' netInputParam: (none) positions: [] range: [10x2 double] size: 10 topologyFcn: (none) transferFcn: 'tansig' transferParam: (none) userdata: (your custom info)
一个层中神经元的数量由其 size
属性给出。在本例中,该层有 10 个神经元,这是 feedforwardnet
命令的默认大小。net 输入函数是 netsum
(求和),传递函数是 tansig
。例如,如果您要将传递函数更改为 logsig
,可以执行以下命令:
net.layers{1}.transferFcn = 'logsig';
要查看 layerWeights
子对象以了解层 1 和层 2 之间的权重,请使用以下命令:
net.layerWeights{2,1}
Neural Network Weight delays: 0 initFcn: (none) initConfig: .inputSize learn: true learnFcn: 'learngdm' learnParam: .lr, .mc size: [0 10] weightFcn: 'dotprod' weightParam: (none) userdata: (your custom info)
权重函数是 dotprod
,它表示标准矩阵乘法(点积)。请注意,该层权重的大小为 0×10。我们有全部为 0 的行,是因为网络尚未针对特定数据集进行配置。输出神经元的数量等于目标向量中的行数。在配置过程中,您将为网络提供示例输入和目标,然后可以分配输出神经元的数量。
这可以让您大致了解网络对象是如何组织的。对于许多应用,您不需要直接对网络对象进行更改,因为这由网络创建函数负责。通常仅当您要覆盖系统默认值时,才需要直接访问网络对象。其他主题将说明如何针对特定网络和训练方法做到这一点。
要更详细地研究网络对象,您可能会发现对象列表(如上所示)包含每个子对象的帮助链接。点击链接,您可以有选择地调查对象中您感兴趣的部分。