Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mlassign2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Sushant Mahajan
mlassign2
Commits
e13e9135
Commit
e13e9135
authored
Apr 09, 2016
by
Sushant Mahajan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
completed first impl of the model
parent
c6d699cc
Pipeline
#276
skipped
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
20 deletions
+59
-20
model.py
model.py
+59
-20
No files found.
model.py
View file @
e13e9135
...
...
@@ -4,6 +4,9 @@ import os
import
csv
from
random
import
random
from
pprint
import
pprint
as
pp
from
math
import
log
,
exp
from
functools
import
reduce
import
numpy
as
np
params
=
{
"layers"
:[
57
,
int
(
57
/
2
),
1
],
"test"
:
"TestX.csv"
,
"train"
:
"Train.csv"
}
...
...
@@ -26,36 +29,72 @@ def getData(srcF, isTrain=True, addBias=True):
return
(
X
,
y
)
def
getMatrix
(
r
,
c
,
vec
):
mat
=
[]
for
i
in
range
(
r
):
temp
=
[]
for
j
in
range
(
c
):
idx
=
i
*
(
c
)
+
j
temp
.
append
(
vec
[
j
])
mat
.
append
(
temp
)
def
sigmoid
(
v
):
return
1.0
/
(
1
+
exp
(
-
v
))
return
mat
def
sigmoidGradient
(
v
):
return
[
a
*
b
for
a
,
b
in
zip
([
sigmoid
(
x
)
for
x
in
v
],
[
sigmoid
(
x
)
for
x
in
v
])]
def
regularization
(
cost
,
w1
,
w2
,
lamb
,
m
):
reg
=
sum
(
w1
*
w1
)
+
sum
(
w2
*
w2
)
reg
=
reg
*
lamb
/
(
2
*
m
)
return
cost
+
reg
def
gradient
(
del1
,
del2
,
w1
,
w2
,
lamb
,
m
):
del1
,
del2
=
del1
/
m
,
del2
/
m
tgrad1
=
del1
[:,
1
:]
+
lamb
*
w1
[:,
1
:]
/
m
tgrad2
=
del2
[:,
1
:]
+
lamb
*
w2
[:,
1
:]
/
m
return
np
.
append
(
tgrad1
.
reshape
(
tgrad1
.
size
),
tgrad2
.
reshape
(
tgrad2
.
size
))
def
cost
(
li
,
lh
,
lo
,
weights
,
X
,
y
):
def
cost
(
li
,
lh
,
lo
,
weights
,
X
,
y
,
lamb
):
w1t
=
weights
[:(
li
+
1
)
*
lh
]
#28x58
w2t
=
weights
[(
li
+
1
)
*
lh
:]
#1x29
w1
,
w2
=
getMatrix
(
lh
,
li
+
1
,
w1t
),
getMatrix
(
lo
,
lh
+
1
,
w2t
)
#28x58, 1x29
tdel1
,
tdel2
=
np
.
zeros
((
lh
,
li
+
1
),
dtype
=
float
),
np
.
zeros
((
lo
,
lh
+
1
),
dtype
=
float
)
#w1,w2 = getMatrix(lh, li+1, w1t), getMatrix(lo, lh+1, w2t)
w1
=
np
.
array
(
w1t
)
w2
=
np
.
array
(
w2t
)
#cost
m
=
len
(
X
)
J
=
0.0
for
i
in
range
(
m
):
a1
=
X
[
i
]
#1x58
z2
=
np
.
dot
(
w1
.
reshape
((
lh
,
li
+
1
)),
a1
)
#28x58 * 58x1 = 28x1
a2
=
[
1.0
]
#bias for hidden
a2
.
extend
([
sigmoid
(
x
)
for
x
in
z2
])
#29x1
z3
=
np
.
dot
(
w2
.
reshape
((
lo
,
lh
+
1
)),
a2
)
#1x29 *29x1 = 1x1
z3
=
z3
[
0
]
h
=
sigmoid
(
z3
)
# J += -y[i]*log(fw) - (1-y[i])*log(1-fw)
#backpropagation
del3
=
h
-
y
[
i
]
#1x1
z2
=
[
1
]
+
z2
.
tolist
()
#29x1
del2
=
w2
.
reshape
((
lo
,
lh
+
1
))
*
del3
*
np
.
array
(
sigmoidGradient
(
z2
))
.
reshape
((
1
,
29
))
#1x29 x 1x1 x 1x29 = 1x29
del2
=
del2
.
reshape
(
lh
+
1
,
lo
)
.
tolist
()
del
(
del2
[
0
])
#28x1
tdel1
=
np
.
dot
(
np
.
array
(
del2
)
.
reshape
(
lh
,
lo
),
np
.
array
(
a1
)
.
reshape
(
lo
,
li
+
1
))
+
tdel1
#28x1 x 1x58
tdel2
=
np
.
multiply
(
np
.
array
(
a2
)
.
reshape
((
lo
,
lh
+
1
)),
del3
)
+
tdel2
#1x29 * 1x1 + 1x29 = 1x29
J
+=
y
[
i
]
*
log
(
h
)
+
(
1
-
y
[
i
])
*
log
(
1
-
h
)
J
=
-
J
/
m
#regularization
J
=
regularization
(
J
,
w1
,
w2
,
lamb
,
m
)
#calculate gradient
grad
=
gradient
(
tdel1
,
tdel2
,
w1
.
reshape
((
lh
,
li
+
1
)),
w2
.
reshape
((
lo
,
lh
+
1
)),
lamb
,
m
)
return
J
,
grad
if
__name__
==
"__main__"
:
X
,
y
=
getData
(
params
[
"train"
])
# tX,ty = getData(params["test"], isTrain=False)
print
(
len
(
X
),
len
(
X
[
0
]),
len
(
y
),
X
[
0
])
# print(len(X), len(X[0]), len(y), X[0])
# print(len(tX), len(ty), tX[0])
li
,
lh
,
lo
=
tuple
(
params
[
"layers"
])
weights
=
[
random
()
for
_
in
range
(
lh
*
(
li
+
1
)
+
lo
*
(
lh
+
1
))]
w1
,
w2
=
cost
(
li
,
lh
,
lo
,
weights
,
X
,
y
)
J
,
grad
=
cost
(
li
,
lh
,
lo
,
weights
,
X
,
y
,
0.1
)
print
(
J
,
grad
)
#print(len(w1), len(w1[0]), len(w2), len(w2[0]))
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment