Skip to content

[ENH] Adding CNNRegressor and BaseDeepRegressor #2902

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

Merged
merged 17 commits into from
Jul 23, 2022

Conversation

AurumnPegasus
Copy link
Contributor

Reference Issues/PRs

See aso #2894.

What does this implement/fix? Explain your changes.

Implements BaseDeepRegressor, which is a base class for Deep-Learning based Regression models. Also implements CNNRegressor using the base class.

Does your contribution introduce a new dependency? If yes, which one?

No.

What should a reviewer concentrate their feedback on?

I am a bit unsure about what all is required in BaseDeepRegressor. Once that is finalised, implementing CNNRegressor should be easy addition to it.

PR checklist

For all contributions
  • I've added unit tests and made sure they pass locally.
  • The PR title starts with either [ENH], [MNT], [DOC], or [BUG] indicating whether the PR topic is related to enhancement, maintenance, documentation, or bug.

@AurumnPegasus
Copy link
Contributor Author

About the tests that fail:

AssertionError: 
Arrays are not almost equal to 6 decimals

Mismatched elements: 5 / 5 (100%)
Max absolute difference: 0.002462
Max relative difference: 0.00493456
 x: array([0.5     , 0.501392, 0.499438, 0.5     , 0.501738], dtype=float32)
 y: array([0.499986, 0.49893 , 0.499301, 0.500023, 0.500554], dtype=float

If the error is caused by the difference, I think we should introduce a threshold here. Since CNN / most neural have randomly initialised initial state, it is not uncommon to get different results (and in this case its a very minor difference).

Just a note, there are 2 tests which are failing, and both of them are related to idempotent matrices. It feels odd that only those two cases are failing, but I do not understand why its just those two matrices.

@AurumnPegasus AurumnPegasus marked this pull request as ready for review July 6, 2022 10:52
@fkiraly fkiraly self-assigned this Jul 8, 2022
@AurumnPegasus
Copy link
Contributor Author

Something I found out about reproduciblity in keras.
official docs, stack overflow answer

In short, to be absolutely sure that you will get reproducible results with your python script on one computer's/laptop's CPU then you will have to do the following:

  1. Set PYTHONHASHSEED environment variable at a fixed value
  2. Set python built-in pseudo-random generator at a fixed value
  3. Set numpy pseudo-random generator at a fixed value
  4. Set tensorflow pseudo-random generator at a fixed value
  5. Configure a new global tensorflow session

I can test this out to see if it also works on windows 🤞🏽
Though I do think we need to redo our tests since:

Moreover, when running on a GPU, some operations have non-deterministic outputs, in particular tf.reduce_sum(). This is due to the fact that GPUs run many operations in parallel, so the order of execution is not always guaranteed. Due to the limited precision of floats, even adding several numbers together may give slightly different results depending on the order in which you add them. You can try to avoid the non-deterministic operations, but some may be created automatically by TensorFlow to compute the gradients

So when we do implement the added functionality of GPU compute, the test is meant to fail.

@TonyBagnall
Copy link
Contributor

hi, sorry for the delay in looking at this. I have looked back at CNNClassifier, and basically I found the same. keras is non deterministic, and even if you could fix all seeds and reproduce results, fixing a global variable like numpy.random in one classifier seems like it may have nasty ramifications down the line.

My decision was to just not do the correctness tests for CNNClassifier. Instead, I have run the whole lot on the UCR data and can show no significant difference to the published results with a fixed version of sktime. If we ever have a correctness problem, we can compare back to this. I can do the same for the Regressor, and the other deep learners. Note I also excluded these two test

# test fail with deep problem with pickling inside tensorflow.
"CNNClassifier": [
    "test_fit_idempotent",
    "test_persistence_via_pickle",
],

I suggest you do the same for regressors

TonyBagnall
TonyBagnall previously approved these changes Jul 13, 2022
Copy link
Contributor

@TonyBagnall TonyBagnall left a comment

Choose a reason for hiding this comment

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

we might need to look for a tidier way of excluding classifiers/regressors, since when we port in more the config will start to bloat, but that is beyond the scope of this PR.

@AurumnPegasus
Copy link
Contributor Author

@TonyBagnall My commit seems to fail the test for soft dependencies

RuntimeError: Estimator CNNRegressor does not require soft dependencies according to tags, but raises ModuleNotFoundError on __init__. Any required soft dependencies should be added to the "python_dependencies" tag, and python version bouds should be added to the "python_version" tag. Exception text: tensorflow

I have added the required dependency in test_softdeps.py but it still is not reflecting that. Any idea what could be going wrong?

@TonyBagnall
Copy link
Contributor

Estimator CNNRegressor does not require soft dependencies according to tags

so just looking at CNNClassifier, which does run, the BaseDeepClassifier has these tags,
_tags = {
"X_inner_mtype": "numpy3D",
"capability:multivariate": True,
"python_dependencies": "tensorflow",
}
I've not seen the python_dependencies tag before. Does the base regressor have it? If not, try adding it. Ask franz if you want an overview of tags logic.

TonyBagnall
TonyBagnall previously approved these changes Jul 14, 2022
Copy link
Collaborator

@fkiraly fkiraly left a comment

Choose a reason for hiding this comment

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

Looks ok, great!

One comment: if you wrote this alone, there is no need to credit @TonyBagnall or @james-large as authors, kindly remove that.

@AurumnPegasus
Copy link
Contributor Author

Done, @fkiraly

Copy link
Collaborator

@fkiraly fkiraly left a comment

Choose a reason for hiding this comment

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

Looks good!

@fkiraly fkiraly merged commit 694c449 into sktime:main Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:regression regression module: time series regression
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants