Maximum Recursion Occurs, what went wrong in Keller Box? - python
This is the non syntax error code, but i cant seem to fix the recursion error. need some help here. the algorithm based on matlab, i've read the tutorial on matlab but i can seem to figure out which part did i miss.
import numpy as npy
blt = int(raw_input("Input the boundary layer thickness = "))
deleta = float(raw_input("Input the step size of boundary layer thickness = "))
np = int((blt/deleta) + 1)
stop = 1
k=1
l=2
g=2
eselon = 0.00001
def eta(j,k):
if j == 1 and k == 1:
return 0
else:
return eta(j-1,k) + deleta;
deta = deleta
def etanp():
return eta(np,k)
def f(j,k):
return -eta(np,1)*etab2 + etanp()*etab1 + eta(j,1)
def u(j,k):
return -3*etab1 + 2*etab +1
def v(j,k):
return (-6/etanp()*etab + (2/etanp()))
def fb(j,k):
return 0.5 * (f(j,k) + f(j-1,k))
def ub(j,k):
return 0.5 * (u(j,k) + u(j-1,k))
def vb(j,k):
return 0.5*(v(j,k) + v(j-1,k))
def fvb(j,k):
return fb(j,k)*vb(j,k)
def uub(j,k):
return ub(j,k) * ub(j,k)
def a1(j,k):
return 1 + 0.5*deta *fb(j,k)
def a2(j,k):
return -1 + 0.5*deta *fb(j,k)
def a3(j,k):
return 0.5 * deta * vb(j,k)
def a4(j,k):
return a3(j,k)
def a5(j,k):
return -1 * deta * ub(j,k)
def a6(j,k):
return a5(j,k)
def r1(j,k):
return f(j-1,k) - f(j,k) + deta * ub(j,k)
def r2(j,k):
return u(j-1,k) - u(j,k) + deta * vb(j,k)
def r3(j,k):
return v(j-1,k)-v(j,k) - deta *((fvb(j,k)*uub(j,k)))-deta
def AJ(j,k):
if j == 2:
return npy.matrix([[0,1,0],[-0.5*deta,0,-0.5*deta],[a2(2,k), a3(2,k), a1(2,k)]])
else:
return npy.matrix([[-0.5*deta,0,0],[-1,0,-0.5*deta],[a6(j,k),a3(j,k),a1(j,k)]])
def BJ(j,k):
return npy.matrix([[0,-1,0],[0,0,-0.5*deta],[0,a4(j,k),a2(j,k)]])
def CJ(j,k):
return npy.matrix([[-0.5*deta,0,0],[1,0,0],[a5(j,k),0,0]])
def alfa(j,k):
if j == 2:
return AJ(2,k)
else:
return AJ(j,k) - (BJ(j,k)*gamma(j-1,k))
def gamma(j,k):
if j == 2:
return npy.matrix.I((alfa(2,k))*CJ(2,k))
else:
return npy.matrix.I((alfa(j,k))*CJ(j,k))
def rr(j,k):
return npy.matrix([[r1(j,k)],[r2(j,k)],[r3(j,k)]])
def ww(j,k):
if j == 2:
return npy.matrix.I(AJ(2,k)*rr(2,k))
else:
return npy.matrix.I((alfa(j,k))*(rr(j,k)-(BJ(j,k)*ww(j-1,k))))
def dell(j,k):
if j == np:
return ww(np,k)
else:
return ww(j,k) - (gamma(j,k)*dell(j+1,k))
def delf(j,k):
if j == 1:
return 0
elif j == 2:
return dell(2,k)[1,0]
else:
return dell(j,k)
def delu(j,k):
if j == 1 or j == np:
return 0
elif j == np-1:
return dell(j,k)[0,0]
def delv(j,k):
if j == 1:
return dell(2,k)[0,0]
elif j == 2:
return dell(2,k)[2,0]
else:
return dell(j,k)[2,0]
def ffinal(j,l):
return f(j,k) + delf(j,k)
def ufinal(j,l):
return u(j,k) + delu(j,k)
def vfinal(j,l):
return v(j,k) + delv(j,k)
# Beginning of calculation for Keller-Box
while stop > eselon:
eta(1,1)
for j in range (2,np):
eta(j,k)
# Initial condition
etab = eta(j,k) / eta(np,k)
etab1 = etab**2
etab2 = etab**3
for j in range (1,np):
deta
f(j,1)
u(j,1)
v(j,1)
# Current value of Central Differentiation
for j in range (2,np):
fb(j,k)
ub(j,k)
vb(j,k)
fvb(j,k)
uub(j,k)
a1(j,k)
a2(j,k)
a3(j,k)
r1(j,k)
r2(j,k)
r3(j,k)
# Matrices Value for A1, Aj, Bj, and CJ
CJ(j,k)
AJ(j,k)
BJ(j,k)
# Recursion: Forward Sweeping
for j in range (3,np):
alfa(j,k)
gamma(j,k)
for j in range(2,np):
rr(j,k)
for j in range(3,np):
ww(j,k)
# Recursion: Backward Sweeping
for j in range (np-1,2,-1):
dell(j,k)
for j in range (np,3,-1):
delu(j-1,k)
delf(j,k)
delv(j,k)
# Newton's Method
for j in range (1,np):
ffinal(j,l)
ufinal(j,l)
vfinal(j,l)
# Check the convergence of iteration
stop = npy.abs(delv(1,k))
kmax = k
k =+ 1
cfrex = vfinal(1,kmax)
print cfrex
Here's the referential that i used from mathlab
*******************************************************************
Input
*******************************************************************
blt = input ('Input the boundary layer thickness = ');
deleta=0.1; %input('Input the step size of boundary layer thickness=');
np = (blt / deleta)+ 1;
pr = 7; %input ('Input the prandtl number = ');
K = 0; %input ('Input the material parameter K = ');
lambda = 1; %input ('Input the mixed convection parameter = ');
stop = 1.0; k = 1; eselon = 0.00001;
while stop > eselon
eta(1,1) = 0.0;
for j = 2:np
eta(j,1) = eta(j-1,1) + deleta;
end
*******************************************************************
Initial Condition for f, u, v, h, p, s, t
*******************************************************************
etanpq = eta(np,1)/3;
etau15 = 1/eta(np,1);
etau16 = 2/eta(np,1);
etanp = eta(np,1);
for j =1:np
deta(j,k)= deleta;
etab = eta(j,1)/eta(np,1);
etab1 = etab^2;
etab2 = etab^3;
etau152 = etau15^2;
etau162 = etau16^2;
f(j,1) = -etanpq * etab2 + etanp * etab1;
u(j,1) = -etab1 + 2 * etab;
v(j,1) = -etau16 * etab + etau16;
h(j,1) = etau15 * etab - etau15;
p(j,1) = etau152;
s(j,1) = -eta(j,1) + eta(j,1) * etab;
t(j,1) = -1 + 2 * etab;
end
*******************************************************************
Current Central Differention Value
*******************************************************************
for j = 2:np
fb(j,k) = 0.5*(f(j,k) + f(j-1,k));
ub(j,k) = 0.5*(u(j,k) + u(j-1,k));
vb(j,k) = 0.5*(v(j,k) + v(j-1,k));
hb(j,k) = 0.5*(h(j,k) + h(j-1,k));
pb(j,k) = 0.5*(p(j,k) + p(j-1,k));
sb(j,k) = 0.5*(s(j,k) + s(j-1,k));
tb(j,k) = 0.5*(t(j,k) + t(j-1,k));
fvb(j,k) = fb(j,k) * vb(j,k);
uub(j,k) = ub(j,k) ^ 2;
pfb(j,k) = pb(j,k) * fb(j,k);
hub(j,k) = hb(j,k) * ub(j,k);
tfb(j,k) = tb(j,k) * fb(j,k);
sub(j,k) = sb(j,k) * ub(j,k);
*******************************************************************
Momentum Differential Equation
*******************************************************************
a1(j,k) = (1.0 + K) + 0.5 * deta(j,k) * fb(j,k);
a2(j,k) = -(1.0 + K) + 0.5 * deta(j,k) * fb(j,k);
a3(j,k) = 0.5 * deta(j,k) * vb(j,k);
a4(j,k) = a3(j,k);
a5(j,k) = -1 * deta(j,k) * ub(j,k);
a6(j,k) = a5(j,k);
a7(j,k) = 0.5 * K * deta(j,k);
a8(j,k) = a7(j,k);
a9(j,k) = 0.5 * lambda * deta(j,k);
a10(j,k) = a9(j,k);
*******************************************************************
Angel Differential
*******************************************************************
b1(j,k) = (1 + K/2) + 0.5 * deta(j,k) * fb(j,k);
b2(j,k) = -(1 + K/2) + 0.5 * deta(j,k) * fb(j,k);
b3(j,k) = 0.5 * deta(j,k) * pb(j,k);
b4(j,k) = b3(j,k);
b5(j,k) = -0.5 * deta(j,k) * hb(j,k);
b6(j,k) = b5(j,k);
b7(j,k) = -0.5 * deta(j,k) * ub(j,k) - K * deta(j,k);
b8(j,k) = b7(j,k);
b9(j,k) = -0.5 * K * deta(j,k);
b10(j,k) = b9(j,k);
*******************************************************************
Energy Differential
*******************************************************************
c1(j,k) = 1/pr + 0.5 * deta(j,k) * fb(j,k);
c2(j,k) = -1/pr + 0.5 * deta(j,k) * fb(j,k);
c3(j,k) = 0.5 * deta(j,k) * tb(j,k);
c4(j,k) = c3(j,k);
c5(j,k) = -0.5 * deta(j,k) * sb(j,k);
c6(j,k) = c5(j,k);
c7(j,k) = -0.5 * deta(j,k) * ub(j,k);
c8(j,k) = c7(j,k);
*******************************************************************
Definition value of rj-1/2
*******************************************************************
r1(j,k) = f(j-1,k) - f(j,k) + deta(j,k) * ub(j,k);
r2(j,k) = u(j-1,k) - u(j,k) + deta(j,k) * vb(j,k);
r3(j,k) = h(j-1,k) - h(j,k) + deta(j,k) * pb(j,k);
r4(j,k) = s(j-1,k) - s(j,k) + deta(j,k) * tb(j,k);
r5(j,k) = (1.0 + K) * (v(j-1,k) - v(j,k)) - deta(j,k) * fvb(j,k) -...
deta(j,k)+ deta(j,k) * uub(j,k) - K * deta(j,k)...
* pb(j,k) - lambda * deta(j,k) * sb(j,k);
r6(j,k) = (1 + K/2) * (p(j-1,k) - p(j,k)) - deta(j,k) * pfb(j,k) + ...
deta(j,k) * hub(j,k) + 2 * K * deta(j,k) * hb(j,k) + ...
K * deta(j,k) * vb(j,k);
r7(j,k) = 1/pr * (t(j-1,k) - t(j,k)) - deta(j,k) * tfb(j,k) +...
deta(j,k) * sub(j,k);
end
*******************************************************************
Matrices Value A1, Aj, Bj, Cj
*******************************************************************
a{2,k} = [0 0 0 1 0 0 0 ;...
-0.5 * deta(2,k) 0 0 0 -0.5 * deta(2,k) 0 0;...
0 -0.5 * deta(2,k) 0 0 0 -0.5 * deta(2,k) 0;...
0 0 -1 0 0 0 -0.5 * deta(2,k);...
a2(2,k) a8(2,k) a10(2,k) a3(2,k) a1(2,k) a7(2,k) 0;...
b10(2,k) b2(2,k) 0 b3(2,k) b9(2,k) b1(2,k) 0;...
0 0 c8(2,k) c3(2,k) 0 0 c1(2,k)];
for j = 3:np
a{j,k} = [-0.5 * deta(j,k) 0 0 1 0 0 0 ;...
-1 0 0 0 -0.5 * deta(j,k) 0 0 ;...
0 -1 0 0 0 -0.5 * deta(j,k) 0 ;...
0 0 -1 0 0 0 -0.5 * deta(j,k);...
a6(j,k) 0 a10(j,k) a3(j,k) a1(j,k) a7(j,k) 0;...
b6(j,k) b8(j,k) 0 b3(j,k) b9(j,k) b1(j,k) 0;...
c6(j,k) 0 c8(j,k) c3(j,k) 0 0 c1(j,k)];
b{j,k} = [0 0 0 -1 0 0 0 ;...
0 0 0 0 -0.5 * deta(j,k) 0 0;...
0 0 0 0 0 -0.5 * deta(j,k) 0;...
0 0 0 0 0 0 -0.5 * deta(j,k);...
0 0 0 a4(j,k) a2(j,k) a8(j,k) 0;...
0 0 0 b4(j,k) b10(j,k) b2(j,k) 0;...
0 0 0 c4(j,k) 0 0 c2(j,k)];
end
for j = 2:np
c{j,k} = [-0.5 * deta(j,k) 0 0 0 0 0 0 ;...
1 0 0 0 0 0 0;...
0 1 0 0 0 0 0;...
0 0 1 0 0 0 0;...
a5(j,k) 0 a9(j,k) 0 0 0 0;...
b5(j,k) b7(j,k) 0 0 0 0 0;...
c5(j,k) 0 c7(j,k) 0 0 0 0];
end
*******************************************************************
Recursion of block Elimination
*******************************************************************
Forward Sweeping
*******************************************************************
alfa{2,k} = a{2,k};
gamma{2,k} = inv(alfa{2,k}) * c{2,k};
for j = 3:np
alfa{j,k} = a{j,k} - (b{j,k} * gamma{j-1,k});
gamma{j,k} = inv(alfa{j,k}) * c{j,k};
end
for j = 2:np
rr{j,k} = [r1(j,k); r2(j,k); r3(j,k); r4(j,k); r5(j,k); r6(j,k);...
r7(j,k)];
end
ww{2,k} = inv(alfa{2,k}) * rr{2,k};
for j = 3:np
ww{j,k} = inv(alfa{j,k}) * (rr{j,k} - (b{j,k} * ww{j-1,k}));
end
*******************************************************************
Backward Sweeping
*******************************************************************
delf(1,k) = 0.0;
delu(1,k) = 0.0;
delh(1,k) = 0.0;
delt(1,k) = 0.0;
delu(np,k) = 0.0;
delh(np,k) = 0.0;
dels(np,k) = 0.0;
dell{np,k} = ww{np,k};
for j = np-1:-1:2
dell{j,k} = ww{j,k} -(gamma{j,k} * dell{j+1,k});
end
delv(1,k) = dell{2,k}(1,1);
delp(1,k) = dell{2,k}(2,1);
dels(1,k) = dell{2,k}(3,1);
delf(2,k) = dell{2,k}(4,1);
delv(2,k) = dell{2,k}(5,1);
delp(2,k) = dell{2,k}(6,1);
delt(2,k) = dell{2,k}(7,1);
for j = np:-1:3
delu(j-1,k) = dell{j,k}(1,1);
delh(j-1,k) = dell{j,k}(2,1);
dels(j-1,k) = dell{j,k}(3,1);
delf(j,k) = dell{j,k}(4,1);
delv(j,k) = dell{j,k}(5,1);
delp(j,k) = dell{j,k}(6,1);
delt(j,k) = dell{j,k}(7,1);
end
*******************************************************************
Newton method
*******************************************************************
for j = 1:np
f(j,k+1) = f(j,k) + delf(j,k);
u(j,k+1) = u(j,k) + delu(j,k);
v(j,k+1) = v(j,k) + delv(j,k);
h(j,k+1) = h(j,k) + delh(j,k);
p(j,k+1) = p(j,k) + delp(j,k);
s(j,k+1) = s(j,k) + dels(j,k);
t(j,k+1) = t(j,k) + delt(j,k);
h(j,k+1) = -0.5 * v(j,k+1);
end
*******************************************************************
Convergence Check
*******************************************************************
stop = abs(delv(1,k));
kmax = k;
k = k + 1;
end
*******************************************************************
Skin Friction and Nusselt Number
*******************************************************************
cfrex = v(1,kmax)
nuxrex = 1/s(1,kmax)
nuxrex2 = s(1,kmax)
however, my case of study covers only the f, u and v, therefore, there are some changes on the initial condition. and many others part.
The infinite recursion will occur in these two functions:
def alfa(j,k):
return AJ(j,k) - (BJ(j,k)*gamma(j,k))
def gamma(j,k):
return npy.matrix.I((alfa(g,k))*CJ(g,k))
Each of which calls the other one under any circumstances. So, alfa calls gamma which calls alfa which calls gamma and so on forever.
We can see this if we add a print statement:
def alfa(j,k):
print 'alfa({},{}) called'.format(j,k)
return AJ(j,k) - (BJ(j,k)*gamma(j,k))
def gamma(j,k):
print 'gamma({},{}) called'.format(j,k)
return npy.matrix.I((alfa(g,k))*CJ(g,k))
Then we can see that this is what happens:
In [199]: alfa(3,1)
alfa(3,1) called
gamma(3,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
alfa(2,1) called
gamma(2,1) called
... and so on
Related
Python: Write-Length Control
I am trying to create a specific output pattern for a textfile I wanted to later load into C++. I wrote a file in Python that creates random coordinates and movement values in a circle. The output is supposed to have the output: Place_1 Place_2 Place_3 Movement_1 Movement_2 Movement_3\n Place_1 Place_2 Place_3 Movement_1 Movement_2 Movement_3\n Place_1 Place_2 Place_3 Movement_1 Movement_2 Movement_3 The Code is use is import numpy as np file = open('log.txt', 'a') def f(n, center, radius, ecc): pos = np.zeros((n,6)) r = ecc * radius for i in range(n): while 1: x_1 = -1 + 2 * np.random.rand(1) x_2 = -1 + 2 * np.random.rand(1) if (x_1*x_1 + x_2*x_2)<1 : pos[i,0] = center[0] + r * 2 * x_1 * np.sqrt(1 - x_1*x_1 - x_2*x_2) pos[i,1] = center[1] + r * 2 * x_2 * np.sqrt(1 - x_1*x_1 - x_2*x_2) pos[i,2] = center[2] + r * (1 - 2 * (x_1*x_1 + x_2*x_2)) pos[i,3] = (-1 + 2 * np.random.rand(1)) pos[i,4] = (-1 + 2 * np.random.rand(1)) pos[i,5] = (-1 + 2 * np.random.rand(1)) break string = str(pos[i,:]).strip('[]').rstrip('\n') file.write(string) return f(10000, np.array((127,127,127)), 92, 0.9) file.close() The log I create is however very badly formated. How can I get the required format?
You're going to a lot of trouble here that you don't need. This seems to solve the problem, simply. import numpy as np file = open('log.txt', 'a') def f(n, center, radius, ecc): r = ecc * radius for i in range(n): while 1: pos = [0]*6 x_1 = -1 + 2 * np.random.rand(1)[0] x_2 = -1 + 2 * np.random.rand(1)[0] if (x_1*x_1 + x_2*x_2) < 1: pos[0] = center[0] + r * 2 * x_1 * np.sqrt(1 - x_1*x_1 - x_2*x_2) pos[1] = center[1] + r * 2 * x_2 * np.sqrt(1 - x_1*x_1 - x_2*x_2) pos[2] = center[2] + r * (1 - 2 * (x_1*x_1 + x_2*x_2)) pos[3] = (-1 + 2 * np.random.rand(1)[0]) pos[4] = (-1 + 2 * np.random.rand(1)[0]) pos[5] = (-1 + 2 * np.random.rand(1)[0]) break print(*pos, file=file) f(10000, np.array((127,127,127)), 92, 0.9) file.close() A solution without numpy: import math import random file = open('log.txt', 'a') def f(n, center, radius, ecc): r = ecc * radius for _ in range(n): pos = [0]*6 while 1: x_1 = 2 * random.random() - 1 x_2 = 2 * random.random() - 1 vector = x_1*x_1 + x_2*x_2 if vector < 1: break pos[0] = center[0] + r * 2 * x_1 * math.sqrt(1 - vector) pos[1] = center[1] + r * 2 * x_2 * math.sqrt(1 - vector) pos[2] = center[2] + r * (1 - 2 * vector) pos[3] = 2 * random.random() -1 pos[4] = 2 * random.random() -1 pos[5] = 2 * random.random() -1 print(*pos, file=file) f(10000, (127,127,127), 92, 0.9) file.close()
Use np.savetxt: import numpy as np def f(n, center, radius, ecc): pos = np.zeros((n,6)) r = ecc * radius for i in range(n): while 1: x_1 = -1 + 2 * np.random.rand(1) x_2 = -1 + 2 * np.random.rand(1) if (x_1*x_1 + x_2*x_2)<1 : pos[i,0] = center[0] + r * 2 * x_1 * np.sqrt(1 - x_1*x_1 - x_2*x_2) pos[i,1] = center[1] + r * 2 * x_2 * np.sqrt(1 - x_1*x_1 - x_2*x_2) pos[i,2] = center[2] + r * (1 - 2 * (x_1*x_1 + x_2*x_2)) pos[i,3] = (-1 + 2 * np.random.rand(1)) pos[i,4] = (-1 + 2 * np.random.rand(1)) pos[i,5] = (-1 + 2 * np.random.rand(1)) break np.savetxt('file.txt',pos,delimiter=';') return f(100, np.array((127,127,127)), 92, 0.9)
For the formatting issue you can make the coordinates into characters (What I mean is that turn coordinates into a character that would be printed like _ or / something like that) Also you can put empty space in a print like print(" Hello.") also to go to a different line in the console do print("\n Hello."). This probably was not what you were looking for but I hope this somewhat helps.
Python - Dijkstra's algorithm in O(V+ElogV) time complexity
I would like to implement the above mentioned algorithm in Python. Time Complexity of Dijkstra's Algorithm is O(V2), but I would like to implement it using min-priority queue so it drops down to O(V+ElogV). Heres an example input: The program should 2 problems, src: 0 dest: 2 and src: 1 dest: 2. 3 vertexes will be provided by the input and there will be 3 edges provided also, all of these are separated by an empty line. 2 3 3 0 2 1 2 2 0 -4 1 6 3 1 0 1 2 0 2 Solution: 5.0 10.2 Heres my current code: import math import sys def build_graph(edges, weights, e): graph = edges for i in range(e): graph[i].append(weights[i]) return graph def debug(): print(pontparok) print(csucsok) print(utszakaszok) print(hosszak) def read(n, bemenet): for i in range(n): temp = input().split("\t") bemenet[i] = temp bemenet[i][0] = int(bemenet[i][0]) bemenet[i][1] = int(bemenet[i][1]) def dijkstra(edges, src, dest, n): dist = [0] * n current = src for i in range(n): dist[i] = sys.maxsize dist[src] = 0 explored = [False] * n q = 0 while not explored[dest] and q < 1000: q += 1 min = sys.maxsize minVertex = current for edge in edges: if edge[0] == current and not explored[edge[1]]: if min > dist[edge[1]]: min = dist[edge[1]] minVertex = edge[1] elif edge[1] == current and not explored[edge[0]]: if min > dist[edge[0]]: min = dist[edge[0]] minVertex = edge[0] current = minVertex explored[current] = True for edge in edges: if edge[0] == current: if dist[current] + edge[2] < dist[edge[1]]: dist[edge[1]] = dist[current] + edge[2] elif edge[1] == current: if dist[current] + edge[2] < dist[edge[0]]: dist[edge[0]] = dist[current] + edge[2] return round(dist[dest], 2) p = int(input()) n = int(input()) e = int(input()) input() pontparok = [[0] * 2] * p csucsok = [[0] * 2] * n utszakaszok = [[0] * 2] * e hosszak = [0] * e read(p, pontparok) input() read(n, csucsok) input() read(e, utszakaszok) for i in range(e): hosszak[i] = math.sqrt(pow((csucsok[utszakaszok[i][0]][0] - csucsok[utszakaszok[i][1]][0]), 2) + pow((csucsok[utszakaszok[i][0]][1] - csucsok[utszakaszok[i][1]][1]), 2)) #debug() graph = build_graph(utszakaszok, hosszak, e) #print(hosszak) for i in range(p): if i == p - 1: print(dijkstra(graph, pontparok[i][0], pontparok[i][1], n)) else: print(dijkstra(graph, pontparok[i][0], pontparok[i][1], n), end="\t")
import heapq import math def read(n, bemenet): for i in range(n): temp = input().split("\t") bemenet[i] = temp bemenet[i][0] = int(bemenet[i][0]) bemenet[i][1] = int(bemenet[i][1]) def dijkstra(graph, starting_vertex, destination_vertex): distances = {vertex: float('infinity') for vertex in graph} distances[starting_vertex] = 0 pq = [(0, starting_vertex)] while len(pq) > 0: current_distance, current_vertex = heapq.heappop(pq) for neighbor, weight in graph[current_vertex].items(): distance = current_distance + weight if distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(pq, (distance, neighbor)) return round(distances[destination_vertex], 2) def build_graph(edges, weights, e): graph = edges for i in range(e): graph[i].append(weights[i]) return graph p = int(input()) n = int(input()) e = int(input()) input() pontparok = [[0] * 2] * p csucsok = [[0] * 2] * n utszakaszok = [[0] * 2] * e hosszak = [0] * e read(p, pontparok) input() read(n, csucsok) input() read(e, utszakaszok) for i in range(e): hosszak[i] = math.sqrt(pow((csucsok[utszakaszok[i][0]][0] - csucsok[utszakaszok[i][1]][0]), 2) + pow((csucsok[utszakaszok[i][0]][1] - csucsok[utszakaszok[i][1]][1]), 2)) graph2 = build_graph(utszakaszok, hosszak, e) graph = { } [graph.setdefault(i, []) for i in range(n)] for i in range(n): graph[i] = {} for edge in graph2: graph[edge[0]].update({edge[1]: edge[2]}) graph[edge[1]].update({edge[0]: edge[2]}) for i in range(p): if i == p - 1: print(dijkstra(graph, pontparok[i][0], pontparok[i][1])) else: print(dijkstra(graph, pontparok[i][0], pontparok[i][1]), end="\t")
Python neural network does not train
I have a simple neural network with 2 input neurons, 3 hidden neurons and 1 output neuron. hidden layer has bias. I'm not used matrix operations to doing feed forward and backpropagation. when I run training function on a simple linear dataset, the error raises up and the predication result is wrong. import random from math import exp,pow,tanh def random_weight(): return random.random() def sigmoid(x): return 1.0 / (1.0 + exp(-x)) def sigmoid_drv(x): return sigmoid(x)*(1.0-sigmoid(x)) w11_I = random_weight() w12_I = random_weight() w21_I = random_weight() w22_I = random_weight() w31_I = random_weight() w32_I = random_weight() w11_II = random_weight() w12_II = random_weight() w13_II = random_weight() b_I = 1 activation = sigmoid activation_drv = sigmoid_drv def predict(x1,x2): global w11_I,w12_I,w21_I,w22_I,w31_I,w32_I,w11_II,w12_II,w13_II,b_I a1_I = w11_I*x1 + w12_I*x2 + b_I z1_I = activation(a1_I) a2_I = w21_I*x1 + w22_I*x2 + b_I z2_I = activation(a2_I) a3_I = w31_I*x1 + w32_I*x2 + b_I z3_I = activation(a3_I) a1_II = w11_II*z1_I + w12_II*z2_I + w13_II*z3_I z1_II = activation(a1_II) return a1_I, z1_I, a2_I, z2_I, a3_I, z3_I, a1_II, z1_II def train(x1,x2,y,alpha): global w11_I,w12_I,w21_I,w22_I,w31_I,w32_I,w11_II,w12_II,w13_II,b_I a1_I, z1_I, a2_I, z2_I, a3_I, z3_I, a1_II, z1_II = predict(x1,x2) error = 0.5 * pow(y-z1_II,2) delta = y-z1_II * activation_drv(a1_II) w11_II += delta * z1_I * alpha w12_II += delta * z2_I * alpha w13_II += delta * z3_I * alpha w11_I += delta * w11_II * activation_drv(a1_I) * x1 * alpha w12_I += delta * w11_II * activation_drv(a1_I) * x2 * alpha w21_I += delta * w12_II * activation_drv(a2_I) * x1 * alpha w22_I += delta * w12_II * activation_drv(a2_I) * x2 * alpha w31_I += delta * w13_II * activation_drv(a3_I) * x1 * alpha w32_I += delta * w13_II * activation_drv(a3_I) * x2 * alpha b_I += (delta * w11_II * activation_drv(a1_I) + delta * w12_II * activation_drv(a2_I) + delta * w13_II * activation_drv(a3_I)) * alpha return error data = [ [0,0,0], [0,1,1], [1,0,1], [1,1,1], ] for i in range(0,10): err = 0 dt = data[::] random.shuffle(dt) for j in dt: err += train(j[0],j[1],j[2],0.01) print(err) print("-"*30) for j in data: _, _, _, _, _, _, _, res = predict(j[0],j[1]) print(j[0],",",j[1],"=",res) For example the result of the code is: 0.363894453262 0.366966815948 0.366406041572 0.369982058232 0.36988850637 0.375869833099 0.378106172616 0.380456639936 0.37901554717 0.383723920259 ------------------------------ (0, ',', 0, '=', 0.8439871540493414) (0, ',', 1, '=', 0.861714406183168) (1, ',', 0, '=', 0.8515477541104413) (1, ',', 1, '=', 0.8676931366534011) ---------------- UPDATE ---------------- I change codes to this : import random from math import exp,pow def random_weight(): return random.random() def sigmoid(x): return 1.0 / (1.0 + exp(-x)) def sigmoid_drv(x): return sigmoid(x)*(1.0-sigmoid(x)) w11_I = random_weight() w12_I = random_weight() w21_I = random_weight() w22_I = random_weight() w31_I = random_weight() w32_I = random_weight() w11_II = random_weight() w12_II = random_weight() w13_II = random_weight() b_I = random_weight() activation = sigmoid activation_drv = sigmoid_drv def predict(x1,x2): global w11_I,w12_I,w21_I,w22_I,w31_I,w32_I,w11_II,w12_II,w13_II,b_I a1_I = w11_I*x1 + w12_I*x2 + b_I z1_I = activation(a1_I) a2_I = w21_I*x1 + w22_I*x2 + b_I z2_I = activation(a2_I) a3_I = w31_I*x1 + w32_I*x2 + b_I z3_I = activation(a3_I) a1_II = w11_II*z1_I + w12_II*z2_I + w13_II*z3_I z1_II = activation(a1_II) return a1_I, z1_I, a2_I, z2_I, a3_I, z3_I, a1_II, z1_II def train(x1,x2,y,alpha): global w11_I,w12_I,w21_I,w22_I,w31_I,w32_I,w11_II,w12_II,w13_II,b_I a1_I, z1_I, a2_I, z2_I, a3_I, z3_I, a1_II, z1_II = predict(x1,x2) error = 0.5 * pow(z1_II-y,2) delta = z1_II-y * activation_drv(a1_II) d_w11_II = delta * z1_I * alpha d_w12_II = delta * z2_I * alpha d_w13_II = delta * z3_I * alpha d_w11_I = delta * w11_II * activation_drv(a1_I) * x1 * alpha d_w12_I = delta * w11_II * activation_drv(a1_I) * x2 * alpha d_w21_I = delta * w12_II * activation_drv(a2_I) * x1 * alpha d_w22_I = delta * w12_II * activation_drv(a2_I) * x2 * alpha d_w31_I = delta * w13_II * activation_drv(a3_I) * x1 * alpha d_w32_I = delta * w13_II * activation_drv(a3_I) * x2 * alpha d_b_I = (delta * w11_II * activation_drv(a1_I) + delta * w12_II * activation_drv(a2_I) + delta * w13_II * activation_drv(a3_I)) * alpha w11_II -= d_w11_II w12_II -= d_w12_II w13_II -= d_w13_II w11_I -= d_w11_I w12_I -= d_w12_I w21_I -= d_w21_I w22_I -= d_w22_I w31_I -= d_w31_I w32_I -= d_w32_I b_I -= d_b_I return error data = [ [0,0,0], [0,1,0], [1,0,0], [1,1,1], ] for i in range(0,10): err = 0 dt = data[::] random.shuffle(dt) for j in dt: err += train(j[0],j[1],j[2],0.01) print(err) print("-"*30) for j in data: _, _, _, _, _, _, _, res = predict(j[0],j[1]) print(j[0],",",j[1],"=",res) I'm subtract weight errors with weights now. Error of network reduces. But prediction is still wrong. The result of above code: 0.7793443881847488 0.7577581315356949 0.7432698222320477 0.7316129719356839 0.7160385688813552 0.6943522088277978 0.6862277294774705 0.6656984495700775 0.6584361784187711 0.6410006126876817 ------------------------------ 0 , 0 = 0.6049212721996029 0 , 1 = 0.6227402202339664 1 , 0 = 0.6139758543180651 1 , 1 = 0.6293581473456563
One possible error is in the calculation of delta: delta = z1_II-y * activation_drv(a1_II) Add braces and change this to: delta = (z1_II-y) * activation_drv(a1_II)
I found the problem the sigmoid function was not good for this network. I change it to tanh and prediction results is correct now. the final code : import random from math import exp,pow class ANN: def random_weight(self): return random.random() def sigmoid(self,x): return 1.0 / (1.0 + exp(-x)) def sigmoid_drv(self,x): return self.sigmoid(x)*(1.0-self.sigmoid(x)) def tanh(self, x): return (exp(x) - exp(-x)) / (exp(x) + exp(-x)) def tanh_drv(self,x): return 1 - pow(self.tanh(x),2) def __init__(self): self.w11_I = self.random_weight() self.w12_I = self.random_weight() self.w21_I = self.random_weight() self.w22_I = self.random_weight() self.w31_I = self.random_weight() self.w32_I = self.random_weight() self.w11_II = self.random_weight() self.w12_II = self.random_weight() self.w13_II = self.random_weight() self.b_I = self.random_weight() self.activation = self.tanh self.activation_drv = self.tanh_drv def predict(self,x1,x2): a1_I = self.w11_I*x1 + self.w12_I*x2 + self.b_I z1_I = self.activation(a1_I) a2_I = self.w21_I*x1 + self.w22_I*x2 + self.b_I z2_I = self.activation(a2_I) a3_I = self.w31_I*x1 + self.w32_I*x2 + self.b_I z3_I = self.activation(a3_I) a1_II = self.w11_II*z1_I + self.w12_II*z2_I + self.w13_II*z3_I z1_II = self.activation(a1_II) return a1_I, z1_I, a2_I, z2_I, a3_I, z3_I, a1_II, z1_II def train(self,x1,x2,y,alpha): a1_I, z1_I, a2_I, z2_I, a3_I, z3_I, a1_II, z1_II = self.predict(x1,x2) error = 0.5 * pow(z1_II-y,2) delta = (z1_II-y) * self.activation_drv(a1_II) d_w11_II = delta * z1_I * alpha d_w12_II = delta * z2_I * alpha d_w13_II = delta * z3_I * alpha d_w11_I = delta * self.w11_II * self.activation_drv(a1_I) * x1 * alpha d_w12_I = delta * self.w11_II * self.activation_drv(a1_I) * x2 * alpha d_w21_I = delta * self.w12_II * self.activation_drv(a2_I) * x1 * alpha d_w22_I = delta * self.w12_II * self.activation_drv(a2_I) * x2 * alpha d_w31_I = delta * self.w13_II * self.activation_drv(a3_I) * x1 * alpha d_w32_I = delta * self.w13_II * self.activation_drv(a3_I) * x2 * alpha d_b_I = (delta * self.w11_II * self.activation_drv(a1_I) + delta * self.w12_II * self.activation_drv(a2_I) + delta * self.w13_II * self.activation_drv(a3_I)) * alpha self.w11_II -= d_w11_II self.w12_II -= d_w12_II self.w13_II -= d_w13_II self.w11_I -= d_w11_I self.w12_I -= d_w12_I self.w21_I -= d_w21_I self.w22_I -= d_w22_I self.w31_I -= d_w31_I self.w32_I -= d_w32_I self.b_I -= d_b_I return error model = ANN() data = [ [0,0,0], [0,1,0], [1,0,0], [1,1,1], ] for i in range(0,200): err = 0 dt = data[::] random.shuffle(dt) for j in dt: err += model.train(j[0],j[1],j[2],0.1) print(err) print("-"*30) for j in data: _, _, _, _, _, _, _, res = model.predict(j[0],j[1]) print(j[0],",",j[1],"=",res) Result of code : ... 0.1978539306282795 0.19794670251861882 0.19745074826953185 0.19529942727878868 0.19779970636626873 0.19661596298810918 ------------------------------ 0 , 0 = -0.24217968147818447 0 , 1 = 0.236033934015224 1 , 0 = 0.24457439328909888 1 , 1 = 0.5919949310028919
Neural Network in python only learning for 1 in-/output pair per dataset?
I wrote code for generating and training artifiacial neural networks in python recently (with backpropagation). It all works very well, but this one problem bugs me for days now and I can't seem to find why: The network I generate only decreases the error over my datasets if the sets only contain 1 in-/output pair. E.g. my input is [[1,1]] for 2 input neurons and for simplicity also [[1,1]] as output. That works well, the error decreases. But if I choose [[1,1],[0,0]] as input, the error alternates or somestimes just goes against 2, no matter how long I let it work. Here is my code: import numpy as np from graphics import * import math class Neuron(): def __init__(self,input=0,output=0,activation_func_key="logistic",bias=False,inp=False): self.b = bias self.input = input + self.b self.output = output self.key = activation_func_key self.inp = inp def add_to_input(self,input_=0): self.input += input_ def add_to_input(self,input_=0): self.input += float(input_) def get_output(self): if self.b: return 1 if self.inp: return self.input if self.key is "relu": return np.maximum((self.input),0) if self.key is "tanh": return np.tanh(self.input) if self.key is "softplus": return np.log(1+np.e**(self.input)) if self.key is "logistic": return 1/(1+np.e**-(self.input)) def reset(self): self.input = 0 self.ouput = 0 class Network(): def __init__(self,weight_matrix,input_neuron_num,hidden_neuron_nums,ouput_neuron_num,activation_f="relu"): self.weight_matrix = weight_matrix self.neurons = [Neuron(activation_func_key=activation_f) for _ in range(weight_matrix[0].__len__())] back_layers = 0 val = input_neuron_num counter = 0 for i in range(weight_matrix[0].__len__()): bias = False inp = False if i is val: bias = True if counter < hidden_neuron_nums.__len__(): val += hidden_neuron_nums[counter]+1 counter += 1 if i < input_neuron_num: inp = True self.neurons[i] = Neuron(activation_func_key=activation_f,inp=inp,bias=bias) self.input_neuron_num = input_neuron_num self.hidden_neuron_nums = hidden_neuron_nums self.output_neuron_num = ouput_neuron_num def propagate_forward(self,inputv,output_len): for i in range(self.neurons.__len__()): if i < self.input_neuron_num: self.neurons[i].add_to_input(inputv[i]) else: spalte = self.weight_matrix[:][i] for j in range(spalte.__len__()): #if i > j: #nur obere dreiecksmatrix self.neurons[i].add_to_input(self.weight_matrix[j][i] * self.neurons[j].get_output()) output = [] for i in range(output_len): output.append(self.neurons[self.weight_matrix[0].__len__()-i-1].get_output()) output.reverse() return output def reset(self): for n in self.neurons: n.reset() def make_net(inp,hidden,out,activation_f="logistic"): hsum = 0 for i in range(hidden.__len__()): hsum += hidden[i]+1 sum = inp + 1 + hsum + out weights = [[0 for _ in range(sum)] for _ in range(sum)] neurons_till_this_layer = 0 for i in range(inp+1): for j in range(hidden[0]): weights[i][inp+j+1] = np.random.uniform(-1,1) neurons_till_this_layer += inp+1 for i in range(hidden.__len__()-1): for n in range(hidden[i]+1): for m in range(hidden[i+1]): weights[n+neurons_till_this_layer][m+neurons_till_this_layer+hidden[i]+1] = np.random.uniform(-1,1) neurons_till_this_layer += hidden[i]+1 for i in range(hidden[hidden.__len__()-1]+1): for j in range(out): weights[i+neurons_till_this_layer][j+neurons_till_this_layer+hidden[hidden.__len__()-1]+1] = np.random.uniform(-1,1) net = Network(weights,inp,hidden,out,activation_f=activation_f) return net def backpropagation(net,inputs,outputs,learning_rate=0.001,epsilon=0.1,draw=False,win=0): if draw: win = 0 if win is not 0: win = win else: win = draw_window(net) if inputs[0][:].__len__() is not net.input_neuron_num: print("Error: Input length does not match! No learning could be done!") return net if outputs[0][:].__len__() is not net.output_neuron_num: print("Error: Output length does not match! No learning could be done!") return net hsum = 0 for i in range(net.hidden_neuron_nums.__len__()): hsum += net.hidden_neuron_nums[i]+1 outer_delta_w_matrix = [[0 for _ in range(net.input_neuron_num + 1 + hsum + net.output_neuron_num)] for _ in range(net.input_neuron_num + 1 + hsum + net.output_neuron_num)] for N in range(inputs.__len__()): net_output = net.propagate_forward(inputs[N],outputs[0].__len__()) delta_w_matrix = [[0 for _ in range(net.input_neuron_num + 1 + hsum + net.output_neuron_num)] for _ in range(net.input_neuron_num + 1 + hsum + net.output_neuron_num)] delta_j_list = [float(0) for _ in range(net.input_neuron_num + 1 + hsum + net.output_neuron_num)] for i in range(net.input_neuron_num + 1 + hsum + net.output_neuron_num): j = net.input_neuron_num + 1 + hsum + net.output_neuron_num - i - 1 derivate = "error" if net.neurons[j].key is "relu": derivate = 0 if net.neurons[j].input <= 0 else 1 if net.neurons[j].key is "tanh": derivate = (1 / (np.cosh(net.neurons[j].input) * np.cosh(net.neurons[j].input))) if net.neurons[j].key is "softplus": derivate = 1 / (1 + np.e ** -net.neurons[j].input) if net.neurons[j].key is "logistic": #print(" input: %f" % net.neurons[j].input) derivate = (np.exp(net.neurons[j].input)) / (((np.exp(net.neurons[j].input)) + 1) ** 2) #derivate = net.neurons[j].output * (1 - net.neurons[i].get_output()) if i < net.output_neuron_num: delta_j_list[j] = derivate * (net.neurons[j].get_output() - float(outputs[N][net.output_neuron_num-i-1])) else: prev_delta_sum = 0 for x in range(net.weight_matrix[j][:].__len__()): prev_delta_sum += (delta_j_list[j-x] * net.weight_matrix[j][x]) #print(net.neurons[j].input) delta_j_list[j] = derivate * prev_delta_sum for i in range(net.input_neuron_num + 1 + hsum + net.output_neuron_num): for j in range(net.input_neuron_num + 1 + hsum + net.output_neuron_num): if net.weight_matrix[i][j]: delta_w_matrix[i][j] = -learning_rate * delta_j_list[j] * net.neurons[i].get_output() for i in range(net.input_neuron_num + 1 + hsum + net.output_neuron_num): for j in range(net.input_neuron_num + 1 + hsum + net.output_neuron_num): outer_delta_w_matrix[i][j] += (delta_w_matrix[i][j] / (inputs.__len__())) #print_matrix(net.weight_matrix) #print() #print("Completed cycle") net.reset() for i in range(net.input_neuron_num + hsum + net.output_neuron_num): for j in range(net.input_neuron_num + hsum + net.output_neuron_num): if net.weight_matrix[i][j]: net.weight_matrix[i][j] = net.weight_matrix[i][j] + outer_delta_w_matrix[i][j] if draw: draw_net(net, win) return net def print_matrix(matrix): a = matrix[0][:] b = matrix[:][0] for i in range(a.__len__()): print() for j in range(b.__len__()): sys.stdout.write("% 1.7f " % matrix[i][j]) print() def get_dataset_binary(dataset_length=1000,max=4): rdm_nums = [np.random.randint(0,2**max) for _ in range(dataset_length)] bin_nums = [0 for _ in range(rdm_nums.__len__())] length = max for i in range(rdm_nums.__len__()): bin_nums[i] = "{0:b}".format(rdm_nums[i]) while bin_nums[i].__len__() < length: bin_nums[i] = "0" + bin_nums[i] r_value = [0 for _ in range(dataset_length)] for i in range(dataset_length): r_value[i] = list(bin_nums[i]) return r_value def draw_window(net,autoflush=False): width = 0 for i in range(net.hidden_neuron_nums.__len__()): if net.hidden_neuron_nums[i] > width: width = net.hidden_neuron_nums[i] if net.input_neuron_num > width: width = net.input_neuron_num if net.output_neuron_num > width: width = net.output_neuron_num scale_factor = 0.7 counter = 0 r = 50*scale_factor v_buf = 100*scale_factor h_buf = 50*scale_factor d1 = 20*scale_factor d2 = 12*scale_factor return GraphWin("Netzwerk", (2*h_buf)+(width*2*r)+(width*h_buf)-h_buf, 2 * v_buf + 2*r+v_buf + net.hidden_neuron_nums.__len__()*2*r + net.hidden_neuron_nums.__len__()*v_buf-v_buf + 2*r+v_buf, autoflush=autoflush) def draw_net(net,win=0): width = 0 for i in range(net.hidden_neuron_nums.__len__()): if net.hidden_neuron_nums[i] > width: width = net.hidden_neuron_nums[i]+1 if net.input_neuron_num > width: width = net.input_neuron_num+1 if net.output_neuron_num > width: width = net.output_neuron_num scale_factor = 0.7 counter = 0 r = 50*scale_factor v_buf = 100*scale_factor h_buf = 50*scale_factor d1 = 20*scale_factor d2 = 12*scale_factor if win is 0: win = GraphWin("Netzwerk", (2*h_buf)+(width*2*r)+(width*h_buf)-h_buf, 2 * v_buf + 2*r+v_buf + net.hidden_neuron_nums.__len__()*2*r + net.hidden_neuron_nums.__len__()*v_buf-v_buf + 2*r+v_buf) for i in range(net.weight_matrix.__len__()): for j in range(net.weight_matrix.__len__()): if net.weight_matrix[i][j] is not 0: from_pos = get_neuron_pos(net,i,r,v_buf,h_buf,width) to_pos = get_neuron_pos(net,j,r,v_buf,h_buf,width) thickness = np.abs(net.weight_matrix[i][j]) * 10 weight = Polygon([Point(from_pos.x-thickness,from_pos.y), Point(from_pos.x+thickness,from_pos.y),Point(to_pos.x+thickness,to_pos.y), Point(to_pos.x-thickness,to_pos.y)]) if net.weight_matrix[i][j] > 0: weight.setFill("firebrick4") else: weight.setFill("lightskyblue") label = Text(Point((from_pos.x+to_pos.x)/2, (from_pos.y+to_pos.y)/2), "%0.5f" % net.weight_matrix[i][j]) label.setSize(np.maximum(int(10*scale_factor),5)) weight.draw(win) label.draw(win) hidden_neuron_num = 0 for i in range(net.hidden_neuron_nums.__len__()): hidden_neuron_num += net.hidden_neuron_nums[i]+1 for i in range(net.input_neuron_num+1): inp_neuron = Circle(Point( (win.width - ((net.input_neuron_num+1) * 2 * r + (net.input_neuron_num+1) * h_buf - h_buf))/2 + i*(2*r+h_buf) + r, v_buf+r),r) inp_neuron.setFill("springgreen3") if i is net.input_neuron_num: inp_neuron.setFill("snow") name = Text(Point((win.width - ((net.input_neuron_num+1) * 2 * r + (net.input_neuron_num+1) * h_buf - h_buf))/2 + i*(2*r+h_buf) + r, v_buf+r-d1), "%s" % ("Neuron(in) " + str(counter+1))) if i is net.input_neuron_num: name.setText("%s" % ("Neuron(bias) " + str(counter+1))) label = Text(Point((win.width - ((net.input_neuron_num+1) * 2 * r + (net.input_neuron_num+1) * h_buf - h_buf))/2 + i*(2*r+h_buf) + r, v_buf+r+d2), "Output:\n%1.6f" % float(net.neurons[counter].get_output())) name.setSize(np.maximum(int(10*scale_factor),5)) label.setSize(np.maximum(int(10*scale_factor),5)) inp_neuron.draw(win) name.draw(win) label.draw(win) counter +=1 for i in range(net.hidden_neuron_nums.__len__()): for j in range(net.hidden_neuron_nums[i]+1): hidden_neuron = Circle(Point((win.width-((net.hidden_neuron_nums[i]+1)*2*r+(net.hidden_neuron_nums[i]+1)*h_buf-h_buf))/2+j*(2*r+h_buf)+r,(3*r+2*v_buf)+i*(2*r+v_buf)),r) hidden_neuron.setFill("lavender") if j is net.hidden_neuron_nums[i]: hidden_neuron.setFill("snow") name = Text(Point((win.width-((net.hidden_neuron_nums[i]+1)*2*r+(net.hidden_neuron_nums[i]+1)*h_buf-h_buf))/2+j*(2*r+h_buf)+r, (3*r+2*v_buf)+i*(2*r+v_buf)-d1), "%s" % ("Neuron " + str(counter+1))) if j is net.hidden_neuron_nums[i]: name.setText("%s" % ("Neuron(bias) " + str(counter+1))) label = Text(Point((win.width-((net.hidden_neuron_nums[i]+1)*2*r+(net.hidden_neuron_nums[i]+1)*h_buf-h_buf))/2+j*(2*r+h_buf)+r, (3*r+2*v_buf)+i*(2*r+v_buf)+d2), "Output:\n%1.6f" % net.neurons[counter].get_output()) name.setSize(np.maximum(int(10*scale_factor),5)) label.setSize(np.maximum(int(10*scale_factor),5)) hidden_neuron.draw(win) name.draw(win) label.draw(win) counter += 1 for i in range(net.output_neuron_num): out_neuron = Circle(Point((win.width-(net.output_neuron_num*2*r+net.output_neuron_num*h_buf-h_buf))/2+i*(2*r+h_buf)+r, net.hidden_neuron_nums.__len__()*2*r + 2*r + net.hidden_neuron_nums.__len__()*v_buf + 2* v_buf + r),r) out_neuron.setFill("darkgoldenrod3") name = Text(Point((win.width-(net.output_neuron_num*2*r+net.output_neuron_num*h_buf-h_buf))/2+i*(2*r+h_buf)+r, net.hidden_neuron_nums.__len__()*2*r + 2*r + net.hidden_neuron_nums.__len__()*v_buf + 2* v_buf + r - d1), "%s" % ("Neuron(out) " + str(counter+1))) label = Text(Point((win.width-(net.output_neuron_num*2*r+net.output_neuron_num*h_buf-h_buf))/2+i*(2*r+h_buf)+r, net.hidden_neuron_nums.__len__()*2*r + 2*r + net.hidden_neuron_nums.__len__()*v_buf + 2* v_buf + r + d2), "Output:\n%1.6f" % net.neurons[counter].get_output()) name.setSize(np.maximum(int(10*scale_factor),5)) label.setSize(np.maximum(int(10*scale_factor),5)) out_neuron.draw(win) name.draw(win) label.draw(win) counter += 1 win.update() win.wait_window() def get_neuron_pos(net, n, r, v_buf, h_buf, width): if n < net.input_neuron_num+1: return Point(((2*h_buf)+(width*2*r)+(width*h_buf)-h_buf - ((net.input_neuron_num+1) * 2 * r + (net.input_neuron_num+1) * h_buf - h_buf)) / 2 + n * (2 * r + h_buf) + r, v_buf + r) current = net.input_neuron_num+1 for h in range(net.hidden_neuron_nums.__len__()): current += (net.hidden_neuron_nums[h]+1) if n < current: return Point(((2*h_buf)+(width*2*r)+(width*h_buf)-h_buf-((net.hidden_neuron_nums[h]+1)*2*r+(net.hidden_neuron_nums[h]+1)*h_buf-h_buf))/2+(n-(current-net.hidden_neuron_nums[h]-1))*(2*r+h_buf)+r,(3*r+2*v_buf)+h*(2*r+v_buf)) if n - current < net.output_neuron_num: return Point(((2*h_buf)+(width*2*r)+(width*h_buf)-h_buf-((net.output_neuron_num)*2*r+(net.output_neuron_num)*h_buf-h_buf))/2+(n-current)*(2*r+h_buf)+r, net.hidden_neuron_nums.__len__()*2*r + 2*r + net.hidden_neuron_nums.__len__()*v_buf + 2* v_buf + r) return -1 def backpropagation_(net,inputs,outputs,learning_rate=0.01,epsilon=0.1): endlich_fertig = False while not endlich_fertig: net = backpropagation(net,inputs,outputs,learning_rate,epsilon) endlich_fertig = True error = 0 for i in range(inputs.__len__()): output = net.propagate_forward(inputs[i], outputs[0][:].__len__()) for j in range(output.__len__()): error += np.abs(output[j] - float(outputs[i][j])) if error > epsilon: endlich_fertig = False print("Error: %f" % error) return net net = make_net(2,[3,2],2) print_matrix(net.weight_matrix) inputs = [[1,1],[0,0]] outputs = inputs draw_net(net) trained_net = backpropagation_(net,inputs,outputs) draw_net(trained_net) EDIT: Put in full code so you can easily reproduce the behaviour locally
Python function gets stuck (no error) but I can't understand why
The function does what I want it to, but when it's done it just sits there rather than continuing from where I called it and I can't figure out why. The code is: x = 9 y = 9 n = 10 ty = 1 tx = 1 while ty <= y: while tx <= x: vars()["p" + str(ty) + str(tx)] = 0 tx += 1 ty += 1 tx = 1 tn = 1 while tn <= n: vars()["m" + str(tn)] = tn tn += 1 t = x * y tm = n def recursion(z): global tm if z < n: for x in range(n): recursion(z + 1) else: if tm > 0: tv = "m" + str(tm) otm = eval(tv) while eval(tv) < t - n + tm: vars()[tv] = eval(tv) + 1 print(tv + " = " + str(eval(tv))) vars()[tv] = otm + 1 print(tv + " = " + str(eval(tv))) if tm > 1: vars()["m" + str(tm - 1)] = eval("m" + str(tm - 1)) + 1 print(str("m" + str(tm - 1) + " = " + str(eval("m" + str(tm -1))))) tm -= 1 recursion(1) print("done") I've put the return in where I would expect it to end but as far as I know it shouldn't actually need it. Can anyone see what I've done to cause it to get stuck? Thanks
Note for people not going through the change history: This is based on the comments on other answers. UPDATE: Better version. import itertools def permutations(on, total): all_indices = range(total) for indices in itertools.combinations(all_indices, on): board = ['0'] * total for index in indices: board[index] = '1' yield ''.join(board)
If anyone is interested in what the original code did, I rearranged the conditionals to prune the tree of function calls: x = 9 y = 9 n = 10 ty = 1 tx = 1 while ty <= y: while tx <= x: vars()["p" + str(ty) + str(tx)] = 0 tx += 1 ty += 1 tx = 1 tn = 1 while tn <= n: vars()["m" + str(tn)] = tn tn += 1 t = x * y tm = n def recursion(z): global tm if tm > 0: if z < n: for x in range(n): recursion(z + 1) else: tv = "m" + str(tm) otm = eval(tv) while eval(tv) < t - n + tm: vars()[tv] = eval(tv) + 1 print(tv + " = " + str(eval(tv))) vars()[tv] = otm + 1 print(tv + " = " + str(eval(tv))) if tm > 1: vars()["m" + str(tm - 1)] = eval("m" + str(tm - 1)) + 1 print(str("m" + str(tm - 1) + " = " + str(eval("m" + str(tm -1))))) tm -= 1 recursion(1) print("done") This could be made much clearer through the use of lists and range objects, but that takes effort.
I wasn't able to work out what was happening (turns out if I left it for a few minutes it would actually finish though), instead, I realised that I didn't need to use recursion to achieve what I wanted (and I also realised the function didn't actually do what I want to do). For anyone interested, I simplified and rewrote it to be a few while loops instead: x = 9 y = 9 t = x * y n = 10 tt = 1 while tt <= t: vars()["p" + str(tt)] = 0 tt += 1 tn = 1 while tn <= n: vars()["m" + str(tn)] = tn vars()["p" + str(tn)] = 1 tn += 1 def cl(): w = "" tt = 1 while tt <= t: w = w + str(eval("p" + str(tt))) tt += 1 p.append(w) tm = n tv = "m" + str(tm) p = [] while m1 < t - n + tm - 1: cl() while tm == n and eval(tv) < t - n + tm: vars()["p" + str(eval(tv))] = 0 vars()[tv] = eval(tv) + 1 vars()["p" + str(eval(tv))] = 1 cl() tm -= 1 tv = "m" + str(tm) while tm < n and tm > 0: if eval(tv) < t - n + tm: vars()["p" + str(eval(tv))] = 0 vars()[tv] = eval(tv) + 1 vars()["p" + str(eval(tv))] = 1 while tm < n: tm += 1 ptv = tv tv = "m" + str(tm) vars()["p" + str(eval(tv))] = 0 vars()[tv] = eval(ptv) + 1 vars()["p" + str(eval(tv))] = 1 else: tm -= 1 tv = "m" + str(tm)