Vectorize leaf dynamics#36
Conversation
b2bf166 to
69320a2
Compare
|
@SarahAlidoost after the discussion we had today, I realized it was very little changes to add the functionality to work with arbitrarily-shaped tensors as parameters, so I have added it. In the current form the At the current stage, we should be able to reproduce simulations with scalar parameters (all tests pass), I am now working to add some tests on the use of parameter tensors! |
|
@SarahAlidoost, I left one question below, but this is otherwise ready to be reviewed. I have added few tests that show that the module can now work with arbitrarily shaped arrays as parameters (provided all parameters have the same shape). There is quite some repeated code in the tests, happy to hear whether you have suggestions for improvements there. Otherwise, I think that things will simplify significantly when we will modify the engine class and make it easier to pass input and get output. |
SarahAlidoost
left a comment
There was a problem hiding this comment.
@fnattino great job, thanks 👍 The tests of class TestLeafDynamics were passedd with test data test_leafdynamics_wofost72_17.yaml too, well done!
But those tests only check the correctness of the model and not the gradient graphs. The classes TestDiffLeafDynamicsTDWI and TestDiffLeafDynamicsSPAN check the gradient computation graph, but right now they don’t have any tests for the vector case. For example, similar to test_gradients_tdwi_lai_leaf_dynamics, we should add a test where
tdwi = torch.nn.Parameter(torch.tensor([0.1, 0.2, 0.3], dtype=torch.float32)). So each class could have tests such as:
- test_gradients_tdwi_lai_leaf_dynamics_vector
- test_gradients_tdwi_twlv_leaf_dynamics_vector
I added an example. Can you please add those tests? if something is unclear, let me know. Thanks.
Co-authored-by: SarahAlidoost <55081872+SarahAlidoost@users.noreply.github.com>
it can be problematic with conditional statements
the same index was used for all parameter values
also check that all parameters have the same shape
safer and easier to read
There was a problem hiding this comment.
@SarahAlidoost I should have addressed your comments above. When adding tests, I have slightly modified the structure of tests for leaf dynamics to be able to run them with parameters, so it would be easier to add more of these tests.
Now, one of the thing I did to make this implementation easier, is to have DiffLeafDynamics return a dictionary of tensors instead of stacking all the tensors in a larger tensor. I have then ported the same structure to the tests on root dynamics, which unveiled an issue there: the RD variable seems not to have the gradient defined (see failing tests, e.g. here). Earlier, this issue was not caught by the tests, because when stacking RD and TWRT in a single tensor, a gradient would somehow become available - but now this seems to have emerged. Do you agree with the assesment?
Thanks for refactoring the tests using
That's correct. Actually variable def test_gradients_tdwi_rd_root_dynamics(self):
model = get_test_diff_root_model()
tdwi = torch.nn.Parameter(torch.tensor(0.2, dtype=torch.float32))
output = model({"TDWI": tdwi})
rd = output["RD"]
loss = rd.sum()
assert loss.grad_fn is None # tdwi doesnot contribute to rd |
add one class per parameter
|
Thanks for all the clarifications (and for bearing with me in this PR :) ). I have followed your suggestion and:
|
SarahAlidoost
left a comment
There was a problem hiding this comment.
@fnattino looks great! Thank you for implementing the vectorization and addressing the comments and suggestions. Ready to merge with only one small change, could you please remove the test test_gradients_tdwi_rd_root_dynamics_numerical before merging? 🚀
|
Done @SarahAlidoost - the lasts checks are running, feel free to merge when you think it's ready! |
|



Closes #19