导出用 MATLAB 用其他编程语言训练的神经网络 [英] Export a neural network trained with MATLAB in other programming languages

查看:65
本文介绍了导出用 MATLAB 用其他编程语言训练的神经网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MATLAB 神经网络工具箱训练了一个神经网络,特别是使用命令 nprtool,它提供了一个简单的 GUI 来使用工具箱功能,并导出一个 net 包含生成的 NN 信息的对象.

I trained a neural network using the MATLAB Neural Network Toolbox, and in particular using the command nprtool, which provides a simple GUI to use the toolbox features, and to export a net object containing the informations about the NN generated.

通过这种方式,我创建了一个可以用作分类器的工作神经网络,代表它的图表如下:

In this way, I created a working neural network, that I can use as classifier, and a diagram representing it is the following:

有 200 个输入,第一个隐藏层有 20 个神经元,最后一层有 2 个神经元,提供二维输出.

There are 200 inputs, 20 neurons in the first hidden layer, and 2 neurons in the last layer that provide a bidimensional output.

我想做的是在其他一些编程语言(C#、Java 等)中使用网络.

What I want to do is to use the network in some other programming language (C#, Java, ...).

为了解决这个问题,我尝试在MATLAB中使用如下代码:

In order to solve this problem, I try to use the following code in MATLAB:

y1 = tansig(net.IW{1} * input + net.b{1});
Results = tansig(net.LW{2} * y1 + net.b{2});

假设 input 是一个包含 200 个元素的单维数组,如果 net.IW{1} 是一个 20x200 矩阵(20 个神经元,200 个权重).

Assuming that input is a monodimensional array of 200 elements, the previous code would work if net.IW{1} is a 20x200 matrix (20 neurons, 200 weights).

问题是我注意到 size(net.IW{1}) 返回意外值:

The problem is that I noticed that size(net.IW{1}) returns unexpected values:

>> size(net.IW{1})

    ans =

    20   199

我在输入为 10000 的网络时遇到了同样的问题.在这种情况下,结果不是 20x10000,而是类似于 20x9384(我不记得确切的值).

I got the same problem with a network with 10000 input. In this case, the result wasn't 20x10000, but something like 20x9384 (I don't remember the exact value).

那么,问题是:如何获得每个神经元的权重?在那之后,有人能解释一下我如何使用它们来产生与 MATLAB 相同的输出吗?

So, the question is: how can I obtain the weights of each neuron? And after that, can someone explain me how can I use them to produce the same output of MATLAB?

推荐答案

我解决了上述问题,我认为分享我所学到的东西很有用.

I solved the problems described above, and I think it is useful to share what I've learned.

场地

首先,我们需要一些定义.让我们考虑下图,取自 [1]:

First of all, we need some definitions. Let's consider the following image, taken from [1]:

在上图中,IW代表初始权重:它们代表第1层神经元的权重,每个权重为与每个输入相连,如下图所示[1]:

In the above figure, IW stands for initial weights: they represent the weights of neurons on the Layer 1, each of which is connected with each input, as the following image shows [1]:

所有其他权重,称为层权重(第一张图中的LW),它们也与前一层的每个输出相连.在我们的研究案例中,我们使用只有两层的网络,因此我们将只使用一个 LW 阵列来解决我们的问题.

All the other weights, are called layer weights (LW in the first figure), that are also connected with each output of the previous layer. In our case of study, we use a network with only two layers, so we will use only one LW array to solve our problems.

问题的解决方法

经过上面的介绍,我们可以将问题分成两步进行:

After the above introduction, we can proceed by dividing the issue in two steps:

  • 强制初始权重的数量与输入数组长度匹配
  • 使用权重来实现和使用刚刚用其他编程语言训练的神经网络

A - 强制初始权重的数量与输入数组长度匹配

使用nprtool,我们可以训练我们的网络,并且在过程结束时,我们还可以在工作区中导出有关整个训练过程的一些信息.特别是,我们需要导出:

Using the nprtool, we can train our network, and at the end of the process, we can also export in the workspace some information about the entire training process. In particular, we need to export:

  • 代表创建的神经网络的 MATLAB 网络对象
  • 用于训练网络的输入数组
  • 用于训练网络的目标数组

此外,我们需要生成一个 M 文件,其中包含 MATLAB 用于创建神经网络的代码,因为我们需要修改它并更改一些训​​练选项.

Also, we need to generate a M-file that contains the code used by MATLAB to create the neural network, because we need to modify it and change some training options.

下图显示了如何执行这些操作:

The following image shows how to perform these operations:

生成的 M 代码将类似于以下代码:

The M-code generated will be similar to the following one:

function net = create_pr_net(inputs,targets)
%CREATE_PR_NET Creates and trains a pattern recognition neural network.
%
%  NET = CREATE_PR_NET(INPUTS,TARGETS) takes these arguments:
%    INPUTS - RxQ matrix of Q R-element input samples
%    TARGETS - SxQ matrix of Q S-element associated target samples, where
%      each column contains a single 1, with all other elements set to 0.
%  and returns these results:
%    NET - The trained neural network
%
%  For example, to solve the Iris dataset problem with this function:
%
%    load iris_dataset
%    net = create_pr_net(irisInputs,irisTargets);
%    irisOutputs = sim(net,irisInputs);
%
%  To reproduce the results you obtained in NPRTOOL:
%
%    net = create_pr_net(trainingSetInput,trainingSetOutput);

% Create Network
numHiddenNeurons = 20;  % Adjust as desired
net = newpr(inputs,targets,numHiddenNeurons);
net.divideParam.trainRatio = 75/100;  % Adjust as desired
net.divideParam.valRatio = 15/100;  % Adjust as desired
net.divideParam.testRatio = 10/100;  % Adjust as desired

% Train and Apply Network
[net,tr] = train(net,inputs,targets);
outputs = sim(net,inputs);

% Plot
plotperf(tr)
plotconfusion(targets,outputs)

在开始训练过程之前,我们需要移除 MATLAB 对输入和输出执行的所有预处理和后处理函数.这可以通过在 % Train and Apply Network 行之前添加以下几行来完成:

Before start the training process, we need to remove all preprocessing and postprocessing functions that MATLAB executes on inputs and outputs. This can be done adding the following lines just before the % Train and Apply Network lines:

net.inputs{1}.processFcns = {};
net.outputs{2}.processFcns = {};

在对 create_pr_net() 函数进行这些更改之后,我们可以简单地使用它来创建我们最终的神经网络:

After these changes to the create_pr_net() function, simply we can use it to create our final neural network:

net = create_pr_net(input, target);

其中inputtarget是我们通过nprtool导出的值.

where input and target are the values we exported through nprtool.

这样,我们确定权重的数量等于输入数组的长度.此外,此过程对于简化向其他编程语言的移植非常有用.

In this way, we are sure that the number of weights is equal to the length of input array. Also, this process is useful in order to simplify the porting to other programming languages.

B - 实现和使用刚刚用其他编程语言训练的神经网络

通过这些更改,我们可以定义这样的函数:

With these changes, we can define a function like this:

function [ Results ] = classify( net, input )
    y1 = tansig(net.IW{1} * input + net.b{1});

    Results = tansig(net.LW{2} * y1 + net.b{2});
end

在这段代码中,我们使用了上面提到的 IW 和 LW 数组,还有 biases b,由 nprtool 在网络模式中使用.在这种情况下,我们不关心偏见的作用;简单地说,我们需要使用它们,因为 nprtool 做到了.

In this code, we use the IW and LW arrays mentioned above, but also the biases b, used in the network schema by the nprtool. In this context, we don't care about the role of biases; simply, we need to use them because nprtool does it.

现在,我们可以使用上面定义的classify()函数,或者同样使用sim()函数,得到相同的结果,如下例所示:

Now, we can use the classify() function defined above, or the sim() function equally, obtaining the same results, as shown in the following example:

>> sim(net, input(:, 1))

ans =

    0.9759
   -0.1867
   -0.1891

>> classify(net, input(:, 1))

ans =

   0.9759   
  -0.1867
  -0.1891

显然,classify() 函数可以解释为伪代码,然后在任何可以定义 MATLAB tansig() 函数的编程语言中实现[2] 以及数组之间的基本操作.

Obviously, the classify() function can be interpreted as a pseudocode, and then implemented in every programming languages in which is possibile to define the MATLAB tansig() function [2] and the basic operations between arrays.

参考资料

[1] Howard Demuth、Mark Beale、Martin Hagan:神经网络工具箱 6 - 用户指南,MATLAB

[1] Howard Demuth, Mark Beale, Martin Hagan: Neural Network Toolbox 6 - User Guide, MATLAB

[2] Mathworks, tansig - 双曲正切 sigmoid传递函数,MATLAB 文档中心

[2] Mathworks, tansig - Hyperbolic tangent sigmoid transfer function, MATLAB Documentation center

附加说明

看看robott 的答案Sangeun Chi 的回答 了解更多详情.

Take a look to the robott's answer and the Sangeun Chi's answer for more details.

这篇关于导出用 MATLAB 用其他编程语言训练的神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆