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

Feat binary class in tree_ensemble_classifier operator #510

Merged
merged 13 commits into from
Jan 6, 2024
Merged
Changes from 1 commit
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
150 changes: 150 additions & 0 deletions src/operators/ml/tree_ensemble/tree_ensemble_classifier.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,156 @@ impl TreeEnsembleClassifierImpl<
i += 1;
};

// Binary class
let mut binary = false;
let mut i: usize = 0;
let mut class_ids = self.class_ids;
let mut class_id: usize = 0;
// Get first class_id in class_ids
match class_ids.pop_front() {
Option::Some(c_id) => {
let mut class_id = *c_id;
},
Option::None(_) => {
let mut class_id: usize = 0;
}
};
loop {
if i == self.class_ids.len() {
break;
}
match class_ids.pop_front() {
Option::Some(c_id) => {
if *c_id == class_id {
binary = true;
continue;
}else{
binary = false;
break;
}

},
Option::None(_) => { break; }
};

};

// Clone res
if binary{
let mut new_res: MutMatrix<T> = MutMatrixImpl::new(res.rows, res.cols);
let mut i: usize = 0;
loop {
if i == res.rows {
break;
}
// Exchange
let res_ele_1 = match res.get(i, 0) {
Option::Some(res_0) => {
new_res.set(i, 1, res_0);
},
Option::None(_) => {
new_res.set(i, 1, NumberTrait::zero());
},
};
i+=1;
};
match self.post_transform {
POST_TRANSFORM::NONE => {
let mut i: usize = 0;
loop {
if i == res.rows {
break;
}
// Exchange
let res_ele_0 = match res.get(i, 1) {
Option::Some(res_1) => {
let value = NumberTrait::sub(NumberTrait::one(), res_1);
new_res.set(i, 0, value);
},
Option::None(_) => {
new_res.set(i, 0, NumberTrait::zero());
},
};
i+=1;
};
},
POST_TRANSFORM::SOFTMAX => {
let mut i: usize = 0;
loop {
if i == res.rows {
break;
}
// Exchange
let res_ele_0 = match res.get(i, 1) {
Option::Some(res_1) => {
new_res.set(i, 0, res_1.neg());
},
Option::None(_) => {
new_res.set(i, 0, NumberTrait::zero());
},
};
i+=1;
};
},
POST_TRANSFORM::LOGISTIC => {
let mut i: usize = 0;
loop {
if i == res.rows {
break;
}
// Exchange
let res_ele_0 = match res.get(i, 1) {
Option::Some(res_1) => {
new_res.set(i, 0, res_1.neg());
},
Option::None(_) => {
new_res.set(i, 0, NumberTrait::zero());
},
};
i+=1;
};
},
POST_TRANSFORM::SOFTMAXZERO => {
let mut i: usize = 0;
loop {
if i == res.rows {
break;
}
// Exchange
let res_ele_0 = match res.get(i, 1) {
Option::Some(res_1) => {
new_res.set(i, 0, res_1.neg());
},
Option::None(_) => {
new_res.set(i, 0, NumberTrait::zero());
},
};
i+=1;
};
},
POST_TRANSFORM::PROBIT => {
let mut i: usize = 0;
loop {
if i == res.rows {
break;
}
// Exchange
let res_ele_0 = match res.get(i, 1) {
Option::Some(res_1) => {
let value = NumberTrait::sub(NumberTrait::one(), res_1);
new_res.set(i, 0, value);
},
Option::None(_) => {
new_res.set(i, 0, NumberTrait::zero());
},
};
i+=1;
};
},
};
res = new_res;
}

// Post Transform
let mut new_scores = match self.post_transform {
POST_TRANSFORM::NONE => res, // No action required
Expand Down