Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50983
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.008 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; '21,': 0.07; 'correct.': 0.07; 'subject:code': 0.07; 'oh,': 0.09; 'subject:How': 0.10; 'def': 0.12; 'translation': 0.12; 'effect,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inputs': 0.16; 'iterator': 0.16; 'loop.': 0.16; 'loops': 0.16; 'margin,': 0.16; 'readable': 0.16; 'subject:make': 0.16; 'temp': 0.16; 'wrote:': 0.18; 'replacing': 0.19; 'input': 0.22; 'replace': 0.24; 'decide': 0.24; 'looks': 0.24; "i've": 0.25; 'this:': 0.26; 'second': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'slot': 0.31; 'probably': 0.32; 'quite': 0.32; "i'd": 0.34; "can't": 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'raising': 0.36; 'shorter': 0.36; 'next': 0.36; 'subject:?': 0.36; 'two': 0.37; 'list': 0.37; 'performance': 0.37; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'structure': 0.39; 'subject:can': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'negative': 0.60; 'entire': 0.61; 'skip:n 10': 0.64; 'confirm': 0.64; 'more': 0.64; 'finally': 0.65; 'number:': 0.66; 'jul': 0.74; 'subject:this': 0.83; 'end.': 0.84; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=GblbpJK74A6tp7cfCBgJJm9IjXKMJKZjw1DPHM3qjOI=; b=w8Zq1wbwjIpUUw4FizkdAQ1WGgmovDn2cOPXMLwhajm3z/ANUjoQVqwhMytf8VdCDD tLf5yLmJY4gkaslPLCyzTnsuH/C4rHBCdSeq90mNCXzBXcyigER51Wl4slDNxzBXi9gY hpVMlQ7YAZ3BnbQ0GGaHpEwvxap5k5hxikmM6JMwTiHU8i2t2bIf58sXl/3Da75XIqrb dH15vVfbZrd6yx+E2RRhwZzRTbp+hcRKneEUvJ6gea2e4+kFvhO9Ps6t/xGLRZ/UreKi fCoJ3At0dpoDj8rXKx22BntQUYQsnlT57AUOaalV0goQEuVaNzKVZ25v8TJJKK7m7NGp RPgg== |
| MIME-Version | 1.0 |
| X-Received | by 10.220.169.146 with SMTP id z18mr7589087vcy.80.1374360908076; Sat, 20 Jul 2013 15:55:08 -0700 (PDT) |
| In-Reply-To | <6bf4d298-b425-4357-9c1a-192e6e6cd9f0@googlegroups.com> |
| References | <6bf4d298-b425-4357-9c1a-192e6e6cd9f0@googlegroups.com> |
| Date | Sun, 21 Jul 2013 08:55:07 +1000 |
| Subject | Re: How can I make this piece of code even faster? |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4934.1374360916.3114.python-list@python.org> (permalink) |
| Lines | 66 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1374360916 news.xs4all.nl 15914 [2001:888:2000:d::a6]:58273 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:50983 |
Show key headers only | View raw
On Sun, Jul 21, 2013 at 6:22 AM, <pablobarhamalzas@gmail.com> wrote:
> temp = 0
> for y in range(input_num):
> count += 1
> temp += inputs[y] * h_weight[count]
> hidden[x] = 1/(1+e**(-temp))
It's a micro-optimization that'll probably have negligible effect, but
it can't hurt: Instead of adding to temp and raising e to -temp, carry
the value of temp as a negative number:
temp -= inputs[y] * h_weight[count]
hidden[x] = 1/(1+e**temp)
Ditto in the second loop.
Not sure which way performance would go, but would it be more readable
to take an iterator for h_weight and o_weight? Something like this:
# Slot this into your existing structure
inputs = self.input
h_weight = iter(self.h_weight)
o_weight = iter(self.o_weight)
e = math.e
for x in range(hidden_num):
temp = 0
for y in inputs:
temp += y * next(h_weight)
hidden[x] = 1/(1+e**(-temp))
for x in range(output_num):
temp = 0
for y in hidden:
temp += y * next(o_weight)
output[x] = 1/(1+e**(-temp))
# End.
If that looks better, the next change I'd look to make is replacing
the 'for y' loops with sum() calls on generators:
temp = sum(y * next(o_weight) for y in hidden)
And finally replace the entire 'for x' loops with list comps... which
makes for two sizeable one-liners, which I like and many people
detest:
def tick(self):
inputs = self.inputs
h_weight = iter(self.h_weight)
o_weight = iter(self.o_weight)
e = math.e
hidden = [1/(1+e**sum(-y * next(h_weight) for y in inputs))
for _ in range(hidden_num)]
self.output = [1/(1+e**sum(-y * next(o_weight) for y in
hidden)) for _ in range(output_num)]
Up to you to decide whether you find that version more readable, or at
least sufficiently readable, and then to test performance :) But it's
shorter by quite a margin, which I personally like. Oh, and I'm
relying on you to make sure I've made the translation correctly, which
I can't confirm without a pile of input data to test it on. All I can
say is that it's syntactically correct.
ChrisA
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