#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# initialize
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
X=[[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 2], [1, 3], [2, 3], [3, 3], [4, 1], [4, 2], [4, 3]]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t=[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
v1 = [.1, -.1, .1]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
v2 = [.1, .1, -.1]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
w = [-.1, .1, .1]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lr = 0.0075
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
alpha = 0.0
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for _ in range(1000):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
total_error = 0
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for i in range(len(X)):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x=X[i]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#print(classify(x, v1, v2, w))
# feedforward
z1 = np.tanh(v1[0]+np.dot(x, v1[1:]))
z2 = np.tanh(v2[0]+np.dot(x, v2[1:]))
y = 1./(1+np.exp(-w[0]-np.dot(w[1:], [z1, z2])))
# backward
y_error = (1-t[i])*y - t[i]*(1-y)
total_error -= (1-t[i])*np.log(1-y) + t[i]*np.log(y)
#print(y_error)
#print()
z1_error = y_error*w[1]*(1-z1)*(1+z1)
z2_error = y_error*w[2]*(1-z2)*(1+z2)
# update weights
w[0] = w[0] - lr*y_error*1 - lr*alpha*w[0]
w[1] = w[1] - lr*y_error*z1 - lr*alpha*w[1]
w[2] = w[2] - lr*y_error*z2 - lr*alpha*w[2]
v1[0] = v1[0] - lr*z1_error*1 - lr*alpha*v1[0]
v1[1] = v1[1] - lr*z1_error*x[0] - lr*alpha*v1[1]
v1[2] = v1[2] - lr*z1_error*x[1] - lr*alpha*v1[2]
v2[0] = v2[0] - lr*z2_error*1 - lr*alpha*v2[0]
v2[1] = v2[1] - lr*z2_error*x[0] - lr*alpha*v2[1]
v2[2] = v2[2] - lr*z2_error*x[1] - lr*alpha*v2[2]
print("TE: \t%0.3f" %total_error)
for x in X:
print(classify(x, v1, v2, w))
nx = ny = 200
x_min = y_min = 0
x_max = y_max = 5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, nx), np.linspace(y_min, y_max, ny))
D=np.c_[xx.ravel(), yy.ravel()]
p=np.array([classify(d, v1, v2, w) for d in D])
p=p.reshape(xx.shape)
for i in range(len(X)):
if t[i] == 1:
plt.plot(X[i][0], X[i][1], 'o', color='blue', markersize=20)
else:
plt.plot(X[i][0], X[i][1], 'o', color='red', markersize=20)
plt.pcolormesh(xx, yy, p, cmap='red_blue_classes',
norm=colors.Normalize(0., 1.))
plt.contour(xx, yy, p, [0.5], linewidths=2., colors='k')
nx = ny = 200
x_min = y_min = 0
x_max = y_max = 5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, nx), np.linspace(y_min, y_max, ny))
D=np.c_[xx.ravel(), yy.ravel()]
Dp = []
t = []
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for d in D:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if d[0] > 2 and d[0] < 3 and d[1] > 2 and d[1] < 3:
Dp.append(d)
t.append(1)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
elif (d[0] < 1 or d[0] > 4) or (d[1] < 1 or d[1] > 4):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if np.random.rand() < 0.05:
Dp.append(d)
t.append(0)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
np.mean(t)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
X=Dp
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for i in range(len(X)):
if t[i] == 1:
plt.plot(X[i][0], X[i][1], 'o', color='blue', markersize=3)
else:
plt.plot(X[i][0], X[i][1], 'o', color='red', markersize=3)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
num_hidden = 4
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def classify2(x, vs, w):
nh = len(vs)
zs = []
for i in range(nh):
v = vs[i]
zs.append(np.tanh(v[0] + np.dot(v[1:], x)))
y = 1./(1.+np.exp(-w[0]-np.dot(w[1:], zs)))
return y
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
X=Dp
vs = []
w = []
w.append(np.random.rand()*2-1)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for _ in range(num_hidden):
v = []
for _ in range(3):
v.append(np.random.rand()*2-1)
vs.append(v)
w.append(np.random.rand()*2-1)
lr = 0.005
alpha = 0.0
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
try:
for _ in range(100):
total_error = 0
for i in range(len(X[:])):
x=X[i]
#print(classify(x, v1, v2, w))
# feedforward
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# initialize
X=[[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 2], [1, 3], [2, 3], [3, 3], [4, 1], [4, 2], [4, 3]]
t=[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
v1 = [.1, -.1, .1]
v2 = [.1, .1, -.1]
w = [-.1, .1, .1]
lr = 0.0075
alpha = 0.0
for _ in range(1000):
total_error = 0
for i in range(len(X)):
x=X[i]
#print(classify(x, v1, v2, w))
# feedforward
z1 = np.tanh(v1[0]+np.dot(x, v1[1:]))
z2 = np.tanh(v2[0]+np.dot(x, v2[1:]))
y = 1./(1+np.exp(-w[0]-np.dot(w[1:], [z1, z2])))
# backward
y_error = (1-t[i])*y - t[i]*(1-y)
total_error -= (1-t[i])*np.log(1-y) + t[i]*np.log(y)
#print(y_error)
#print()
z1_error = y_error*w[1]*(1-z1)*(1+z1)
z2_error = y_error*w[2]*(1-z2)*(1+z2)
# update weights
w[0] = w[0] - lr*y_error*1 - lr*alpha*w[0]
w[1] = w[1] - lr*y_error*z1 - lr*alpha*w[1]
w[2] = w[2] - lr*y_error*z2 - lr*alpha*w[2]
v1[0] = v1[0] - lr*z1_error*1 - lr*alpha*v1[0]
v1[1] = v1[1] - lr*z1_error*x[0] - lr*alpha*v1[1]
v1[2] = v1[2] - lr*z1_error*x[1] - lr*alpha*v1[2]
v2[0] = v2[0] - lr*z2_error*1 - lr*alpha*v2[0]
v2[1] = v2[1] - lr*z2_error*x[0] - lr*alpha*v2[1]
v2[2] = v2[2] - lr*z2_error*x[1] - lr*alpha*v2[2]
print("TE: \t%0.3f" %total_error)
for x in X:
print(classify(x, v1, v2, w))
nx = ny = 200
x_min = y_min = 0
x_max = y_max = 5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, nx), np.linspace(y_min, y_max, ny))
D=np.c_[xx.ravel(), yy.ravel()]
p=np.array([classify(d, v1, v2, w) for d in D])
p=p.reshape(xx.shape)
for i in range(len(X)):
if t[i] == 1:
plt.plot(X[i][0], X[i][1], 'o', color='blue', markersize=20)
else:
plt.plot(X[i][0], X[i][1], 'o', color='red', markersize=20)
plt.pcolormesh(xx, yy, p, cmap='red_blue_classes',
norm=colors.Normalize(0., 1.))
plt.contour(xx, yy, p, [0.5], linewidths=2., colors='k')
nx = ny = 200
x_min = y_min = 0
x_max = y_max = 5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, nx), np.linspace(y_min, y_max, ny))
D=np.c_[xx.ravel(), yy.ravel()]
Dp = []
t = []
for d in D:
if d[0] > 2 and d[0] < 3 and d[1] > 2 and d[1] < 3:
Dp.append(d)
t.append(1)
elif (d[0] < 1 or d[0] > 4) or (d[1] < 1 or d[1] > 4):
if np.random.rand() < 0.05:
Dp.append(d)
t.append(0)
np.mean(t)
X=Dp
for i in range(len(X)):
if t[i] == 1:
plt.plot(X[i][0], X[i][1], 'o', color='blue', markersize=3)
else:
plt.plot(X[i][0], X[i][1], 'o', color='red', markersize=3)
num_hidden = 4
def classify2(x, vs, w):
nh = len(vs)
zs = []
for i in range(nh):
v = vs[i]
zs.append(np.tanh(v[0] + np.dot(v[1:], x)))
y = 1./(1.+np.exp(-w[0]-np.dot(w[1:], zs)))
return y
X=Dp
vs = []
w = []
w.append(np.random.rand()*2-1)
for _ in range(num_hidden):
v = []
for _ in range(3):
v.append(np.random.rand()*2-1)
vs.append(v)
w.append(np.random.rand()*2-1)
lr = 0.005
alpha = 0.0
try:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for _ in range(100):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
total_error = 0
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for i in range(len(X[:])):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x=X[i]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#print(classify(x, v1, v2, w))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# feedforward
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zs = []
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~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~
#print(y)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# backward
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
y_error = (1-t[i])*y - t[i]*(1-y)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
total_error -= (1-t[i])*np.log(1-y) + t[i]*np.log(y)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#print(y_error)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
z_errors = []
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for j in range(num_hidden):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
z_errors.append(y_error*w[j+1]*(1-zs[j])*(1+zs[j]))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#print(z_errors)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# update weights
#~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~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p=[classify2(d, vs, w) for d in D]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p=np.array(p)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p=p.reshape(xx.shape)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
X=Dp
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for i in range(len(X)):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if t[i] == 1:
plt.plot(X[i][0], X[i][1], 'o', color='blue', markersize=8)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else:
plt.plot(X[i][0], X[i][1], 'o', color='red', markersize=8)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plt.pcolormesh(xx, yy, p, cmap='red_blue_classes',
norm=colors.Normalize(0., 1.))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plt.contour(xx, yy, p, [0.5], linewidths=2., colors='k')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
classify2([2.5, 2.5], vs, w)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
classify2([0, 0], vs, w)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
classify2([5, 5], vs, w)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vs = [[-1.4395892690558445, -0.6994640575516157, 2.4763238083720416],
[-5.832152599201117, 1.2801824017482004, 0.5347291187258975],
[1.0461615987520945, -3.683980121271858, 2.024501795098323],
[0.35189345674770495, 1.577772129315875, 1.1894009103278471]]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
w = [-5.266158175713795,
4.933742705326487,
-5.537202130283304,
-5.736361194605192,
-4.393480175813042]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#OR
plt.plot([-1], [-1], 'o', color='red', markersize=20)
plt.plot([-1], [1], 'o', color='blue', markersize=20)
plt.plot([1], [-1], 'o', color='blue', markersize=20)
plt.plot([1], [1], 'o', color='blue', markersize=20)
w=[1, 1, 1]
p=np.array([perceptron(w, d) for d in D])
p=p.reshape(xx.shape)
plt.pcolormesh(xx, yy, p, cmap='red_blue_classes',
norm=colors.Normalize(0., 1.))
plt.contour(xx, yy, p, [0.5], linewidths=2., colors='k')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# XOR
X = np.array([[-1, -1], [-1, 1], [1, -1], [1, 1]])
y = [1, -1, -1, 1]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# AND
X = np.array([[-1, -1], [-1, 1], [1, -1], [1, 1]])
y = [-1, -1, -1, 1]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def solve_for_w(X, y):
ni, nf = X.shape
w=np.zeros(nf+1)
max_iter = 2
lr = 1
for it in range(max_iter):
print("\nIteration", it)
print("Weights", w)
for obj in range(ni):
print("Object %s, label: %d" %(X[obj], y[obj]))
p = perceptron(w, X[obj])
print("Prediction", p)
if p == y[obj]:
print("Prediction is correct")
else:
print("*Prediction is incorrect*")
delta_weights = np.append(lr*y[obj], lr*X[obj]*y[obj])
print("Delta", delta_weights)
w += delta_weights
print("New weights", w)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
r_noisy = r_tr+rg.normal(loc=11, scale=11, size=len(r_tr))
pf = PolynomialFeatures(degree=11)
X_tr=pf.fit_transform(D_tr)
A=construct_A(X_tr)
b=construct_b(X_tr, r_noisy)
w=np.squeeze(np.asarray(A.I*b.T))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def f(x):
return 1 + 2*x
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def likelihood(counts, probs):
return np.product([np.power(probs[i],counts[i]) for i in range(len(counts))])
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def kernel_matrix(X, kf):
ns, nf = X.shape
km = np.zeros((ns, ns))
for i in range(ns):
for j in range(ns):
km[i, j] = kf(X[i], X[j])
return km
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def tanh_kernel(x1, x2):
return np.tanh(2*np.dot(x1, x2)+1)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def radial_basis_kernel(x1, x2, s):
return np.exp(-1*(np.dot(x1-x2, x1-x2)/(2*(s**2))))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def poly_kernel(x1, x2, d=2):
return (np.dot(x1, x2)+1)**d
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def linear_kernel(x1, x2):
return np.dot(x1, x2)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def compute_b(X, y, alphas, kf):
sv_inds = np.arange(len(y))[alphas > 1e-5]
b=0
for i in sv_inds:
b += (y[i] - classify(X, y, alphas, 0, kf, X[i]))
return b / len(sv_inds)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def construct_A(X):
A=[]
for i in range(X.shape[1]):
row = []
for j in range(X.shape[1]):
s = 0
for d in X:
s += d[i]*d[j]
row.append(s)
A.append(row)
return np.matrix(A)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def classify(x, v1, v2, w):
z1 = np.tanh(v1[0]+np.dot(x, v1[1:]))
#print(z1)
z2 = np.tanh(v2[0]+np.dot(x, v2[1:]))
#print(z2)
y = 1./(1+np.exp(-w[0]-np.dot(w[1:], [z1, z2])))
return y
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Perceptron, perspective, perception is $ = 0 = b + x ^.*.^ m = y = z = y = m ^.*.^ x + b = 0 = $ each and everyone of our own asserting understanding the classifying the cases possible pre-invested pre-saved pre-hashed pre-refreshed pre-capacitified pre-capablized definitely all executed if.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Codename, username, name: 99999999999, 88888888888, 77777777777, 66666666666, 55555555555, 44444444444, 33333333333, 22222222222, 11111111111, 00000000000, -11111111111, -22222222222, -33333333333, -44444444444, -55555555555, -66666666666, -77777777777, -88888888888, -99999999999. Manhattanmn, import Khash. K. Otgon. as in Hashaa.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
AI is a 0. symbol, 1. document, 2. thesis, 3. essay, 4. letter. 5. article, 6. paragraph 7. sentence 8. name. 9. word 10. color 11. character 12. pixel.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unpacked. Uniterated. Uncased.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
True code, source code, original code is 50.01%, spinor, quaternion, tensor, matrix, vector, scalar.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Pre-share, Pre-comment, Pre-dislike, Pre-like, Pre-Loyallove, Pre-report, Pre-stop-ads, Pre-download, Pre-clip, Like, Pre-Thanks, Pre-save, Pre-watchlater, Follow, Subscribe, Time /|\ Goldeny Whitey Ultraviolety Darkey Greeny Yellowy Bluey Purpley Pinky Browny Redy Orangey