-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatisticalMachineLearning with BayesianInferece.py
167 lines (122 loc) · 3.57 KB
/
StatisticalMachineLearning with BayesianInferece.py
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 20 15:24:14 2017
Bayesian Neural Networks (with single neuron)
@author: Ali
"""
# ------------------------ Importing Labs ----------------------
import pymc as pm
import numpy as np
import pdb
# matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
# -------- --------- Defining functions ----- ------ -------
def sigmoid(z):
return 1./(1.+np.exp(-z))
#
def gradM(w,e,x,y,alpha):
g = -2.*np.dot(np.multiply(np.multiply(y,1.-y),e),x.T)
gM = alpha * w + g
return gM
#
def findM(w,y,t,alpha):
G = np.dot((t-y).T,t-y)
Ew = np.trace(np.dot(w.T,w)/2.)
return G + alpha*Ew
#
# -- -- --- ---- defining variables --- -- ---
#
# ssetting the prevars
x = np.zeros((2,1,8))
t = np.zeros((2,1,8))
x[:,:,0] = np.array([[2], [0]])
t[:,:,0] = np.array([[0], [1]])
x[:,:,1] = np.array([[-1], [2]])
t[:,:,1] = np.array([[1], [0]])
x[:,:,2] = np.array([[-2], [1]])
t[:,:,2] = np.array([[1], [0]])
x[:,:,3] = np.array([[1], [1]])
t[:,:,3] = np.array([[0], [0]])
x[:,:,4] = np.array([[1], [2]])
t[:,:,4] = np.array([[0], [0]])
x[:,:,5] = np.array([[2], [-1]])
t[:,:,5] = np.array([[0], [1]])
x[:,:,6] = np.array([[-1], [-1]])
t[:,:,6] = np.array([[1], [1]])
x[:,:,7] = np.array([[-2], [-2]])
t[:,:,7] = np.array([[1], [1]])
# initial values
w = np.zeros((2,2))
w[0,0] = .5
w[1,1]= .5
b = np.array([[0.],[0.]])
alpha = 0.01
epsilon = 0.09
# ------------------ Computations ----------------
#
# MC iteration
L = 10000
# -------- Hamilton iteration
Tau = 20
samp_w = np.zeros((2*L,2))
sw1,sw2 = w.shape
# doing job for 8 points
# pdb.set_trace()
## -----------------------------
# -------------Part 1: Sampling from posterior distribution p(w|D)
# ------------------------------------
for j in range(1):
j=5
# pdb.set_trace()
z = np.dot(w,x[:,:,j]) + b
y = sigmoid(z)
e = t[:,:,j] - y
g = gradM(w,e,x[:,:,j],y,alpha)
M = findM(w,y,t[:,:,j],alpha)
count = 0
for i in range(L):
p = np.random.randn(sw1,sw2)
H = np.trace(np.dot(p.T,p)/2) + M
#
# Hamilton Monte Carlo Iteration
#
for tau in range(Tau):
p -= epsilon*g/2.
wnew = w + epsilon*p
# new computations -------------------------------------------
z = np.dot(wnew, x[:,:,j]) + b
y = sigmoid(z)
e = t[:,:,j] - y
# gradient update ------------------------------------
gnew = gradM(wnew,e,x[:,:,j],y,alpha)
p -= epsilon*gnew/2.
# objective update ------------------------------------
Mnew = findM(wnew,y,t[:,:,j],alpha)
Hnew = np.trace(np.dot(p.T,p)/2.) + Mnew
dH = Hnew - H
print dH
if (dH < 0):
accept = 1
elif (np.random.uniform(0,1,1) < np.exp(-dH)):
accept = 1
else:
accept = 0
if (accept):
g = gnew
w = wnew
# pdb.set_trace()
samp_w[2*i:2*i+2,:] = wnew
count += 1.
M = Mnew
print('the error: ',np.sqrt(np.dot(e.T,e)))
# ----------------------------
# --------Approximating Integrals By Monte Carlo
# ----------------------------
yb = 0.
for i in range(int(count)):
# pdb.set_trace()(1/L)*
z = np.dot(samp_w[2*i:2*i+2,:],x[:,:,5]) +b
yb += (1./count)*sigmoid(z)
print("The approximated value", yb)