Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate exp(x) for really big integers in Python?

I'm using a sigmoid function for my artificial neural network. The value that I'm passing to the function ranges from 10,000 to 300,000. I need a high-precision answer because that would serve as the weights of the connection between the nodes in my artificial neural network. I've tried looking in numpy but no luck. Is there a way to compute the e^(-x)

like image 993
Earl Bunao Avatar asked Dec 26 '22 11:12

Earl Bunao


1 Answers

The regular python math and numpy modules will overflow on exp(300000).

What you need is an arbitrary precision floating point library.

Prereq: pip install mpmath

from mpmath import *
mp.dps=300
print exp(300000)
2.21090954962043147554031964344003334958746533182776533253160702399084245726328190320934903726540800347936047182773804396858994958295396516475277561815722954583856797032504775443385287094864178178111231967140927970972263439977028621274619241097429676587262948251263990280758512853239132411057394977398e+130288

see also http://code.google.com/p/mpmath/

like image 182
Paul Avatar answered Dec 28 '22 11:12

Paul