I have a bug in my code, but finding the exact cause of it is difficult because of how theano works.
Following the tips in the exception details, I set theano.config.optimizer='None' and theano.config.exception_verbosity='high', but that doesn't tell me enough.
In my case, for example, there is a problem with the dot product between two tensors. The stacktrace leads me through a lot and to a particular function which seems to contain in it, somewhere, the problematic call to theano.tensor.dot, but I can't find where exactly that part of the code is, and since I'm trying to implement things through keras, it gets even more complicated and tangled up.
Is there any way to get more details on an apply node? I've tried using StepMode, as it seems to be attached to the nodes, but if there is a way of making that tool print the exact lines from which the code in the node is executed, I don't know what it is. I tried using that to print a stacktrace when the problem occurs, but it prints just about the same stacktrace as the exception.
If you want to find the spots in your code that use theano.tensor.dot you can monkeypatch it with wrapper code that uses traceback.print_stack:
import traceback
original_dot = theano.tensor.dot
def debug_wrapper(*args,**kw):
traceback.print_stack()
return original_dot(*args,**kw)
theano.tensor.dot = debug_wrapper
This way any time theano.tensor.dot is called (after it is patched) it will show you the stack like the one in a traceback message and still do it's job. Note that I am not very familiar with theano so this is a general python debugging solution, there might well be ways specific to theano that let you do similar.
You should try using theano test_values. That way the exception will be raised exactly on the line where the error occurs and not after the compilation of the graph.
You need to change the theano.config.compute_test_value flag to 'raise' so that you get an error if there is an input tensor without a test_value, to make sure that all of the test computation will be propagated to the point where your error occurs.
Related
I know little about Python warnings and exceptions but find myself in a situation where I'm designing a tkinter gui that has an option to execute an outside function imported from a module written by someone else. The function performs calculations but produces no graphs.
Numeric stability issues may arise when I call the function, in which case it returns an output along with a RunTimeWarning that indicates the output may not be very reliable and suggests I adjust the input I used when calling the function.
Of course, the warnings are not visible to the user from within the gui, so I'd like to inform the user of the warning and give them options to proceed. The pseudo code would look something like the following:
output=outside_function(input)
if outside_function issued RuntimeWarning:
do something such as issuing error message, asking user to adjust input,etc.
else:
proceed with further calculations utilizing the output obtained.
I apologize that this question is somewhat vague and lacks a minimal reproducible example. However is there some way, perhaps using the warnings module, which allows me to determine whether
outside_function issued RuntimeWarning is true or false?
If the warning extended Warning (which RuntimeWarning should), you should be able to catch it with the catch_warnings context manager, see here for an example.
But you should be able to do something like:
import warnings
with warnings.catch_warnings(record=True) as w:
outside_function(...)
# code to check if there was a warning
if len(w) > 0:
# do something, e.g. check to see the warning type/whatnot
I am working on some soon-to-be obsolete scientific software written in Python (Enthought Python Distribution, Python 2.7.1, IPython 0.10.1). We need to check if the older results are correct, but with the massive amount of data we have the GUI procedure we need to make working with non-GUI mode. The important piece of code is to save the data:
def _save_button_fired(self):
self.savedata()
In GUI when we click on the button the file is saved correctly. In non-GUI mode, when we do the following:
m.savedata()
the file is created, but a number of errors appear starting with:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
etc. etc. When I use CTRL+D, select Y to kill the Python, the file is quite surprisingly written correctly. This is suffcient for our needs. However, is there any way to ignore the error and just proceed further with the code? I looked at the forum for solutions but non of them seems to fit my situation. Also, I am not keen on Python, thus would be grateful for a working solution. With many thanks.
You could wrap it in a try/pass :)
try:
self.savedata()
except TypeError:
pass
An alternative to the try/pass solution is contextlib.suppress.
with contextlib.suppress(TypeError):
self.savedata()
Why one over the other?
The context manager slightly shortens the code and significantly clarifies the author's intention to ignore the specific errors. The standard library feature was introduced following a discussion, where the consensus was that: A key benefit here is in the priming effect for readers... The with statement form makes it clear before you start reading the code that certain exceptions won't propagate.
Source: sourcery.ai
I have been attempting to force ArrayFire to use its CPU backend, rather than the default CUDA backend. According to this documentation page, you only need to call arrayfire.set_backend('cpu'). However, when I attempt to do this, an error is thrown with the message global name 'backend' is not defined. If you take a look at the source code, you will see that a global variable backend is defined within the module directly before the set_backend function is implemented. The following functions set and get various attributes of this backend object. My question is: is an internal implementation error on their part causing this error, or is there something I'm doing wrong (or that can be done on my part to fix this)? I haven't worked much with Python modules before, and would greatly appreciate any help!
I have the following python code inside a large loop
arr_a[indx]*arr_b[arr_c[indx],]
and with one running, an exception occurred and it said index out of range, but there are two possibilities(indx is out of range for predictErr, or arr_c[indx]), how do I know which part goes wrong?
This problem also extend to some general case that when one write several operations in one line and when things goes wrong, it is hard to tell which part causes this, and note that the above mentioned expression is inside a large loop, which means one can not simply start a debug mode and find that out.
Add a print statement for each segment that might be to blame to see which one fails:
print arr_a[indx]
print arr_c[indx]
arr_a[indx]*arr_b[arr_c[indx],]
Suppose, I have a 3rd party library that I am not allowed to modify. Suppose, it is called Fabric, but that is important only to explain the symptoms.
The script processes a list of existing files to get them using fabric.operations.get, which in its turn calls fabric.sftp.SFTP.get. Using fabric.sftp.SFTP.get produced some Warning: get() encountered an exception while downloading ... Underlying exception: Permission denied. I noticed the implementation was too old, and swapped the implementation of that function for one that uses sudo to work around the Permission denied:
import fabric.sftp
def sftpget(....same args as in current implementation....):
...here I pasted fabric.sftp.SFTP.get from the Internet
# swapping the implementation
fabric.sftp.SFTP.get=sftpget
This worked in 99.999% of cases. But getting three files still results in the same error. I tried to see if that is caused by some other codepath, but the only place where that string is printed is in fabric.operations.get in except: clause (grepped /usr/lib/python2.6/site-packages/ for get() encountered an exception while downloading). I tried to swap that function for a implementation that will print the stack trace of the exception, but I still only get the Permission denied message, and no stack trace.
It looks like the function does not get swapped in this case.
What could be the reasons for some invocations to use the original fabric.operations.get (since I don't see the stack traces printed) (and possibly the unpatched fabric.sftp.SFTP.get, since it seems sudo fix is not being used - I did check manually that those operations can be done on those files)?
during import, before you replace the get function some other piece of code might save a reference to the get function, for example:
class a():
def __init__(self,getter):
self.getter=getter
b=a(sftp.SFTP.get)
class a would then still hold a reference to the old piece of code despite it being replaced by you in the namespace.