On Sat, Jul 20, 2013 at 5:22 PM, <pablobarhamal...@gmail.com> wrote: > Ok, I'm working on a predator/prey simulation, which evolve using genetic > algorithms. At the moment, they use a quite simple feed-forward neural > network, which can change size over time. Each brain "tick" is performed by > the following function (inside the Brain class): > > def tick(self): > input_num = self.input_num > hidden_num = self.hidden_num > output_num = self.output_num > > hidden = [0]*hidden_num > output = [0]*output_num > > inputs = self.input > h_weight = self.h_weight > o_weight = self.o_weight > > e = math.e > > count = -1 > for x in range(hidden_num): > temp = 0 > for y in range(input_num): > count += 1 > temp += inputs[y] * h_weight[count] > hidden[x] = 1/(1+e**(-temp)) > > count = -1 > for x in range(output_num): > temp = 0 > for y in range(hidden_num): > count += 1 > temp += hidden[y] * o_weight[count] > output[x] = 1/(1+e**(-temp)) > > self.output = output > > The function is actually quite fast (~0.040 seconds per 200 calls, using > 10 input, 20 hidden and 3 output neurons), and used to be much slower > untill I fiddled about with it a bit to make it faster. However, it is > still somewhat slow for what I need it. > > My question to you is if you an see any obvious (or not so obvious) way of > making this faster. I've heard about numpy and have been reading about it, > but I really can't see how it could be implemented here. > > Cheers! > -- > http://mail.python.org/mailman/listinfo/python-list >
Low level optimizations: If you're in Python 2.x (and not 3), you should use xrange() instead of range(), or maybe even create a local variable and increment it and check its value within a while (that way you can save a few instructions on method invocations from xrange/range). Anyways, if that's not fast enough, just port it to c/c++ (or one of the alternatives to speed it up while still in python: numba, cython, shedskin). Or (if you can), try to use PyPy and see if you get more speed without doing anything. Cheers, Fabio
-- http://mail.python.org/mailman/listinfo/python-list