Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
ML11_P1_G1
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Heetak Chung
ML11_P1_G1
Commits
2a2c8223
Commit
2a2c8223
authored
May 08, 2018
by
tverrbjelke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
es ist 8 uhr 5
parent
2ee9143f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
130 additions
and
62 deletions
+130
-62
data_gen.py
data_gen.py
+57
-62
model.py
model.py
+73
-0
No files found.
data_gen.py
View file @
2a2c8223
import
numpy
as
np
class
Rectangle
:
def
__init__
(
self
,
x
,
y
,
width
,
height
):
self
.
x
=
x
self
.
y
=
y
self
.
width
=
width
self
.
height
=
height
self
.
matrix_size
=
10
self
.
matrix
=
self
.
_init_zeros_matrix
()
self
.
fill
()
def
_init_zeros_matrix
(
self
):
return
np
.
zeros
(
shape
=
(
self
.
matrix_size
,
self
.
matrix_size
),
dtype
=
np
.
float32
)
def
add_noise
(
self
):
new_matrix
=
np
.
zeros
(
shape
=
(
self
.
matrix_size
,
self
.
matrix_size
),
dtype
=
np
.
float32
)
for
i
in
range
(
10
):
for
j
in
range
(
10
):
new_matrix
[
i
][
j
]
+=
self
.
matrix
[
i
][
j
]
+
np
.
random
.
poisson
(
lam
=
2.0
)
return
new_matrix
def
fill
(
self
):
for
i
in
range
(
self
.
width
):
for
j
in
range
(
self
.
height
):
self
.
matrix
[
self
.
x
+
i
][
self
.
y
+
j
]
=
10.0
def
get_matrix
(
self
):
return
self
.
matrix
rect1
=
Rectangle
(
0
,
0
,
width
=
3
,
height
=
5
)
rect1
=
Rectangle
(
0
,
0
,
width
=
3
,
height
=
5
)
recatangle
=
Rectangle
(
0
,
0
,
width
=
3
,
height
=
5
)
recatangle
=
Rectangle
(
0
,
0
,
width
=
3
,
height
=
5
)
rectangles
=
{}
rectangles
[
0
]
=
Rectangle
(
0
,
0
,
2
,
2
)
rectangles
[
1
]
=
Rectangle
(
3
,
3
,
2
,
2
)
rectangles
[
2
]
=
Rectangle
(
5
,
5
,
2
,
2
)
rectangles
[
3
]
=
Rectangle
(
7
,
7
,
2
,
2
)
rect_data
=
[]
for
i
in
range
(
1
,
10001
):
index
=
i
%
4
rect_data
.
append
((
rectangles
[
index
]
.
add_noise
(),
index
))
with
open
(
'data.txt'
,
'w'
):
for
datapoint
in
range
(
len
(
rect_data
)):
\ No newline at end of file
import
numpy
as
np
class
Rectangle
:
def
__init__
(
self
,
x
,
y
,
width
,
height
):
self
.
x
=
x
self
.
y
=
y
self
.
width
=
width
self
.
height
=
height
self
.
matrix_size
=
10
self
.
matrix
=
self
.
_init_zeros_matrix
()
self
.
fill
()
def
_init_zeros_matrix
(
self
):
return
np
.
zeros
(
shape
=
(
self
.
matrix_size
,
self
.
matrix_size
),
dtype
=
np
.
float32
)
def
add_noise
(
self
):
new_matrix
=
np
.
zeros
(
shape
=
(
self
.
matrix_size
,
self
.
matrix_size
),
dtype
=
np
.
float32
)
for
i
in
range
(
10
):
for
j
in
range
(
10
):
new_matrix
[
i
][
j
]
+=
self
.
matrix
[
i
][
j
]
+
np
.
random
.
poisson
(
lam
=
2.0
)
return
new_matrix
def
fill
(
self
):
for
i
in
range
(
self
.
width
):
for
j
in
range
(
self
.
height
):
self
.
matrix
[
self
.
x
+
i
][
self
.
y
+
j
]
=
10.0
def
get_matrix
(
self
):
return
self
.
matrix
def
gen_data
():
rectangles
=
{
0
:
Rectangle
(
0
,
0
,
2
,
2
),
1
:
Rectangle
(
3
,
3
,
2
,
2
),
2
:
Rectangle
(
5
,
5
,
2
,
2
),
3
:
Rectangle
(
7
,
7
,
2
,
2
),}
rect_data
=
[]
labels
=
[]
for
i
in
range
(
0
,
10
):
label
=
i
%
4
rect
=
np
.
array
(
rectangles
[
label
]
.
add_noise
())
rect_reshaped
=
np
.
reshape
(
rect
,[
1
,
100
])
rect_data
.
append
(
rect_reshaped
)
labels
.
append
(
label
)
return
rect_data
,
labels
rect_data
,
labels
=
gen_data
()
model.py
0 → 100644
View file @
2a2c8223
import
numpy
as
np
from
data_gen
import
gen_data
class
Model
:
n_input
=
100
# number is
n_output
=
4
n_h1
=
50
def
__init__
(
self
,
nodes
=
[
n_input
,
n_h1
,
n_output
]):
self
.
nodes
=
nodes
self
.
num_layer
=
len
(
self
.
nodes
)
self
.
weights
=
[]
for
i
in
range
(
0
,
self
.
num_layer
-
1
):
temp_weights
=
np
.
random
.
random
((
self
.
nodes
[
i
],
self
.
nodes
[
i
+
1
]))
self
.
weights
.
append
(
temp_weights
)
def
activation
(
self
,
x
,
derivative
=
False
):
'''Activation function (sigmoid by default)
@param x: input data
@param derivative: boolean if we need a derivative of the sigmoid'''
if
not
derivative
:
return
1
/
(
1
+
np
.
exp
(
-
x
))
else
:
return
self
.
activation
(
x
,
False
)
*
(
1
-
self
.
activation
(
x
,
False
))
def
forward_step
(
self
,
x
):
z_array
=
[]
# dot product solution, before activation
a_array
=
[]
a_array
.
append
(
x
)
# the inner states
outputs
=
[]
#Forward propagation
for
i
in
range
(
0
,
self
.
num_layer
-
1
):
z
=
np
.
dot
(
self
.
weights
[
i
]
.
T
,
a_array
[
i
])
z_array
.
append
(
z
)
a
=
self
.
activation
(
z_array
[
i
],
False
)
a_array
.
append
(
a
)
outputs
.
append
(
a_array
[
-
1
])
return
outputs
def
train_all
(
self
,
X
,
Y
):
'''
@param x: all 10000 reshaped matrices
@param y: all 10000 labels
'''
for
i
in
np
.
arange
(
0
,
len
(
X
)):
x
=
np
.
reshape
(
X
[
i
],
[
100
,
1
])
y
=
Y
[
i
]
self
.
train_one
(
x
)
def
train_one
(
self
,
x
):
out
=
self
.
forward_step
(
x
)
print
(
out
)
sd
X
,
Y
=
gen_data
()
model
=
Model
()
model
.
train_all
(
X
,
Y
)
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