From 4cd504d3b4fbab768ea8720830cd5048612e510d Mon Sep 17 00:00:00 2001 From: tangwei12 Date: Mon, 6 Aug 2018 21:07:31 +0800 Subject: [PATCH] bug fix --- paddle/fluid/operators/sampling_id_op.h | 12 ++++++++---- .../fluid/tests/unittests/test_sampling_id_op.py | 15 ++++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/operators/sampling_id_op.h b/paddle/fluid/operators/sampling_id_op.h index 4d962b480..3d724e3ae 100644 --- a/paddle/fluid/operators/sampling_id_op.h +++ b/paddle/fluid/operators/sampling_id_op.h @@ -13,7 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. */ #pragma once +#include +#include +#include #include +#include #include #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/op_registry.h" @@ -34,17 +38,17 @@ class SamplingIdKernel : public framework::OpKernel { std::vector ins_vector; framework::TensorToVector(*input, context.device_context(), &ins_vector); - std::vector ids(batch_size); + std::vector ids(batch_size); for (size_t i = 0; i < batch_size; ++i) { double r = this->get_rand(); - int id = width - 1; + int idx = width - 1; for (int j = 0; j < width; ++j) { if ((r -= ins_vector[i * width + j]) < 0) { - id = j; + idx = j; break; } } - ids[i] = id; + ids[i] = ins_vector[i * width + idx]; } std::vector out_dim; diff --git a/python/paddle/fluid/tests/unittests/test_sampling_id_op.py b/python/paddle/fluid/tests/unittests/test_sampling_id_op.py index 86d86acfb..e3e715304 100644 --- a/python/paddle/fluid/tests/unittests/test_sampling_id_op.py +++ b/python/paddle/fluid/tests/unittests/test_sampling_id_op.py @@ -25,17 +25,18 @@ class TestSamplingIdOp(OpTest): self.op_type = "sampling_id" self.use_mkldnn = False self.init_kernel_type() - X = np.random.random((3, 4)).astype('float32') - self.inputs = {"X": X} - Y = np.random.random(3).astype('float32') - self.outputs = {'Out': Y} + self.X = np.random.random((8, 4)).astype('float32') + self.inputs = {"X": self.X} + self.Y = np.random.random(8).astype('float32') + self.outputs = {'Out': self.Y} self.attrs = {'use_mkldnn': self.use_mkldnn} def test_check_output(self): - self.check_output() + self.check_output_customized(self.verify_output) - def test_check_grad(self): - self.check_grad(['X'], 'Out') + def verify_output(self, outs): + out = np.array(outs[0]) + self.assertEqual(len(out), len(self.Y)) def init_kernel_type(self): pass -- GitLab