在vb .net 3.5中,相同的算法比在matlab中运行慢得多 [英] Same algorithm runs much slower in vb .net 3.5 than in matlab

查看:74
本文介绍了在vb .net 3.5中,相同的算法比在matlab中运行慢得多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试在vb .net中实现神经网络训练算法.我已经找到了该算法的开源matlab代码,因此基本上我只是将matlab代码转换为vb并添加了一些UI.

现在,我已经完成了工作(如果测试了相同的数据,它们将获得相同的结果),但是该程序在vb中的运行速度比在matlab中要慢得多,大约是30倍!我所做的只是运行一个小功能并比较运行时间.

以下是函数(VB .NET)的代码:

Hi guys,

I am trying to implement a neural network training algorithm in vb .net. I have found a open-source matlab code of this algorithm, so basically I am simply converting the matlab code to vb and adding some UIs.

Right now I have already finished the job (they are getting the same results if the same data was tested), but the program runs much much slower in vb than in matlab, like about 30 times! What I did is just to run a small function and to compare the running time.

Here are the codes of the function (VB .NET):

Public Function CalculateError(ByVal input As Matrix, ByVal output As Matrix) As Double
        Dim result As Double
        result = 0
        Dim Nm, Nn, Ni, No As Integer
        Dim net As Double
        Nm = input.mRow
        Nn = CInt(parameters.value(2))
        Ni = CInt(parameters.value(4))
        No = CInt(parameters.value(5))

        For p As Integer = 1 To Nm
            Dim temp = New List(Of Double)
            'compute the first Ni entries of temp
            For i As Integer = 1 To Ni
                temp.Add(input.value(p, i))
            Next

            For n As Integer = 1 To Nn
                net = weights.value(iw.value(n))
                For i As Integer = iw.value(n) + 1 To iw.value(n + 1) - 1
                    'Make sure topology is a matrix with integer entries only
                    net += temp(topology.value(i) - 1) * weights.value(i)
                Next

                temp.Add(ActivationFunction(n, net))
                'temp.Add(0.0)
            Next



            For k As Integer = 1 To No
                result += Math.Pow(output.value(p, 1) - temp(Nn + Ni - No + 1 - 1), 2)
            Next

        Next


        ' SSE
        Return result
    End Function



和matlab:



and matlab:

function [err] = calculate_error(input,output, topo, weight,param, iw, gain, act)
err = 0;
for p = 1:size(input, 1)     % number of patterns
   temp(1:param(4)) = input(p,1:param(4));
   for n = 1:param(2) % number of neurons
      j = param(4) + n;
      net = weight(iw(n));
      for i = (iw(n)+1):(iw(n+1)-1)
         net = net + temp(topo(i))*weight(i);
      end;
      tic
      out=actFunc(n,net,act,gain);
      toc
      temp(j) = out;
   end;
   for k = 1:param(5)
      err = err + (output(p,k)-temp(param(2)+param(4)-param(5)+k))^2;                % calculate total error
   end;
end;



Matrix是我定义的一个自定义类,它是使用Array实现的; ActivationFunction是另一个简单的函数(我还将附加代码);和其他变量(例如iw,权重)是一些内部变量,我认为它们对运行时间的影响并不大.

ActivationFunction的代码为:



The Matrix is a custom class I defined and it is implemented using Array; ActivationFunction is another simple function (I will attach the code also); and other variables like iw, weights are some internal variables, I don''t think they really affect the runnning time much.

The code for ActivationFunction is:

Public Function ActivationFunction(ByVal n As Integer, ByVal net As Double) As Double
        'Dim result As Double
        Select Case activations.value(n)
            Case 0
                Return gain.value(n) * net           'linear neuron
            Case 1
                Return 1.0 / (1.0 + System.Math.Exp(-1.0 * gain.value(n) * net))     'unipolar neuron
            Case 2
                Return 2.0 / (1.0 + System.Math.Exp(-1.0 * gain.value(n) * net)) - 1.0     'bipolar neuron
            Case 3
                Return (gain.value(n) * net) / (1.0 + gain.value(n) * System.Math.Abs(net))     'unipolar elliot neuron
            Case 4
                Return (2.0 * gain.value(n) * net) / (1.0 + gain.value(n) * System.Math.Abs(net)) - 1     'bipolar elliot neuron
        End Select
    End Function



我现在遇到的问题是我试图运行这两个CalculateError函数(一个在vb.net中,另一个在matlab中),vb代码需要大约50毫秒,而matlab代码则需要1.5毫秒!顺便说一句,即使对于ActivationFunction(甚至更简单),matlab的运行速度也要快得多.

任何人都可以帮助我解决这个问题,或者任何想法,为什么VB代码这么慢?

TIA.



The problem I have now is that I tried to run these two CalculateError functions (one in vb. net and other in matlab), the vb code needs like 50 milliseconds, while the matlab code runs in 1.5 millisecond! BTW, even for the ActivationFunction (even simpler), matlab runs much faster.

Anyone can help me with this or any idea that why the VB code is that slow?

TIA.

推荐答案

仅通过观察很难说算法的实现速度有多快,但是……我只能给出一个想法.

您是否对所有算法方法都运行了两次?你是怎么计时的?

事情是这样的:如果只调用某个耗时的方法一次,则可能计时的时间不是方法的执行时间,而是方法的编译时间.如果是这样,那么就不会有任何错误或坏处:第一次不算重要,因为-谁会在意大约50毫秒的时间,因为它们仅是第一次使用?在相同的过程中再次运行所有这些,然后再次调整性能.请与我们分享您的发现.

之所以会发生这种情况,是因为.NET是基于JIT(即时)编译的,通常它是按方法进行的:该方法是在首次调用之前进行编译的.请参阅:
http://en.wikipedia.org/wiki/Just-in-time_compilation [ ^ ].

-SA
It''s hard to say how fast is your implementation of the algorithm by just looking at it, but… I can give you just one idea.

Did you run all the algorithm methods twice? How did you time it?

Here is the thing: if you call some time-consuming method only once, it could be possible that you time not the method execution, but… its compilation. If so, there would not be anything wrong or bad about it: first time does not count, because — who would care much about some 50 milliseconds, it they are spent only for the first time? Run it all again in the same process and time the performance again. Please share with us your findings.

Such things can happen, because .NET is based in JIT (Just-In-Time) compilation, and usually it happens on per-method basis: the method is compiled just before it''s called for the very first time. Please see:
http://en.wikipedia.org/wiki/Just-in-time_compilation[^].

—SA


首先,在使用数组时不要使用List(Of Double).您正在创建一个已知大小的列表,该列表在循环期间不会更改大小,因此数组将更合适,更快捷.如果列表确实改变了大小,请尝试首先计算数组的最大大小,然后提前分配数组.

当输入值似乎从未改变时,您还将填充列表NM次.这意味着您只需要执行一次就可以一次又一次地执行完全相同的操作.
First, don''t use a List(Of Double) when an array will do. You''re creating a list of a known size that doesn''t change size during the loop, so an array will be more appropriate and faster. If the list does change size, try to compute the maxiumum size of the array first and allocate the array ahead of time.

You''re also populating the list NM times when it imput values never appear to change. This means you''re doing the exact same thing over and over again NM times when you only need to do it once.


实现中的变量使用二维数组.将它们更改为一维数组和锯齿状数组(微软建议)使函数运行更快.
Variables in my implementation use two-dimensional Arrays. Change them into one-dimensional Arrays and jagged Arrays (which microsoft recommends) makes the function run much faster.


这篇关于在vb .net 3.5中,相同的算法比在matlab中运行慢得多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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