-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/add-ci-linting
- Loading branch information
Showing
58 changed files
with
2,973 additions
and
237 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v2.4.0 | ||
- repo: https://github.com/pycqa/flake8 | ||
rev: 6.0.0 | ||
hooks: | ||
- id: flake8 | ||
- repo: https://github.com/pre-commit/mirrors-mypy | ||
rev: v1.0.1 | ||
hooks: | ||
- id: mypy | ||
additional_dependencies: [ | ||
types-requests~=2.25, | ||
types-requests~=2.28, | ||
types-toml~=0.10 | ||
] |
This file contains 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 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 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 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 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 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 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 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 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,42 @@ | ||
from torch import nn | ||
import torch | ||
|
||
|
||
class GMMModel(nn.Module): | ||
def __init__(self, n_components: int, dim: int) -> None: | ||
"""Gaussian Mixture Model (GMM). | ||
Parameters | ||
---------- | ||
n_components | ||
The number of mixture components. | ||
dim | ||
The dimensionality of the data. | ||
""" | ||
super().__init__() | ||
self.weight_logits = nn.Parameter(torch.zeros(n_components)) | ||
self.means = nn.Parameter(torch.randn(n_components, dim)) | ||
self.inv_cov_factor = nn.Parameter(torch.randn(n_components, dim, dim)/10) | ||
|
||
@property | ||
def _inv_cov(self) -> torch.Tensor: | ||
return torch.bmm(self.inv_cov_factor, self.inv_cov_factor.transpose(1, 2)) | ||
|
||
@property | ||
def _weights(self) -> torch.Tensor: | ||
return nn.functional.softmax(self.weight_logits, dim=0) | ||
|
||
def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
"""Compute the log-likelihood of the data. | ||
Parameters | ||
---------- | ||
x | ||
Data to score. | ||
""" | ||
det = torch.linalg.det(self._inv_cov) # Note det(A^-1)=1/det(A) | ||
to_means = x[:, None, :] - self.means[None, :, :] | ||
likelihood = ((-0.5 * ( | ||
torch.einsum('bke,bke->bk', (torch.einsum('bkd,kde->bke', to_means, self._inv_cov), to_means)) | ||
)).exp()*det[None, :]*self._weights[None, :]).sum(-1) | ||
return -likelihood.log() |
This file contains 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
Oops, something went wrong.