pytorch

Форк
0
42 строки · 1.6 Кб
1

2

3

4

5

6

7
from caffe2.python import core, workspace
8
import caffe2.python.hypothesis_test_util as hu
9

10
from hypothesis import given
11
import numpy as np
12

13

14
class TestCastOp(hu.HypothesisTestCase):
15

16
    @given(**hu.gcs)
17
    def test_cast_int_float(self, gc, dc):
18
        data = np.random.rand(5, 5).astype(np.int32)
19
        # from int to float
20
        op = core.CreateOperator('Cast', 'data', 'data_cast', to=1, from_type=2)
21
        self.assertDeviceChecks(dc, op, [data], [0])
22
        # This is actually 0
23
        self.assertGradientChecks(gc, op, [data], 0, [0])
24

25
    @given(**hu.gcs)
26
    def test_cast_int_float_empty(self, gc, dc):
27
        data = np.random.rand(0).astype(np.int32)
28
        # from int to float
29
        op = core.CreateOperator('Cast', 'data', 'data_cast', to=1, from_type=2)
30
        self.assertDeviceChecks(dc, op, [data], [0])
31
        # This is actually 0
32
        self.assertGradientChecks(gc, op, [data], 0, [0])
33

34
    @given(data=hu.tensor(dtype=np.int32), **hu.gcs_cpu_only)
35
    def test_cast_int_to_string(self, data, gc, dc):
36
        op = core.CreateOperator(
37
            'Cast', 'data', 'data_cast', to=core.DataType.STRING)
38

39
        def ref(data):
40
            ret = data.astype(dtype=str)
41
            # the string blob will be fetched as object, we feed and re-fetch
42
            # to mimic this.
43
            with hu.temp_workspace('tmp_ref_int_to_string'):
44
                workspace.FeedBlob('tmp_blob', ret)
45
                fetched_ret = workspace.FetchBlob('tmp_blob')
46
            return (fetched_ret, )
47

48
        self.assertReferenceChecks(gc, op, inputs=[data], reference=ref)
49

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.