-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineLearningSimpleM.py
More file actions
348 lines (260 loc) · 6.88 KB
/
MachineLearningSimpleM.py
File metadata and controls
348 lines (260 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 16 16:44:56 2017
@author: ali
"""
import numpy as np
gamma = .9
r=(-0.04)*gamma**5
for i in range(6,6):
r += gamma**(i)*(-0.04)
print("i and reward:", i, r)
U = r + gamma**6
print U
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 7 14:55:56 2017
-- Programming with PyMC
@author: ali
"""
import pymc as pm
import numpy as np
from numpy.linalg import norm
import scipy.stats as stats
from IPython.core.pylabtools import figsize
from matplotlib import pyplot as plt
import pdb
import random
import time
#
#
#
#p = pm.Uniform('p', lower=0, upper=1)
#p = .05
#def sigmoid(x, W, b):
# z = 1/(1 + np.exp(-(np.dot(W,x)+b)))
# return z
def logsig(x, W, b):
z = 1./(1. + np.exp(-(np.dot(W,x)+b)))
return z
def hardlim(x, W, b):
z = np.dot(W,x) + b
n = len(z)
for i in range(n):
if z[i] >= 0. :
z[i] = 1.
else:
z[i] = 0
return z
def pureline(x,w,b):
z = np.dot(w, x) + b
return z
#W = np.array([1 , 2])
#b = np.array([0])
#x = [0,-1]
# My first NNet program GREETING!
# print "Ali! Welcome to the Neural Networks World"
#
# Three layer NNET for classification
# forward prop
#w=np.zeros((2,2,11))
#w_1 = np.array([1, –1, 1, –1, 1, –1, 1, –1, –1, 1, 1],[1, –1, –1, 1, –1, 1, –1, 1, –1, 1, 1])
'''
p = np.zeros((2,1,8))
t = np.zeros((2,1,8))
p[:,:,0] = np.array([[2], [0]])
t[:,:,0] = np.array([[0], [1]])
p[:,:,1] = np.array([[-1], [2]])
t[:,:,1] = np.array([[1], [0]])
p[:,:,2] = np.array([[-2], [1]])
t[:,:,2] = np.array([[1], [0]])
p[:,:,3] = np.array([[1], [1]])
t[:,:,3] = np.array([[0], [0]])
p[:,:,4] = np.array([[1], [2]])
t[:,:,4] = np.array([[0], [0]])
p[:,:,5] = np.array([[2], [-1]])
t[:,:,5] = np.array([[0], [1]])
p[:,:,6] = np.array([[-1], [-1]])
t[:,:,6] = np.array([[1], [1]])
p[:,:,7] = np.array([[-2], [-2]])
t[:,:,7] = np.array([[1], [1]])
#
# initial values
w = np.zeros((2,2))
pdb.set_trace()
alpha = .1
w[0,0] = 1.
w[1,1]= .9
ind = np.zeros((8,1))
b =np.array([[0.01],[1]])
#cont=0
epsilon = 0.0001
# using Gradient decent considering F = e^2
et = np.zeros((2,8))
et = np.array(et)
a = np.zeros((2,8))
et = np.array(a)
for i in range(8):
# a = sigmoid(p[:,:,i],w,b)
a[:,i] = (np.dot(w,p[:,:,i]) + b).T
et[:,i] = (t[:,:,i].T - a[:,i])
cont =0
Merr = np.dot(et,et.T)
err = np.sqrt(Merr.trace())
while (err > epsilon):
print("iteration :", cont)
# print("gradient :", grad)
print("error :", np.sqrt(np.dot(et,et.T).trace()))
# continue
progress = err - np.sqrt(np.dot(et, et.T).trace())
err = np.sqrt(np.dot(et,et.T).trace())
print("progress :", progress)
if progress < 0:
alpha /= 2
else:
alpha *=1.2
# else:
# a = hardlim(p[:,:,i],w,b)
# e = t[:,:,i] - a
# aprim = np.multiply(a,1-a)
ep = np.dot(et[:,0].reshape(2,1), p[:,:,0].T)
eg = et[:,0].reshape(2,1)
for i in range(1,8):
ep += np.dot(et[:,i].reshape(2,1), p[:,:,i].T)
eg += (t[:,:,i].T - a[:,i]).T
# grad = 2* np.multiply(aprim,ep)
# grad = (2 * ep)
w = w + 2* alpha*ep
# b = b + 2*alpha*np.multiply(et,aprim)
b = b + 2* alpha*eg.reshape(2,1)
#
for i in range(8):
# a = sigmoid(p[:,:,i],w,b)
a[:,i] = (np.dot(w,p[:,:,i]) + b).T
et[:,i] = (t[:,:,i].T - a[:,i])
cont +=1
# if (sum(grad*grad) < epsilon):
# print("Convergence obtained at point", i)
# break
if cont > 10000:
print("maximum iteration reached at point", i)
err = 0
print("Converged with iterations :", cont)
# for double checking
# ind[cont] = i
# cont +=1
# double checking:
#if cont>0:
# for i in range(cont):
# a = hardlim(p[:,:,int(ind[i])],w,b)
# e = t[:,:,int(ind[i])] - a
# if (sum(e*e) !=0):
# w = w + alpha*np.dot(e,p[:,:,int(ind[i])].T)
# b = b + alpha*e
# else:
# print("it now works for point: ", ind[i])
# testing the Algorithm
'''
#
# Problem for two perceptron case; a 1-2-1 Network
#
#for i in range(21):
# np.random.rand()
def Qfunc(x):
return 1. + np.sin((np.pi/4.)*x)
N=50
a1 = np.zeros((2,N))
a2 = np.zeros((1,N))
p = np.zeros((1,N))
p[0,0] = 1.
w1 = np.array([[-.27],[-.41]])
w1_s=w1
w2 = np.array([[.09, -0.17]])
w2_s=w2
b1 = np.array([[-.48], [-.13]])
b1_s=b1
b2 = np.array([[.48]])
b2_s=b2
i=0
a1[:,i] = (logsig(p[0,i],w1,b1)).T
a2[0,i] = pureline(a1[:,i].reshape(2,1),w2,b2)
err = tfunc(p[0,0])-a2[0,0]
# forward propagation network
tol = 0.001
for i in range(N):
if (i>0):
p[0,i] = (np.random.rand(1) +2.)/4.
cont =1
print("----------------------------")
print("Trying the point: ", p[0,i])
alpha = .1
w1_tr = w1_s
w2_tr = w2_s
b1_tr = b1_s
b2_tr = b2_s
while (1):
# pdb.set_trace()
# backpropagation
# time.sleep(1)
# computing derivatives
c = -2.*err
F2 = 1.
F1 = np.array([[(1.-a1[0,i])*a1[0,i], 0.],[0., (1.-a1[1,i])*a1[1,i]]])
s2 = c*F2
s1 = np.dot(F1, w2.T)*s2
# using gradient
w2_new = w2_tr - alpha*s2
b2_new = b2_tr - alpha*s2
w1_new = w1_tr - alpha*s1
b1_new = b1_tr - alpha*s1
# foreard propagation
a1[:,i] = (logsig(p[0,i],w1_new,b1_new)).T
a2[0,i] = pureline(a1[:,i].reshape(2,1),w2_new,b2_new)
err_new = np.abs(tfunc(p[0,i])-a2[0,i])
prgss = err - err_new
err = err_new
# print("Progress & alpha: ", prgss, alpha)
# print("----------------------------------------------iter:",cont)
# print("error --",err)
if (prgss <= 0.):
alpha /= 2.
# else:
w1 = w1_new
w2 = w2_new
b1 = b1_new
b2 = b2_new
w1_tr = w1_new
w2_tr = w2_new
b1_tr = b1_new
b2_tr = b2_new
if (err < tol):
print("convergence achieved with error: ", err)
print("and iteration: ", cont)
break
elif (cont > 500):
print("Maximum iteration limit reached: ", err)
break
cont += 1
# if (err<tol):
w1_s += (1./float(N))* (w1 - w1_s)
w2_s += (1./float(N))* (w2 - w2_s)
b1_s += (1./float(N))* (b1 - b1_s)
b2_s += (1./float(N))* (b2 - b2_s)
err=1.
#print w , p1
#decision(p1)
#decision(p2)
#print "p1 is apple (1)?", decision(p1)
#def frwrd(p):
# a_1 = hardline(p,w_1,b_1)
# a_2 = hardline(a_1,w_2,b_2)
# a_3 = hardline(a_2,w_3,b_3)
#
# return a_3
# for i in range(1,3):
# a[i] = hardline(a[i-1],w[i],b[i])
# end
print "---------"
print "Thats it!"