Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
adaptive
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Quantum Tinkerer
adaptive
Commits
78cd74eb
Commit
78cd74eb
authored
6 years ago
by
Bas Nijholt
Browse files
Options
Downloads
Patches
Plain Diff
fix that the 'AverageLearner' only returns new points, and introduce 'learner.pending_points'
parent
62e46954
No related branches found
No related tags found
No related merge requests found
Pipeline
#12139
passed
6 years ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
adaptive/learner/average_learner.py
+29
-13
29 additions, 13 deletions
adaptive/learner/average_learner.py
with
29 additions
and
13 deletions
adaptive/learner/average_learner.py
+
29
−
13
View file @
78cd74eb
# -*- coding: utf-8 -*-
import
itertools
from
math
import
sqrt
import
warnings
import
numpy
as
np
...
...
@@ -14,12 +15,19 @@ class AverageLearner(BaseLearner):
The learned function must depend on an integer input variable that
represents the source of randomness.
Parameters
:
----------
-
Parameters
----------
atol : float
Desired absolute tolerance
rtol : float
Desired relative tolerance
Attributes
----------
data : dict
Sampled points and values.
pending_points : set
Points that still have to be evaluated.
"""
def
__init__
(
self
,
function
,
atol
=
None
,
rtol
=
None
):
...
...
@@ -31,6 +39,7 @@ class AverageLearner(BaseLearner):
rtol
=
np
.
inf
self
.
data
=
{}
self
.
pending_points
=
set
()
self
.
function
=
function
self
.
atol
=
atol
self
.
rtol
=
rtol
...
...
@@ -40,28 +49,35 @@ class AverageLearner(BaseLearner):
@property
def
n_requested
(
self
):
return
len
(
self
.
data
)
return
len
(
self
.
data
)
+
len
(
self
.
pending_points
)
def
ask
(
self
,
n
,
add_data
=
True
):
points
=
list
(
range
(
self
.
n_requested
,
self
.
n_requested
+
n
))
if
any
(
True
for
p
in
points
if
p
in
self
.
data
or
p
in
self
.
pending_points
):
# This means some of the points `< self.n_requested` do not exist.
points
=
list
(
set
(
range
(
self
.
n_requested
+
n
))
-
set
(
self
.
data
)
-
set
(
self
.
pending_points
))[:
n
]
loss_improvements
=
[
self
.
loss_improvement
(
n
)
/
n
]
*
n
if
add_data
:
self
.
tell_many
(
points
,
itertools
.
repeat
(
None
))
return
points
,
loss_improvements
def
tell
(
self
,
n
,
value
):
value_is_new
=
not
(
n
in
self
.
data
and
value
==
self
.
data
[
n
])
if
not
value_is_new
:
value_old
=
self
.
data
[
n
]
self
.
data
[
n
]
=
value
if
value
is
not
None
:
if
n
in
self
.
data
:
warnings
.
warn
(
f
"
n=
{
n
}
already exists in `learner.data` with value
{
self
.
data
[
n
]
}
.
"
)
return
if
value
is
None
:
self
.
pending_points
.
add
(
n
)
else
:
self
.
data
[
n
]
=
value
self
.
pending_points
.
discard
(
n
)
self
.
sum_f
+=
value
self
.
sum_f_sq
+=
value
**
2
if
value_is_new
:
self
.
npoints
+=
1
else
:
self
.
sum_f
-=
value_old
self
.
sum_f_sq
-=
value_old
**
2
self
.
npoints
+=
1
@property
def
mean
(
self
):
...
...
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