karpathy / neuraltalk

NeuralTalk is a Python+numpy project for learning Multimodal Recurrent Neural Networks that describe images with sentences.
5.4k stars 1.32k forks source link

Encountered runtime warning while computing logistic function #2

Open vimalthilak opened 9 years ago

vimalthilak commented 9 years ago

@karpathy Thanks for open sourcing your image-to-sentences work. I got the code up & running with the Flickr30K dataset but encountered a runtime warning " RuntimeWarning: overflow encountered in exp"

I have fixed it locally by using scipy.special.expit function. I have attached the patch below in case you want to "cherry-pick' my commit. Let me know if this patch is useful to you and whether you'd like me to make a PR with a fix:

From d3b8d3401a7ebeae1aff88538f1f5eff440b31cf Mon Sep 17 00:00:00 2001 From: Vimal Thilak Date: Wed, 3 Dec 2014 15:16:28 -0800 Subject: [PATCH] [bugfix] Fix overflow runtime warning

diff --git a/imagernn/lstm_generator.py b/imagernn/lstm_generator.py index 011e333..af6797f 100644 --- a/imagernn/lstm_generator.py +++ b/imagernn/lstm_generator.py @@ -1,5 +1,6 @@ import numpy as np import code +import scipy.special

from imagernn.utils import initw

@@ -75,7 +76,7 @@ class LSTMGenerator: IFOG[t] = Hin[t].dot(WLSTM)

   # non-linearities
karpathy commented 9 years ago

Hi thank you for reporting this. The exp should be guarded, you're right, but despite the warning I don't believe this should be too big of an issue. I will fix it soon, I'm overwhelmed with other work right now. Thank you!

amangupta199334 commented 8 years ago

@vimalthilak , Thank you sir. It really removed the warning. But, as @karpathy sir said, warning does't make any trouble. Anyways, good job sir.

monajalal commented 6 years ago

@karpathy I changed my sigmoid to expit from scipy.special to overcome the overflow but now I am getting overflow somewhere else.

/Users/mona/machine_learning/pset4/mlp.py:304: RuntimeWarning: overflow encountered in exp
  return expit(-x)

What should I use?

Would it make sense to replace sigmoid with tanh?

def logistic_function(x):
    return .5 * (1 + np.tanh(.5 * x))