Having a problem with handling errors on Python - python

Well, after quite some research, I've figured out I have no idea what is wrong with my code. This is a Newton's Method optimization code that I am working on, and I want to test it on several functions on a for loop on another program, along with several other optimization methods, but there is a problem on a certain function that just won't let the code keep on going to test everything.
The thing is that the Newton's method requires a matrix not to be singular in order to solve a system, and on this problematic function, that matrix ends up being singular at some point, and I get an error message telling me that and stopping the whole process. I knew nothing about try and except error handling up until this point, so after some research I figured it out and implemented it on my code to try and avoid this error, however, it will still stop the entire program because of the error, would anyone take a look at it for me?
This is the code for the problematic part:
while np.dot(grad(x),grad(x)) > 10**(-4):
if f(x+a*p) <= f(x)+c1*a*np.dot(grad(x),p):
x = x+a*p
try:
p = -np.linalg.solve(hess(x),grad(x))
except:
break
k = k+1
else:
a = 0.9*a
if k >= 100:
break
return [x, f(x),grad(x)]
hess(x) ends at some point being a singular matrix and giving me an exception error, but the code won't attempt to not do the try block and do the except block in this situation as I think it should, what is going on?
I've tried on a smaller problem like
i = 0
while i < 10:
if 2 < 3:
try:
-np.linalg.solve(A,b)
except:
print(9)
break
i = i+1
with A a singular matrix, and it works just fine, by just printing one "9", so why on the main program this won't happen at all? Perhaps it has something to do with the fact that I call this function on another program or something like that?
I could try and get around this problem by having an if statement testing if the matrix is singular beforehand and breaking the loop if so, but I feel like that would be quite costy for an iteration, so I would like to avoid it to better compare the methods later on, so I want to avoid that as much as possible.
This is what I get when I execute my code:
Traceback (most recent call last):
File "<ipython-input-1-31bd7312c08f>", line 1, in <module>
runfile('C:/Users/CLIENTE/Desktop/Coisas/Estudos 2.0/Ñ Linear/Testes.py', wdir='C:/Users/CLIENTE/Desktop/Coisas/Estudos 2.0/Ñ Linear')
File "C:\Users\CLIENTE\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\CLIENTE\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/CLIENTE/Desktop/Coisas/Estudos 2.0/Ñ Linear/Testes.py", line 52, in <module>
effer = método(dado)
File "C:\Users\CLIENTE\Desktop\Coisas\Estudos 2.0\Ñ Linear\Método_de_Newton.py", line 27, in MétodoDeNewton
p = -np.linalg.solve(hess(x),grad(x))
File "C:\Users\CLIENTE\Anaconda3\lib\site-packages\numpy\linalg\linalg.py", line 394, in solve
r = gufunc(a, b, signature=signature, extobj=extobj)
File "C:\Users\CLIENTE\Anaconda3\lib\site-packages\numpy\linalg\linalg.py", line 89, in _raise_linalgerror_singular
raise LinAlgError("Singular matrix")
LinAlgError: Singular matrix

Have you tried catching the fully-qualified name of the Exception?
while np.dot(grad(x),grad(x)) > 10**(-4):
if f(x+a*p) <= f(x)+c1*a*np.dot(grad(x),p):
x = x+a*p
try:
p = -np.linalg.solve(hess(x),grad(x))
# HEY! Look here.
except np.linalg.LinAlgError:
break
k = k+1
else:
a = 0.9*a
if k >= 100:
break
return [x, f(x),grad(x)]

Related

How to set a condition statement on a loop process

I am using Python 3; I have a problem in setting a condition statement over some groups (to consider pixel only when there are more than 5 available data) in a loop and I expect to get a blank pixel whether the condition isn't satisfied.
I tried some 'if' statement, but I am constantly getting a KeyError when the condition isn't maybe satisfied.
I'll show the code:
Xpix = 78
Ypix = 30
row = []
mean_val = []
for i in range (0,Ypix):
for j in range (0,Xpix):
if(len(data_pixel.groupby(['lin','col']).get_group((i,j))[['gamma']])>=5):
means = data_pixel.groupby(['lin','col']).get_group((i,j))[['gamma'].mean()
else:
means = 0
row.append(means)
mean_val = np.array(row).reshape(Ypix, Xpix)
I expect a 78 x 30 array to plot with blank pixels and mean pixels.
Here I show the error I got:
Traceback (most recent call last):
File "map.py", line 415, in <module>
proc.process()
File "map.py", line 215, in process
if (len(data_pixel.groupby(['lin', 'col']).get_group((i,j))[['gamma']])>=5):
File "/xxx/yyy/anaconda3/envs/gnss/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 680, in get_group
raise KeyError(name)
KeyError: (10,41)
data_pixel refers to a big dataframe with a lot of data. I would appreciate a lot if anyone could help with this.

I am trying to solve a system of equation with two variable in tkinter python

Good morning,
I am trying to solve a system of equation with 2 variables in Python, but using Tkinter to display the answers on the screen. I did most of it, but I can not display the answes.
That is the error I am seeing:
enter coException in Tkinter callback
Traceback (most recent call last):
File "C:\Users\edwin\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\Users\edwin\AppData\Local\Programs\Python\Python36-32\ed.py", line 122, in Calculate
z = np.linalg.solve ( a, b)
File "C:\Users\edwin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\linalg\linalg.py", line 375, in solve
r = gufunc(a, b, signature=signature, extobj=extobj)
File "C:\Users\edwin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\linalg\linalg.py", line 90, in _raise_linalgerror_singular
raise LinAlgError("Singular matrix")
numpy.linalg.linalg.LinAlgError: Singular matrixde here
A singular matrix is not invertible. A singular matrix does not satisfy the property: The equation Ax = b has exactly one solution for each b in Kn. This means that the system you are attempting to solve is either incorrectly converted into matrix form, or does not have a unique solution.

broadcasting linalg.pinv on a 3D theano tensor

in the example below, there is a 3d numpy matrix of size (4, 3, 3)+ a solution about how to calculate pinv of each of 4 of those 3*3 matrices in numpy. I also tried to use the same function worked in numpy, in theano hoping that it is implemented the same, but it failed. Any idea how to do it in theano?
dt = np.dtype(np.float32)
a=[[[12,3,1],
[2,4,1],
[2,4,2],],
[[12,3,3],
[2,4,4],
[2,4,5],],
[[12,3,6],
[2,4,5],
[2,4,4],],
[[12,3,3],
[2,4,5],
[2,4,6]]]
a=np.asarray(a,dtype=dt)
print(a.shape)
apinv=np.zeros((4,3,3))
print(np.linalg.pinv(a[0,:,:]).shape)
#numpy solution
apinv = map(lambda n: np.linalg.pinv(n), a)
apinv = np.asarray(apinv,dtype=dt)
#theano solution (not working)
at=T.tensor3('a')
apinvt = map(lambda n: T.nlinalg.pinv(n), at)
The error is:
Original exception was:
Traceback (most recent call last):
File "pydevd.py", line 2403, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "pydevd.py", line 1794, in run
launch(file, globals, locals) # execute the script
File "exp_thn_pinv_map.py", line 35, in <module>
apinvt = map(lambda n: T.nlinalg.pinv(n), at)
File "theano/tensor/var.py", line 549, in __iter__
raise TypeError(('TensorType does not support iteration. '
TypeError: TensorType does not support iteration. Maybe you are using builtin.sum instead of theano.tensor.sum? (Maybe .max?)
The error message is
Traceback (most recent call last):
File "D:/Dropbox/source/intro_theano/pinv.py", line 32, in <module>
apinvt = map(lambda n: T.nlinalg.pinv(n), at)
File "d:\dropbox\source\theano\theano\tensor\var.py", line 549, in __iter__
raise TypeError(('TensorType does not support iteration. '
TypeError: TensorType does not support iteration. Maybe you are using builtin.sum instead of theano.tensor.sum? (Maybe .max?)
This is occurring because, as the error message indicates, the symbolic variable at is not iterable.
The fundamental problem here is that you're incorrectly mixing immediately executed Python code with delayed execution Theano symbolic code.
You need to use a symbolic loop, not a Python loop. The correct solution is to use Theano's scan operator:
at=T.tensor3('a')
apinvt, _ = theano.scan(lambda n: T.nlinalg.pinv(n), at, strict=True)
f = theano.function([at], apinvt)
print np.allclose(f(a), apinv)

Global function is not defined

I am facing this error as I define my module. I am trying to write a program for edit distance problem via dynamic programming method.
Here is the module where I am stuck:
def cost(i,j,M,w,text,pattern,compare): #Defining the cost functions or can say recurrence formula
M[0,j]=0
text1=list(text)
pattern1=list(pattern)
for i in range(1,m+1):
for j in range(1,n+1):
insertions = M[i-1,j]+1
deletions = M[i,j-1]+1
matches=M[i-1,j-1]
if text1[i]==patttern1[j]:
matches = matches+1
return matches
else :
return matches
and the error is :
Traceback (most recent call last): File
"/Users/sayaneshome/Documents/plschk.py", line 202, in
fill(M, w, text, max) #Filling matrix M with scores File
"/Users/sayaneshome/Documents/plschk.py", line 117, in fill c =
cost(i,j,M,w,text,pattern,compare) File
"/Users/sayaneshome/Documents/plschk.py", line 95, in cost if
text1[i]==patttern1[j]: NameError: global name 'patttern1' is not
defined
Your patttern1 has three t's. Remove one to get pattern1.

why i get this traceback?

This is part of my code:
if ind_1<>0:
rbrcol=[]
brdod1=[]
for i in range(27):
if Add_Cyc_1[1,i]!=0:
rbrcol.append(Add_Cyc_1[0,i])
brdod1.append(Add_Cyc_1[1,i])
Probrani_1=vstack((rbrcol,brdod1))
pok=0
for i in (rbrcol):
pok+=1
broj1=0
for j in range(21):
if SYS_STATE_1[i,j]==0:
broj1+=1
if broj1 <= Probrani_1[1,pok-1]:
SYS_STATE_1[i,j]=123456
And when i run program i get this:
Traceback (most recent call last):
File "C:/Python26/pokusaj2.py", line 157, in <module>
for i in (rbrcol):
NameError: name 'rbrcol' is not defined
What i do wrong???
I think the real problem is the if at the very top. Your indenting is incorrect - the code as written won't run because the line after the if is not indented.
Assuming it is indented in the original code, then rbrcol is not initialized if ind_1 is 0 and as ghostdog says if the if statement never fires, then rbrcol would not be set at all.
just as the error says, "rbrcol" doesn't have value. check your for loop
for i in range(27):
if Add_Cyc_1[1,i]!=0: <----- this part doesn't get through
rbrcol.append(Add_Cyc_1[0,i])
brdod1.append(Add_Cyc_1[1,i])
Probrani_1=vstack((rbrcol,brdod1))
also, what is Add_Cyc_1 ? To assign multidimension list
Add_Cyc_1[1,i] should be Add_Cyc_1[1][i]
further, this
if ind_1<>0: <<--- if this is not true, then rbrcol will not be defined
rbrcol=[] << --- <> should be != , although <> its also valid, but now ppl use !=
brdod1=[]

Categories

Resources