pytorch
51 строка · 2.0 Кб
1# Copyright (c) 2016-present, Facebook, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14##############################################################################
15
16
17
18
19
20
21from caffe2.python import core
22import caffe2.python.hypothesis_test_util as hu
23import functools
24from hypothesis import given
25import hypothesis.strategies as st
26import numpy as np
27
28class TestWeightScale(hu.HypothesisTestCase):
29@given(inputs=hu.tensors(n=1),
30ITER=st.integers(min_value=0, max_value=100),
31stepsize=st.integers(min_value=20, max_value=50),
32upper_bound_iter=st.integers(min_value=5, max_value=100),
33scale=st.floats(min_value=0.01, max_value=0.99, allow_nan=False, allow_infinity=False),
34**hu.gcs)
35def test_weight_scale(self, inputs, ITER, stepsize, upper_bound_iter, scale, gc, dc):
36ITER = np.array([ITER], dtype=np.int64)
37op = core.CreateOperator(
38"WeightScale", ["w", "iter"], ["nw"], stepsize=stepsize, upper_bound_iter=upper_bound_iter, scale=scale)
39
40def ref_weight_scale(w, iter, stepsize, upper_bound_iter, scale):
41iter = iter + 1
42return [w * scale if iter % stepsize == 0 and iter < upper_bound_iter else w]
43
44input_device_options = {'iter': hu.cpu_do}
45self.assertReferenceChecks(
46gc,
47op,
48[inputs[0], ITER],
49functools.partial(ref_weight_scale, stepsize=stepsize, upper_bound_iter=upper_bound_iter, scale=scale),
50input_device_options=input_device_options
51)
52