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

Method _softmax_api_helper

test/python/test_api.py:204–241  ·  view source on GitHub ↗
(self, dev)

Source from the content-addressed store, hash-verified

202 _run_testing(x_0, s_0, b_0, rm_0, rv_0, m_0=1.0)
203
204 def _softmax_api_helper(self, dev):
205
206 def _run_test(dev, org_shape, axis, aft_shape):
207 x_0 = np.random.random(org_shape).astype(np.float32)
208 x_0 = x_0 + 1000
209 x0 = tensor.Tensor(device=dev, data=x_0)
210
211 # test with axis
212 y0 = tensor._call_singa_func(singa_api.SoftMax, x0.data, axis)
213
214 # test with numpy
215 x_0 = x_0.reshape(aft_shape)
216 x_0 = x_0 - np.max(x_0)
217 y1 = np.divide(np.exp(x_0),
218 np.sum(np.exp(x_0), axis=1).reshape(x_0.shape[0],
219 1)) # 2d softmax
220 y1 = y1.reshape(org_shape)
221
222 np.testing.assert_array_almost_equal(tensor.to_numpy(y0), y1)
223
224 _run_test(dev, [2, 2], 1, [2, 2])
225 _run_test(dev, [2, 2], 0, [1, 4])
226 _run_test(dev, [2, 2], -1, [2, 2])
227 _run_test(dev, [2, 2], -2, [1, 4])
228 _run_test(dev, [2, 2, 2], 2, [4, 2])
229 _run_test(dev, [2, 2, 2], 1, [2, 4])
230 _run_test(dev, [2, 2, 2], 0, [1, 8])
231 _run_test(dev, [2, 2, 2], -1, [4, 2])
232 _run_test(dev, [2, 2, 2], -2, [2, 4])
233 _run_test(dev, [2, 2, 2], -3, [1, 8])
234 _run_test(dev, [2, 2, 2, 2], 3, [8, 2])
235 _run_test(dev, [2, 2, 2, 2], 2, [4, 4])
236 _run_test(dev, [2, 2, 2, 2], 1, [2, 8])
237 _run_test(dev, [2, 2, 2, 2], 0, [1, 16])
238 _run_test(dev, [2, 2, 2, 2], -1, [8, 2])
239 _run_test(dev, [2, 2, 2, 2], -2, [4, 4])
240 _run_test(dev, [2, 2, 2, 2], -3, [2, 8])
241 _run_test(dev, [2, 2, 2, 2], -4, [1, 16])
242
243 def test_softmax_api_cpu(self):
244 self._softmax_api_helper(cpu_dev)

Callers 2

test_softmax_api_cpuMethod · 0.95
test_softmax_api_gpuMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected