Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50976
| From | Roy Smith <roy@panix.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: How can I make this piece of code even faster? |
| Date | 2013-07-20 17:25 -0400 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <roy-B4482C.17255220072013@70-1-84-166.pools.spcsdns.net> (permalink) |
| References | <6bf4d298-b425-4357-9c1a-192e6e6cd9f0@googlegroups.com> |
In article <6bf4d298-b425-4357-9c1a-192e6e6cd9f0@googlegroups.com>, pablobarhamalzas@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. First thing, I would add some instrumentation to see where the most time is being spent. My guess is in the first set of nested loops, where the inner loop gets executed hidden_num * input_num (i.e. 10 * 20 = 200) times. But timing data is better than my guess. Assuming I'm right, though, you do compute range(input_num) 20 times. You don't need to do that. You might try xrange(), or you might just factor out creating the list outside the outer loop. But, none of that seems like it should make much difference. What possible values can temp take? If it can only take certain discrete values and you can enumerate them beforehand, you might want to build a dict mapping temp -> 1/(1+e**(-temp)) and then all that math becomes just a table lookup.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How can I make this piece of code even faster? pablobarhamalzas@gmail.com - 2013-07-20 13:22 -0700
Re: How can I make this piece of code even faster? Fabio Zadrozny <fabiofz@gmail.com> - 2013-07-20 18:05 -0300
Re: How can I make this piece of code even faster? Roy Smith <roy@panix.com> - 2013-07-20 17:25 -0400
Re: How can I make this piece of code even faster? pablobarhamalzas@gmail.com - 2013-07-20 15:45 -0700
Re: How can I make this piece of code even faster? Chris Angelico <rosuav@gmail.com> - 2013-07-21 08:55 +1000
Re: How can I make this piece of code even faster? pablobarhamalzas@gmail.com - 2013-07-20 16:24 -0700
Re: How can I make this piece of code even faster? Chris Angelico <rosuav@gmail.com> - 2013-07-21 09:29 +1000
Re: How can I make this piece of code even faster? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-21 05:11 +0000
Re: How can I make this piece of code even faster? Paul Rudin <paul.nospam@rudin.co.uk> - 2013-07-21 08:11 +0100
Re: How can I make this piece of code even faster? Chris Angelico <rosuav@gmail.com> - 2013-07-21 19:21 +1000
Re: How can I make this piece of code even faster? Peter Otten <__peter__@web.de> - 2013-07-21 09:10 +0200
Re: How can I make this piece of code even faster? Serhiy Storchaka <storchaka@gmail.com> - 2013-07-21 10:11 +0300
Re: How can I make this piece of code even faster? Christian Gollwitzer <auriocus@gmx.de> - 2013-07-21 09:24 +0200
Re: How can I make this piece of code even faster? pablobarhamalzas@gmail.com - 2013-07-21 03:19 -0700
Re: How can I make this piece of code even faster? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-21 10:31 +0000
Re: How can I make this piece of code even faster? pablobarhamalzas@gmail.com - 2013-07-21 03:48 -0700
Re: How can I make this piece of code even faster? Stefan Behnel <stefan_ml@behnel.de> - 2013-07-21 14:49 +0200
Re: How can I make this piece of code even faster? Chris Angelico <rosuav@gmail.com> - 2013-07-21 20:48 +1000
Re: How can I make this piece of code even faster? Michael Torrie <torriem@gmail.com> - 2013-07-21 09:27 -0600
Re: How can I make this piece of code even faster? Joshua Landau <joshua@landau.ws> - 2013-07-21 12:39 +0100
csiph-web