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
c0f8e38d
Commit
c0f8e38d
authored
Jan 19, 2017
by
cbecker
Committed by
Guolin Ke
Jan 19, 2017
Browse files
Added `SaveModelToString` and modified `SaveModelToFile` to use it. (#235)
parent
df358b2d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
37 deletions
+60
-37
include/LightGBM/boosting.h
include/LightGBM/boosting.h
+7
-0
src/boosting/gbdt.cpp
src/boosting/gbdt.cpp
+46
-37
src/boosting/gbdt.h
src/boosting/gbdt.h
+7
-0
No files found.
include/LightGBM/boosting.h
View file @
c0f8e38d
...
...
@@ -147,6 +147,13 @@ public:
*/
virtual
bool
SaveModelToFile
(
int
num_iterations
,
const
char
*
filename
)
const
=
0
;
/*!
* \brief Save model to string
* \param num_used_model Number of model that want to save, -1 means save all
* \return Non-empty string if succeeded
*/
virtual
std
::
string
SaveModelToString
(
int
num_iterations
)
const
=
0
;
/*!
* \brief Restore from a serialized string
* \param model_str The string of model
...
...
src/boosting/gbdt.cpp
View file @
c0f8e38d
...
...
@@ -509,49 +509,58 @@ std::string GBDT::DumpModel(int num_iteration) const {
return
str_buf
.
str
();
}
bool
GBDT
::
SaveModelToFile
(
int
num_iteration
,
const
char
*
filename
)
const
{
/*! \brief File to write models */
std
::
ofstream
output_file
;
output_file
.
open
(
filename
);
std
::
string
GBDT
::
SaveModelToString
(
int
num_iterations
)
const
{
std
::
stringstream
ss
;
// output model type
output_file
<<
SubModelName
()
<<
std
::
endl
;
ss
<<
SubModelName
()
<<
std
::
endl
;
// output number of class
output_file
<<
"num_class="
<<
num_class_
<<
std
::
endl
;
ss
<<
"num_class="
<<
num_class_
<<
std
::
endl
;
// output label index
output_file
<<
"label_index="
<<
label_idx_
<<
std
::
endl
;
ss
<<
"label_index="
<<
label_idx_
<<
std
::
endl
;
// output max_feature_idx
output_file
<<
"max_feature_idx="
<<
max_feature_idx_
<<
std
::
endl
;
ss
<<
"max_feature_idx="
<<
max_feature_idx_
<<
std
::
endl
;
// output objective name
if
(
object_function_
!=
nullptr
)
{
output_file
<<
"objective="
<<
object_function_
->
GetName
()
<<
std
::
endl
;
ss
<<
"objective="
<<
object_function_
->
GetName
()
<<
std
::
endl
;
}
// output sigmoid parameter
output_file
<<
"sigmoid="
<<
sigmoid_
<<
std
::
endl
;
ss
<<
"sigmoid="
<<
sigmoid_
<<
std
::
endl
;
output_file
<<
"feature_names="
<<
Common
::
Join
(
feature_names_
,
" "
)
<<
std
::
endl
;
ss
<<
"feature_names="
<<
Common
::
Join
(
feature_names_
,
" "
)
<<
std
::
endl
;
output_file
<<
std
::
endl
;
ss
<<
std
::
endl
;
int
num_used_model
=
static_cast
<
int
>
(
models_
.
size
());
if
(
num_iteration
>
0
)
{
num_used_model
=
std
::
min
(
num_iteration
*
num_class_
,
num_used_model
);
if
(
num_iteration
s
>
0
)
{
num_used_model
=
std
::
min
(
num_iteration
s
*
num_class_
,
num_used_model
);
}
// output tree models
for
(
int
i
=
0
;
i
<
num_used_model
;
++
i
)
{
output_file
<<
"Tree="
<<
i
<<
std
::
endl
;
output_file
<<
models_
[
i
]
->
ToString
()
<<
std
::
endl
;
ss
<<
"Tree="
<<
i
<<
std
::
endl
;
ss
<<
models_
[
i
]
->
ToString
()
<<
std
::
endl
;
}
std
::
vector
<
std
::
pair
<
size_t
,
std
::
string
>>
pairs
=
FeatureImportance
();
output_file
<<
std
::
endl
<<
"feature importances:"
<<
std
::
endl
;
ss
<<
std
::
endl
<<
"feature importances:"
<<
std
::
endl
;
for
(
size_t
i
=
0
;
i
<
pairs
.
size
();
++
i
)
{
output_file
<<
pairs
[
i
].
second
<<
"="
<<
std
::
to_string
(
pairs
[
i
].
first
)
<<
std
::
endl
;
ss
<<
pairs
[
i
].
second
<<
"="
<<
std
::
to_string
(
pairs
[
i
].
first
)
<<
std
::
endl
;
}
output_file
<<
std
::
endl
<<
"feature information:"
<<
std
::
endl
;
ss
<<
std
::
endl
<<
"feature information:"
<<
std
::
endl
;
for
(
size_t
i
=
0
;
i
<
max_feature_idx_
+
1
;
++
i
)
{
output_file
<<
feature_names_
[
i
]
<<
"="
<<
feature_infos_
[
i
]
<<
std
::
endl
;
ss
<<
feature_names_
[
i
]
<<
"="
<<
feature_infos_
[
i
]
<<
std
::
endl
;
}
return
ss
.
str
();
}
bool
GBDT
::
SaveModelToFile
(
int
num_iteration
,
const
char
*
filename
)
const
{
/*! \brief File to write models */
std
::
ofstream
output_file
;
output_file
.
open
(
filename
);
output_file
<<
SaveModelToString
(
num_iteration
);
output_file
.
close
();
return
(
bool
)
output_file
;
...
...
src/boosting/gbdt.h
View file @
c0f8e38d
...
...
@@ -158,6 +158,13 @@ public:
*/
virtual
bool
SaveModelToFile
(
int
num_iterations
,
const
char
*
filename
)
const
override
;
/*!
* \brief Save model to string
* \param num_used_model Number of model that want to save, -1 means save all
* \return Non-empty string if succeeded
*/
virtual
std
::
string
SaveModelToString
(
int
num_iterations
)
const
override
;
/*!
* \brief Restore from a serialized string
*/
...
...
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