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
bb06cf28
Commit
bb06cf28
authored
Apr 10, 2016
by
Sushant Mahajan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
removed old model file
parent
bf87ab8c
Pipeline
#292
skipped
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
0 additions
and
169 deletions
+0
-169
model.py
model.py
+0
-169
No files found.
model.py
deleted
100644 → 0
View file @
bf87ab8c
#! /usr/bin/env python3
import
sys
import
os
import
csv
from
random
import
seed
,
random
from
pprint
import
pprint
as
pp
from
math
import
log
,
exp
import
numpy
as
np
params
=
{
"layers"
:[
57
,
int
(
57
/
2
),
1
],
"test"
:
"TestX.csv"
,
"train"
:
"Train.csv"
}
def
doNormalize
(
X
):
#do 0 mean 1 std normalization
x1
=
np
.
array
(
X
,
dtype
=
float
)
for
i
in
range
(
len
(
X
[
0
])):
col
=
x1
[:,
i
]
mean
,
std
=
col
.
mean
(),
col
.
std
()
std
=
std
if
std
!=
0.0
else
1.0
x1
[:,
i
]
=
(
x1
[:,
i
]
-
mean
)
/
std
return
x1
.
tolist
()
def
getData
(
srcF
,
isTrain
=
True
,
addBias
=
True
,
normalize
=
True
):
X
,
y
=
[],[]
with
open
(
srcF
)
as
src
:
reader
=
csv
.
reader
(
src
,
delimiter
=
','
)
for
i
,
row
in
enumerate
(
reader
):
temp
=
[]
if
addBias
:
temp
.
append
(
1
)
end
=
-
1
if
isTrain
else
len
(
row
)
temp
.
extend
(
row
[:
end
])
#correct data type
X
.
append
([
x
for
x
in
map
(
float
,
temp
)])
if
isTrain
:
y
.
append
(
int
(
row
[
-
1
]))
if
normalize
:
X
=
doNormalize
(
X
)
#print(X[0])
return
(
X
,
y
)
def
sigmoid
(
v
):
return
1.0
/
(
1
+
exp
(
-
v
))
def
sigmoidGradient
(
v
):
return
[
a
*
b
for
a
,
b
in
zip
([
sigmoid
(
x
)
for
x
in
v
],
[
1
-
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
w1
[:,
1
:]
=
np
.
copy
(
tgrad1
)
w2
[:,
1
:]
=
np
.
copy
(
tgrad2
)
return
np
.
append
(
w1
.
reshape
(
w1
.
size
),
w2
.
reshape
(
w2
.
size
))
def
cost
(
li
,
lh
,
lo
,
weights
,
X
,
y
,
lamb
):
w1
=
weights
[:(
li
+
1
)
*
lh
]
#28x58
w2
=
weights
[(
li
+
1
)
*
lh
:]
#1x29
#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
def
fit
(
X
,
y
,
li
,
lh
,
lo
,
weight
,
lamb
,
eta
,
passes
=
1000
,
verbose
=
True
):
weights
=
np
.
copy
(
weight
)
for
i
in
range
(
1
,
passes
+
1
):
J
,
dw
=
cost
(
li
,
lh
,
lo
,
weights
,
X
,
y
,
lamb
)
#print(weights.shape, dw.shape)
weights
+=
-
eta
*
dw
print
(
i
,
"
\r
"
,
end
=
''
)
if
verbose
and
i
%
(
passes
/
10
)
==
0
:
print
()
print
(
J
)
return
weights
def
predict
(
x
,
w1
,
w2
):
#x=[1]+x #58x1
x
=
np
.
array
(
x
)
h1
=
[
sigmoid
(
z
)
for
z
in
np
.
dot
(
w1
,
x
)
.
tolist
()]
#28x58 * 58x1 = 28x1
h1
=
[
1
]
+
h1
h2
=
sigmoid
(
np
.
dot
(
w2
,
h1
)
.
tolist
()[
0
])
#1x29 * 29x1 = 1x1
return
h2
def
setWeightsFromFile
(
weights
):
if
os
.
path
.
isfile
(
"weights"
):
with
open
(
"weights"
,
"rb"
)
as
wfile
:
weights
=
np
.
load
(
wfile
)
return
True
return
False
if
__name__
==
"__main__"
:
np
.
random
.
rand
(
47
)
X
,
y
=
getData
(
params
[
"train"
])
tX
,
ty
=
getData
(
params
[
"test"
],
isTrain
=
False
)
# print(len(X), len(X[0]), len(y), X[0])
# print(len(tX), len(ty), tX[0])
li
,
lh
,
lo
=
tuple
(
params
[
"layers"
])
weights
=
np
.
random
.
rand
(
lh
*
(
li
+
1
)
+
lo
*
(
lh
+
1
))
lamb
,
eta
=
0.1
,
0.1
if
not
setWeightsFromFile
(
weights
):
weights
=
fit
(
X
,
y
,
li
,
lh
,
lo
,
weights
,
lamb
,
eta
,
passes
=
300
)
with
open
(
"weights"
,
"wb"
)
as
wfile
:
np
.
save
(
wfile
,
weights
)
w1
=
weights
[:(
li
+
1
)
*
lh
]
.
reshape
(
lh
,
li
+
1
)
#28x58
w2
=
weights
[(
li
+
1
)
*
lh
:]
.
reshape
(
lo
,
lh
+
1
)
#1x29
py
=
[]
for
x
in
X
:
py
.
append
(
predict
(
x
,
w1
,
w2
))
print
(
py
)
# print("train accuracy", len(list(filter(lambda z:z==0,[a-b for a,b in zip(py,y)])))*1.0/len(y))
# pty = []
# for x in tX:
# pty.append(predict(x,w1,w2))
# with open("answer.txt","w") as dest:
# writer = csv.writer(dest)
# for i,ans in enumerate(pty):
# writer.writerow([i,ans])
# 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