File: lars_test.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (45 lines) | stat: -rw-r--r-- 1,354 bytes parent folder | download | duplicates (2)
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





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
        )