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
1d7acf57
Commit
1d7acf57
authored
Jan 01, 2017
by
Guolin Ke
Browse files
change output model to double precision.
parent
487bd835
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
55 deletions
+61
-55
include/LightGBM/utils/common.h
include/LightGBM/utils/common.h
+15
-10
src/c_api.cpp
src/c_api.cpp
+5
-5
src/io/tree.cpp
src/io/tree.cpp
+41
-40
No files found.
include/LightGBM/utils/common.h
View file @
1d7acf57
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
#include <functional>
#include <functional>
#include <memory>
#include <memory>
#include <type_traits>
#include <type_traits>
#include <iomanip>
namespace
LightGBM
{
namespace
LightGBM
{
...
@@ -246,6 +247,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, char delimite
...
@@ -246,6 +247,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, char delimite
return
std
::
string
(
""
);
return
std
::
string
(
""
);
}
}
std
::
stringstream
str_buf
;
std
::
stringstream
str_buf
;
str_buf
<<
std
::
setprecision
(
std
::
numeric_limits
<
double
>::
digits10
+
1
);
str_buf
<<
arr
[
0
];
str_buf
<<
arr
[
0
];
for
(
size_t
i
=
1
;
i
<
arr
.
size
();
++
i
)
{
for
(
size_t
i
=
1
;
i
<
arr
.
size
();
++
i
)
{
str_buf
<<
delimiter
;
str_buf
<<
delimiter
;
...
@@ -260,6 +262,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, size_t n, cha
...
@@ -260,6 +262,7 @@ inline static std::string ArrayToString(const std::vector<T>& arr, size_t n, cha
return
std
::
string
(
""
);
return
std
::
string
(
""
);
}
}
std
::
stringstream
str_buf
;
std
::
stringstream
str_buf
;
str_buf
<<
std
::
setprecision
(
std
::
numeric_limits
<
double
>::
digits10
+
1
);
str_buf
<<
arr
[
0
];
str_buf
<<
arr
[
0
];
for
(
size_t
i
=
1
;
i
<
std
::
min
(
n
,
arr
.
size
());
++
i
)
{
for
(
size_t
i
=
1
;
i
<
std
::
min
(
n
,
arr
.
size
());
++
i
)
{
str_buf
<<
delimiter
;
str_buf
<<
delimiter
;
...
@@ -308,13 +311,14 @@ inline static std::string Join(const std::vector<T>& strs, const char* delimiter
...
@@ -308,13 +311,14 @@ inline static std::string Join(const std::vector<T>& strs, const char* delimiter
if
(
strs
.
empty
())
{
if
(
strs
.
empty
())
{
return
std
::
string
(
""
);
return
std
::
string
(
""
);
}
}
std
::
stringstream
ss
;
std
::
stringstream
str_buf
;
ss
<<
strs
[
0
];
str_buf
<<
std
::
setprecision
(
std
::
numeric_limits
<
double
>::
digits10
+
1
);
str_buf
<<
strs
[
0
];
for
(
size_t
i
=
1
;
i
<
strs
.
size
();
++
i
)
{
for
(
size_t
i
=
1
;
i
<
strs
.
size
();
++
i
)
{
s
s
<<
delimiter
;
s
tr_buf
<<
delimiter
;
s
s
<<
strs
[
i
];
s
tr_buf
<<
strs
[
i
];
}
}
return
s
s
.
str
();
return
s
tr_buf
.
str
();
}
}
template
<
typename
T
>
template
<
typename
T
>
...
@@ -324,13 +328,14 @@ inline static std::string Join(const std::vector<T>& strs, size_t start, size_t
...
@@ -324,13 +328,14 @@ inline static std::string Join(const std::vector<T>& strs, size_t start, size_t
}
}
start
=
std
::
min
(
start
,
static_cast
<
size_t
>
(
strs
.
size
())
-
1
);
start
=
std
::
min
(
start
,
static_cast
<
size_t
>
(
strs
.
size
())
-
1
);
end
=
std
::
min
(
end
,
static_cast
<
size_t
>
(
strs
.
size
()));
end
=
std
::
min
(
end
,
static_cast
<
size_t
>
(
strs
.
size
()));
std
::
stringstream
ss
;
std
::
stringstream
str_buf
;
ss
<<
strs
[
start
];
str_buf
<<
std
::
setprecision
(
std
::
numeric_limits
<
double
>::
digits10
+
1
);
str_buf
<<
strs
[
start
];
for
(
size_t
i
=
start
+
1
;
i
<
end
;
++
i
)
{
for
(
size_t
i
=
start
+
1
;
i
<
end
;
++
i
)
{
s
s
<<
delimiter
;
s
tr_buf
<<
delimiter
;
s
s
<<
strs
[
i
];
s
tr_buf
<<
strs
[
i
];
}
}
return
s
s
.
str
();
return
s
tr_buf
.
str
();
}
}
static
inline
int64_t
Pow2RoundUp
(
int64_t
x
)
{
static
inline
int64_t
Pow2RoundUp
(
int64_t
x
)
{
...
...
src/c_api.cpp
View file @
1d7acf57
...
@@ -735,8 +735,8 @@ DllExport int LGBM_BoosterPredictForFile(BoosterHandle handle,
...
@@ -735,8 +735,8 @@ DllExport int LGBM_BoosterPredictForFile(BoosterHandle handle,
API_END
();
API_END
();
}
}
int
GetNumPredOneRow
(
const
Booster
*
ref_booster
,
int
predict_type
,
int64_t
num_iteration
)
{
int
64_t
GetNumPredOneRow
(
const
Booster
*
ref_booster
,
int
predict_type
,
int64_t
num_iteration
)
{
int
num_preb_in_one_row
=
ref_booster
->
GetBoosting
()
->
NumberOfClasses
();
int
64_t
num_preb_in_one_row
=
ref_booster
->
GetBoosting
()
->
NumberOfClasses
();
if
(
predict_type
==
C_API_PREDICT_LEAF_INDEX
)
{
if
(
predict_type
==
C_API_PREDICT_LEAF_INDEX
)
{
int64_t
max_iteration
=
ref_booster
->
GetBoosting
()
->
GetCurrentIteration
();
int64_t
max_iteration
=
ref_booster
->
GetBoosting
()
->
GetCurrentIteration
();
if
(
num_iteration
>
0
)
{
if
(
num_iteration
>
0
)
{
...
@@ -776,7 +776,7 @@ DllExport int LGBM_BoosterPredictForCSR(BoosterHandle handle,
...
@@ -776,7 +776,7 @@ DllExport int LGBM_BoosterPredictForCSR(BoosterHandle handle,
Booster
*
ref_booster
=
reinterpret_cast
<
Booster
*>
(
handle
);
Booster
*
ref_booster
=
reinterpret_cast
<
Booster
*>
(
handle
);
auto
predictor
=
ref_booster
->
NewPredictor
(
static_cast
<
int
>
(
num_iteration
),
predict_type
);
auto
predictor
=
ref_booster
->
NewPredictor
(
static_cast
<
int
>
(
num_iteration
),
predict_type
);
auto
get_row_fun
=
RowFunctionFromCSR
(
indptr
,
indptr_type
,
indices
,
data
,
data_type
,
nindptr
,
nelem
);
auto
get_row_fun
=
RowFunctionFromCSR
(
indptr
,
indptr_type
,
indices
,
data
,
data_type
,
nindptr
,
nelem
);
int
num_preb_in_one_row
=
GetNumPredOneRow
(
ref_booster
,
predict_type
,
num_iteration
);
int
64_t
num_preb_in_one_row
=
GetNumPredOneRow
(
ref_booster
,
predict_type
,
num_iteration
);
int
nrow
=
static_cast
<
int
>
(
nindptr
-
1
);
int
nrow
=
static_cast
<
int
>
(
nindptr
-
1
);
#pragma omp parallel for schedule(guided)
#pragma omp parallel for schedule(guided)
for
(
int
i
=
0
;
i
<
nrow
;
++
i
)
{
for
(
int
i
=
0
;
i
<
nrow
;
++
i
)
{
...
@@ -806,7 +806,7 @@ DllExport int LGBM_BoosterPredictForCSC(BoosterHandle handle,
...
@@ -806,7 +806,7 @@ DllExport int LGBM_BoosterPredictForCSC(BoosterHandle handle,
API_BEGIN
();
API_BEGIN
();
Booster
*
ref_booster
=
reinterpret_cast
<
Booster
*>
(
handle
);
Booster
*
ref_booster
=
reinterpret_cast
<
Booster
*>
(
handle
);
auto
predictor
=
ref_booster
->
NewPredictor
(
static_cast
<
int
>
(
num_iteration
),
predict_type
);
auto
predictor
=
ref_booster
->
NewPredictor
(
static_cast
<
int
>
(
num_iteration
),
predict_type
);
int
num_preb_in_one_row
=
GetNumPredOneRow
(
ref_booster
,
predict_type
,
num_iteration
);
int
64_t
num_preb_in_one_row
=
GetNumPredOneRow
(
ref_booster
,
predict_type
,
num_iteration
);
int
ncol
=
static_cast
<
int
>
(
ncol_ptr
-
1
);
int
ncol
=
static_cast
<
int
>
(
ncol_ptr
-
1
);
Threading
::
For
<
int64_t
>
(
0
,
num_row
,
Threading
::
For
<
int64_t
>
(
0
,
num_row
,
...
@@ -849,7 +849,7 @@ DllExport int LGBM_BoosterPredictForMat(BoosterHandle handle,
...
@@ -849,7 +849,7 @@ DllExport int LGBM_BoosterPredictForMat(BoosterHandle handle,
Booster
*
ref_booster
=
reinterpret_cast
<
Booster
*>
(
handle
);
Booster
*
ref_booster
=
reinterpret_cast
<
Booster
*>
(
handle
);
auto
predictor
=
ref_booster
->
NewPredictor
(
static_cast
<
int
>
(
num_iteration
),
predict_type
);
auto
predictor
=
ref_booster
->
NewPredictor
(
static_cast
<
int
>
(
num_iteration
),
predict_type
);
auto
get_row_fun
=
RowPairFunctionFromDenseMatric
(
data
,
nrow
,
ncol
,
data_type
,
is_row_major
);
auto
get_row_fun
=
RowPairFunctionFromDenseMatric
(
data
,
nrow
,
ncol
,
data_type
,
is_row_major
);
int
num_preb_in_one_row
=
GetNumPredOneRow
(
ref_booster
,
predict_type
,
num_iteration
);
int
64_t
num_preb_in_one_row
=
GetNumPredOneRow
(
ref_booster
,
predict_type
,
num_iteration
);
#pragma omp parallel for schedule(guided)
#pragma omp parallel for schedule(guided)
for
(
int
i
=
0
;
i
<
nrow
;
++
i
)
{
for
(
int
i
=
0
;
i
<
nrow
;
++
i
)
{
auto
one_row
=
get_row_fun
(
i
);
auto
one_row
=
get_row_fun
(
i
);
...
...
src/io/tree.cpp
View file @
1d7acf57
...
@@ -12,6 +12,7 @@
...
@@ -12,6 +12,7 @@
#include <vector>
#include <vector>
#include <string>
#include <string>
#include <memory>
#include <memory>
#include <iomanip>
namespace
LightGBM
{
namespace
LightGBM
{
...
@@ -121,72 +122,72 @@ void Tree::AddPredictionToScore(const Dataset* data, const data_size_t* used_dat
...
@@ -121,72 +122,72 @@ void Tree::AddPredictionToScore(const Dataset* data, const data_size_t* used_dat
}
}
std
::
string
Tree
::
ToString
()
{
std
::
string
Tree
::
ToString
()
{
std
::
stringstream
s
s
;
std
::
stringstream
s
tr_buf
;
s
s
<<
"num_leaves="
<<
num_leaves_
<<
std
::
endl
;
s
tr_buf
<<
"num_leaves="
<<
num_leaves_
<<
std
::
endl
;
s
s
<<
"split_feature="
s
tr_buf
<<
"split_feature="
<<
Common
::
ArrayToString
<
int
>
(
split_feature_real_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
int
>
(
split_feature_real_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
s
s
<<
"split_gain="
s
tr_buf
<<
"split_gain="
<<
Common
::
ArrayToString
<
double
>
(
split_gain_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
double
>
(
split_gain_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
s
s
<<
"threshold="
s
tr_buf
<<
"threshold="
<<
Common
::
ArrayToString
<
double
>
(
threshold_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
double
>
(
threshold_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
s
s
<<
"decision_type="
s
tr_buf
<<
"decision_type="
<<
Common
::
ArrayToString
<
int
>
(
Common
::
ArrayCast
<
int8_t
,
int
>
(
decision_type_
),
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
int
>
(
Common
::
ArrayCast
<
int8_t
,
int
>
(
decision_type_
),
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
s
s
<<
"left_child="
s
tr_buf
<<
"left_child="
<<
Common
::
ArrayToString
<
int
>
(
left_child_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
int
>
(
left_child_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
s
s
<<
"right_child="
s
tr_buf
<<
"right_child="
<<
Common
::
ArrayToString
<
int
>
(
right_child_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
int
>
(
right_child_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
s
s
<<
"leaf_parent="
s
tr_buf
<<
"leaf_parent="
<<
Common
::
ArrayToString
<
int
>
(
leaf_parent_
,
num_leaves_
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
int
>
(
leaf_parent_
,
num_leaves_
,
' '
)
<<
std
::
endl
;
s
s
<<
"leaf_value="
s
tr_buf
<<
"leaf_value="
<<
Common
::
ArrayToString
<
double
>
(
leaf_value_
,
num_leaves_
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
double
>
(
leaf_value_
,
num_leaves_
,
' '
)
<<
std
::
endl
;
s
s
<<
"leaf_count="
s
tr_buf
<<
"leaf_count="
<<
Common
::
ArrayToString
<
data_size_t
>
(
leaf_count_
,
num_leaves_
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
data_size_t
>
(
leaf_count_
,
num_leaves_
,
' '
)
<<
std
::
endl
;
s
s
<<
"internal_value="
s
tr_buf
<<
"internal_value="
<<
Common
::
ArrayToString
<
double
>
(
internal_value_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
double
>
(
internal_value_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
s
s
<<
"internal_count="
s
tr_buf
<<
"internal_count="
<<
Common
::
ArrayToString
<
data_size_t
>
(
internal_count_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
<<
Common
::
ArrayToString
<
data_size_t
>
(
internal_count_
,
num_leaves_
-
1
,
' '
)
<<
std
::
endl
;
s
s
<<
std
::
endl
;
s
tr_buf
<<
std
::
endl
;
return
s
s
.
str
();
return
s
tr_buf
.
str
();
}
}
std
::
string
Tree
::
ToJSON
()
{
std
::
string
Tree
::
ToJSON
()
{
std
::
stringstream
ss
;
std
::
stringstream
str_buf
;
str_buf
<<
std
::
setprecision
(
std
::
numeric_limits
<
double
>::
digits10
+
1
);
str_buf
<<
"
\"
num_leaves
\"
:"
<<
num_leaves_
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
num_leaves
\"
:"
<<
num_leaves_
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
tree_structure
\"
:"
<<
NodeToJSON
(
0
)
<<
std
::
endl
;
ss
<<
"
\"
tree_structure
\"
:"
<<
NodeToJSON
(
0
)
<<
std
::
endl
;
return
str_buf
.
str
();
return
ss
.
str
();
}
}
std
::
string
Tree
::
NodeToJSON
(
int
index
)
{
std
::
string
Tree
::
NodeToJSON
(
int
index
)
{
std
::
stringstream
s
s
;
std
::
stringstream
s
tr_buf
;
str_buf
<<
std
::
setprecision
(
std
::
numeric_limits
<
double
>::
digits10
+
1
);
if
(
index
>=
0
)
{
if
(
index
>=
0
)
{
// non-leaf
// non-leaf
s
s
<<
"{"
<<
std
::
endl
;
s
tr_buf
<<
"{"
<<
std
::
endl
;
s
s
<<
"
\"
split_index
\"
:"
<<
index
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
split_index
\"
:"
<<
index
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
split_feature
\"
:"
<<
split_feature_real_
[
index
]
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
split_feature
\"
:"
<<
split_feature_real_
[
index
]
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
split_gain
\"
:"
<<
split_gain_
[
index
]
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
split_gain
\"
:"
<<
split_gain_
[
index
]
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
threshold
\"
:"
<<
threshold_
[
index
]
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
threshold
\"
:"
<<
threshold_
[
index
]
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
decision_type
\"
:
\"
"
<<
Tree
::
GetDecisionTypeName
(
decision_type_
[
index
])
<<
"
\"
,"
<<
std
::
endl
;
s
tr_buf
<<
"
\"
decision_type
\"
:
\"
"
<<
Tree
::
GetDecisionTypeName
(
decision_type_
[
index
])
<<
"
\"
,"
<<
std
::
endl
;
s
s
<<
"
\"
internal_value
\"
:"
<<
internal_value_
[
index
]
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
internal_value
\"
:"
<<
internal_value_
[
index
]
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
internal_count
\"
:"
<<
internal_count_
[
index
]
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
internal_count
\"
:"
<<
internal_count_
[
index
]
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
left_child
\"
:"
<<
NodeToJSON
(
left_child_
[
index
])
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
left_child
\"
:"
<<
NodeToJSON
(
left_child_
[
index
])
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
right_child
\"
:"
<<
NodeToJSON
(
right_child_
[
index
])
<<
std
::
endl
;
s
tr_buf
<<
"
\"
right_child
\"
:"
<<
NodeToJSON
(
right_child_
[
index
])
<<
std
::
endl
;
s
s
<<
"}"
;
s
tr_buf
<<
"}"
;
}
else
{
}
else
{
// leaf
// leaf
index
=
~
index
;
index
=
~
index
;
s
s
<<
"{"
<<
std
::
endl
;
s
tr_buf
<<
"{"
<<
std
::
endl
;
s
s
<<
"
\"
leaf_index
\"
:"
<<
index
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
leaf_index
\"
:"
<<
index
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
leaf_parent
\"
:"
<<
leaf_parent_
[
index
]
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
leaf_parent
\"
:"
<<
leaf_parent_
[
index
]
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
leaf_value
\"
:"
<<
leaf_value_
[
index
]
<<
","
<<
std
::
endl
;
s
tr_buf
<<
"
\"
leaf_value
\"
:"
<<
leaf_value_
[
index
]
<<
","
<<
std
::
endl
;
s
s
<<
"
\"
leaf_count
\"
:"
<<
leaf_count_
[
index
]
<<
std
::
endl
;
s
tr_buf
<<
"
\"
leaf_count
\"
:"
<<
leaf_count_
[
index
]
<<
std
::
endl
;
s
s
<<
"}"
;
s
tr_buf
<<
"}"
;
}
}
return
s
s
.
str
();
return
s
tr_buf
.
str
();
}
}
Tree
::
Tree
(
const
std
::
string
&
str
)
{
Tree
::
Tree
(
const
std
::
string
&
str
)
{
...
...
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