|
Thanks for replies to everyone. I have sorted the problem
out. Prepared mex file in c to calculate above. Works at
least 20 times faster than using matlab only. Here it is,
just in case anyone will need it in future or if anyone can
spot any bugs:
#include "mex.h"
#include "math.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const
mxArray *prhs[] )
{
double *vw, *in;
double *y;
double *out;
double *n;
double tmp;
int sIn, sVw, i, m, z, sm;
int k = 0;
int j = 0;
double yy[1000];
vw = mxGetPr(prhs[0]);
in = mxGetPr(prhs[1]);
n = mxGetPr(prhs[2]);
sVw = mxGetN(prhs[0]);
sIn = mxGetN(prhs[1]);
sm = sIn + (int)n;
y = yy;
for (z = 0; z < sIn; z++)
{
y[z] = in[z];
}
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
out = mxGetPr(plhs[0]);
for (i = sIn; i < sIn + n[0]; i++ ){
tmp = 0;
for (m = 0; m < i; m++ ){
tmp = tmp + vw[ m + k ] * y[ m ];
}
k = k+i;
tmp = exp( 2 * tmp );
tmp = ( tmp - 1 )/( tmp + 1 );
y[i] = tmp;
j = j + 1;
}
out[0] = y[i-1];
}
"Martin " <macin@o2.pl> wrote in message
<fak7ok$hij$1@fred.mathworks.com>...
> Ok, here are more details:
>
> The function is supposed to calculate an output of neural
> network. vw is a vector of weights which is produced by EP
> (Evolutionary Programming algorithm).
> Neural network I have designed is represented in a matrix
> form (figure 3.3 and 3.4 in pdf file here:
> http://radlak.com/martin.pdf)
> As it is only Feed-Forward NN, upper triangle is considered
> thus redundant zeros are omitted when converting this matrix
> into a string vw. Inputs are also ommited as they are not
> connected between each other (according to figure 3.3
> matrix[1:4,1:4]). The string is constructed in the following
> way:
>
> vw = [matrix[1:5,5] matrix[1:6,6] matrix[1:7,7] ... ] and
so on.
>
> Going further, to calculate the output of this NN, value for
> each neuron has to be calculated, i.e.
>
> output of neuron 5, y5, from figure 3.3 is
>
> y5 = sum( matrix[1:4,5] * [y1 y2 y3 y4])
>
> where y1, y2, y3, y4 are outputs of neurons connected to
> neuron y5.
>
> Finally, by applying squashing function (tangent sigmoid in
> this example) output is produced.
>
> If this description is not fully clear I am very happy to
> make it brighter - just ask, please.
>
> Thank you for your time,
> Martin
>
>
>
> "Martin " <macin@o2.pl> wrote in message
> <fajmoa$3nv$1@fred.mathworks.com>...
> > Dear All,
> >
> > I am struggling with my algorithm to work fast enough. Is
> > there any possibility to make this work faster?
> > Thank you very much for any advice:
> >
> > function o = netoutput( vw, in, n )
> > % n - number of hidden neurons
> > % vw - weights string
> > % in - input neurons values
> >
> > sIn = max( size( in, 2 ) ); % number of input neurons
> >
> > Y = in';
> > k = 1;
> > j = 1;
> >
> > for i = sIn : sIn + n - 1
> >
> > tmp = sum(vw( k : k + i -1 )'.*Y );
> > k = k + i;
> >
> > tmp = exp( 2 * tmp );
> > tmp = ( tmp - 1 )./ ( tmp + 1 );
> >
> > Y( i+1, 1 ) = tmp;
> > j = j + 1;
> > end
> >
> > o = Y( end, 1 );
>
|