Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tianlh
LightGBM-DCU
Commits
81e2485a
Unverified
Commit
81e2485a
authored
Sep 29, 2018
by
Guolin Ke
Committed by
GitHub
Sep 29, 2018
Browse files
add indices in shuffle model. (#1710)
* add indexs in shuffle model. * fix pep * fix bug
parent
172caee1
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
27 additions
and
11 deletions
+27
-11
include/LightGBM/boosting.h
include/LightGBM/boosting.h
+1
-1
include/LightGBM/c_api.h
include/LightGBM/c_api.h
+1
-1
python-package/lightgbm/basic.py
python-package/lightgbm/basic.py
+13
-2
src/boosting/gbdt.h
src/boosting/gbdt.h
+8
-3
src/c_api.cpp
src/c_api.cpp
+4
-4
No files found.
include/LightGBM/boosting.h
View file @
81e2485a
...
...
@@ -47,7 +47,7 @@ public:
/*!
* \brief Shuffle Existing Models
*/
virtual
void
ShuffleModels
()
=
0
;
virtual
void
ShuffleModels
(
int
start_iter
,
int
end_iter
)
=
0
;
virtual
void
ResetTrainingData
(
const
Dataset
*
train_data
,
const
ObjectiveFunction
*
objective_function
,
const
std
::
vector
<
const
Metric
*>&
training_metrics
)
=
0
;
...
...
include/LightGBM/c_api.h
View file @
81e2485a
...
...
@@ -374,7 +374,7 @@ LIGHTGBM_C_EXPORT int LGBM_BoosterFree(BoosterHandle handle);
/*!
* \brief Shuffle Models
*/
LIGHTGBM_C_EXPORT
int
LGBM_BoosterShuffleModels
(
BoosterHandle
handle
);
LIGHTGBM_C_EXPORT
int
LGBM_BoosterShuffleModels
(
BoosterHandle
handle
,
int
start_iter
,
int
end_iter
);
/*!
* \brief Merge model in two booster to first handle
...
...
python-package/lightgbm/basic.py
View file @
81e2485a
...
...
@@ -1945,15 +1945,26 @@ class Booster(object):
_save_pandas_categorical
(
filename
,
self
.
pandas_categorical
)
return
self
def
shuffle_models
(
self
):
def
shuffle_models
(
self
,
start_iteration
=
0
,
end_iteration
=-
1
):
"""Shuffle models.
Parameters
----------
start_iteration : int, optional (default=0)
Index of the iteration that will start to shuffle.
end_iteration : int, optional (default=-1)
The last iteration that will be shuffled.
If <= 0, means the last iteration.
Returns
-------
self : Booster
Booster with shuffled models.
"""
_safe_call
(
_LIB
.
LGBM_BoosterShuffleModels
(
self
.
handle
))
_safe_call
(
_LIB
.
LGBM_BoosterShuffleModels
(
self
.
handle
,
ctypes
.
c_int
(
start_iter
),
ctypes
.
c_int
(
end_iter
)))
return
self
def
model_from_string
(
self
,
model_str
,
verbose
=
True
):
...
...
src/boosting/gbdt.h
View file @
81e2485a
...
...
@@ -70,16 +70,21 @@ public:
num_iteration_for_pred_
=
static_cast
<
int
>
(
models_
.
size
())
/
num_tree_per_iteration_
;
}
void
ShuffleModels
()
override
{
void
ShuffleModels
(
int
start_iter
,
int
end_iter
)
override
{
int
total_iter
=
static_cast
<
int
>
(
models_
.
size
())
/
num_tree_per_iteration_
;
start_iter
=
std
::
max
(
0
,
start_iter
);
if
(
end_iter
<=
0
)
{
end_iter
=
total_iter
;
}
end_iter
=
std
::
min
(
total_iter
,
end_iter
);
auto
original_models
=
std
::
move
(
models_
);
std
::
vector
<
int
>
indices
(
total_iter
);
for
(
int
i
=
0
;
i
<
total_iter
;
++
i
)
{
indices
[
i
]
=
i
;
}
Random
tmp_rand
(
17
);
for
(
int
i
=
0
;
i
<
total
_iter
-
1
;
++
i
)
{
int
j
=
tmp_rand
.
NextShort
(
i
+
1
,
total
_iter
);
for
(
int
i
=
start_iter
;
i
<
end
_iter
-
1
;
++
i
)
{
int
j
=
tmp_rand
.
NextShort
(
i
+
1
,
end
_iter
);
std
::
swap
(
indices
[
i
],
indices
[
j
]);
}
models_
=
std
::
vector
<
std
::
unique_ptr
<
Tree
>>
();
...
...
src/c_api.cpp
View file @
81e2485a
...
...
@@ -294,9 +294,9 @@ public:
dynamic_cast
<
GBDTBase
*>
(
boosting_
.
get
())
->
SetLeafValue
(
tree_idx
,
leaf_idx
,
val
);
}
void
ShuffleModels
()
{
void
ShuffleModels
(
int
start_iter
,
int
end_iter
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
boosting_
->
ShuffleModels
();
boosting_
->
ShuffleModels
(
start_iter
,
end_iter
);
}
int
GetEvalCounts
()
const
{
...
...
@@ -919,10 +919,10 @@ int LGBM_BoosterFree(BoosterHandle handle) {
API_END
();
}
int
LGBM_BoosterShuffleModels
(
BoosterHandle
handle
)
{
int
LGBM_BoosterShuffleModels
(
BoosterHandle
handle
,
int
start_iter
,
int
end_iter
)
{
API_BEGIN
();
Booster
*
ref_booster
=
reinterpret_cast
<
Booster
*>
(
handle
);
ref_booster
->
ShuffleModels
();
ref_booster
->
ShuffleModels
(
start_iter
,
end_iter
);
API_END
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment