generated from joshuaspear/python-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added weight standard deviation metric
- Loading branch information
1 parent
aef7618
commit c0ffaa0
Showing
6 changed files
with
55 additions
and
3 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
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,21 @@ | ||
import torch | ||
from .MetricBase import MetricBase | ||
from jaxtyping import jaxtyped | ||
from typeguard import typechecked as typechecker | ||
|
||
from ..types import WeightTensor | ||
|
||
__all__ = [ | ||
"WeightStd" | ||
] | ||
|
||
class WeightStd(MetricBase): | ||
|
||
@jaxtyped(typechecker=typechecker) | ||
def __call__( | ||
self, | ||
weights:WeightTensor, | ||
weight_msk:WeightTensor | ||
) -> float: | ||
sum_weights = torch.mul(weights,weight_msk).sum(dim=1) | ||
return torch.std(sum_weights).item() |
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,3 +1,4 @@ | ||
from .EffectiveSampleSize import * | ||
from .ValidWeightsProp import * | ||
from .WeightStd import * | ||
from .MetricBase import * |
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,31 @@ | ||
import unittest | ||
import torch | ||
import numpy as np | ||
import copy | ||
from offline_rl_ope.Metrics import WeightStd | ||
from offline_rl_ope import logger | ||
from parameterized import parameterized_class | ||
from ..base import test_configs_fmt_class, TestConfig | ||
|
||
@parameterized_class(test_configs_fmt_class) | ||
class TestWeightStd(unittest.TestCase): | ||
|
||
test_conf:TestConfig | ||
|
||
def test_call(self): | ||
fnl_weights = [] | ||
for idx,i in enumerate(self.test_conf.traj_lengths): | ||
fnl_weights.append( | ||
self.test_conf.weight_test_res[idx,:i].sum( | ||
dim=0, | ||
keepdim=True | ||
) | ||
) | ||
fnl_weights_tens = torch.concat(fnl_weights, axis=0) | ||
act_res = torch.std(fnl_weights_tens).item() | ||
metric = WeightStd() | ||
pred_res = metric( | ||
weights=self.test_conf.weight_test_res, | ||
weight_msk=self.test_conf.msk_test_res | ||
) | ||
self.assertEqual(act_res,pred_res) |