-
Notifications
You must be signed in to change notification settings - Fork 5
Add Linear System Operators to Bonsai.ML.Torch
#83
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
ncguilbeault
wants to merge
10
commits into
bonsai-rx:main
Choose a base branch
from
ncguilbeault:dev/torch-linalg-additions
base: main
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
10 commits
Select commit
Hold shift + click to select a range
26e1dc3
Added operator for performing `CrossProduct`
ncguilbeault 383de20
Added operator `LeastSquaresSolve` for solving systems of linear equa…
ncguilbeault 5bccb08
Added operator `TensorSolve` to compute a tensor solution to the prob…
ncguilbeault dacb4fa
Updated `SingularValueDecomposition` to return a struct output instea…
ncguilbeault b44b79d
Added operator to support chaining matrix multiplication
ncguilbeault 35ba73f
Improved XML documentation
ncguilbeault 854e7f7
Refactored `MatrixMultiply` overload with a tuple of 2 tensors to use…
ncguilbeault 5f7c670
Added operator to compute the rank of a matrix
ncguilbeault 62495e8
Added operator to compute the QR decomposition of a matrix
ncguilbeault 838a90d
Added an operator to solve a triangular system of equations
ncguilbeault 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,34 @@ | ||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Reactive.Linq; | ||
| using static TorchSharp.torch; | ||
| using static TorchSharp.torch.linalg; | ||
|
|
||
| namespace Bonsai.ML.Torch.LinearAlgebra; | ||
|
|
||
| /// <summary> | ||
| /// Represents an operator that computes the cross product of 2 tensors. | ||
| /// </summary> | ||
| [Combinator] | ||
| [Description("Computes the cross product of 2 tensors.")] | ||
| [WorkflowElementCategory(ElementCategory.Transform)] | ||
| public class CrossProduct | ||
| { | ||
| /// <summary> | ||
| /// The dimension to perform the operation. | ||
| /// </summary> | ||
| public long Dimension { get; set; } = -1; | ||
|
|
||
| /// <summary> | ||
| /// Computes the cross product of 2 tensors. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<Tensor> Process(IObservable<Tuple<Tensor, Tensor>> source) | ||
| { | ||
| return source.Select(value => | ||
| { | ||
| return cross(value.Item1, value.Item2, Dimension); | ||
| }); | ||
| } | ||
| } |
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
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,57 @@ | ||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Reactive.Linq; | ||
| using static TorchSharp.torch; | ||
|
|
||
| namespace Bonsai.ML.Torch.LinearAlgebra; | ||
|
|
||
| /// <summary> | ||
| /// Represents an operator that computes the solution to the least squares and least norm problems for a full rank matrix A of size m*n and a matrix B of size m*k. | ||
| /// </summary> | ||
| [Combinator] | ||
| [Description("Computes the solution to the least squares and least norm problems for a full rank matrix A of size m*n and a matrix B of size m*k.")] | ||
| [WorkflowElementCategory(ElementCategory.Transform)] | ||
| public class LeastSquaresSolve | ||
| { | ||
| /// <summary> | ||
| /// Computes the solution to the least squares and least norm problems for a full rank matrix A of size m*n and a matrix B of size m*k. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<LeastSquaresResult> Process(IObservable<Tuple<Tensor, Tensor>> source) | ||
| { | ||
| return source.Select(value => new LeastSquaresResult(linalg.lstsq(value.Item1, value.Item2))); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Represents the result of solving of linear equations using the least squares method. | ||
| /// </summary> | ||
| /// <param name="result"></param> | ||
| public readonly struct LeastSquaresResult(( | ||
| Tensor solution, | ||
| Tensor residuals, | ||
| Tensor rank, | ||
| Tensor singularValues | ||
| ) result) | ||
| { | ||
| /// <summary> | ||
| /// The solution to the system of equations. | ||
| /// </summary> | ||
| public Tensor Solution => result.solution; | ||
|
|
||
| /// <summary> | ||
| /// The residual error. | ||
| /// </summary> | ||
| public Tensor Residuals => result.residuals; | ||
|
|
||
| /// <summary> | ||
| /// The effective rank of the solution. | ||
| /// </summary> | ||
| public Tensor Rank => result.rank; | ||
|
|
||
| /// <summary> | ||
| /// The singular values of the solution. | ||
| /// </summary> | ||
| public Tensor SingularValues => result.singularValues; | ||
| } | ||
| } | ||
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,123 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
| using System.Reactive.Linq; | ||
| using static TorchSharp.torch; | ||
| using static TorchSharp.torch.linalg; | ||
|
|
||
| namespace Bonsai.ML.Torch.LinearAlgebra; | ||
|
|
||
| /// <summary> | ||
| /// Represents an operator that performs matrix multiplication of 2 or more tensors. | ||
| /// </summary> | ||
| [Combinator] | ||
| [Description("Performs matrix multiplication of 2 or more tensors.")] | ||
| [WorkflowElementCategory(ElementCategory.Transform)] | ||
| public class MatrixMultiply | ||
| { | ||
| /// <summary> | ||
| /// Performs matrix multiplication of 2 tensors. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<Tensor> Process(IObservable<Tuple<Tensor, Tensor>> source) | ||
| { | ||
| return source.Select(input =>input.Item1.matmul(input.Item2)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs matrix multiplication of 3 tensors. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<Tensor> Process(IObservable<Tuple<Tensor, Tensor, Tensor>> source) | ||
| { | ||
| return source.Select(input => | ||
| { | ||
| return multi_dot([input.Item1, input.Item2, input.Item3]); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs matrix multiplication of 4 tensors. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<Tensor> Process(IObservable<Tuple<Tensor, Tensor, Tensor, Tensor>> source) | ||
| { | ||
| return source.Select(input => | ||
| { | ||
| return multi_dot([input.Item1, input.Item2, input.Item3, input.Item4]); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs matrix multiplication of 5 tensors. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<Tensor> Process(IObservable<Tuple<Tensor, Tensor, Tensor, Tensor, Tensor>> source) | ||
| { | ||
| return source.Select(input => | ||
| { | ||
| return multi_dot([input.Item1, input.Item2, input.Item3, input.Item4, input.Item5]); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs matrix multiplication of 6 tensors. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<Tensor> Process(IObservable<Tuple<Tensor, Tensor, Tensor, Tensor, Tensor, Tensor>> source) | ||
| { | ||
| return source.Select(input => | ||
| { | ||
| return multi_dot([input.Item1, input.Item2, input.Item3, input.Item4, input.Item5, input.Item6]); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs matrix multiplication of 7 tensors. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<Tensor> Process(IObservable<Tuple<Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor>> source) | ||
| { | ||
| return source.Select(input => | ||
| { | ||
| return multi_dot([input.Item1, input.Item2, input.Item3, input.Item4, input.Item5, input.Item6, input.Item7]); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs matrix multiplication of an array of tensors. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<Tensor> Process(IObservable<Tensor[]> source) | ||
| { | ||
| return source.Select(multi_dot); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs matrix multiplication of a list of tensors. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<Tensor> Process(IObservable<IList<Tensor>> source) | ||
| { | ||
| return source.Select(multi_dot); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs matrix multiplication of an enumerable of tensors. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| /// <returns></returns> | ||
| public IObservable<Tensor> Process(IObservable<IEnumerable<Tensor>> source) | ||
| { | ||
| return source.Select(input => multi_dot([.. input])); | ||
| } | ||
| } |
Oops, something went wrong.
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.
It feels off to have the verb at the end. I would either move it to the beginning, i.e.
SolveLeastSquares, or follow thelinalgconvention and drop it, i.e. just call this operatorLeastSquares.