Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AI Progamming Exercises
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michael Mutote
AI Progamming Exercises
Commits
54598b3a
Commit
54598b3a
authored
1 year ago
by
Michael Mutote
Browse files
Options
Downloads
Patches
Plain Diff
22202956 - Threshold Perceptron completed, skeleton of linear created
parent
c6243c84
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Reinforcement_Learning/Perceptrons.py
+17
-28
17 additions, 28 deletions
Reinforcement_Learning/Perceptrons.py
Reinforcement_Learning/Solution_Testing.py
+19
-0
19 additions, 0 deletions
Reinforcement_Learning/Solution_Testing.py
with
36 additions
and
28 deletions
Reinforcement_Learning/Perceptrons.py
+
17
−
28
View file @
54598b3a
...
...
@@ -2,10 +2,14 @@ import numpy as np
import
Training_data
rng
=
np
.
random
.
default_rng
(
123
)
TEACHDATA
=
9
0000
TESTDATA
=
1
TEACHDATA
=
1
0000
TESTDATA
=
1
000
# ETA = 0.5
T_NUMBER
=
3
T_NUMBER
=
0
def
linear_act
(
val
):
return
val
def
threshold
(
val
):
...
...
@@ -16,45 +20,30 @@ class Perceptron:
def
__init__
(
self
,
input_count
,
activation
=
threshold
):
self
.
input_count
=
input_count
self
.
activation
=
activation
self
.
weights
=
rng
.
random
(
input_count
+
1
)
*
0.3
self
.
weights
=
rng
.
random
(
input_count
+
1
)
def
train
(
self
,
ETA
):
teach_data
=
Training_data
.
make_testset
(
TEACHDATA
)
for
i
in
rng
.
permutation
(
TEACHDATA
):
for
i
in
r
a
ng
e
(
TEACHDATA
):
old_weights
=
np
.
copy
(
self
.
weights
)
for
j
in
rng
.
permutation
(
len
(
teach_data
)):
T
=
1
if
j
==
T_NUMBER
else
0
ix
=
np
.
insert
(
teach_data
[
j
][
i
].
ravel
(),
0
,
1
)
# for wx in rng.permutation(len(ix)):
RI
=
self
.
activation
(
ix
.
dot
(
self
.
weights
))
if
not
RI
=
=
T
:
if
RI
!
=
T
:
delta
=
ETA
*
(
T
-
self
.
activation
(
ix
.
dot
(
self
.
weights
)))
*
ix
self
.
weights
=
self
.
weights
+
delta
if
np
.
linalg
.
norm
(
old_weights
-
self
.
weights
)
<=
0.00000000001
:
# print(self.weights[0], self.weights[1], self.weights[2], self.weights[3], self.weights[4], self.weights[5], self.weights[6])
if
np
.
linalg
.
norm
(
old_weights
-
self
.
weights
)
==
0.00
:
return
self
.
weights
return
self
.
weights
def
test
(
self
):
test_data
=
Training_data
.
make_testset
(
TESTDATA
)
# print(test_data)
res
=
[]
for
number
in
test_data
:
# print(
number
[0])
ix
=
np
.
insert
(
number
[
0
]
.
ravel
(),
0
,
1
)
res
.
append
(
self
.
activation
(
ix
.
dot
(
self
.
weights
)))
res
=
[
0
for
_
in
range
(
len
(
test_data
))
]
for
number
in
range
(
len
(
test_data
))
:
for
sample
in
test_data
[
number
]:
ix
=
np
.
insert
(
sample
.
ravel
(),
0
,
1
)
res
[
number
]
=
res
[
number
]
+
(
self
.
activation
(
ix
.
dot
(
self
.
weights
)))
return
res
def
test_function
():
input_count
=
20
# the number of elements in each input array
ETA_list
=
[
0.0001
,
0.001
,
0.01
,
0.1
,
0.5
,
1
,
1.5
,
2
]
# the list of values for ETA
results
=
[]
# the list of results for each ETA value
for
ETA
in
ETA_list
:
p
=
Perceptron
(
input_count
)
# create a perceptron instance
p
.
train
(
ETA
)
# train the perceptron with the given ETA value
output
=
p
.
test
()
# test the perceptron on a single test pattern
results
.
append
((
ETA
,
output
))
# append the ETA value and the output to the results list
return
results
# return the results list
print
(
test_function
())
# print the results list
This diff is collapsed.
Click to expand it.
Reinforcement_Learning/Solution_Testing.py
0 → 100644
+
19
−
0
View file @
54598b3a
import
Perceptrons
def
test_function
(
ETA
):
input_count
=
20
results
=
[]
p
=
Perceptrons
.
Perceptron
(
input_count
)
p
.
train
(
ETA
)
output
=
p
.
test
()
results
.
append
((
ETA
,
output
))
return
results
for
ETA
in
([
0.05
,
0.1
,
0.2
,
0.4
,
0.75
,
1
,
2
,
5
]):
# the list of values for ETA
for
i
in
range
(
5
):
res
=
test_function
(
ETA
)
print
(
res
)
# print the results list
print
(
"
\n\n
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment