Skip to content

Commit

Permalink
Merge pull request #3643 from shrit/coot_12
Browse files Browse the repository at this point in the history
Coot 12, refactor, `arma::min`, `arma::imbue` and some other bugs
  • Loading branch information
shrit committed Mar 21, 2024
2 parents 9de14e5 + 2f71e92 commit ac88020
Show file tree
Hide file tree
Showing 28 changed files with 45 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/mlpack/core/data/scaler_methods/max_abs_scaler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MaxAbsScaler
template<typename MatType>
void Fit(const MatType& input)
{
itemMin = arma::min(input, 1);
itemMin = min(input, 1);
itemMax = arma::max(input, 1);
scale = arma::max(arma::abs(itemMin), arma::abs(itemMax));
// Handling zeros in scale vector.
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/core/data/scaler_methods/mean_normalization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MeanNormalization
void Fit(const MatType& input)
{
itemMean = arma::mean(input, 1);
itemMin = arma::min(input, 1);
itemMin = min(input, 1);
itemMax = arma::max(input, 1);
scale = itemMax - itemMin;
// Handling zeros in scale vector.
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/core/data/scaler_methods/min_max_scaler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class MinMaxScaler
template<typename MatType>
void Fit(const MatType& input)
{
itemMin = arma::min(input, 1);
itemMin = min(input, 1);
itemMax = arma::max(input, 1);
scale = itemMax - itemMin;
// Handle zeros in scale vector.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool RPTreeMaxSplit<BoundType, MatType>::GetSplitVal(
values[k] = dot(data.col(samples[k]), direction);

const ElemType maximum = arma::max(values);
const ElemType minimum = arma::min(values);
const ElemType minimum = min(values);
if (minimum == maximum)
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ bool RPTreeMeanSplit<BoundType, MatType>::GetDotMedian(
values[k] = dot(data.col(samples[k]), direction);

const ElemType maximum = arma::max(values);
const ElemType minimum = arma::min(values);
const ElemType minimum = min(values);
if (minimum == maximum)
return false;

Expand Down Expand Up @@ -129,7 +129,7 @@ bool RPTreeMeanSplit<BoundType, MatType>::GetMeanMedian(
}

const ElemType maximum = arma::max(values);
const ElemType minimum = arma::min(values);
const ElemType minimum = min(values);
if (minimum == maximum)
return false;

Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/core/tree/cellbound_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ CellBound<MetricType, ElemType>::operator|=(const MatType& data)
{
Log::Assert(data.n_rows == dim);

arma::Col<ElemType> mins(arma::min(data, 1));
arma::Col<ElemType> mins(min(data, 1));
arma::Col<ElemType> maxs(arma::max(data, 1));

minWidth = std::numeric_limits<ElemType>::max();
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/core/tree/cosine_tree/cosine_tree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ inline void CosineTree::CosineNodeSplit()
// Compute maximum and minimum cosine values.
double cosineMax, cosineMin;
cosineMax = arma::max(cosines % (cosines < 1));
cosineMin = arma::min(cosines);
cosineMin = min(cosines);

std::vector<size_t> leftIndices, rightIndices;

Expand Down
2 changes: 2 additions & 0 deletions src/mlpack/core/util/using.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace mlpack {
/* using for armadillo namespace*/
using arma::conv_to;
using arma::exp;
using arma::distr_param;
using arma::dot;
using arma::join_cols;
using arma::join_rows;
Expand All @@ -46,6 +47,7 @@ namespace mlpack {
/* using for bandicoot namespace*/
using coot::conv_to;
using coot::exp;
using coot::distr_param;
using coot::dot;
using coot::join_cols;
using coot::join_rows;
Expand Down
4 changes: 2 additions & 2 deletions src/mlpack/methods/ann/init_rules/gaussian_init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class GaussianInitialization
if (W.is_empty())
W.set_size(rows, cols);

W.imbue( [&]() { return arma::as_scalar(RandNormal(mean, stddev)); } );
W = randn<MatType>(rows, cols) * stddev + mean;
}

/**
Expand All @@ -68,7 +68,7 @@ class GaussianInitialization
if (W.is_empty())
Log::Fatal << "Cannot initialize an empty matrix." << std::endl;

W.imbue( [&]() { return arma::as_scalar(RandNormal(mean, stddev)); } );
W = randn<MatType>(W.n_rows, W.n_cols) * stddev + mean;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/mlpack/methods/ann/init_rules/he_init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class HeInitialization

// Multipling a random variable X with variance V(X) by some factor c,
// then the variance V(cX) = (c^2) * V(X).
W.imbue( [&]() { return std::sqrt(variance) * arma::randn(); } );
W = randn<MatType>(rows, cols) * std::sqrt(variance);
}

/**
Expand All @@ -96,7 +96,7 @@ class HeInitialization

// Multipling a random variable X with variance V(X) by some factor c,
// then the variance V(cX) = (c^2) * V(X).
W.imbue( [&]() { return std::sqrt(variance) * arma::randn(); } );
W = randn<MatType>(W.n_rows, W.n_cols) * std::sqrt(variance);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/mlpack/methods/ann/init_rules/lecun_normal_init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class LecunNormalInitialization
const size_t rows,
const size_t cols)
{
// He initialization rule says to initialize weights with random
// Lecun initialization rule says to initialize weights with random
// values taken from a gaussian distribution with mean = 0 and
// standard deviation = sqrt(1 / rows), i.e. variance = (1 / rows).
const double variance = 1.0 / ((double) rows);
Expand All @@ -79,7 +79,7 @@ class LecunNormalInitialization

// Multipling a random variable X with variance V(X) by some factor c,
// then the variance V(cX) = (c ^ 2) * V(X).
W.imbue( [&]() { return std::sqrt(variance) * arma::randn(); } );
W = randn<MatType>(rows, cols) * std::sqrt(variance);
}

/**
Expand All @@ -92,7 +92,7 @@ class LecunNormalInitialization
void Initialize(MatType& W,
const typename std::enable_if_t<IsMatrix<MatType>::value>* = 0)
{
// He initialization rule says to initialize weights with random
// Lecun initialization rule says to initialize weights with random
// values taken from a gaussian distribution with mean = 0 and
// standard deviation = sqrt(1 / rows), i.e. variance = (1 / rows).
const double variance = 1.0 / (double) W.n_rows;
Expand All @@ -102,7 +102,7 @@ class LecunNormalInitialization

// Multipling a random variable X with variance V(X) by some factor c,
// then the variance V(cX) = (c ^ 2) * V(X).
W.imbue( [&]() { return std::sqrt(variance) * arma::randn(); } );
W = randn<MatType>(W.n_rows, W.n_cols) * std::sqrt(variance);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/ann/layer/parametric_relu_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void PReLUType<MatType>::Gradient(
{
MatType zeros = arma::zeros<MatType>(input.n_rows, input.n_cols);
gradient.set_size(1, 1);
gradient(0) = accu(error % arma::min(zeros, input)) / input.n_cols;
gradient(0) = accu(error % min(zeros, input)) / input.n_cols;
}

template<typename MatType>
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/ann/layer/softmin_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void SoftminType<MatType>::Forward(
MatType& output)
{
MatType softminInput = exp(-(input.each_row() -
arma::min(input, 0)));
min(input, 0)));
output = softminInput.each_row() / sum(softminInput, 0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/approx_kfn/approx_kfn_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers)
const double averageError = sum(exactDistances.row(0) /
distances.row(0)) / distances.n_cols;
const double minError = arma::min(exactDistances.row(0) /
const double minError = min(exactDistances.row(0) /
distances.row(0));
const double maxError = max(exactDistances.row(0) /
distances.row(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ double AllCategoricalSplit<FitnessFunction>::SplitIfBetter(

// If each child will have the minimum number of points in it, we can split.
// Otherwise we can't.
if (arma::min(counts) < minimumLeafSize)
if (min(counts) < minimumLeafSize)
return DBL_MAX;

// Calculate the gain of the split. First we have to calculate the labels
Expand Down Expand Up @@ -150,7 +150,7 @@ double AllCategoricalSplit<FitnessFunction>::SplitIfBetter(

// If each child will have the minimum number of points in it, we can split.
// Otherwise we can't.
if (arma::min(counts) < minimumLeafSize)
if (min(counts) < minimumLeafSize)
return DBL_MAX;

// Calculate the gain of the split. First we have to calculate the labels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ double RandomBinaryNumericSplit<FitnessFunction>::SplitIfBetter(
return DBL_MAX; // It can't be outperformed.

typename VecType::elem_type maxValue = max(data);
typename VecType::elem_type minValue = arma::min(data);
typename VecType::elem_type minValue = min(data);

// Sanity check: if the maximum element is the same as the minimum, we
// can't split in this dimension.
Expand Down Expand Up @@ -162,7 +162,7 @@ double RandomBinaryNumericSplit<FitnessFunction>::SplitIfBetter(
return DBL_MAX; // It can't be outperformed.

typename VecType::elem_type maxValue = max(data);
typename VecType::elem_type minValue = arma::min(data);
typename VecType::elem_type minValue = min(data);

// Sanity check: if the maximum element is the same as the minimum, we
// can't split in this dimension.
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/det/dtree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ DTree<MatType, TagType>::DTree(MatType & data) :
start(0),
end(data.n_cols),
maxVals(arma::max(data, 1)),
minVals(arma::min(data, 1)),
minVals(min(data, 1)),
splitDim(size_t(-1)),
splitValue(std::numeric_limits<ElemType>::max()),
subtreeLeavesLogNegError(-DBL_MAX),
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/kmeans/elkan_kmeans_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ double ElkanKMeans<MetricType, MatType>::Iterate(const arma::mat& centroids,

// Now find the closest cluster to each other cluster. We multiply by 0.5 so
// that this is equivalent to s(c) for each cluster c.
minClusterDistances = 0.5 * arma::min(clusterDistances).t();
minClusterDistances = 0.5 * min(clusterDistances).t();

// Now loop over all points, and see which ones need to be updated.
for (size_t i = 0; i < dataset.n_cols; ++i)
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/lmnn/constraints_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Constraints<MetricType>::Constraints(
precalculated(false)
{
// Ensure a valid k is passed.
size_t minCount = arma::min(arma::histc(labels, arma::unique(labels)));
size_t minCount = min(arma::histc(labels, arma::unique(labels)));

if (minCount < k + 1)
{
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/lmnn/lmnn_function_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ LMNNFunction<MetricType>::LMNNFunction(const arma::mat& dataset,
oldTransformationCounts.push_back(dataset.n_cols);

// Check if we can impose bounds over impostors.
size_t minCount = arma::min(arma::histc(labels, arma::unique(labels)));
size_t minCount = min(arma::histc(labels, arma::unique(labels)));
if (minCount <= k + 1)
{
// Initialize target neighbors & impostors.
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/lmnn/lmnn_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers)
else if (normalize)
{
// Find the minimum and maximum values for each dimension.
arma::vec ranges = max(data, 1) - arma::min(data, 1);
arma::vec ranges = max(data, 1) - min(data, 1);
for (size_t d = 0; d < ranges.n_elem; ++d)
if (ranges[d] == 0.0)
ranges[d] = 1; // A range of 0 produces NaN later on.
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/preprocess/preprocess_describe_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timers)

// f at the front of the variable names means "feature".
const double fMax = max(feature);
const double fMin = arma::min(feature);
const double fMin = min(feature);
const double fMean = arma::mean(feature);
const double fStd = arma::stddev(feature, population);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class MountainCar
State state;
stepsPerformed = 0;
state.Velocity() = 0.0;
state.Position() = arma::as_scalar(arma::randu(1)) * 0.2 - 0.6;
state.Position() = randu() * 0.2 - 0.6;
return state;
}

Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/reinforcement_learning/sac_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void SAC<
targetQ1Network.Predict(targetQInput, Q1);
targetQ2Network.Predict(targetQInput, Q2);
arma::rowvec nextQ = sampledRewards + config.Discount() * ((1 - isTerminal)
% arma::min(Q1, Q2));
% min(Q1, Q2));

arma::mat sampledActionValues(action.size, sampledActions.size());
for (size_t i = 0; i < sampledActions.size(); i++)
Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/methods/reinforcement_learning/td3_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void TD3<
targetQ1Network.Predict(targetQInput, Q1);
targetQ2Network.Predict(targetQInput, Q2);
arma::rowvec nextQ = sampledRewards + config.Discount() * ((1 - isTerminal)
% arma::min(Q1, Q2));
% min(Q1, Q2));

arma::mat sampledActionValues(action.size, sampledActions.size());
for (size_t i = 0; i < sampledActions.size(); i++)
Expand Down
16 changes: 6 additions & 10 deletions src/mlpack/tests/ann/loss_functions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ TEST_CASE("HuberLossTest", "[LossFunctionsTest]")

// Test the Backward function.
module.Backward(input, target, output);
REQUIRE(arma::as_scalar(accu(output)) ==
Approx(-0.8032).epsilon(1e-3));
REQUIRE(accu(output) == Approx(-0.8032).epsilon(1e-3));
REQUIRE(output.n_rows == input.n_rows);
REQUIRE(output.n_cols == input.n_cols);
CheckMatrices(output, expectedOutput, 0.1);
Expand All @@ -69,8 +68,7 @@ TEST_CASE("HuberLossTest", "[LossFunctionsTest]")

// Test the Backward function.
module.Backward(input, target, output);
REQUIRE(arma::as_scalar(accu(output)) ==
Approx(-0.0669333).epsilon(1e-3));
REQUIRE(accu(output) == Approx(-0.0669333).epsilon(1e-3));
REQUIRE(output.n_rows == input.n_rows);
REQUIRE(output.n_cols == input.n_cols);
CheckMatrices(output, expectedOutput, 0.1);
Expand Down Expand Up @@ -171,7 +169,7 @@ TEST_CASE("SimpleKLDivergenceTest", "[LossFunctionsTest]")

// Test the Backward function.
module.Backward(input, target, output);
REQUIRE(arma::as_scalar(accu(output)) == Approx(-5.8127).epsilon(1e-3));
REQUIRE(accu(output) == Approx(-5.8127).epsilon(1e-3));
REQUIRE(output.n_rows == input.n_rows);
REQUIRE(output.n_cols == input.n_cols);
CheckMatrices(output, expectedOutput, 0.1);
Expand All @@ -189,7 +187,7 @@ TEST_CASE("SimpleKLDivergenceTest", "[LossFunctionsTest]")

// Test the Backward function.
module.Backward(input, target, output);
REQUIRE(arma::as_scalar(accu(output)) == Approx(-0.484392).epsilon(1e-3));
REQUIRE(accu(output) == Approx(-0.484392).epsilon(1e-3));
REQUIRE(output.n_rows == input.n_rows);
REQUIRE(output.n_cols == input.n_cols);
CheckMatrices(output, expectedOutput, 0.1);
Expand Down Expand Up @@ -221,8 +219,7 @@ TEST_CASE("SimpleMeanSquaredLogarithmicErrorTest", "[LossFunctionsTest]")

// Test the Backward function.
module.Backward(input, target, output);
REQUIRE(arma::as_scalar(accu(output)) ==
Approx(-10.5619).epsilon(1e-3));
REQUIRE(accu(output) == Approx(-10.5619).epsilon(1e-3));
REQUIRE(output.n_rows == input.n_rows);
REQUIRE(output.n_cols == input.n_cols);
CheckMatrices(output, expectedOutput, 0.1);
Expand Down Expand Up @@ -459,8 +456,7 @@ TEST_CASE("SimpleEarthMoverDistanceLayerTest", "[LossFunctionsTest]")

// Test the Backward function.
module.Backward(input3, target3, output);
REQUIRE(arma::as_scalar(accu(output)) ==
Approx(-0.168867).epsilon(1e-3));
REQUIRE(accu(output) == Approx(-0.168867).epsilon(1e-3));
REQUIRE(output.n_rows == input3.n_rows);
REQUIRE(output.n_cols == input3.n_cols);
CheckMatrices(output, expectedOutput, 0.1);
Expand Down
10 changes: 4 additions & 6 deletions src/mlpack/tests/ann/recurrent_network_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,21 @@ void GenerateNoisySines(arma::cube& data,
{
arma::colvec x = arma::linspace<arma::colvec>(0, points - 1, points) /
points * 20.0;
arma::colvec y1 = arma::sin(x + arma::as_scalar(arma::randu(1)) * 3.0);
arma::colvec y2 = arma::sin(x / 2.0 + arma::as_scalar(arma::randu(1)) * 3.0);
arma::colvec y1 = arma::sin(x + randu() * 3.0);
arma::colvec y2 = arma::sin(x / 2.0 + randu() * 3.0);

data = arma::zeros(1 /* single dimension */, sequences * 2, points);
labels = arma::zeros(2 /* 2 classes */, sequences * 2);

for (size_t seq = 0; seq < sequences; seq++)
{
arma::vec sequence = arma::randu(points) * noise + y1 +
arma::as_scalar(arma::randu(1) - 0.5) * noise;
arma::vec sequence = randu(points) * noise + y1 + (randu() - 0.5) * noise;
for (size_t i = 0; i < points; ++i)
data(0, seq, i) = sequence[i];

labels(0, seq) = 1;

sequence = arma::randu(points) * noise + y2 +
arma::as_scalar(arma::randu(1) - 0.5) * noise;
sequence = randu(points) * noise + y2 + (randu() - 0.5) * noise;
for (size_t i = 0; i < points; ++i)
data(0, sequences + seq, i) = sequence[i];

Expand Down
2 changes: 1 addition & 1 deletion src/mlpack/tests/cosine_tree_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ TEST_CASE("CosineNodeCosineSplit", "[CosineTreeTest]")
// CosineNodeSplit may differ from cosineMax below, so we have to handle
// minor differences.
double cosineMax = arma::max(cosines % (cosines < 1.0 + precision));
double cosineMin = arma::min(cosines);
double cosineMin = min(cosines);
// If max(cosines) is close to 1.0 cosineMax and cosineMax2 may
// differ significantly.
double cosineMax2 = arma::max(cosines % (cosines < 1.0 - precision));
Expand Down

0 comments on commit ac88020

Please sign in to comment.