forked from google-deepmind/deepmind-research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.py
52 lines (39 loc) · 1.53 KB
/
utils_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Lint as: python3
# Copyright 2020 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for `utils.py`."""
from absl.testing import absltest
import haiku as hk
import jax
import numpy as np
from gated_linear_networks.examples import utils
class MeanStdEstimator(absltest.TestCase):
def test_statistics(self):
num_features = 100
feature_size = 3
samples = np.random.normal(
loc=5., scale=2., size=(num_features, feature_size))
true_mean = np.mean(samples, axis=0)
true_std = np.std(samples, axis=0)
def tick_(sample):
return utils.MeanStdEstimator()(sample)
init_fn, apply_fn = hk.without_apply_rng(hk.transform_with_state(tick_))
tick = jax.jit(apply_fn)
params, state = init_fn(rng=None, sample=samples[0])
for sample in samples:
(mean, std), state = tick(params, state, sample)
np.testing.assert_array_almost_equal(mean, true_mean, decimal=5)
np.testing.assert_array_almost_equal(std, true_std, decimal=5)
if __name__ == '__main__':
absltest.main()