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)
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
net += temp(topology.value(i) - 1) * weights.value(i)
Next
temp.Add(ActivationFunction(n, net))
Next
For k As Integer = 1 To No
result += Math.Pow(output.value(p, 1) - temp(Nn + Ni - No + 1 - 1), 2)
Next
Next
Return result
End Function
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;
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
Select Case activations.value(n)
Case 0
Return gain.value(n) * net
Case 1
Return 1.0 / (1.0 + System.Math.Exp(-1.0 * gain.value(n) * net))
Case 2
Return 2.0 / (1.0 + System.Math.Exp(-1.0 * gain.value(n) * net)) - 1.0
Case 3
Return (gain.value(n) * net) / (1.0 + gain.value(n) * System.Math.Abs(net))
Case 4
Return (2.0 * gain.value(n) * net) / (1.0 + gain.value(n) * System.Math.Abs(net)) - 1
End Select
End Function
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.