MCPcopy Create free account
hub / github.com/apache/singa / onehot_test

Method onehot_test

test/python/test_operation.py:3120–3152  ·  view source on GitHub ↗
(self, dev)

Source from the content-addressed store, hash-verified

3118 self.cast_test(gpu_dev)
3119
3120 def onehot_test(self, dev):
3121
3122 def one_hot(indices, depth, axis=-1, dtype=np.float32): # type: ignore
3123 ''' Compute one hot from indices at a specific axis '''
3124 values = np.asarray(indices)
3125 rank = len(values.shape)
3126 depth_range = np.arange(depth)
3127 if axis < 0:
3128 axis += (rank + 1)
3129 ls = values.shape[0:axis]
3130 rs = values.shape[axis:rank]
3131 targets = np.reshape(depth_range, (1,) * len(ls) +
3132 depth_range.shape + (1,) * len(rs))
3133 values = np.reshape(np.mod(values, depth), ls + (1,) + rs)
3134 return np.asarray(targets == values, dtype=dtype)
3135
3136 axisValue = 1
3137 on_value = 3
3138 off_value = 1
3139 output_type = np.float32
3140 indices = np.array([[1, 9], [2, 4]], dtype=np.float32)
3141 depth = np.array([10], dtype=np.float32)
3142 values = np.array([off_value, on_value], dtype=output_type)
3143 y = one_hot(indices, depth, axis=axisValue, dtype=output_type)
3144 y = y * (on_value - off_value) + off_value
3145
3146 x = tensor.from_numpy(indices)
3147 x.to_device(dev)
3148
3149 result = autograd.onehot(axisValue, x, depth, values)
3150 np.testing.assert_array_almost_equal(tensor.to_numpy(result),
3151 y,
3152 decimal=5)
3153
3154 def test_onehot_cpu(self):
3155 self.onehot_test(cpu_dev)

Callers 2

test_onehot_cpuMethod · 0.95
test_onehot_gpuMethod · 0.95

Calls 1

to_deviceMethod · 0.45

Tested by

no test coverage detected