Thread Subject: Code optimization

Subject: Code optimization

From: Martin

Date: 23 Aug, 2007 10:15:38

Message: 1 of 6

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 );

Subject: Code optimization

From: Titus

Date: 23 Aug, 2007 13:24:57

Message: 2 of 6


"Martin " <macin@o2.pl> schrieb im Newsbeitrag
news: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 );

Hi Martin,
it would probably make the help somewhat easier to give, if you could give
some sample call to your function...

Titus


Subject: Code optimization

From: First Last

Date: 23 Aug, 2007 13:42:51

Message: 3 of 6

I am also not entirely sure how the code is supposed to
work. What's the goal of this function, and what are the
types for the inputs, specifically -- i.e. is 'in' a vector
and is 'vw' really a type 'string' or 'char', used as a
function as indicated below, or is it something else?

and then, what is the function vw ...

etc.


"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 );

Subject: Code optimization

From: Gautam Vallabha

Date: 23 Aug, 2007 15:00:05

Message: 4 of 6

In article <fajmoa$3nv$1@fred.mathworks.com>, macin@o2.pl says...
> 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 );

Your data structures are badly chosen. You have all your weights as one
long vector, and all your units as another long vector, so you end up
having to step along these vectors yourself. Better to explicitly have
the weight vector be a matrix. Then your entire function is simply a
couple of matrix multiplies, and also (this is a real time saver) you
reduce your calls to EXP. Furthermore, the code is also more readable
and works for multiple output units.

----------------------------------------
function output = netoutput(Whi, Woh, in)
% Whi = m x n weight matrix,
% m = # of hidden units, n = # of input units
% Woh = k x m weight matrix
% k = # of output units, m = # of hidden units

 % hidden unit outputs
 hnetinput = sum(Whi * in(:), 2);
 tmp = exp( 2 * hnetinput );
 houtput = (tmp - 1) ./ (tmp + 1);
 
 % final network outputs
 netinput = sum(Woh * houtput(:), 2);
 tmp = exp( 2 * netinput );
 output = (tmp - 1) ./ (tmp + 1);
----------------------------------------

You can profile the above code to make it tighter. Also, google for
"Matlab exp faster", and see if you can get a faster implementation of
EXP.

--
Gautam Vallabha
The MathWorks
Gautam.Vallabha@mathworks.com

Subject: Code optimization

From: Martin

Date: 23 Aug, 2007 15:05:56

Message: 5 of 6

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 );

Subject: Code optimization

From: Martin

Date: 24 Aug, 2007 03:59:11

Message: 6 of 6

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 );
>

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com