parent
f487797694
commit
fb2bd156c9
@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "minddata/dataset/kernels/data/unique_op.h"
|
||||||
|
|
||||||
|
#include "minddata/dataset/core/tensor.h"
|
||||||
|
#include "minddata/dataset/kernels/tensor_op.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
Status UniqueOp::Compute(const TensorRow &input, TensorRow *output) {
|
||||||
|
IO_CHECK_VECTOR(input, output);
|
||||||
|
CHECK_FAIL_RETURN_UNEXPECTED(input.size() == 1, "Input should be one tensor");
|
||||||
|
|
||||||
|
auto in_tensor = input[0];
|
||||||
|
auto in_tensor_shape = in_tensor->shape();
|
||||||
|
auto in_tensor_type = in_tensor->type();
|
||||||
|
|
||||||
|
CHECK_FAIL_RETURN_UNEXPECTED(in_tensor_type.IsNumeric(), "Tensor type must be numeric.");
|
||||||
|
CHECK_FAIL_RETURN_UNEXPECTED(in_tensor_shape.Rank() >= 2, "Tensor must be at least 2-D in order to do unique op.");
|
||||||
|
CHECK_FAIL_RETURN_UNEXPECTED(
|
||||||
|
in_tensor->Size() <= std::numeric_limits<int32_t>::max(),
|
||||||
|
"UniqueOp does not support input tensor large than " + std::to_string(std::numeric_limits<int32_t>::max()));
|
||||||
|
|
||||||
|
RETURN_IF_NOT_OK(in_tensor->Reshape(TensorShape({in_tensor->Size()})));
|
||||||
|
|
||||||
|
std::shared_ptr<Tensor> out;
|
||||||
|
std::shared_ptr<Tensor> out_idx;
|
||||||
|
std::shared_ptr<Tensor> out_cnt;
|
||||||
|
|
||||||
|
RETURN_IF_NOT_OK(Unique(in_tensor, &out, &out_idx, &out_cnt));
|
||||||
|
|
||||||
|
output->push_back(out);
|
||||||
|
output->push_back(out_idx);
|
||||||
|
output->push_back(out_cnt);
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_DATA_UNIQUE_OP_H_
|
||||||
|
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_DATA_UNIQUE_OP_H_
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "minddata/dataset/core/tensor.h"
|
||||||
|
#include "minddata/dataset/kernels/tensor_op.h"
|
||||||
|
#include "minddata/dataset/kernels/data/data_utils.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
class UniqueOp : public TensorOp {
|
||||||
|
public:
|
||||||
|
UniqueOp() = default;
|
||||||
|
|
||||||
|
~UniqueOp() override = default;
|
||||||
|
|
||||||
|
Status Compute(const TensorRow &input, TensorRow *output) override;
|
||||||
|
|
||||||
|
uint32_t NumOutput() override { return 0; }
|
||||||
|
|
||||||
|
std::string Name() const override { return kUniqueOp; }
|
||||||
|
};
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
||||||
|
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_UNIQUE_OP_H_
|
@ -0,0 +1,45 @@
|
|||||||
|
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
# ==============================================================================
|
||||||
|
"""
|
||||||
|
Testing unique op in DE
|
||||||
|
"""
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
import mindspore.dataset as ds
|
||||||
|
import mindspore.dataset.transforms.c_transforms as ops
|
||||||
|
|
||||||
|
|
||||||
|
def compare(array, res, idx, cnt):
|
||||||
|
data = ds.NumpySlicesDataset([array], column_names="x")
|
||||||
|
data = data.batch(2)
|
||||||
|
data = data.map(operations=ops.Unique(), input_columns=["x"], output_columns=["x", "y", "z"],
|
||||||
|
column_order=["x", "y", "z"])
|
||||||
|
for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
|
||||||
|
np.testing.assert_array_equal(res, d["x"])
|
||||||
|
np.testing.assert_array_equal(idx, d["y"])
|
||||||
|
np.testing.assert_array_equal(cnt, d["z"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_duplicate_basics():
|
||||||
|
compare([0, 1, 2, 1, 2, 3], np.array([0, 1, 2, 3]),
|
||||||
|
np.array([0, 1, 2, 1, 2, 3]), np.array([1, 2, 2, 1]))
|
||||||
|
compare([0.0, 1.0, 2.0, 1.0, 2.0, 3.0], np.array([0.0, 1.0, 2.0, 3.0]),
|
||||||
|
np.array([0, 1, 2, 1, 2, 3]), np.array([1, 2, 2, 1]))
|
||||||
|
compare([1, 1, 1, 1, 1, 1], np.array([1]),
|
||||||
|
np.array([0, 0, 0, 0, 0, 0]), np.array([6]))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_duplicate_basics()
|
Loading…
Reference in new issue