Sunday, 11 November 2018

Numpy Overflow in calculations disrupting code

I am trying to train a neural network in Python 3.7. For this, I am using Numpy to perform calculations and do my matrix multiplications. I find this error

RuntimeWarning: overflow encountered in multiply (when I am multiplying matrices)

This, in turn, results in nan values, which raises errors like

RuntimeWarning: invalid value encountered in multiply

RuntimeWarning: invalid value encountered in sign

Now, I have seen many answers related to this question, all explaining why this happens. But I want to know, "How do I solve this problem?". I have tried using the default math module, but that still doesn't work and raises errors like

TypeError: only size-1 arrays can be converted to Python scalars

I know I can use for loops to do the multiplications, but that is computationally very expensive, and also lengthens and complicates the code a lot. Is there any solution to this problem? Like doing something with Numpy (I am aware that there are ways to handle exceptions, but not solve them), and if not, then perhaps alternative to Numpy, which doesn't require me to change my code much?

I don't really mind if the precision of my data is compromised a bit. (If it helps the dtype for the matrices is float64)

EDIT: Here is a dummy version of my code:

import numpy as np
network = np.array([np.ones(10), np.ones(5)])
for i in range(100000):
    for lindex, layer in enumerate(network):
        network[lindex] *= abs(np.random.random(len(layer)))*200000

I think the overflow error occurs when I am adding large values to the network.



from Numpy Overflow in calculations disrupting code

No comments:

Post a Comment