Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gext
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michele Nottoli
gext
Compare revisions
a7068085b16e966c7e67f35470bd024418ebecfe to 63489817bff2070179bd8b3b7ab3699af8c4b396
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
nottolme/gext
Select target project
No results found
63489817bff2070179bd8b3b7ab3699af8c4b396
Select Git revision
Branches
askarpza-main-patch-76094
main
optimization
polynomial_regression
Tags
v0.2.0
v0.3.0
v0.4.0
v0.4.1
v0.5.0
v0.6.0
v0.7.0
v0.7.1
v0.8.0
13 results
Swap
Target
nottolme/gext
Select target project
nottolme/gext
1 result
a7068085b16e966c7e67f35470bd024418ebecfe
Select Git revision
Branches
askarpza-main-patch-76094
main
optimization
polynomial_regression
Tags
v0.2.0
v0.3.0
v0.4.0
v0.4.1
v0.5.0
v0.6.0
v0.7.0
v0.7.1
v0.8.0
13 results
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source
2
Test fitting done.
· 8685c877
Michele Nottoli
authored
Mar 4, 2024
8685c877
Converted diff coefficients to normal coefficients.
· 63489817
Michele Nottoli
authored
Mar 4, 2024
63489817
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
gext/fitting.py
+14
-20
14 additions, 20 deletions
gext/fitting.py
tests/test_descriptor_fitting.py
+43
-0
43 additions, 0 deletions
tests/test_descriptor_fitting.py
with
57 additions
and
20 deletions
gext/fitting.py
View file @
63489817
...
...
@@ -62,35 +62,27 @@ class DiffFitting(AbstractFitting):
"""
Given a set of vectors and a target return the fitting
coefficients.
"""
if
len
(
vectors
)
==
1
:
raise
ValueError
(
"
DiffFit does not work for one vector
"
)
raise
ValueError
(
"
DiffFit
ting
does not work for one vector
"
)
target
=
target
-
vectors
[
-
1
]
target
=
target
.
flatten
()
VECTORS
=
[]
diff_vectors
=
[]
for
i
in
range
(
1
,
len
(
vectors
)):
each_vector
=
vectors
[
i
-
1
]
-
vectors
[
-
1
]
VECTORS
.
append
(
each_vector
.
flatten
())
matrix
=
np
.
array
(
VECTORS
).
T
diff_vectors
.
append
(
each_vector
)
matrix
=
np
.
array
(
diff_vectors
).
T
a
=
matrix
.
T
@
matrix
b
=
matrix
.
T
@
target
if
self
.
options
[
"
regularization
"
]
>
0.0
:
a
+=
np
.
identity
(
len
(
b
))
*
self
.
options
[
"
regularization
"
]
coefficients
=
np
.
linalg
.
solve
(
a
,
b
)
return
np
.
array
(
coefficients
,
dtype
=
np
.
float64
)
def
linear_combination
(
self
,
vectors
:
List
[
np
.
ndarray
],
coefficients
:
np
.
ndarray
)
->
np
.
ndarray
:
"""
Given a set of vectors (or matrices) and the corresponding
coefficients, build their linear combination.
"""
if
len
(
vectors
)
==
1
:
raise
ValueError
(
"
DiffFit does not work for one vector
"
)
result
=
np
.
zeros
(
vectors
[
0
].
shape
,
dtype
=
np
.
float64
)
VECTORS_DiffFitting
=
[]
for
i
in
range
(
1
,
len
(
vectors
)):
VECTORS_DiffFitting
.
append
(
vectors
[
i
-
1
]
-
vectors
[
-
1
])
for
coeff
,
vector
in
zip
(
coefficients
,
VECTORS_DiffFitting
):
result
+=
vector
*
coeff
result
=
result
+
vectors
[
-
1
]
return
result
# Convert diff coefficients to normal coefficients (like in
# least square fitting)
coefficients
=
np
.
concatenate
((
coefficients
,
[
1.0
-
np
.
sum
(
coefficients
)]))
return
coefficients
class
LeastSquare
(
AbstractFitting
):
...
...
@@ -157,6 +149,8 @@ class QuasiTimeReversible(AbstractFitting):
a
+=
np
.
identity
(
len
(
b
))
*
self
.
options
[
"
regularization
"
]
coefficients
=
np
.
linalg
.
solve
(
a
,
b
)
# Convert quasi time reversible coefficients to normal
# coefficients (like in least square fitting)
if
q
==
1
:
full_coefficients
=
np
.
concatenate
(([
-
1.0
],
coefficients
))
elif
q
%
2
==
0
:
...
...
This diff is collapsed.
Click to expand it.
tests/test_descriptor_fitting.py
View file @
63489817
...
...
@@ -130,3 +130,46 @@ def test_time_reversibility(datafile):
# check the time reversibility
assert
np
.
linalg
.
norm
(
fitted_target
-
fitted_target_reverse
,
ord
=
np
.
inf
)
<
SMALL
@pytest.mark.parametrize
(
"
datafile
"
,
[
"
urea.json
"
,
"
glucose.json
"
])
@pytest.mark.parametrize
(
"
regularization
"
,
[
0.0
,
0.01
,
0.05
])
def
test_diff_fitting
(
datafile
,
regularization
):
# load test data from json file
data
=
utils
.
load_json
(
f
"
tests/
{
datafile
}
"
)
nelectrons
=
data
[
"
nelectrons
"
]
natoms
=
data
[
"
trajectory
"
].
shape
[
1
]
nbasis
=
data
[
"
overlaps
"
].
shape
[
1
]
nframes
=
data
[
"
trajectory
"
].
shape
[
0
]
# initialize an extrapolator
extrapolator
=
gext
.
Extrapolator
(
nelectrons
,
nbasis
,
natoms
,
nsteps
=
nframes
,
fitting
=
"
diff
"
,
fitting_regularization
=
regularization
,
descriptor
=
"
distance
"
)
# load data in the extrapolator
for
(
coords
,
coeff
,
overlap
)
in
zip
(
data
[
"
trajectory
"
],
data
[
"
coefficients
"
],
data
[
"
overlaps
"
]):
extrapolator
.
load_data
(
coords
,
coeff
,
overlap
)
descriptors
=
extrapolator
.
descriptors
.
get
(
10
)
target
=
descriptors
[
-
1
]
fitting_calculator
=
extrapolator
.
fitting_calculator
# check if things are reasonable
for
start
in
range
(
0
,
8
):
vectors
=
descriptors
[
start
:
-
1
]
fit_coefficients
=
fitting_calculator
.
fit
(
vectors
,
target
)
fitted_target
=
fitting_calculator
.
linear_combination
(
vectors
,
fit_coefficients
)
error
=
np
.
linalg
.
norm
(
target
-
fitted_target
,
ord
=
np
.
inf
)
assert
error
<
THRESHOLD
# if we put the target in the vectors used for the fitting,
# check that we get an error smaller than the regularization
vectors
=
descriptors
[:
-
1
]
vectors
[
0
]
=
target
fit_coefficients
=
fitting_calculator
.
fit
(
vectors
,
target
)
fitted_target
=
fitting_calculator
.
linear_combination
(
vectors
,
fit_coefficients
)
assert
np
.
linalg
.
norm
(
target
-
fitted_target
,
ord
=
np
.
inf
)
<
max
(
SMALL
,
regularization
)
This diff is collapsed.
Click to expand it.