Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added checks for relative input shapes in linear regression using the utility introduced in #2370 #3640

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 4 additions & 11 deletions src/mlpack/methods/linear_regression/linear_regression_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,30 +289,23 @@ LinearRegression<ModelMatType>::ComputeError(

// Get the number of columns and rows of the dataset.
const size_t nCols = predictors.n_cols;
const size_t nRows = predictors.n_rows;

// Calculate the differences between actual responses and predicted responses.
// We must also add the intercept (parameters(0)) to the predictions.
arma::Row<typename ResponsesType::elem_type> temp;
if (intercept)
{
// Ensure that we have the correct number of dimensions in the dataset.
if (nRows != parameters.n_rows - 1)
{
Log::Fatal << "The test data must have the same number of columns as the "
"training file." << std::endl;
}
util::CheckSameDimensionality(predictors, parameters.n_rows - 1,
"LinearRegression::ComputeError()", "predictors");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can remove the nRows variable now; it appears to be unused.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I'll remove the nRows declaration.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done @rcurtin

temp = responses - (parameters(0) +
parameters.subvec(1, parameters.n_elem - 1).t() * predictors);
}
else
{
// Ensure that we have the correct number of dimensions in the dataset.
if (nRows != parameters.n_rows)
{
Log::Fatal << "The test data must have the same number of columns as the "
"training file." << std::endl;
}
util::CheckSameDimensionality(predictors, parameters.n_rows,
"LinearRegression::ComputeError()", "predictors");
temp = responses - parameters.t() * predictors;
}
const ElemType cost = dot(temp, temp) / nCols;
Expand Down
17 changes: 7 additions & 10 deletions src/mlpack/methods/linear_regression/linear_regression_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timer)
timer.Start("load_responses");
responses = params.Get<rowvec>("training_responses");
timer.Stop("load_responses");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, what I meant was that there was no need to replace the blank line with a line that has four spaces. I didn't mean that we should remove the line. 👍

if (responses.n_cols != regressors.n_cols)
{
Log::Fatal << "The responses must have the same number of columns "
Expand Down Expand Up @@ -198,16 +197,14 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timer)
mat points = std::move(params.Get<mat>("test"));

// Ensure that test file data has the right number of features.
if ((lr->Parameters().n_elem - 1) != points.n_rows)
try
{
util::CheckSameDimensionality(points, lr->Parameters().n_elem - 1,
"Linear Regression Prediction", "test points");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's even necessary to keep this check---Predict() already has a CheckSameDimensionality() call inside it. So I think we can remove this (and the same for linear_regression_predict_main.cpp).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure I'll remove this check

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done @rcurtin

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the build tests failed upon removing this check I reverted this change and made a commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did you try to debug the issue? "The build tests failed" is not a sufficient reason to give up on the comment here.

}
catch (std::invalid_argument& e)
{
// If we built the model, nothing will free it so we have to...
const size_t dimensions = lr->Parameters().n_elem - 1;
if (computeModel)
delete lr;

Log::Fatal << "The model was trained on " << dimensions << "-dimensional "
<< "data, but the test points in '" << testOutput << "' are "
<< points.n_rows << "-dimensional!" << endl;
Log::Fatal << e.what() << std::endl;
}

// Perform the predictions using our model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& timer)
mat points = std::move(params.Get<mat>("test"));

// Ensure that test file data has the right number of features.
if ((lr->Parameters().n_elem - 1) != points.n_rows)
try
{
// If we built the model, nothing will free it so we have to...
const size_t dimensions = lr->Parameters().n_elem - 1;
Log::Fatal << "The model was trained on " << dimensions << "-dimensional "
<< "data, but the test points in '" << testOutput << "' are "
<< points.n_rows << "-dimensional!" << endl;
util::CheckSameDimensionality(points, lr->Parameters().n_elem - 1,
"Linear Regression Prediction", "test points");
}
catch (std::invalid_argument& e)
{
Log::Fatal << e.what() << std::endl;
}

// Perform the predictions using our model.
Expand Down