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

use long double to align max-depth 13 #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 19 additions & 17 deletions linear_tree_shap/cext/linear_tree_shap.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using namespace std;

typedef double tfloat;
typedef long double tdouble;

struct Tree {
tfloat *weights;
Expand Down Expand Up @@ -48,8 +49,9 @@ struct BioCoeff {
return C+i*col;
}

tfloat psi(tfloat* E, tfloat q, int e_size, int d) {
tfloat res=0., quo = 0.;
tfloat psi(tdouble* E, tdouble q, int e_size, int d) {
tfloat res=0.;
tdouble quo = 0.;
tfloat *n = get_n(d);
for(int i=0; i<d+1; i++){
if (i<e_size){
Expand All @@ -75,7 +77,7 @@ struct Dataset {
}
};

void polymul(tfloat * C, tfloat q, int c_size, tfloat * out){
void polymul(tdouble * C, tdouble q, int c_size, tdouble * out){
tfloat tmp = 0.;
for(int i=0; i<c_size; i++){
out[i] = C[i] + q*tmp;
Expand All @@ -84,27 +86,27 @@ void polymul(tfloat * C, tfloat q, int c_size, tfloat * out){
out[c_size] = q*tmp;
};

void polyquo(tfloat * C, tfloat q, int c_size, tfloat * out){
void polyquo(tdouble * C, tdouble q, int c_size, tdouble * out){
tfloat tmp = 0.;
for(int i=0; i<c_size-1; i++){
tmp = C[i] - q*tmp;
out[i] = tmp;
}
};

void multiply(tfloat * input, tfloat *output, tfloat scalar, int size){
void multiply(tdouble * input, tdouble *output, tfloat scalar, int size){
for(int i =0; i<size; i++){
output[i] = input[i]*scalar;
}
};

void sum(tfloat * input, tfloat* output, int size){
void sum(tdouble * input, tdouble* output, int size){
for(int i =0; i<size; i++){
output[i] += input[i];
}
};

void copy(tfloat *from, tfloat *to, int size){
void copy(tdouble *from, tdouble *to, int size){
for (int i=0; i<size; i++){
to[i] = from[i];
}
Expand All @@ -115,21 +117,21 @@ void inference(const Tree& tree,
bool *A,
tfloat* V,
BioCoeff& N,
tfloat* C,
tfloat* E,
tdouble* C,
tdouble* E,
tfloat* x,
int n=0,
int feature=-1,
int depth=0,
int prev_c_size=0){
tfloat q = -1.;
tfloat s = -1.;
tdouble q = -1.;
tdouble s = -1.;
int m = tree.parents[n];
int left = tree.children_left[n];
int right = tree.children_right[n];
tfloat *current_c = C+depth*tree.max_depth;
tfloat *current_e = E+depth*tree.max_depth;
tfloat *child_e = E+(depth+1)*tree.max_depth;
tdouble *current_c = C+depth*tree.max_depth;
tdouble *current_e = E+depth*tree.max_depth;
tdouble *child_e = E+(depth+1)*tree.max_depth;
int current_c_size = prev_c_size + 1;
if(x[tree.features[n]] <= tree.thresholds[n]){
A[left] = true;
Expand All @@ -148,7 +150,7 @@ void inference(const Tree& tree,
if (A[n]) {
q = 1/tree.weights[n]-1;
}
tfloat *prev_c = C+(depth-1)*tree.max_depth;
tdouble *prev_c = C+(depth-1)*tree.max_depth;
polymul(prev_c, q, prev_c_size, current_c);

if (m >= 0){
Expand Down Expand Up @@ -185,8 +187,8 @@ inline void linear_tree_shap(const Tree& tree,
Dataset& data,
Dataset& out,
BioCoeff& N){
tfloat * C = new tfloat[tree.max_depth*tree.max_depth];
tfloat * E = new tfloat[tree.max_depth*tree.max_depth];
tdouble * C = new tdouble[tree.max_depth*tree.max_depth];
tdouble * E = new tdouble[tree.max_depth*tree.max_depth];
bool *A = new bool[tree.num_nodes];
for (unsigned i=0; i<data.row; i++)
{
Expand Down