-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental multiclass configuration
- Loading branch information
1 parent
3a88036
commit 50e3259
Showing
6 changed files
with
183 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
Metric related public interface. | ||
""" | ||
from qusi.internal.metric import CrossEntropyAlt, MulticlassAccuracyAlt, MulticlassAUROCAlt | ||
|
||
__all__ = [ | ||
'CrossEntropyAlt', | ||
'MulticlassAccuracyAlt', | ||
'MulticlassAUROCAlt', | ||
] | ||
|
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,9 @@ | ||
""" | ||
Neural network model related public interface. | ||
""" | ||
from qusi.internal.hadryss_model import HadryssBinaryClassEndModule, HadryssMultiClassEndModule | ||
|
||
__all__ = [ | ||
'HadryssBinaryClassEndModule', | ||
'HadryssMultiClassEndModule', | ||
] |
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,48 @@ | ||
import torch | ||
from torch import Tensor | ||
from torch.nn import NLLLoss, Module | ||
from torchmetrics.classification import MulticlassAUROC, MulticlassAccuracy | ||
|
||
|
||
class CrossEntropyAlt(Module): | ||
@classmethod | ||
def new(cls): | ||
return cls() | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.nll_loss = NLLLoss() | ||
|
||
def __call__(self, preds: Tensor, target: Tensor): | ||
predicted_log_probabilities = torch.log(preds) | ||
target_int = target.to(torch.int64) | ||
cross_entropy = self.nll_loss(predicted_log_probabilities, target_int) | ||
return cross_entropy | ||
|
||
class MulticlassAUROCAlt(Module): | ||
@classmethod | ||
def new(cls, number_of_classes: int): | ||
return cls(number_of_classes=number_of_classes) | ||
|
||
def __init__(self, number_of_classes: int): | ||
super().__init__() | ||
self.multiclass_auroc = MulticlassAUROC(num_classes=number_of_classes) | ||
|
||
def __call__(self, preds: Tensor, target: Tensor): | ||
target_int = target.to(torch.int64) | ||
cross_entropy = self.multiclass_auroc(preds, target_int) | ||
return cross_entropy | ||
|
||
class MulticlassAccuracyAlt(Module): | ||
@classmethod | ||
def new(cls, number_of_classes: int): | ||
return cls(number_of_classes=number_of_classes) | ||
|
||
def __init__(self, number_of_classes: int): | ||
super().__init__() | ||
self.multiclass_accuracy = MulticlassAccuracy(num_classes=number_of_classes) | ||
|
||
def __call__(self, preds: Tensor, target: Tensor): | ||
target_int = target.to(torch.int64) | ||
cross_entropy = self.multiclass_accuracy(preds, target_int) | ||
return cross_entropy |
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,31 +1,48 @@ | ||
import torch | ||
|
||
from qusi.internal.hadryss_model import Hadryss | ||
from qusi.internal.hadryss_model import Hadryss, HadryssBinaryClassEndModule, \ | ||
HadryssMultiClassEndModule | ||
|
||
|
||
def test_lengths_give_correct_output_size(): | ||
hadryss50 = Hadryss(input_length=50) | ||
hadryss50 = Hadryss.new(input_length=50) | ||
|
||
output50 = hadryss50(torch.arange(50, dtype=torch.float32).reshape([1, 50])) | ||
|
||
assert output50.shape == torch.Size([1]) | ||
|
||
hadryss1000 = Hadryss(input_length=1000) | ||
hadryss1000 = Hadryss.new(input_length=1000) | ||
|
||
output1000 = hadryss1000(torch.arange(1000, dtype=torch.float32).reshape([1, 1000])) | ||
|
||
assert output1000.shape == torch.Size([1]) | ||
|
||
hadryss3673 = Hadryss(input_length=3673) | ||
hadryss3673 = Hadryss.new(input_length=3673) | ||
|
||
output3673 = hadryss3673(torch.arange(3673, dtype=torch.float32).reshape([1, 3673])) | ||
|
||
assert output3673.shape == torch.Size([1]) | ||
|
||
hadryss100000 = Hadryss(input_length=100000) | ||
hadryss100000 = Hadryss.new(input_length=100000) | ||
|
||
output100000 = hadryss100000( | ||
torch.arange(100000, dtype=torch.float32).reshape([1, 100000]) | ||
) | ||
|
||
assert output100000.shape == torch.Size([1]) | ||
|
||
|
||
def test_binary_classification_end_module_produces_expected_shape(): | ||
model = Hadryss.new(input_length=100, end_module=HadryssBinaryClassEndModule.new()) | ||
|
||
output = model(torch.arange(7 * 100, dtype=torch.float32).reshape([7, 100])) | ||
|
||
assert output.shape == torch.Size([7]) | ||
|
||
|
||
def test_multi_class_classification_end_module_produces_expected_shape(): | ||
model = Hadryss.new(input_length=100, end_module=HadryssMultiClassEndModule.new(number_of_classes=3)) | ||
|
||
output = model(torch.arange(7 * 100, dtype=torch.float32).reshape([7, 100])) | ||
|
||
assert output.shape == torch.Size([7, 3]) |