pytorch
86 строк · 3.6 Кб
1
2
3
4
5
6import numpy as np7
8from caffe2.python import core9import caffe2.python.hypothesis_test_util as hu10import caffe2.python.serialized_test.serialized_test_util as serial11from hypothesis import given12import hypothesis.strategies as st13
14
15class TestBatchBucketize(serial.SerializedTestCase):16@serial.given(**hu.gcs_cpu_only)17def test_batch_bucketize_example(self, gc, dc):18op = core.CreateOperator('BatchBucketize',19["FEATURE", "INDICES", "BOUNDARIES", "LENGTHS"],20["O"])21float_feature = np.array([[1.42, 2.07, 3.19, 0.55, 4.32],22[4.57, 2.30, 0.84, 4.48, 3.09],23[0.89, 0.26, 2.41, 0.47, 1.05],24[0.03, 2.97, 2.43, 4.36, 3.11],25[2.74, 5.77, 0.90, 2.63, 0.38]], dtype=np.float32)26indices = np.array([0, 1, 4], dtype=np.int32)27lengths = np.array([2, 3, 1], dtype=np.int32)28boundaries = np.array([0.5, 1.0, 1.5, 2.5, 3.5, 2.5], dtype=np.float32)29
30def ref(float_feature, indices, boundaries, lengths):31output = np.array([[2, 1, 1],32[2, 1, 1],33[1, 0, 0],34[0, 2, 1],35[2, 3, 0]], dtype=np.int32)36return (output,)37
38self.assertReferenceChecks(gc, op,39[float_feature, indices, boundaries, lengths],40ref)41
42@given(43x=hu.tensor(44min_dim=2, max_dim=2, dtype=np.float32,45elements=hu.floats(min_value=0, max_value=5),46min_value=5),47seed=st.integers(min_value=2, max_value=1000),48**hu.gcs_cpu_only)49def test_batch_bucketize(self, x, seed, gc, dc):50op = core.CreateOperator('BatchBucketize',51["FEATURE", "INDICES", "BOUNDARIES", "LENGTHS"],52['O'])53np.random.seed(seed)54d = x.shape[1]55lens = np.random.randint(low=1, high=3, size=d - 3)56indices = np.random.choice(range(d), d - 3, replace=False)57indices.sort()58boundaries = []59for i in range(d - 3):60# add [0, 0] as duplicated boundary for duplicated bucketization61if lens[i] > 2:62cur_boundary = np.append(63np.random.randn(lens[i] - 2) * 5, [0, 0])64else:65cur_boundary = np.random.randn(lens[i]) * 566cur_boundary.sort()67boundaries += cur_boundary.tolist()68
69lens = np.array(lens, dtype=np.int32)70boundaries = np.array(boundaries, dtype=np.float32)71indices = np.array(indices, dtype=np.int32)72
73def ref(x, indices, boundaries, lens):74output_dim = indices.shape[0]75ret = np.zeros((x.shape[0], output_dim)).astype(np.int32)76boundary_offset = 077for i, l in enumerate(indices):78temp_bound = boundaries[boundary_offset : lens[i] + boundary_offset]79for j in range(x.shape[0]):80for k, bound_val in enumerate(temp_bound):81if k == len(temp_bound) - 1 and x[j, l] > bound_val:82ret[j, i] = k + 183elif x[j, l] > bound_val:84continue85else:86ret[j, i] = k87break88boundary_offset += lens[i]89return (ret,)90
91self.assertReferenceChecks(gc, op, [x, indices, boundaries, lens], ref)92