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

Adding Multi-Task ElasticNet support #238

Merged
merged 47 commits into from
Nov 12, 2022

Conversation

YuhanLiin
Copy link
Collaborator

Continuation of #194

@YuhanLiin
Copy link
Collaborator Author

Running the new multi-task example gives the following output:

intercept:  [182.11111111111111, 35.666666666666664, 55.55555555555556]
params: [[-0.9723742003724933, -0.12992938479472216, 0.20256364290951492],
 [0.017231919622246364, -0.00785311972200309, 0.006638074127064588],
 [0.0269082650844912, 0.021197761913871658, -0.027310155988705367]]
z score: Ok([[-1.0608739975723132, -1.6800812346255631, 2.2563865434388126],
 [0.018800267889146287, -0.10154653698276617, 0.0739424949093583],
 [0.029357297568142263, 0.274102444676571, -0.3042118890982639]], shape=[3, 3], strides=[3, 1], layout=Cc (0x5), const ndim=2)
predicted variance: [-47.348308658688005, -2.278139252532177, -50.96366188947603]

The variance looks pretty high, but I'm not sure if that's an issue.

@codecov-commenter
Copy link

codecov-commenter commented Aug 14, 2022

Codecov Report

Base: 38.68% // Head: 38.59% // Decreases project coverage by -0.09% ⚠️

Coverage data is based on head (9551892) compared to base (44b244c).
Patch coverage: 31.70% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #238      +/-   ##
==========================================
- Coverage   38.68%   38.59%   -0.10%     
==========================================
  Files          93       93              
  Lines        6087     6223     +136     
==========================================
+ Hits         2355     2402      +47     
- Misses       3732     3821      +89     
Impacted Files Coverage Δ
algorithms/linfa-elasticnet/src/hyperparams.rs 14.58% <0.00%> (ø)
algorithms/linfa-elasticnet/src/lib.rs 0.00% <0.00%> (ø)
algorithms/linfa-elasticnet/src/algorithm.rs 35.05% <33.33%> (-2.14%) ⬇️
...rithms/linfa-trees/src/decision_trees/algorithm.rs 39.73% <0.00%> (+1.78%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@bytesnake bytesnake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 can you point me to the test with high variance?

penalty: F,
) -> (Array2<F>, F, u32) {
let n_samples = F::cast(x.shape()[0]);
let n_features = x.shape()[1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using ncols / nrows is a bit more expressive

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the current naming better expresses the properties of the dataset (# of rows = # of samples, # of cols = # of features). Plus this naming convention is used basically everywhere in this crate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah I meant the method name, not the variable

algorithms/linfa-elasticnet/src/algorithm.rs Outdated Show resolved Hide resolved
algorithms/linfa-elasticnet/src/algorithm.rs Show resolved Hide resolved
algorithms/linfa-elasticnet/src/algorithm.rs Outdated Show resolved Hide resolved
#[derive(Clone, Debug, PartialEq)]
pub struct ElasticNetValidParams<F> {
pub struct ElasticNetValidParamsBase<F, const MULTI_TASK: bool> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the multi task flag not be derived from the dataset?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should be able to encapsulate multi-task use case in another Fit impl on the same type

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried to do that and it didn't work. The single and multitarget Fit impls bound the target data type with AsSingleTarget and AsMultiTarget respectively. If I put both impls on a unified param type then I get a conflicting impl error, even though AsSingleTarget and AsMultiTarget are implemented on completely different types. This idea would be doable if we made the target types Array1 and Array2 instead of generics bounded by traits.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the conflicting bounds are probably introduced here: https://github.com/rust-ml/linfa/blob/master/src/dataset/impl_targets.rs#L25-L26 have you also tried bounding the type with T: AsTargets<Ix = Ix?> directly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried it. Still the same error. Rust compiler probably isn't smart enough to realize that those two bounds are completely disjoint.

@YuhanLiin
Copy link
Collaborator Author

+1 can you point me to the test with high variance?

It's not a test, but the new example I added

@bytesnake
Copy link
Member

reviewed the example and made two changes to make the explained variance more usable

  1. use more than two samples for validation, otherwise the second class has zero variance
  2. compare validation dataset to estimated values (R2 is not symmetric)
--- a/algorithms/linfa-elasticnet/examples/multitask_elasticnet.rs
+++ b/algorithms/linfa-elasticnet/examples/multitask_elasticnet.rs
@@ -3,7 +3,7 @@ use linfa_elasticnet::{MultiTaskElasticNet, Result};

 fn main() -> Result<()> {
     // load Diabetes dataset
-    let (train, valid) = linfa_datasets::linnerud().split_with_ratio(0.90);
+    let (train, valid) = linfa_datasets::linnerud().split_with_ratio(0.80);

     // train pure LASSO model with 0.1 penalty
     let model = MultiTaskElasticNet::params()
@@ -18,7 +18,7 @@ fn main() -> Result<()> {

     // validate
     let y_est = model.predict(&valid);
-    println!("predicted variance: {}", valid.r2(&y_est)?);
+    println!("predicted variance: {}", y_est.r2(&valid)?);

     Ok(())
 }

which gives

predicted variance: [-4.143623744690414, -0.2630142563112303, -0.2542410304293199]

so worse than taking the average, but the dataset is really small 😅

@YuhanLiin YuhanLiin merged commit 21357e2 into rust-ml:master Nov 12, 2022
@YuhanLiin YuhanLiin deleted the mtl_elastic_net branch November 12, 2022 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants