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

Bug fix for first_metric_only on earlystopping #2127

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions .ci/dockers/ubuntu-14.04/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,6 @@ RUN curl -sL https://cmake.org/files/v3.14/cmake-3.14.1-Linux-x86_64.sh -o cmake
&& ./cmake.sh --prefix=/usr/local --exclude-subdir \
&& rm cmake.sh

# Install Java
RUN add-apt-repository ppa:openjdk-r/ppa -y \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
openjdk-8-jdk \
&& rm -rf /var/lib/apt/lists/*

ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/

# Install SWIG
RUN curl -sL https://downloads.sourceforge.net/project/swig/swig/swig-3.0.12/swig-3.0.12.tar.gz -o swig.tar.gz \
&& tar -xzf swig.tar.gz \
&& cd swig-3.0.12 \
&& ./configure --prefix=/usr/local --without-pcre \
&& make \
&& make install \
&& cd .. \
&& rm swig.tar.gz \
&& rm -rf swig-3.0.12

# Install Miniconda
RUN curl -sL https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -o miniconda.sh \
&& chmod +x miniconda.sh \
Expand Down
13 changes: 0 additions & 13 deletions .ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,6 @@ else
CMAKE_OPTS=()
fi

if [[ $AZURE == "true" ]] && [[ $OS_NAME == "linux" ]] && [[ $TASK == "swig" ]]; then
mkdir $BUILD_DIRECTORY/build && cd $BUILD_DIRECTORY/build
cmake -DUSE_SWIG=ON "${CMAKE_OPTS[@]}" ..
make -j4 || exit -1
if [[ $COMPILER == "gcc" ]]; then
objdump -T $BUILD_DIRECTORY/lib_lightgbm.so > $BUILD_DIRECTORY/objdump.log || exit -1
objdump -T $BUILD_DIRECTORY/lib_lightgbm_swig.so >> $BUILD_DIRECTORY/objdump.log || exit -1
python $BUILD_DIRECTORY/helpers/check_dynamic_dependencies.py $BUILD_DIRECTORY/objdump.log || exit -1
fi
cp $BUILD_DIRECTORY/build/lightgbmlib.jar $BUILD_ARTIFACTSTAGINGDIRECTORY/lightgbmlib.jar
exit 0
fi

conda create -q -y -n $CONDA_ENV python=$PYTHON_VERSION
source activate $CONDA_ENV

Expand Down
2 changes: 1 addition & 1 deletion .ci/test_windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ $env:PATH += ";$env:CONDA_PREFIX\Library\bin\graphviz" # temp graphviz hotfix

if ($env:TASK -eq "regular") {
mkdir $env:BUILD_SOURCESDIRECTORY/build; cd $env:BUILD_SOURCESDIRECTORY/build
cmake -A x64 .. ; cmake --build . --target ALL_BUILD --config Release ; Check-Output $?
cmake -DCMAKE_GENERATOR_PLATFORM=x64 .. ; cmake --build . --target ALL_BUILD --config Release ; Check-Output $?
cd $env:BUILD_SOURCESDIRECTORY/python-package
python setup.py install --precompile ; Check-Output $?
cp $env:BUILD_SOURCESDIRECTORY/Release/lib_lightgbm.dll $env:BUILD_ARTIFACTSTAGINGDIRECTORY
Expand Down
4 changes: 1 addition & 3 deletions .vsts-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
vmImage: 'ubuntu-16.04'
container: ubuntu1404
strategy:
maxParallel: 7
maxParallel: 6
matrix:
regular:
TASK: regular
Expand All @@ -25,8 +25,6 @@ jobs:
bdist:
TASK: bdist
PYTHON_VERSION: 3.6
swig:
TASK: swig
inference:
TASK: if-else
mpi_source:
Expand Down
2 changes: 1 addition & 1 deletion R-package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ You will need:

In addition, if you are using a Visual Studio precompiled DLL, assuming you do not have Visual Studio installed (if you have it installed, ignore the warnings below):

* Visual Studio 2015/2017/2019 precompiled DLL: download and install Visual Studio Runtime for [2015](https://www.microsoft.com/en-us/download/details.aspx?id=52685)/[2017](https://aka.ms/vs/15/release/vc_redist.x64.exe)/[2019](https://aka.ms/vs/16/release/vc_redist.x64.exe) (you will get an error about MSVCP140.dll missing otherwise)
* Visual Studio 2015/2017 precompiled DLL: download and install Visual Studio Runtime for [2015](https://www.microsoft.com/en-us/download/details.aspx?id=52685)/[2017](https://go.microsoft.com/fwlink/?LinkId=746572) (you will get an error about MSVCP140.dll missing otherwise)

Once you have all this setup, you can use `lgb.dl` from `lgbdl` package to install LightGBM from repository.

Expand Down
4 changes: 2 additions & 2 deletions R-package/src/install.libs.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ if (!use_precompile) {
} else {
try_vs <- 0
local_vs_def <- ""
vs_versions <- c("Visual Studio 16 2019", "Visual Studio 15 2017", "Visual Studio 14 2015")
vs_versions <- c("Visual Studio 15 2017 Win64", "Visual Studio 14 2015 Win64", "Visual Studio 16 2019")
for(vs in vs_versions){
vs_def <- paste0(" -G \"", vs, "\" -A x64")
vs_def <- paste0(" -G \"", vs, "\"")
tmp_cmake_cmd <- paste0(cmake_cmd, vs_def)
try_vs <- system(paste0(tmp_cmake_cmd, " .."))
if (try_vs == 0) {
Expand Down
28 changes: 13 additions & 15 deletions docs/Installation-Guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ From Command Line
cd LightGBM
mkdir build
cd build
cmake -A x64 ..
cmake -DCMAKE_GENERATOR_PLATFORM=x64 ..
cmake --build . --target ALL_BUILD --config Release

The ``.exe`` and ``.dll`` files will be in ``LightGBM/Release`` folder.
Expand Down Expand Up @@ -230,7 +230,7 @@ With GUI

If you have errors about **Platform Toolset**, go to ``PROJECT`` -> ``Properties`` -> ``Configuration Properties`` -> ``General`` and select the toolset installed on your machine.

The ``.exe`` file will be in ``LightGBM-master/windows/x64/Release`` folder.
The exe file will be in ``LightGBM-master/windows/x64/Release`` folder.

From Command Line
-----------------
Expand All @@ -245,10 +245,10 @@ From Command Line
cd LightGBM
mkdir build
cd build
cmake -A x64 -DUSE_OPENMP=OFF ..
cmake -DCMAKE_GENERATOR_PLATFORM=x64 -DUSE_OPENMP=OFF ..
cmake --build . --target ALL_BUILD --config Release

The ``.exe`` and ``.dll`` files will be in ``LightGBM/Release`` folder.
The exe and dll files will be in ``LightGBM/Release`` folder.

MinGW-w64
*********
Expand All @@ -266,7 +266,7 @@ MinGW-w64
cmake -G "MinGW Makefiles" -DUSE_OPENMP=OFF ..
mingw32-make.exe -j4

The ``.exe`` and ``.dll`` files will be in ``LightGBM/`` folder.
The exe and dll files will be in ``LightGBM/`` folder.

**Note**: You may need to run the ``cmake -G "MinGW Makefiles" -DUSE_OPENMP=OFF ..`` one more time if you encounter the ``sh.exe was found in your PATH`` error.

Expand Down Expand Up @@ -370,7 +370,7 @@ With GUI

If you have errors about **Platform Toolset**, go to ``PROJECT`` -> ``Properties`` -> ``Configuration Properties`` -> ``General`` and select the toolset installed on your machine.

The ``.exe`` file will be in ``LightGBM-master/windows/x64/Release_mpi`` folder.
The exe file will be in ``LightGBM-master/windows/x64/Release_mpi`` folder.

From Command Line
*****************
Expand All @@ -387,10 +387,10 @@ From Command Line
cd LightGBM
mkdir build
cd build
cmake -A x64 -DUSE_MPI=ON ..
cmake -DCMAKE_GENERATOR_PLATFORM=x64 -DUSE_MPI=ON ..
cmake --build . --target ALL_BUILD --config Release

The ``.exe`` and ``.dll`` files will be in ``LightGBM/Release`` folder.
The exe and dll files will be in ``LightGBM/Release`` folder.

**Note**: Building MPI version by **MinGW** is not supported due to the miss of MPI library in it.

Expand Down Expand Up @@ -555,9 +555,7 @@ Following procedure is for the **MSVC** (Microsoft Visual C++) build.

Visual Studio 2015 -> ``msvc-14.0-64.exe``,

Visual Studio 2017 -> ``msvc-14.1-64.exe``,

Visual Studio 2019 -> ``msvc-14.2-64.exe``.
Visual Studio 2017 -> ``msvc-14.1-64.exe``.

4. Run the following commands:

Expand All @@ -569,7 +567,7 @@ Following procedure is for the **MSVC** (Microsoft Visual C++) build.
cd LightGBM
mkdir build
cd build
cmake -A x64 -DUSE_GPU=1 ..
cmake -DCMAKE_GENERATOR_PLATFORM=x64 -DUSE_GPU=1 ..
cmake --build . --target ALL_BUILD --config Release

**Note**: ``C:\local\boost_1_63_0\`` and ``C:\local\boost_1_63_0\lib64-msvc-14.0`` are locations of your **Boost** binaries (assuming you've downloaded 1.63.0 version). You also can set them to the environment variable to avoid ``Set ...`` commands when build.
Expand Down Expand Up @@ -631,10 +629,10 @@ VS Build Tools
cd LightGBM
mkdir build
cd build
cmake -A x64 -DUSE_SWIG=ON ..
cmake -DCMAKE_GENERATOR_PLATFORM=x64 -DUSE_SWIG=ON ..
cmake --build . --target ALL_BUILD --config Release

The ``.jar`` file will be in ``LightGBM/build`` folder and the ``.dll`` files will be in ``LightGBM/Release`` folder.
The jar file will be in ``LightGBM/build`` folder and the dll files will be in ``LightGBM/Release`` folder.

MinGW-w64
*********
Expand All @@ -654,7 +652,7 @@ MinGW-w64
cmake -G "MinGW Makefiles" -DUSE_SWIG=ON ..
mingw32-make.exe -j4

The ``.jar`` file will be in ``LightGBM/build`` folder and the ``.dll`` files will be in ``LightGBM/`` folder.
The jar file will be in ``LightGBM/build`` folder and the dll files will be in ``LightGBM/`` folder.

**Note**: You may need to run the ``cmake -G "MinGW Makefiles" -DUSE_SWIG=ON ..`` one more time if you encounter the ``sh.exe was found in your PATH`` error.

Expand Down
2 changes: 1 addition & 1 deletion docs/Parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ IO Parameters

- **Note**: if you want to get more explanation for your model's predictions using SHAP values like SHAP interaction values, you can install `shap package <https://github.com/slundberg/shap>`__

- **Note**: unlike the shap package, with ``predict_contrib`` we return a matrix with an extra column, where the last column is the expected value
- **Note**: unlike the shap package, with ``pred_contrib`` we return a matrix with an extra column, where the last column is the expected value.

- ``num_iteration_predict`` :raw-html:`<a id="num_iteration_predict" title="Permalink to this parameter" href="#num_iteration_predict">&#x1F517;&#xFE0E;</a>`, default = ``-1``, type = int

Expand Down
373 changes: 0 additions & 373 deletions examples/python-guide/notebooks/interactive_plot_example.ipynb

This file was deleted.

2 changes: 1 addition & 1 deletion include/LightGBM/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ struct Config {
// desc = set this to ``true`` to estimate `SHAP values <https://arxiv.org/abs/1706.06060>`__, which represent how each feature contributes to each prediction
// desc = produces ``#features + 1`` values where the last value is the expected value of the model output over the training data
// desc = **Note**: if you want to get more explanation for your model's predictions using SHAP values like SHAP interaction values, you can install `shap package <https://github.com/slundberg/shap>`__
// desc = **Note**: unlike the shap package, with ``predict_contrib`` we return a matrix with an extra column, where the last column is the expected value
// desc = **Note**: unlike the shap package, with ``pred_contrib`` we return a matrix with an extra column, where the last column is the expected value.
bool predict_contrib = false;

// desc = used only in ``prediction`` task
Expand Down
12 changes: 12 additions & 0 deletions include/LightGBM/json11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@
#include <utility>
#include <vector>

#ifdef _MSC_VER
#if _MSC_VER <= 1800 // VS 2013
#ifndef noexcept
#define noexcept throw()
#endif

#ifndef snprintf
#define snprintf _snprintf_s
#endif
#endif
#endif

namespace json11 {

enum JsonParse {
Expand Down
4 changes: 4 additions & 0 deletions include/LightGBM/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ typedef void(*AllgatherFunction)(char* input, comm_size_t input_size, const comm

#define NO_SPECIFIC (-1)

#if (_MSC_VER <= 1800)
#define __func__ __FUNCTION__
#endif

} // namespace LightGBM

#endif // LightGBM_META_H_
4 changes: 2 additions & 2 deletions python-package/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Preparation
Install from `PyPI <https://pypi.org/project/lightgbm>`_ Using ``pip``
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

For **Windows** users, `VC runtime <https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads>`_ is needed if **Visual Studio** (2015 or newer) is not installed.
For **Windows** users, `VC runtime <https://go.microsoft.com/fwlink/?LinkId=746572>`_ is needed if **Visual Studio** (2015 or newer) is not installed.

For **Linux** users, **glibc** >= 2.14 is required.

Expand All @@ -28,7 +28,7 @@ For **macOS** users:

- For version smaller than 2.1.2, **gcc-7** with **OpenMP** is required.

Install `wheel <https://pythonwheels.com>`_ via ``pip install wheel`` first. After that download the wheel file and install from it:
Install `wheel <http://pythonwheels.com>`_ via ``pip install wheel`` first. After that download the wheel file and install from it:

.. code:: sh

Expand Down
25 changes: 23 additions & 2 deletions python-package/lightgbm/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from operator import gt, lt

from .compat import range_
from .basic import LightGBMError


class EarlyStopException(Exception):
Expand Down Expand Up @@ -214,7 +215,25 @@ def _callback(env):
_init(env)
if not enabled[0]:
return
if first_metric_only:
eval_metric = None
for m in ['metric', 'metrics', 'metric_types']:
if m in env.params.keys():
if isinstance(env.params[m], tuple):
eval_metric = env.params[m][0]
else:
eval_metric = env.params[m]
break
if eval_metric is None:
raise LightGBMError("`metric` should be specified if first_metric_only==True.")
for i in range_(len(env.evaluation_result_list)):
# print(f"env.evaluation_result_list):{env.evaluation_result_list}")
metric_key = env.evaluation_result_list[i][1]
if metric_key.split(" ")[0] == "train":
continue # train metric doesn't used on early stopping.
if first_metric_only:
if metric_key != "valid {}".format(eval_metric) and metric_key != eval_metric and eval_metric != "":
continue
score = env.evaluation_result_list[i][2]
if best_score_list[i] is None or cmp_op[i](score, best_score[i]):
best_score[i] = score
Expand All @@ -224,13 +243,15 @@ def _callback(env):
if verbose:
print('Early stopping, best iteration is:\n[%d]\t%s' % (
best_iter[i] + 1, '\t'.join([_format_eval_result(x) for x in best_score_list[i]])))
if first_metric_only:
print(f"Evaluating only :{metric_key}")
raise EarlyStopException(best_iter[i], best_score_list[i])
if env.iteration == env.end_iteration - 1:
if verbose:
print('Did not meet early stopping. Best iteration is:\n[%d]\t%s' % (
best_iter[i] + 1, '\t'.join([_format_eval_result(x) for x in best_score_list[i]])))
if first_metric_only:
print(f"Evaluating only :{metric_key}")
raise EarlyStopException(best_iter[i], best_score_list[i])
if first_metric_only: # the only first metric is used for early stopping
break
_callback.order = 30
return _callback
2 changes: 1 addition & 1 deletion python-package/lightgbm/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def predict(self, X, raw_score=False, num_iteration=None,
predicted_result : array-like of shape = [n_samples] or shape = [n_samples, n_classes]
The predicted values.
X_leaves : array-like of shape = [n_samples, n_trees] or shape = [n_samples, n_trees * n_classes]
If ``pred_leaf=True``, the predicted leaf of every tree for each sample.
If ``pred_leaf=True``, the predicted leaf every tree for each sample.
X_SHAP_values : array-like of shape = [n_samples, n_features + 1] or shape = [n_samples, (n_features + 1) * n_classes]
If ``pred_contrib=True``, the feature contributions for each sample.
"""
Expand Down
6 changes: 3 additions & 3 deletions python-package/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def compile_cpp(use_mingw=False, use_gpu=False, use_mpi=False, nomp=False,
lib_path = os.path.join(CURRENT_DIR, "compile", "windows", "x64", "DLL", "lib_lightgbm.dll")
if not any((use_gpu, use_mpi, use_hdfs)):
logger.info("Starting to compile with MSBuild from existing solution file.")
platform_toolsets = ("v142", "v141", "v140")
platform_toolsets = ("v141", "v140", "v142")
for pt in platform_toolsets:
status = silent_call(["MSBuild",
os.path.join(CURRENT_DIR, "compile", "windows", "LightGBM.sln"),
Expand All @@ -150,10 +150,10 @@ def compile_cpp(use_mingw=False, use_gpu=False, use_mpi=False, nomp=False,
if status != 0 or not os.path.exists(lib_path):
logger.warning("Compilation with MSBuild from existing solution file failed.")
if status != 0 or not os.path.exists(lib_path):
vs_versions = ("Visual Studio 16 2019", "Visual Studio 15 2017", "Visual Studio 14 2015")
vs_versions = ("Visual Studio 15 2017 Win64", "Visual Studio 14 2015 Win64", "Visual Studio 16 2019")
for vs in vs_versions:
logger.info("Starting to compile with %s." % vs)
status = silent_call(cmake_cmd + ["-G", vs, "-A", "x64"])
status = silent_call(cmake_cmd + ["-G", vs])
if status == 0:
break
else:
Expand Down
12 changes: 6 additions & 6 deletions src/io/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,6 @@ Tree::Tree(const char* str, size_t* used_len) {
} else {
Log::Fatal("Tree model string format error, should contain leaf_value field");
}

if (key_vals.count("shrinkage")) {
Common::Atof(key_vals["shrinkage"].c_str(), &shrinkage_);
} else {
shrinkage_ = 1.0f;
}

if (num_leaves_ <= 1) { return; }

Expand Down Expand Up @@ -582,6 +576,12 @@ Tree::Tree(const char* str, size_t* used_len) {
Log::Fatal("Tree model should contain cat_threshold field");
}
}

if (key_vals.count("shrinkage")) {
Common::Atof(key_vals["shrinkage"].c_str(), &shrinkage_);
} else {
shrinkage_ = 1.0f;
}
max_depth_ = -1;
}

Expand Down
Loading