Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
fix LinearRegressionOutput with empty label (#15620)
Browse files Browse the repository at this point in the history
  • Loading branch information
haohuanw authored and KellenSunderland committed Jul 24, 2019
1 parent 858e171 commit 0e0c7f3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cpp-package/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)

add_executable(test_regress_label test_regress_label.cpp ${CPP_PACKAGE_HAEDERS})
target_link_libraries(test_regress_label ${CPP_EXAMPLE_LIBS})
add_dependencies(test_regress_label ${CPPEX_DEPS})

add_executable(lenet lenet.cpp ${CPP_PACKAGE_HEADERS})
target_link_libraries(lenet ${CPP_EXAMPLE_LIBS})
add_dependencies(lenet ${CPPEX_DEPS})
Expand Down
56 changes: 56 additions & 0 deletions cpp-package/example/test_regress_label.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
* This file is used for testing LinearRegressionOutput can
* still bind if label is not provided
*/

#include <iostream>
#include <vector>
#include <string>
#include "dmlc/logging.h"
#include "mxnet-cpp/MxNetCpp.h"

using namespace mxnet::cpp;

int main() {
LOG(INFO) << "Running LinearRegressionOutput symbol testing, "
"executor should be able to bind without label.";
Symbol data = Symbol::Variable("data");
Symbol label = Symbol::Variable("regress_label");
Symbol symbol = LinearRegressionOutput(data, label);
std::map<std::string, mxnet::cpp::OpReqType> opReqMap;
for (const auto& iter : symbol.ListArguments()) {
opReqMap[iter] = mxnet::cpp::OpReqType::kNullOp;
}
std::map<std::string, mxnet::cpp::NDArray> argMap({
{"data", NDArray(Shape{1, 3}, Context::cpu(), true)}
});

try {
symbol.SimpleBind(Context::cpu(),
argMap,
std::map<std::string, mxnet::cpp::NDArray>(),
opReqMap,
std::map<std::string, mxnet::cpp::NDArray>());
} catch (const std::exception& e) {
LOG(ERROR) << "Error binding the symbol: " << MXGetLastError() << " " << e.what();
throw;
}
return 0;
}
3 changes: 3 additions & 0 deletions cpp-package/tests/ci_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ cp ../../build/cpp-package/example/test_score .
cp ../../build/cpp-package/example/test_ndarray_copy .
./test_ndarray_copy

cp ../../build/cpp-package/example/test_regress_label .
./test_regress_label

sh unittests/unit_test_mlp_csv.sh

cd inference
Expand Down
3 changes: 2 additions & 1 deletion src/operator/regression_output-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ inline bool RegressionOpShape(const nnvm::NodeAttrs& attrs,
const mxnet::TShape &dshape = in_attrs->at(0);
if (!shape_is_known(dshape)) return false;
auto &lshape = (*in_attrs)[1];
if (lshape.ndim() == 0) {
// if label is not defined, manually build the shape based on dshape
if (lshape.ndim() == -1) {
// special treatment for 1D output, to allow 1D label by default.
// Think about change convention later
if (dshape.ndim() == 2 && dshape[1] == 1) {
Expand Down

0 comments on commit 0e0c7f3

Please sign in to comment.