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

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

问题描述

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

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可以做到.

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-双曲正切S形传递函数,MATLAB文档中心

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

附加说明

看看更多,Sangeun Chi的答案.

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

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