def test_softmax(): ctx = ndarray.gpu(0) shape = (400, 1000) x = np.random.uniform(-5, 5, shape).astype(np.float32) arr_x = ndarray.array(x, ctx=ctx) arr_y = ndarray.empty(shape, ctx=ctx) gpu_op.softmax(arr_x, arr_y) y = arr_y.asnumpy() np.testing.assert_allclose(autodiff.softmax_func(x), y, rtol=1e-5)
def test_softmax(): shape = (400, 1000) x = np.random.uniform(-5, 5, shape).astype(dtype) y = np.zeros(shape).astype(dtype) arr_x = tvm.nd.array(x, ctx=ctx) arr_y = tvm.nd.array(y, ctx=ctx) matrix_softmax = tvm_op.make_matrix_softmax(shape, tgt, tgt_host, "matrix_softmax") matrix_softmax(arr_x, arr_y) y = arr_y.asnumpy() np.testing.assert_allclose(autodiff.softmax_func(x), y, rtol=1e-5)
def test_softmax_cross_entropy(): ctx = ndarray.gpu(0) shape = (400, 1000) y = np.random.uniform(-5, 5, shape).astype(np.float32) y_ = np.random.uniform(-5, 5, shape).astype(np.float32) arr_y = ndarray.array(y, ctx=ctx) arr_y_ = ndarray.array(y_, ctx=ctx) arr_out = ndarray.empty((1, ), ctx=ctx) gpu_op.softmax_cross_entropy(arr_y, arr_y_, arr_out) out = arr_out.asnumpy() # numpy calculation cross_entropy = np.mean( -np.sum(y_ * np.log(autodiff.softmax_func(y)), axis=1), keepdims=True) np.testing.assert_allclose(cross_entropy, out, rtol=1e-5)
def test_softmax_cross_entropy(): shape = (400, 1000) y = np.random.uniform(-5, 5, shape).astype(dtype) y_ = np.random.uniform(-5, 5, shape).astype(dtype) out = np.zeros((1,)).astype(dtype) arr_y = tvm.nd.array(y, ctx=ctx) arr_y_ = tvm.nd.array(y_, ctx=ctx) arr_out = tvm.nd.array(out, ctx=ctx) matrix_softmax_cross_entropy = tvm_op.make_matrix_softmax_cross_entropy(shape, tgt, tgt_host, "softmax_cross_entropy") matrix_softmax_cross_entropy(arr_y, arr_y_, arr_out) out = arr_out.asnumpy() # numpy calculation cross_entropy = np.mean( -np.sum(y_ * np.log(autodiff.softmax_func(y)), axis=1), keepdims=True) np.testing.assert_allclose(cross_entropy, out, rtol=1e-5)