Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Add Ridge and StandardScaler
Browse files Browse the repository at this point in the history
  • Loading branch information
PicoCentauri committed Feb 8, 2023
1 parent 8c4ce56 commit 8a1bc45
Show file tree
Hide file tree
Showing 16 changed files with 675 additions and 327 deletions.
49 changes: 4 additions & 45 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,57 +1,16 @@
*.so
/*.egg-info
*.pyc
__pycache__
*.DS_store
*.npz
*.dat
*.npy
*.xvg
*.egg-info
.duecredit.p
.tox/
.swp
*.npz
*.dat
*.npy
*.xvg
*.lock

# Cython generated files
_cutil.c
build/
dist/
*.egg-info
__pycache__/

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Sphinx documentation
docs/_build/
docs/html
docs/latex

# editors and IDEs
*.sw[a-z]
*~
.idea
.vscode

# Ignore build dir
/build
/dist

# Ignore the .DS_Store file in the osx file system
*.DS_store

# duecredit
.duecredit.p

# editors and IDEs
*.sw[a-z]
Expand Down
2 changes: 1 addition & 1 deletion examples/README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Equisolve Examples
==================

This folder consists of introductory examples and examples.
This folder consists of introductory examples.
45 changes: 18 additions & 27 deletions examples/linear-model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
import ase.io
import numpy as np
from equistore import Labels
from equistore.operations import slice, sum_over_samples
from equistore.status import EquistoreError
from equistore.operations import ones_like, slice, sum_over_samples
from rascaline import SoapPowerSpectrum

from equisolve.numpy.models.linear_model import Ridge
from equisolve.utils import dictionary_to_tensormap
from equisolve.utils.convert import ase_to_tensormap


Expand Down Expand Up @@ -81,23 +79,15 @@
# rascaline.
#
# We now move all keys into properties to access them for our model.
try:
descriptor = descriptor.keys_to_samples("species_center")
except EquistoreError:
pass

try:
descriptor = descriptor.keys_to_properties(
["species_neighbor_1", "species_neighbor_2"]
)
except EquistoreError:
pass

descriptor = descriptor.keys_to_samples("species_center")
descriptor = descriptor.keys_to_properties(["species_neighbor_1", "species_neighbor_2"])

# %%
#
# The descriptor contains a represenantion with respect to each central atoms per
# structure. However, our energies as target data is per structure only.
# Therefore, we sum the features of each center atom per structure.
# Therefore, we sum the properties of each center atom per structure.

X = sum_over_samples(descriptor, ["center", "species_center"])

Expand All @@ -109,12 +99,12 @@

# %%
#
# As well as 1800 features and 20 sample.
# As well as 1800 properties and 20 sample.
#
# We acces the data using the :meth:``equistore.TensorMap.block`` method


print(f"X contains {len(X.block().properties)} features.")
print(f"X contains {len(X.block().properties)} properties.")
print(f"X contains {len(X.block().samples)} samples.")

# Target data
Expand All @@ -141,9 +131,8 @@
#
# For this we create a TensorMap containing with the desired regulerizer


alpha_dict = {"values": 1e-5}
alpha = dictionary_to_tensormap(alpha_dict, X)
alpha = ones_like(X)
alpha.block().values[:] *= 1e-5

# %%
#
Expand All @@ -158,15 +147,16 @@
names=["structure"],
values=np.array([(0,)]),
)

alpha = slice(alpha, samples=samples)

# %%
#
# In our regulerizer we use the same values for all features. However,
# In our regulerizer we use the same values for all properties. However,
# :class:`equisolve.numpy.models.linear_model.Ridge` can also handle different
# regularization for each feature. You can apply a feature wise regularization by
# regularization for each property. You can apply a property wise regularization by
# setting ``"values"`` of ``alpha_dict`` with an 1d array of the same length as the
# number of features in the training data X (here 7200)
# number of properties in the training data X (here 7200)
#
# With a valid regulerizer object we now initilize the Ridge object.
# ``parameter_keys`` determines with respect to which parameters the regression is
Expand All @@ -181,8 +171,8 @@
# Next we create a sample weighting :class:`equistiore.TensorMap` that weights energies
# five times more then the forces.

sw_dict = {"values": 5, "positions": 1}
sw = dictionary_to_tensormap(sw_dict, y)
sw = ones_like(y)
sw.block().values[:] *= 5

# %%
#
Expand All @@ -194,11 +184,12 @@

# Finally we can fit the model using the sample weights defined above.

clf.fit(X, y, sample_weights=sw)
clf.fit(X, y, sample_weight=sw)


# Finally we can predict values and calculate the root mean squre error
# of our model.

clf.predict(X)
print(f"RMSE = {clf.score(X, y)}")
print(f"RMSE energies = {clf.score(X, y, parameter_key='values')[0]:.3f} eV")
print(f"RMSE forces = {clf.score(X, y, parameter_key='positions')[0]:.3f} eV/Å")
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ packages = find:
include_package_data = True

install_requires =
#equistore @ https://github.com/lab-cosmo/equistore/archive/6ca7fa3.zip
equistore @ https://github.com/lab-cosmo/equistore/archive/467baf0.zip
numpy>=1.19.0
scipy>=1.0.0

Expand Down
Loading

0 comments on commit 8a1bc45

Please sign in to comment.