473,508 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

algorithm, optimization, or other problem?

Hello,

I am trying to translate some Matlab/mex code to Python, for doing neural
simulations. This application is definitely computing-time limited, and I need to
optimize at least one inner loop of the code, or perhaps even rethink the algorithm.
The procedure is very simple, after initializing any variables:

1) select a random input vector, which I will call "x". right now I have it as an
array, and I choose columns from that array randomly. in other cases, I may need to
take an image, select a patch, and then make that a column vector.

2) calculate an output value, which is the dot product of the "x" and a weight
vector, "w", so

y=dot(x,w)

3) modify the weight vector based on a matrix equation, like:

w=w+ eta * (y*x - y**2*w)
^
|
+---- learning rate constant

4) repeat steps 1-3 many times

I've organized it like:

for e in 100: # outer loop
for i in 1000: # inner loop
(steps 1-3)

display things.

so that the bulk of the computation is in the inner loop, and is amenable to
converting to a faster language. This is my issue:

straight python, in the example posted below for 250000 inner-loop steps, takes 20
seconds for each outer-loop step. I tried Pyrex, which should work very fast on such
a problem, takes about 8.5 seconds per outer-loop step. The same code as a C-mex
file in matlab takes 1.5 seconds per outer-loop step.

Given the huge difference between the Pyrex and the Mex, I feel that there is
something I am doing wrong, because the C-code for both should run comparably.
Perhaps the approach is wrong? I'm willing to take any suggestions! I don't mind
coding some in C, but the Python API seemed a bit challenging to me.

One note: I am using the Numeric package, not numpy, only because I want to be able
to use the Enthought version for Windows. I develop on Linux, and haven't had a
chance to see if I can compile numpy using the Enthought Python for Windows.

If there is anything else anyone needs to know, I'll post it. I put the main script,
and a dohebb.pyx code below.
thanks!

Brian Blais

--
-----------------

bb****@bryant.edu
http://web.bryant.edu/~bblais


# Main script:

from dohebb import *
import pylab as p
from Numeric import *
from RandomArray import *
import time

x=random((100,1000)) # 1000 input vectors

numpats=x.shape[0]
w=random((numpats,1));

th=random((1,1))

params={}
params['eta']=0.001;
params['tau']=100.0;
old_mx=0;
for e in range(100):

rnd=randint(0,numpats,250000)
t1=time.time()
if 0: # straight python
for i in range(len(rnd)):
pat=rnd[i]
xx=reshape(x[:,pat],(1,-1))
y=matrixmultiply(xx,w)
w=w+params['eta']*(y*transpose(xx)-y**2*w);
th=th+(1.0/params['tau'])*(y**2-th);
else: # pyrex
dohebb(params,w,th,x,rnd)
print time.time()-t1
p.plot(w,'o-')
p.xlabel('weights')
p.show()
#=============================================

# dohebb.pyx

cdef extern from "Numeric/arrayobject.h":

struct PyArray_Descr:
int type_num, elsize
char type

ctypedef class Numeric.ArrayType [object PyArrayObject]:
cdef char *data
cdef int nd
cdef int *dimensions, *strides
cdef object base
cdef PyArray_Descr *descr
cdef int flags
def dohebb(params,ArrayType w,ArrayType th,ArrayType X,ArrayType rnd):
cdef int num_iterations
cdef int num_inputs
cdef int offset
cdef double *wp,*xp,*thp
cdef int *rndp
cdef double eta,tau

eta=params['eta'] # learning rate
tau=params['tau'] # used for variance estimate

cdef double y
num_iterations=rnd.dimensions[0]
num_inputs=w.dimensions[0]

# get the pointers
wp=<double *>w.data
xp=<double *>X.data
rndp=<int *>rnd.data
thp=<double *>th.data

for it from 0 <= it < num_iterations:

offset=rndp[it]*num_inputs

# calculate the output
y=0.0
for i from 0 <= i < num_inputs:
y=y+wp[i]*xp[i+offset]

# change in the weights
for i from 0 <= i < num_inputs:
wp[i]=wp[i]+eta*(y*xp[i+offset] - y*y*wp[i])

# estimate the variance
thp[0]=thp[0]+(1.0/tau)*(y**2-thp[0])

Feb 21 '06 #1
2 1371
Brian Blais wrote:
Hello,

I am trying to translate some Matlab/mex code to Python, for doing neural
simulations. This application is definitely computing-time limited, and I need to
optimize at least one inner loop of the code, or perhaps even rethink the algorithm.
The procedure is very simple, after initializing any variables:

1) select a random input vector, which I will call "x". right now I have it as an
array, and I choose columns from that array randomly. in other cases, I may need to
take an image, select a patch, and then make that a column vector.

2) calculate an output value, which is the dot product of the "x" and a weight
vector, "w", so

y=dot(x,w)

3) modify the weight vector based on a matrix equation, like:

w=w+ eta * (y*x - y**2*w)
^
|
+---- learning rate constant

4) repeat steps 1-3 many times
Brian

ETA = 0.001
INV_TAU = 1.0/100.0

rather than:
params={}
params['eta']=0.001;
params['tau']=100.0;
ie. take the dictionary lookup (and the repeated division) out?

If you have a collection of random vectors do you gain anything by
*choosing* them randomly?

I'm not exactly sure if it's equivalent to your existing code, but how
about:

x=random((100,250000))
w=random((100,1));
th=random((1,1))
for vector in x:
...
w=w+ ETA * (y*vector - y**2*w)
th = th + INV_TAU *(y**2-th)
...

? (I'm not familar with Numeric)

Gerard
old_mx=0;
for e in range(100):

rnd=randint(0,numpats,250000)
t1=time.time()
if 0: # straight python
for i in range(len(rnd)):
pat=rnd[i]
xx=reshape(x[:,pat],(1,-1))
y=matrixmultiply(xx,w)
w=w+params['eta']*(y*transpose(xx)-y**2*w);
th=th+(1.0/params['tau'])*(y**2-th);
else: # pyrex
dohebb(params,w,th,x,rnd)
print time.time()-t1


Feb 21 '06 #2

Gerard Flanagan wrote:
Brian Blais wrote:
Hello,

I am trying to translate some Matlab/mex code to Python, for doing neural
simulations. This application is definitely computing-time limited, and I need to
optimize at least one inner loop of the code, or perhaps even rethink the algorithm.
The procedure is very simple, after initializing any variables:

1) select a random input vector, which I will call "x". right now I have it as an
array, and I choose columns from that array randomly. in other cases, I may need to
take an image, select a patch, and then make that a column vector.

2) calculate an output value, which is the dot product of the "x" and a weight
vector, "w", so

y=dot(x,w)

3) modify the weight vector based on a matrix equation, like:

w=w+ eta * (y*x - y**2*w)
^
|
+---- learning rate constant

4) repeat steps 1-3 many times
Brian

ETA = 0.001
INV_TAU = 1.0/100.0

rather than:
params={}
params['eta']=0.001;
params['tau']=100.0;


or:

ETA = params['eta']
INV_TAU = 1.0/params['tau']
Gerard
ie. take the dictionary lookup (and the repeated division) out?

If you have a collection of random vectors do you gain anything by
*choosing* them randomly?

I'm not exactly sure if it's equivalent to your existing code, but how
about:

x=random((100,250000))
w=random((100,1));
th=random((1,1))
for vector in x:
...
w=w+ ETA * (y*vector - y**2*w)
th = th + INV_TAU *(y**2-th)
...

? (I'm not familar with Numeric)

Gerard
old_mx=0;
for e in range(100):

rnd=randint(0,numpats,250000)
t1=time.time()
if 0: # straight python
for i in range(len(rnd)):
pat=rnd[i]
xx=reshape(x[:,pat],(1,-1))
y=matrixmultiply(xx,w)
w=w+params['eta']*(y*transpose(xx)-y**2*w);
th=th+(1.0/params['tau'])*(y**2-th);
else: # pyrex
dohebb(params,w,th,x,rnd)
print time.time()-t1


Feb 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1148
by: Seb... | last post by:
Hi, I m looking for an algorithm for an optimization. I know there are a lot of existing algoritm. Here is my problem : I have serveral ring to do, defined by external and internal...
3
3310
by: PWalker | last post by:
Hi, I have written code that I would like to optimize. I need to push it to the limit interms of speed as the accuracy of results are proportional to runtime. First off, would anyone know any...
32
76373
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
28
3126
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
21
2552
by: mjbackues at yahoo | last post by:
Hello. I'm having a problem with the Visual Studio .net (2003) C++ speed optimization, and hope someone can suggest a workaround. My project includes many C++ files, most of which work fine...
17
6499
by: silverburgh.meryl | last post by:
In STL, there is no copy_if algorithm. But how can I emulate that? I am thinking about using find_if and then a copy. But how can I pass the InputIterator (begin and end) to copy so that it knows...
51
8566
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
4
2620
by: sklett | last post by:
I realize this could be a little off-topic, but there are some great minds on this NG and I hope you can let me slide this time ;0) I'm designing our system to manage what products can fit in...
6
1795
by: baudolino | last post by:
I have a map<string, time_t>; string represents a filesystem path, time_t is a timestamp associated with that path. My problem is to find the best version of a directory and its subdirectories,...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.