Merge pull request #13628 from brave/issues/23209

Remove excess copying of brave ads data structures.
This commit is contained in:
aseren
2022-06-07 10:54:02 -07:00
committed by GitHub
29 changed files with 111 additions and 295 deletions
-1
View File
@@ -190,7 +190,6 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ml/data/text_data_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ml/data/vector_data_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ml/ml_prediction_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ml/ml_transformation_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ml/model/linear/linear_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ml/pipeline/pipeline_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ml/pipeline/text_processing/text_processing_unittest.cc",
-2
View File
@@ -693,8 +693,6 @@ source_set("ads") {
"src/bat/ads/internal/ml/ml_aliases.h",
"src/bat/ads/internal/ml/ml_prediction_util.cc",
"src/bat/ads/internal/ml/ml_prediction_util.h",
"src/bat/ads/internal/ml/ml_transformation_util.cc",
"src/bat/ads/internal/ml/ml_transformation_util.h",
"src/bat/ads/internal/ml/model/linear/linear.cc",
"src/bat/ads/internal/ml/model/linear/linear.h",
"src/bat/ads/internal/ml/pipeline/pipeline_info.cc",
@@ -5,28 +5,21 @@
#include "bat/ads/internal/ml/data/text_data.h"
#include <utility>
namespace ads {
namespace ml {
TextData::TextData() : Data(DataType::kText) {}
TextData::TextData(const TextData& text_data) : Data(DataType::kText) {
text_ = text_data.GetText();
}
TextData::TextData(std::string text)
: Data(DataType::kText), text_(std::move(text)) {}
TextData::TextData(const std::string& text)
: Data(DataType::kText), text_(text) {}
std::string TextData::GetText() const {
const std::string& TextData::GetText() const {
return text_;
}
TextData::~TextData() = default;
TextData& TextData::operator=(const TextData& text_data) {
text_ = text_data.GetText();
return *this;
}
} // namespace ml
} // namespace ads
@@ -16,15 +16,12 @@ namespace ml {
class TextData final : public Data {
public:
TextData();
TextData(const TextData& text_data);
explicit TextData(const std::string& text);
explicit TextData(std::string text);
~TextData() override;
TextData(const TextData& text_data) = delete;
TextData& operator=(const TextData& text_data) = delete;
// Explicit copy assignment operator is required because the class
// inherits const member type_ that cannot be copied by default
TextData& operator=(const TextData& text_data);
std::string GetText() const;
const std::string& GetText() const;
private:
std::string text_;
@@ -67,7 +67,8 @@ VectorData::VectorData(const VectorData& vector_data)
storage_ = std::make_unique<VectorDataStorage>(*vector_data.storage_);
}
VectorData::VectorData(VectorData&& vector_data) : Data(DataType::kVector) {
VectorData::VectorData(VectorData&& vector_data) noexcept
: Data(DataType::kVector) {
storage_ = std::move(vector_data.storage_);
}
@@ -98,7 +99,7 @@ VectorData& VectorData::operator=(const VectorData& vector_data) {
return *this;
}
VectorData& VectorData::operator=(VectorData&& vector_data) {
VectorData& VectorData::operator=(VectorData&& vector_data) noexcept {
storage_ = std::move(vector_data.storage_);
return *this;
}
@@ -20,7 +20,7 @@ class VectorData final : public Data {
public:
VectorData();
VectorData(const VectorData& vector_data);
VectorData(VectorData&& vector_data);
VectorData(VectorData&& vector_data) noexcept;
// Make a "dense" DataVector with points 0..n-1 (n = data.size()):
// ({0, data[0]}, {1, data[0]}, .., {n-1, data[n-1]}}
@@ -34,7 +34,7 @@ class VectorData final : public Data {
// Explicit copy assignment && move operators is required because the class
// inherits const member type_ that cannot be copied by default
VectorData& operator=(const VectorData& vector_data);
VectorData& operator=(VectorData&& vector_data);
VectorData& operator=(VectorData&& vector_data) noexcept;
friend double operator*(const VectorData& lhs, const VectorData& rhs);
@@ -1,60 +0,0 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "bat/ads/internal/ml/ml_transformation_util.h"
#include <memory>
#include "base/notreached.h"
#include "bat/ads/internal/ml/transformation/hashed_ngrams_transformation.h"
#include "bat/ads/internal/ml/transformation/lowercase_transformation.h"
#include "bat/ads/internal/ml/transformation/normalization_transformation.h"
#include "bat/ads/internal/ml/transformation/transformation.h"
namespace ads {
namespace ml {
// The function should always return unique_ptr to transformation copy.
// NOTREACHED() is used to protect from handling unknown transformation types
TransformationPtr GetTransformationCopy(
const TransformationPtr& transformation_ptr) {
switch (transformation_ptr->GetType()) {
case TransformationType::kLowercase: {
LowercaseTransformation* lowercase_ptr =
static_cast<LowercaseTransformation*>(transformation_ptr.get());
LowercaseTransformation lowercase_copy = *lowercase_ptr;
return std::make_unique<LowercaseTransformation>(lowercase_copy);
}
case TransformationType::kHashedNGrams: {
HashedNGramsTransformation* hashed_n_grams_ptr =
static_cast<HashedNGramsTransformation*>(transformation_ptr.get());
HashedNGramsTransformation hashed_n_grams_ptr_copy = *hashed_n_grams_ptr;
return std::make_unique<HashedNGramsTransformation>(
hashed_n_grams_ptr_copy);
}
case TransformationType::kNormalization: {
NormalizationTransformation* normalization_ptr =
static_cast<NormalizationTransformation*>(transformation_ptr.get());
NormalizationTransformation normalization_copy = *normalization_ptr;
return std::make_unique<NormalizationTransformation>(normalization_copy);
}
default: {
NOTREACHED();
return TransformationPtr(nullptr);
}
}
}
TransformationVector GetTransformationVectorDeepCopy(
const TransformationVector& transformation_vector) {
TransformationVector transformation_vector_copy;
for (const TransformationPtr& transformation : transformation_vector) {
transformation_vector_copy.push_back(GetTransformationCopy(transformation));
}
return transformation_vector_copy;
}
} // namespace ml
} // namespace ads
@@ -1,23 +0,0 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_ML_ML_TRANSFORMATION_UTIL_H_
#define BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_ML_ML_TRANSFORMATION_UTIL_H_
#include "bat/ads/internal/ml/ml_aliases.h"
namespace ads {
namespace ml {
TransformationPtr GetTransformationCopy(
const TransformationPtr& transformation_ptr);
TransformationVector GetTransformationVectorDeepCopy(
const TransformationVector& transformation_vector);
} // namespace ml
} // namespace ads
#endif // BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_ML_ML_TRANSFORMATION_UTIL_H_
@@ -1,67 +0,0 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "bat/ads/internal/ml/ml_transformation_util.h"
#include <memory>
#include "bat/ads/internal/base/unittest/unittest_base.h"
#include "bat/ads/internal/ml/transformation/hashed_ngrams_transformation.h"
#include "bat/ads/internal/ml/transformation/normalization_transformation.h"
// npm run test -- brave_unit_tests --filter=BatAds*
namespace ads {
namespace ml {
class BatAdsMLTransformationUtilTest : public UnitTestBase {
protected:
BatAdsMLTransformationUtilTest() = default;
~BatAdsMLTransformationUtilTest() override = default;
};
TEST_F(BatAdsMLTransformationUtilTest, TransformationCopyTest) {
// Arrange
const NormalizationTransformation normalization;
TransformationPtr transformation_ptr =
std::make_unique<NormalizationTransformation>(normalization);
// Act
const TransformationPtr transformation_ptr_copy =
GetTransformationCopy(transformation_ptr);
// Assert
EXPECT_EQ(transformation_ptr_copy->GetType(),
TransformationType::kNormalization);
}
TEST_F(BatAdsMLTransformationUtilTest, TransformationVectorDeepCopyTest) {
// Arrange
const size_t kVectorSize = 2;
TransformationVector transformation_vector;
const HashedNGramsTransformation hashed_ngrams;
transformation_vector.push_back(
std::make_unique<HashedNGramsTransformation>(hashed_ngrams));
const NormalizationTransformation normalization;
transformation_vector.push_back(
std::make_unique<NormalizationTransformation>(normalization));
// Act
const TransformationVector transformation_vector_copy =
GetTransformationVectorDeepCopy(transformation_vector);
// Assert
ASSERT_EQ(kVectorSize, transformation_vector_copy.size());
EXPECT_TRUE(transformation_vector_copy[0]->GetType() ==
TransformationType::kHashedNGrams &&
transformation_vector_copy[1]->GetType() ==
TransformationType::kNormalization);
}
} // namespace ml
} // namespace ads
@@ -17,15 +17,15 @@ namespace model {
Linear::Linear() {}
Linear::Linear(const std::map<std::string, VectorData>& weights,
const std::map<std::string, double>& biases) {
weights_ = weights;
biases_ = biases;
Linear::Linear(std::map<std::string, VectorData> weights,
std::map<std::string, double> biases) {
weights_ = std::move(weights);
biases_ = std::move(biases);
}
Linear::Linear(const Linear& linear_model) = default;
Linear::Linear(Linear&& linear_model) noexcept = default;
Linear& Linear::operator=(const Linear& info) = default;
Linear& Linear::operator=(Linear&& linear_model) noexcept = default;
Linear::~Linear() = default;
@@ -20,10 +20,10 @@ class Linear final {
public:
Linear();
explicit Linear(const std::string& model);
Linear(const Linear& other);
Linear& operator=(const Linear& other);
Linear(const std::map<std::string, VectorData>& weights,
const std::map<std::string, double>& biases);
Linear(Linear&& other) noexcept;
Linear& operator=(Linear&& other) noexcept;
Linear(std::map<std::string, VectorData> weights,
std::map<std::string, double> biases);
~Linear();
PredictionMap Predict(const VectorData& x) const;
@@ -5,7 +5,8 @@
#include "bat/ads/internal/ml/pipeline/pipeline_info.h"
#include "bat/ads/internal/ml/ml_transformation_util.h"
#include <utility>
#include "bat/ads/internal/ml/transformation/transformation.h"
namespace ads {
@@ -14,26 +15,22 @@ namespace pipeline {
PipelineInfo::PipelineInfo() = default;
PipelineInfo::PipelineInfo(const PipelineInfo& info) {
version = info.version;
timestamp = info.timestamp;
locale = info.locale;
linear_model = info.linear_model;
transformations = GetTransformationVectorDeepCopy(info.transformations);
}
PipelineInfo::PipelineInfo(PipelineInfo&& info) noexcept = default;
PipelineInfo& PipelineInfo::operator=(PipelineInfo&& info) noexcept = default;
PipelineInfo::~PipelineInfo() = default;
PipelineInfo::PipelineInfo(const int& version,
PipelineInfo::PipelineInfo(const int version,
const std::string& timestamp,
const std::string& locale,
const TransformationVector& new_transformations,
const model::Linear& linear_model)
TransformationVector new_transformations,
model::Linear linear_model)
: version(version),
timestamp(timestamp),
locale(locale),
linear_model(linear_model) {
transformations = GetTransformationVectorDeepCopy(new_transformations);
linear_model(std::move(linear_model)) {
transformations = std::move(new_transformations);
}
} // namespace pipeline
@@ -17,14 +17,15 @@ namespace pipeline {
struct PipelineInfo final {
PipelineInfo();
PipelineInfo(const PipelineInfo& info);
PipelineInfo(PipelineInfo&& info) noexcept;
PipelineInfo& operator=(PipelineInfo&& info) noexcept;
~PipelineInfo();
PipelineInfo(const int& version,
PipelineInfo(const int version,
const std::string& timestamp,
const std::string& locale,
const TransformationVector& transformations,
const model::Linear& linear_model);
TransformationVector transformations,
model::Linear linear_model);
int version;
std::string timestamp;
@@ -14,7 +14,6 @@
#include "base/values.h"
#include "bat/ads/internal/ml/data/vector_data.h"
#include "bat/ads/internal/ml/ml_aliases.h"
#include "bat/ads/internal/ml/ml_transformation_util.h"
#include "bat/ads/internal/ml/pipeline/pipeline_info.h"
#include "bat/ads/internal/ml/transformation/hashed_ngrams_transformation.h"
#include "bat/ads/internal/ml/transformation/lowercase_transformation.h"
@@ -45,13 +44,12 @@ absl::optional<TransformationVector> ParsePipelineTransformations(
if (parsed_transformation_type.compare("TO_LOWER") == 0) {
transformations.value().push_back(
std::make_unique<LowercaseTransformation>(LowercaseTransformation()));
std::make_unique<LowercaseTransformation>());
}
if (parsed_transformation_type.compare("NORMALIZE") == 0) {
transformations.value().push_back(
std::make_unique<NormalizationTransformation>(
NormalizationTransformation()));
std::make_unique<NormalizationTransformation>());
}
if (parsed_transformation_type.compare("HASHED_NGRAMS") == 0) {
@@ -85,9 +83,9 @@ absl::optional<TransformationVector> ParsePipelineTransformations(
return absl::nullopt;
}
}
HashedNGramsTransformation hashed_ngrams(num_buckets, ngram_range);
transformations.value().push_back(
std::make_unique<HashedNGramsTransformation>(hashed_ngrams));
std::make_unique<HashedNGramsTransformation>(num_buckets,
ngram_range));
}
}
@@ -181,7 +179,7 @@ absl::optional<model::Linear> ParsePipelineClassifier(
}
absl::optional<model::Linear> linear_model =
model::Linear(weights, specified_biases);
model::Linear(std::move(weights), std::move(specified_biases));
return linear_model;
}
@@ -217,21 +215,15 @@ absl::optional<PipelineInfo> ParsePipelineValue(base::Value resource_value) {
return absl::nullopt;
}
const absl::optional<model::Linear> linear_model_optional =
absl::optional<model::Linear> linear_model_optional =
ParsePipelineClassifier(resource_value.FindKey("classifier"));
if (!linear_model_optional.has_value()) {
return absl::nullopt;
}
TransformationVector transformations =
GetTransformationVectorDeepCopy(transformations_optional.value());
const model::Linear linear_model = linear_model_optional.value();
absl::optional<PipelineInfo> pipeline_info =
PipelineInfo(version, timestamp, locale, transformations, linear_model);
return pipeline_info;
return PipelineInfo(version, timestamp, locale,
std::move(transformations_optional.value()),
std::move(linear_model_optional.value()));
}
} // namespace pipeline
@@ -12,7 +12,6 @@
#include "base/values.h"
#include "bat/ads/internal/ml/data/text_data.h"
#include "bat/ads/internal/ml/data/vector_data.h"
#include "bat/ads/internal/ml/ml_transformation_util.h"
#include "bat/ads/internal/ml/pipeline/pipeline_info.h"
#include "bat/ads/internal/ml/pipeline/pipeline_util.h"
#include "bat/ads/internal/ml/transformation/hashed_ngrams_transformation.h"
@@ -47,19 +46,19 @@ TextProcessing::TextProcessing() : is_initialized_(false) {}
TextProcessing::~TextProcessing() = default;
TextProcessing::TextProcessing(const TransformationVector& transformations,
const model::Linear& linear_model)
TextProcessing::TextProcessing(TransformationVector transformations,
model::Linear linear_model)
: is_initialized_(true) {
linear_model_ = linear_model;
transformations_ = GetTransformationVectorDeepCopy(transformations);
linear_model_ = std::move(linear_model);
transformations_ = std::move(transformations);
}
void TextProcessing::SetInfo(const PipelineInfo& info) {
void TextProcessing::SetInfo(PipelineInfo info) {
version_ = info.version;
timestamp_ = info.timestamp;
locale_ = info.locale;
linear_model_ = info.linear_model;
transformations_ = GetTransformationVectorDeepCopy(info.transformations);
linear_model_ = std::move(info.linear_model);
transformations_ = std::move(info.transformations);
}
bool TextProcessing::FromValue(base::Value resource_value) {
@@ -67,7 +66,7 @@ bool TextProcessing::FromValue(base::Value resource_value) {
ParsePipelineValue(std::move(resource_value));
if (pipeline_info.has_value()) {
SetInfo(pipeline_info.value());
SetInfo(std::move(pipeline_info.value()));
is_initialized_ = true;
} else {
is_initialized_ = false;
@@ -78,29 +77,27 @@ bool TextProcessing::FromValue(base::Value resource_value) {
PredictionMap TextProcessing::Apply(
const std::unique_ptr<Data>& input_data) const {
VectorData vector_data;
size_t transformation_count = transformations_.size();
const size_t transformation_count = transformations_.size();
if (!transformation_count) {
DCHECK(input_data->GetType() == DataType::kVector);
vector_data = *static_cast<VectorData*>(input_data.get());
} else {
std::unique_ptr<Data> current_data = transformations_[0]->Apply(input_data);
for (size_t i = 1; i < transformation_count; ++i) {
current_data = transformations_[i]->Apply(current_data);
}
DCHECK(current_data->GetType() == DataType::kVector);
vector_data = *static_cast<VectorData*>(current_data.get());
const VectorData* vector_data = static_cast<VectorData*>(input_data.get());
return linear_model_.GetTopPredictions(*vector_data);
}
return linear_model_.GetTopPredictions(vector_data);
std::unique_ptr<Data> current_data = transformations_[0]->Apply(input_data);
for (size_t i = 1; i < transformation_count; ++i) {
current_data = transformations_[i]->Apply(current_data);
}
DCHECK(current_data->GetType() == DataType::kVector);
const VectorData* vector_data = static_cast<VectorData*>(current_data.get());
return linear_model_.GetTopPredictions(*vector_data);
}
const PredictionMap TextProcessing::GetTopPredictions(
const std::string& html) const {
TextData text_data(html);
PredictionMap predictions = Apply(std::make_unique<TextData>(text_data));
PredictionMap predictions = Apply(std::make_unique<TextData>(html));
double expected_prob =
1.0 / std::max(1.0, static_cast<double>(predictions.size()));
PredictionMap rtn;
@@ -30,15 +30,15 @@ class TextProcessing final {
std::string* error_message);
TextProcessing();
TextProcessing(const TransformationVector& transformations,
const model::Linear& linear_model);
TextProcessing(TransformationVector transformations,
model::Linear linear_model);
~TextProcessing();
TextProcessing(const TextProcessing& pipeline) = delete;
TextProcessing& operator=(const TextProcessing& pipeline) = delete;
bool IsInitialized() const;
void SetInfo(const PipelineInfo& info);
void SetInfo(PipelineInfo info);
bool FromValue(base::Value resource_value);
@@ -57,12 +57,9 @@ TEST_F(BatAdsTextProcessingPipelineTest, BuildSimplePipeline) {
const std::string kTestString = "Test String";
TransformationVector transformations;
LowercaseTransformation lowercase;
transformations.push_back(
std::make_unique<LowercaseTransformation>(lowercase));
HashedNGramsTransformation hashed_ngrams(3, std::vector<int>{1, 2, 3});
transformations.push_back(
std::make_unique<HashedNGramsTransformation>(hashed_ngrams));
transformations.push_back(std::make_unique<LowercaseTransformation>());
transformations.push_back(std::make_unique<HashedNGramsTransformation>(
3, std::vector<int>{1, 2, 3}));
const std::map<std::string, VectorData> weights = {
{"class_1", VectorData({1.0, 2.0, 3.0})},
@@ -72,15 +69,16 @@ TEST_F(BatAdsTextProcessingPipelineTest, BuildSimplePipeline) {
const std::map<std::string, double> biases = {
{"class_1", 0.0}, {"class_2", 0.0}, {"class_3", 0.0}};
const model::Linear linear_model(weights, biases);
const pipeline::TextProcessing pipeline =
pipeline::TextProcessing(transformations, linear_model);
const VectorData data_point_3({1.0, 0.0, 0.0});
// Act
model::Linear linear_model(weights, biases);
const PredictionMap data_point_3_predictions =
linear_model.Predict(data_point_3);
const pipeline::TextProcessing pipeline = pipeline::TextProcessing(
std::move(transformations), std::move(linear_model));
// Act
const PredictionMap predictions = pipeline.GetTopPredictions(kTestString);
// Assert
@@ -115,7 +113,7 @@ TEST_F(BatAdsTextProcessingPipelineTest, TestLoadFromValue) {
std::vector<PredictionMap> prediction_maps(train_texts.size());
for (size_t i = 0; i < train_texts.size(); i++) {
const std::unique_ptr<Data> text_data =
std::make_unique<TextData>(TextData(train_texts[i]));
std::make_unique<TextData>(train_texts[i]);
const PredictionMap prediction_map =
text_processing_pipeline.Apply(text_data);
prediction_maps[i] = prediction_map;
@@ -36,11 +36,6 @@ HashVectorizer::HashVectorizer(const int bucket_count,
bucket_count_ = bucket_count;
}
HashVectorizer::HashVectorizer(const HashVectorizer& hash_vectorizer) {
bucket_count_ = hash_vectorizer.GetBucketCount();
substring_sizes_ = hash_vectorizer.GetSubstringSizes();
}
std::vector<uint32_t> HashVectorizer::GetSubstringSizes() const {
return substring_sizes_;
}
@@ -17,9 +17,10 @@ namespace ml {
class HashVectorizer final {
public:
HashVectorizer();
HashVectorizer(const HashVectorizer& other);
HashVectorizer(const int n_buckets, const std::vector<int>& subgrams);
~HashVectorizer();
HashVectorizer(const HashVectorizer& info) = delete;
HashVectorizer& operator=(const HashVectorizer& info) = delete;
std::map<uint32_t, double> GetFrequencies(const std::string& html) const;
@@ -17,15 +17,11 @@ namespace ml {
HashedNGramsTransformation::HashedNGramsTransformation()
: Transformation(TransformationType::kHashedNGrams) {
hash_vectorizer = std::make_unique<HashVectorizer>(HashVectorizer());
hash_vectorizer = std::make_unique<HashVectorizer>();
}
HashedNGramsTransformation::HashedNGramsTransformation(
const HashedNGramsTransformation& hashed_ngrams)
: Transformation(TransformationType::kHashedNGrams) {
HashVectorizer hash_vectorizer_copy = *(hashed_ngrams.hash_vectorizer);
hash_vectorizer = std::make_unique<HashVectorizer>(hash_vectorizer_copy);
}
HashedNGramsTransformation&& hashed_ngrams) noexcept = default;
HashedNGramsTransformation::~HashedNGramsTransformation() = default;
@@ -33,8 +29,7 @@ HashedNGramsTransformation::HashedNGramsTransformation(
const int bucket_count,
const std::vector<int>& subgrams)
: Transformation(TransformationType::kHashedNGrams) {
hash_vectorizer =
std::make_unique<HashVectorizer>(HashVectorizer(bucket_count, subgrams));
hash_vectorizer = std::make_unique<HashVectorizer>(bucket_count, subgrams);
}
std::unique_ptr<Data> HashedNGramsTransformation::Apply(
@@ -47,7 +42,7 @@ std::unique_ptr<Data> HashedNGramsTransformation::Apply(
hash_vectorizer->GetFrequencies(text_data->GetText());
int dimension_count = hash_vectorizer->GetBucketCount();
return std::make_unique<VectorData>(VectorData(dimension_count, frequences));
return std::make_unique<VectorData>(dimension_count, frequences);
}
} // namespace ml
@@ -20,9 +20,12 @@ class HashVectorizer;
class HashedNGramsTransformation final : public Transformation {
public:
HashedNGramsTransformation();
HashedNGramsTransformation(const HashedNGramsTransformation& hashed_ngrams);
HashedNGramsTransformation(const int bucket_count,
const std::vector<int>& subgrams);
HashedNGramsTransformation(
HashedNGramsTransformation&& hashed_ngrams) noexcept;
HashedNGramsTransformation& operator=(
HashedNGramsTransformation&& hashed_ngrams) = delete;
~HashedNGramsTransformation() override;
explicit HashedNGramsTransformation(const std::string& parameters);
@@ -27,7 +27,7 @@ TEST_F(BatAdsHashedNGramsTest, HashingTest) {
const size_t kExpectedElementCount = 10;
const std::string kTestString = "tiny";
const std::unique_ptr<Data> text_data =
std::make_unique<TextData>(TextData(kTestString));
std::make_unique<TextData>(kTestString);
const HashedNGramsTransformation hashed_ngrams;
@@ -54,7 +54,7 @@ TEST_F(BatAdsHashedNGramsTest, CustomHashingTest) {
const int kHashBucketCount = 3;
const std::string kTestString = "tiny";
const std::unique_ptr<Data> text_data =
std::make_unique<TextData>(TextData(kTestString));
std::make_unique<TextData>(kTestString);
const HashedNGramsTransformation hashed_ngrams(kHashBucketCount,
std::vector<int>{1, 2, 3});
@@ -6,6 +6,7 @@
#include "bat/ads/internal/ml/transformation/lowercase_transformation.h"
#include <string>
#include <utility>
#include "base/check.h"
#include "base/strings/string_util.h"
@@ -19,7 +20,7 @@ LowercaseTransformation::LowercaseTransformation()
: Transformation(TransformationType::kLowercase) {}
LowercaseTransformation::LowercaseTransformation(
const LowercaseTransformation& transformation) = default;
LowercaseTransformation&& transformation) noexcept = default;
LowercaseTransformation::~LowercaseTransformation() = default;
@@ -31,7 +32,7 @@ std::unique_ptr<Data> LowercaseTransformation::Apply(
std::string lowercase_text = base::ToLowerASCII(text_data->GetText());
return std::make_unique<TextData>(TextData(lowercase_text));
return std::make_unique<TextData>(std::move(lowercase_text));
}
} // namespace ml
@@ -18,7 +18,9 @@ class Data;
class LowercaseTransformation final : public Transformation {
public:
LowercaseTransformation();
LowercaseTransformation(const LowercaseTransformation& transformation);
LowercaseTransformation(LowercaseTransformation&& transformation) noexcept;
LowercaseTransformation& operator=(LowercaseTransformation&& transformation) =
delete;
~LowercaseTransformation() override;
std::unique_ptr<Data> Apply(
@@ -5,6 +5,8 @@
#include "bat/ads/internal/ml/transformation/normalization_transformation.h"
#include <utility>
#include "base/check.h"
#include "bat/ads/internal/ml/data/vector_data.h"
@@ -15,7 +17,7 @@ NormalizationTransformation::NormalizationTransformation()
: Transformation(TransformationType::kNormalization) {}
NormalizationTransformation::NormalizationTransformation(
const NormalizationTransformation& transformation) = default;
NormalizationTransformation&& transformation) noexcept = default;
NormalizationTransformation::~NormalizationTransformation() = default;
@@ -27,7 +29,7 @@ std::unique_ptr<Data> NormalizationTransformation::Apply(
VectorData vector_data_copy = *vector_data;
vector_data_copy.Normalize();
return std::make_unique<VectorData>(vector_data_copy);
return std::make_unique<VectorData>(std::move(vector_data_copy));
}
} // namespace ml
@@ -17,7 +17,9 @@ class NormalizationTransformation final : public Transformation {
public:
NormalizationTransformation();
NormalizationTransformation(
const NormalizationTransformation& transformation);
NormalizationTransformation&& transformation) noexcept;
NormalizationTransformation& operator=(
NormalizationTransformation&& transformation) = delete;
~NormalizationTransformation() override;
std::unique_ptr<Data> Apply(
@@ -32,8 +32,7 @@ TEST_F(BatAdsNormalizationTest, NormalizationTest) {
const double kTolerance = 1e-7;
std::string kTestString = "quite a small test string";
TextData text_data(kTestString);
std::unique_ptr<Data> data = std::make_unique<TextData>(text_data);
std::unique_ptr<Data> data = std::make_unique<TextData>(kTestString);
HashedNGramsTransformation hashed_ngrams(10, std::vector<int>{3, 4});
NormalizationTransformation normalization;
@@ -70,17 +69,13 @@ TEST_F(BatAdsNormalizationTest, ChainingTest) {
TransformationVector chain;
const LowercaseTransformation lowercase;
chain.push_back(std::make_unique<LowercaseTransformation>(lowercase));
chain.push_back(std::make_unique<LowercaseTransformation>());
const HashedNGramsTransformation hashed_ngrams;
chain.push_back(std::make_unique<HashedNGramsTransformation>(hashed_ngrams));
chain.push_back(std::make_unique<HashedNGramsTransformation>());
const NormalizationTransformation normalization;
chain.push_back(std::make_unique<NormalizationTransformation>(normalization));
chain.push_back(std::make_unique<NormalizationTransformation>());
const TextData text_data(kTestString);
std::unique_ptr<Data> data = std::make_unique<TextData>(text_data);
std::unique_ptr<Data> data = std::make_unique<TextData>(kTestString);
// Act
for (size_t i = 0; i < chain.size(); ++i) {
@@ -10,8 +10,6 @@ namespace ml {
Transformation::Transformation(const TransformationType& type) : type_(type) {}
Transformation::Transformation(const Transformation& t) = default;
Transformation::~Transformation() = default;
TransformationType Transformation::GetType() const {
@@ -19,7 +19,6 @@ class Transformation {
public:
explicit Transformation(const TransformationType& type);
Transformation(const Transformation& t);
virtual ~Transformation();
TransformationType GetType() const;