small change

This commit is contained in:
yanqingmen 2015-12-29 20:53:56 +08:00
commit 173ef11681
11 changed files with 68 additions and 53 deletions

View File

@ -1,5 +1,5 @@
export CC = $(if $(shell which gcc-5),gcc-5,gcc) export CC = $(if $(shell which gcc-5 2>/dev/null),gcc-5,gcc)
export CXX = $(if $(shell which g++-5),g++-5,g++) export CXX = $(if $(shell which g++-5 2>/dev/null),g++-5,g++)
export MPICXX = mpicxx export MPICXX = mpicxx
export LDFLAGS= -pthread -lm export LDFLAGS= -pthread -lm

View File

@ -188,7 +188,7 @@ By defining it formally, we can get a better idea of what we are learning, and y
Here is the magical part of the derivation. After reformalizing the tree model, we can write the objective value with the ``$ t$``-th tree as: Here is the magical part of the derivation. After reformalizing the tree model, we can write the objective value with the ``$ t$``-th tree as:
```math ```math
Obj^{(t)} &\approx \sum_{i=1}^n [g_i w_q(x_i) + \frac{1}{2} h_i w_{q(x_i)}^2] + \gamma T + \frac{1}{2}\lambda \sum_{j=1}^T w_j^2\\ Obj^{(t)} &\approx \sum_{i=1}^n [g_i w_{q(x_i)} + \frac{1}{2} h_i w_{q(x_i)}^2] + \gamma T + \frac{1}{2}\lambda \sum_{j=1}^T w_j^2\\
&= \sum^T_{j=1} [(\sum_{i\in I_j} g_i) w_j + \frac{1}{2} (\sum_{i\in I_j} h_i + \lambda) w_j^2 ] + \gamma T &= \sum^T_{j=1} [(\sum_{i\in I_j} g_i) w_j + \frac{1}{2} (\sum_{i\in I_j} h_i + \lambda) w_j^2 ] + \gamma T
``` ```

View File

@ -1,12 +1,12 @@
echo "move native library" echo "move native library"
set libsource=..\windows\x64\Release\xgboostjavawrapper.dll set libsource=..\windows\x64\Release\xgboost4j.dll
if not exist %libsource% ( if not exist %libsource% (
goto end goto end
) )
set libfolder=xgboost4j\src\main\resources\lib set libfolder=xgboost4j\src\main\resources\lib
set libpath=%libfolder%\xgboostjavawrapper.dll set libpath=%libfolder%\xgboost4j.dll
if not exist %libfolder% (mkdir %libfolder%) if not exist %libfolder% (mkdir %libfolder%)
if exist %libpath% (del %libpath%) if exist %libpath% (del %libpath%)
move %libsource% %libfolder% move %libsource% %libfolder%

View File

@ -1,12 +1,20 @@
#!/usr/bin/env sh
echo "build java wrapper" echo "build java wrapper"
dylib="so"
# cd to script's directory
pushd `dirname $0` > /dev/null
#settings according to os
dl="so"
dis_omp=0 dis_omp=0
if [ $(uname) == "Darwin" ]; then if [ $(uname) == "Darwin" ]; then
export JAVA_HOME=$(/usr/libexec/java_home) export JAVA_HOME=$(/usr/libexec/java_home)
dylib="dylib" dl="dylib"
#change this to 0 if your compiler support openmp #change this to 0 if your compiler support openmp
dis_omp=1 dis_omp=1
fi fi
cd .. cd ..
make java no_omp=${dis_omp} make java no_omp=${dis_omp}
cd java cd java
@ -17,7 +25,8 @@ if [ ! -d "$libPath" ]; then
mkdir -p "$libPath" mkdir -p "$libPath"
fi fi
rm -f xgboost4j/src/main/resources/lib/libxgboost4j.${dylib} rm -f xgboost4j/src/main/resources/lib/libxgboost4j.${dl}
mv libxgboost4j.so xgboost4j/src/main/resources/lib/libxgboost4j.${dylib} mv libxgboost4j.so xgboost4j/src/main/resources/lib/libxgboost4j.${dl}
popd > /dev/null
echo "complete" echo "complete"

View File

@ -14,6 +14,7 @@
#include "../wrapper/xgboost_wrapper.h" #include "../wrapper/xgboost_wrapper.h"
#include "xgboost4j_wrapper.h" #include "xgboost4j_wrapper.h"
#include <cstring>
//helper functions //helper functions
//set handle //set handle
@ -215,14 +216,14 @@ JNIEXPORT jint JNICALL Java_org_dmlc_xgboost4j_wrapper_XgboostJNI_XGDMatrixGetFl
(JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfield, jobjectArray jout) { (JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfield, jobjectArray jout) {
DMatrixHandle handle = (DMatrixHandle) jhandle; DMatrixHandle handle = (DMatrixHandle) jhandle;
const char* field = jenv->GetStringUTFChars(jfield, 0); const char* field = jenv->GetStringUTFChars(jfield, 0);
bst_ulong len[1]; bst_ulong len;
float *result[1]; float *result;
int ret = XGDMatrixGetFloatInfo(handle, field, len, (const float **) result); int ret = XGDMatrixGetFloatInfo(handle, field, &len, (const float**) &result);
if (field) jenv->ReleaseStringUTFChars(jfield, field); if (field) jenv->ReleaseStringUTFChars(jfield, field);
jsize jlen = (jsize) *len; jsize jlen = (jsize) len;
jfloatArray jarray = jenv->NewFloatArray(jlen); jfloatArray jarray = jenv->NewFloatArray(jlen);
jenv->SetFloatArrayRegion(jarray, 0, jlen, (jfloat *) *result); jenv->SetFloatArrayRegion(jarray, 0, jlen, (jfloat *) result);
jenv->SetObjectArrayElement(jout, 0, (jobject) jarray); jenv->SetObjectArrayElement(jout, 0, (jobject) jarray);
return ret; return ret;
@ -237,15 +238,14 @@ JNIEXPORT jint JNICALL Java_org_dmlc_xgboost4j_wrapper_XgboostJNI_XGDMatrixGetUI
(JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfield, jobjectArray jout) { (JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfield, jobjectArray jout) {
DMatrixHandle handle = (DMatrixHandle) jhandle; DMatrixHandle handle = (DMatrixHandle) jhandle;
const char* field = jenv->GetStringUTFChars(jfield, 0); const char* field = jenv->GetStringUTFChars(jfield, 0);
bst_ulong len[1]; bst_ulong len;
*len = 0; unsigned int *result;
unsigned int *result[1]; int ret = (jint) XGDMatrixGetUIntInfo(handle, field, &len, (const unsigned int **) &result);
int ret = (jint) XGDMatrixGetUIntInfo(handle, field, len, (const unsigned int **) result);
if (field) jenv->ReleaseStringUTFChars(jfield, field); if (field) jenv->ReleaseStringUTFChars(jfield, field);
jsize jlen = (jsize)*len; jsize jlen = (jsize) len;
jintArray jarray = jenv->NewIntArray(jlen); jintArray jarray = jenv->NewIntArray(jlen);
jenv->SetIntArrayRegion(jarray, 0, jlen, (jint *) *result); jenv->SetIntArrayRegion(jarray, 0, jlen, (jint *) result);
jenv->SetObjectArrayElement(jout, 0, jarray); jenv->SetObjectArrayElement(jout, 0, jarray);
return ret; return ret;
} }
@ -367,7 +367,7 @@ JNIEXPORT jint JNICALL Java_org_dmlc_xgboost4j_wrapper_XgboostJNI_XGBoosterEvalO
BoosterHandle handle = (BoosterHandle) jhandle; BoosterHandle handle = (BoosterHandle) jhandle;
DMatrixHandle* dmats = 0; DMatrixHandle* dmats = 0;
char **evnames = 0; char **evnames = 0;
char *result[1]; char *result = 0;
bst_ulong len = (bst_ulong)jenv->GetArrayLength(jdmats); bst_ulong len = (bst_ulong)jenv->GetArrayLength(jdmats);
if(len > 0) { if(len > 0) {
dmats = new DMatrixHandle[len]; dmats = new DMatrixHandle[len];
@ -378,26 +378,28 @@ JNIEXPORT jint JNICALL Java_org_dmlc_xgboost4j_wrapper_XgboostJNI_XGBoosterEvalO
for(bst_ulong i=0; i<len; i++) { for(bst_ulong i=0; i<len; i++) {
dmats[i] = (DMatrixHandle) cjdmats[i]; dmats[i] = (DMatrixHandle) cjdmats[i];
} }
//transfer jObjectArray to char** //transfer jObjectArray to char**, user strcpy and release JNI char* inplace
for(bst_ulong i=0; i<len; i++) { for(bst_ulong i=0; i<len; i++) {
jstring jevname = (jstring)jenv->GetObjectArrayElement(jevnames, i); jstring jevname = (jstring)jenv->GetObjectArrayElement(jevnames, i);
evnames[i] = (char *)jenv->GetStringUTFChars(jevname, 0); const char* cevname = jenv->GetStringUTFChars(jevname, 0);
evnames[i] = new char[jenv->GetStringLength(jevname)];
strcpy(evnames[i], cevname);
jenv->ReleaseStringUTFChars(jevname, cevname);
} }
int ret = XGBoosterEvalOneIter(handle, jiter, dmats, (char const *(*))evnames, len, (const char **) result); int ret = XGBoosterEvalOneIter(handle, jiter, dmats, (char const *(*)) evnames, len, (const char **) &result);
if(len > 0) { if(len > 0) {
delete[] dmats; delete[] dmats;
//release string chars //release string chars
for(bst_ulong i=0; i<len; i++) { for(bst_ulong i=0; i<len; i++) {
jstring jevname = (jstring)jenv->GetObjectArrayElement(jevnames, i); delete[] evnames[i];
jenv->ReleaseStringUTFChars(jevname, (const char*)evnames[i]);
} }
delete[] evnames; delete[] evnames;
jenv->ReleaseLongArrayElements(jdmats, cjdmats, 0); jenv->ReleaseLongArrayElements(jdmats, cjdmats, 0);
} }
jstring jinfo = 0; jstring jinfo = 0;
if (*result) jinfo = jenv->NewStringUTF((const char *) *result); if (result) jinfo = jenv->NewStringUTF((const char *) result);
jenv->SetObjectArrayElement(jout, 0, jinfo); jenv->SetObjectArrayElement(jout, 0, jinfo);
return ret; return ret;
@ -412,14 +414,13 @@ JNIEXPORT jint JNICALL Java_org_dmlc_xgboost4j_wrapper_XgboostJNI_XGBoosterPredi
(JNIEnv *jenv, jclass jcls, jlong jhandle, jlong jdmat, jint joption_mask, jint jntree_limit, jobjectArray jout) { (JNIEnv *jenv, jclass jcls, jlong jhandle, jlong jdmat, jint joption_mask, jint jntree_limit, jobjectArray jout) {
BoosterHandle handle = (BoosterHandle) jhandle; BoosterHandle handle = (BoosterHandle) jhandle;
DMatrixHandle dmat = (DMatrixHandle) jdmat; DMatrixHandle dmat = (DMatrixHandle) jdmat;
bst_ulong len[1]; bst_ulong len;
*len = 0; float *result;
float *result[1]; int ret = XGBoosterPredict(handle, dmat, joption_mask, (unsigned int) jntree_limit, &len, (const float **) &result);
int ret = XGBoosterPredict(handle, dmat, joption_mask, (unsigned int) jntree_limit, len, (const float **) result);
jsize jlen = (jsize) *len; jsize jlen = (jsize) len;
jfloatArray jarray = jenv->NewFloatArray(jlen); jfloatArray jarray = jenv->NewFloatArray(jlen);
jenv->SetFloatArrayRegion(jarray, 0, jlen, (jfloat *) *result); jenv->SetFloatArrayRegion(jarray, 0, jlen, (jfloat *) result);
jenv->SetObjectArrayElement(jout, 0, jarray); jenv->SetObjectArrayElement(jout, 0, jarray);
return ret; return ret;
} }
@ -475,13 +476,12 @@ JNIEXPORT jint JNICALL Java_org_dmlc_xgboost4j_wrapper_XgboostJNI_XGBoosterLoadM
JNIEXPORT jint JNICALL Java_org_dmlc_xgboost4j_wrapper_XgboostJNI_XGBoosterGetModelRaw JNIEXPORT jint JNICALL Java_org_dmlc_xgboost4j_wrapper_XgboostJNI_XGBoosterGetModelRaw
(JNIEnv * jenv, jclass jcls, jlong jhandle, jobjectArray jout) { (JNIEnv * jenv, jclass jcls, jlong jhandle, jobjectArray jout) {
BoosterHandle handle = (BoosterHandle) jhandle; BoosterHandle handle = (BoosterHandle) jhandle;
bst_ulong len[1]; bst_ulong len = 0;
*len = 0; char *result;
char *result[1];
int ret = XGBoosterGetModelRaw(handle, len, (const char **) result); int ret = XGBoosterGetModelRaw(handle, &len, (const char **) &result);
if (*result){ if (result){
jstring jinfo = jenv->NewStringUTF((const char *) *result); jstring jinfo = jenv->NewStringUTF((const char *) result);
jenv->SetObjectArrayElement(jout, 0, jinfo); jenv->SetObjectArrayElement(jout, 0, jinfo);
} }
return ret; return ret;
@ -496,16 +496,15 @@ JNIEXPORT jint JNICALL Java_org_dmlc_xgboost4j_wrapper_XgboostJNI_XGBoosterDumpM
(JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfmap, jint jwith_stats, jobjectArray jout) { (JNIEnv *jenv, jclass jcls, jlong jhandle, jstring jfmap, jint jwith_stats, jobjectArray jout) {
BoosterHandle handle = (BoosterHandle) jhandle; BoosterHandle handle = (BoosterHandle) jhandle;
const char *fmap = jenv->GetStringUTFChars(jfmap, 0); const char *fmap = jenv->GetStringUTFChars(jfmap, 0);
bst_ulong len[1]; bst_ulong len = 0;
*len = 0; char **result;
char **result[1];
int ret = XGBoosterDumpModel(handle, fmap, jwith_stats, len, (const char ***) result); int ret = XGBoosterDumpModel(handle, fmap, jwith_stats, &len, (const char ***) &result);
jsize jlen = (jsize)*len; jsize jlen = (jsize) len;
jobjectArray jinfos = jenv->NewObjectArray(jlen, jenv->FindClass("java/lang/String"), jenv->NewStringUTF("")); jobjectArray jinfos = jenv->NewObjectArray(jlen, jenv->FindClass("java/lang/String"), jenv->NewStringUTF(""));
for(int i=0 ; i<jlen; i++) { for(int i=0 ; i<jlen; i++) {
jenv->SetObjectArrayElement(jinfos, i, jenv->NewStringUTF((const char*) result[0][i])); jenv->SetObjectArrayElement(jinfos, i, jenv->NewStringUTF((const char*) result[i]));
} }
jenv->SetObjectArrayElement(jout, 0, jinfos); jenv->SetObjectArrayElement(jout, 0, jinfos);

View File

@ -117,7 +117,7 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None,
evals_result.update(dict([(key, {}) for key in evals_name])) evals_result.update(dict([(key, {}) for key in evals_name]))
if not early_stopping_rounds: if not early_stopping_rounds:
for i in range(num_boost_round): for i in range(nboost, nboost + num_boost_round):
bst.update(dtrain, i, obj) bst.update(dtrain, i, obj)
nboost += 1 nboost += 1
if len(evals) != 0: if len(evals) != 0:
@ -189,7 +189,7 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None,
if isinstance(learning_rates, list) and len(learning_rates) != num_boost_round: if isinstance(learning_rates, list) and len(learning_rates) != num_boost_round:
raise ValueError("Length of list 'learning_rates' has to equal 'num_boost_round'.") raise ValueError("Length of list 'learning_rates' has to equal 'num_boost_round'.")
for i in range(num_boost_round): for i in range(nboost, nboost + num_boost_round):
if learning_rates is not None: if learning_rates is not None:
if isinstance(learning_rates, list): if isinstance(learning_rates, list):
bst.set_param({'eta': learning_rates[i]}) bst.set_param({'eta': learning_rates[i]})
@ -337,7 +337,8 @@ def aggcv(rlist, show_stdv=True, show_progress=None, as_pandas=True, trial=0):
if show_progress is None: if show_progress is None:
show_progress = True show_progress = True
if (isinstance(show_progress, int) and trial % show_progress == 0) or (isinstance(show_progress, bool) and show_progress): if (isinstance(show_progress, int) and show_progress > 0 and trial % show_progress == 0) or \
(isinstance(show_progress, bool) and show_progress):
sys.stderr.write(msg + '\n') sys.stderr.write(msg + '\n')
sys.stderr.flush() sys.stderr.flush()
@ -432,10 +433,10 @@ def cv(params, dtrain, num_boost_round=10, nfold=3, metrics=(),
best_score = score best_score = score
best_score_i = i best_score_i = i
elif i - best_score_i >= early_stopping_rounds: elif i - best_score_i >= early_stopping_rounds:
sys.stderr.write("Stopping. Best iteration: {}\n".format(best_score_i))
results = results[:best_score_i+1] results = results[:best_score_i+1]
sys.stderr.write("Stopping. Best iteration: {} (mean: {}, std: {})\n".
format(best_score_i, results[-1][0], results[-1][1]))
break break
if as_pandas: if as_pandas:
try: try:
import pandas as pd import pandas as pd

View File

@ -153,7 +153,7 @@ class StdFile : public dmlc::Stream {
return std::fread(ptr, 1, size, fp); return std::fread(ptr, 1, size, fp);
} }
virtual void Write(const void *ptr, size_t size) { virtual void Write(const void *ptr, size_t size) {
std::fwrite(ptr, size, 1, fp); Check(std::fwrite(ptr, size, 1, fp) == 1, "StdFile::Write: fwrite error!");
} }
virtual void Seek(size_t pos) { virtual void Seek(size_t pos) {
std::fseek(fp, static_cast<long>(pos), SEEK_SET); // NOLINT(*) std::fseek(fp, static_cast<long>(pos), SEEK_SET); // NOLINT(*)

View File

@ -33,7 +33,7 @@ class FileStream : public ISeekStream {
return std::fread(ptr, size, 1, fp); return std::fread(ptr, size, 1, fp);
} }
virtual void Write(const void *ptr, size_t size) { virtual void Write(const void *ptr, size_t size) {
std::fwrite(ptr, size, 1, fp); Check(std::fwrite(ptr, size, 1, fp) == 1, "FileStream::Write: fwrite error!");
} }
virtual void Seek(size_t pos) { virtual void Seek(size_t pos) {
std::fseek(fp, static_cast<long>(pos), SEEK_SET); // NOLINT(*) std::fseek(fp, static_cast<long>(pos), SEEK_SET); // NOLINT(*)

View File

@ -24,7 +24,7 @@
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet> <CharacterSet>MultiByte</CharacterSet>
</PropertyGroup> </PropertyGroup>

View File

@ -25,6 +25,11 @@
<ClCompile Include="..\..\src\tree\updater.cpp" /> <ClCompile Include="..\..\src\tree\updater.cpp" />
<ClCompile Include="..\..\src\xgboost_main.cpp" /> <ClCompile Include="..\..\src\xgboost_main.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\subtree\rabit\windows\rabit\rabit.vcxproj">
<Project>{d7b77d06-4f5f-4bd7-b81e-7cc8ebbe684f}</Project>
</ProjectReference>
</ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{19766C3F-7508-49D0-BAAC-0988FCC9970C}</ProjectGuid> <ProjectGuid>{19766C3F-7508-49D0-BAAC-0988FCC9970C}</ProjectGuid>
<RootNamespace>xgboost</RootNamespace> <RootNamespace>xgboost</RootNamespace>

View File

@ -30,6 +30,7 @@
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{20A0E4D7-20C7-4EC1-BDF6-0D469CE239AA}</ProjectGuid> <ProjectGuid>{20A0E4D7-20C7-4EC1-BDF6-0D469CE239AA}</ProjectGuid>
<RootNamespace>xgboost_wrapper</RootNamespace> <RootNamespace>xgboost_wrapper</RootNamespace>
<ProjectName>xgboost4j</ProjectName>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">