-
Notifications
You must be signed in to change notification settings - Fork 10
Initial implementation of SmartGradient #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vincent-maillou
wants to merge
8
commits into
dalia-project:add_gradient_methods
Choose a base branch
from
vincent-maillou:add_smart_gradient
base: add_gradient_methods
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c7436ae
Merge pull request #114 from lisa-gm/dev
vincent-maillou 3336dad
Initial implementation of SmartGradient
vincent-maillou 537df2e
draft of clean implementation of GradientMethod
vincent-maillou 7fb2fd5
separated gradient_method from core DALIA class
vincent-maillou c38327f
added config and structure for smart_gradient
vincent-maillou eaabad6
smart_gradient is working
vincent-maillou fb45be4
Simplification of smart gradient method
vincent-maillou 71f4be2
Merge branch 'lisa-gm:main' into add_smart_gradient
vincent-maillou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| from abc import ABC | ||
| from typing import Literal | ||
|
|
||
| from pydantic import BaseModel, ConfigDict | ||
|
|
||
|
|
||
| class GradientMethodConfig(BaseModel, ABC): | ||
| model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True) | ||
|
|
||
| # Input folder for this specific submodel | ||
| type: Literal["vanilla_gradient", "smart_gradient"] = None | ||
|
|
||
| finite_difference_epsilon: float = 1e-3 | ||
|
|
||
|
|
||
| class VanillaGradientConfig(GradientMethodConfig): | ||
| type: Literal["vanilla_gradient"] = "vanilla_gradient" | ||
|
|
||
|
|
||
| class SmartGradientConfig(GradientMethodConfig): | ||
| type: Literal["smart_gradient"] = "smart_gradient" | ||
|
|
||
| # The diagonal noise ensure to avoid singularities in the QR decomposition | ||
| diagonal_noise: float = 1e-8 | ||
| # Threshold below which scaling is not performed | ||
| scaling_threshold: float = 1e-12 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| from dalia import xp | ||
|
|
||
|
|
||
| class GradientMethod(ABC): | ||
| """Core class for gradient computation methods.""" | ||
|
|
||
| def __init__(self, basis_size, finite_difference_epsilon) -> None: | ||
| """Initialize the gradient computation method. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| basis_size : int | ||
| The size of the basis for finite differences. | ||
| finite_difference_epsilon : float | ||
| The epsilon value for finite difference computations. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| self.basis_size = basis_size | ||
| self.basis = xp.identity(self.basis_size, dtype=xp.float64) | ||
| self.finite_difference_epsilon = finite_difference_epsilon | ||
|
|
||
| @abstractmethod | ||
| def get_evaluation_directions(self, direction_matrix, theta) -> None: | ||
| """Get the evaluation directions for the gradient computation. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| direction_matrix : xp.ndarray | ||
| The matrix to store the evaluation directions. | ||
| theta : xp.ndarray | ||
| The current (hyper)parameter values. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def compute_gradient(self, function_evaluations, gradient) -> None: | ||
| """Compute the gradient using finite differences. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| function_evaluations : xp.ndarray | ||
| The function evaluations at the current and perturbed points. | ||
| gradient : xp.ndarray | ||
| The array to store the computed gradient. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
| """ | ||
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Copyright 2024-2025 DALIA authors. All rights reserved. | ||
|
|
||
| from dalia.gradient_methods.vanilla_gradient import VanillaGradient | ||
| from dalia.gradient_methods.smart_gradient import SmartGradient | ||
|
|
||
| __all__ = ["VanillaGradient", "SmartGradient"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
self.gradient_basisvariable is defined but never used in the updated code. This appears to be leftover from the previous implementation and should be removed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k = self.basis_size
eps = self.finite_difference_epsilon
deltas = eps * self.basis
direction_matrix[:, 0] = theta_dev
direction_matrix[:, 1:1+k] = deltas
direction_matrix[:, 1+k:1+2*k] = -deltas
for j in range(1, direction_matrix.shape[1]):
direction_matrix[:, j] = self._transformed_fun(phi=direction_matrix[:, j])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think copilot might actually be right on this one. I didn't see it being used somewhere else either.