pytorch/caffe2/python/operator_test/lars_test.py
Bugra Akyildiz 27c7158166 Remove __future__ imports for legacy Python2 supports (#45033)
Summary:
There is a module called `2to3` which you can target for future specifically to remove these, the directory of `caffe2` has the most redundant imports:

```2to3 -f future -w caffe2```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/45033

Reviewed By: seemethere

Differential Revision: D23808648

Pulled By: bugra

fbshipit-source-id: 38971900f0fe43ab44a9168e57f2307580d36a38
2020-09-23 17:57:02 -07:00

46 lines
1.3 KiB
Python

from caffe2.python import core
from hypothesis import given
import caffe2.python.hypothesis_test_util as hu
import hypothesis.strategies as st
import numpy as np
class TestLars(hu.HypothesisTestCase):
@given(offset=st.floats(min_value=0, max_value=100),
lr_min=st.floats(min_value=1e-8, max_value=1e-6),
**hu.gcs)
def test_lars(self, offset, lr_min, dc, gc):
X = np.random.rand(6, 7, 8, 9).astype(np.float32)
dX = np.random.rand(6, 7, 8, 9).astype(np.float32)
wd = np.array([1e-4]).astype(np.float32)
trust = np.random.rand(1).astype(np.float32)
lr_max = np.random.rand(1).astype(np.float32)
def ref_lars(X, dX, wd, trust, lr_max):
rescale_factor = \
trust / (np.linalg.norm(dX) / np.linalg.norm(X) + wd + offset)
rescale_factor = np.minimum(rescale_factor, lr_max)
rescale_factor = np.maximum(rescale_factor, lr_min)
return [rescale_factor]
op = core.CreateOperator(
"Lars",
["X", "dX", "wd", "trust", "lr_max"],
["rescale_factor"],
offset=offset,
lr_min=lr_min,
)
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X, dX, wd, trust, lr_max],
reference=ref_lars
)