question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Init parameter check of estimator_checks overly restrictive

See original GitHub issue

Describe the bug

I’m working on a custom estimator that needs an optimizer as one of its hyperparameters. I’m using the pattern of passing in an uninitialized optimizer and its parameters separately, i.e.

my_estimator = MyEstimator(optimizer=SGD, optimizer__lr=1e-5)

which will then initialize the optimizer as needed. I’m attempting to verify this with the check_parameters_default_constructible test, but it fails due to this assertion:

https://github.com/scikit-learn/scikit-learn/blob/cf4fa5c771789ed569b1b40787d9118f4be79737/sklearn/utils/estimator_checks.py#L2624

which restricts type arguments to numpy floats or numpy ints. I don’t understand why this restriction is necessary. With some digging through the history, I found that the general type checks were added in 90d5ef17f88916fb8f2ab6e27d6b681bf3b011ec to verify that all parameters are of immutable types. This was then extended by the restriction on type parameters in ab2f539a32b8099a941cefc598c9625e830ecfe4, as a drive-by change without explanation. Since types are immutable, I don’t understand why this was necessary.

I’m not sure if this is a bug or I am missing something. CC @amueller who added that check originally.

Steps/Code to Reproduce

Define an estimator such as

from keras.optimizers import SGD
from sklearn.base import BaseEstimator
from sklearn.utils.estimator_checks import check_parameters_default_constructible

class MyEstimator(BaseEstimator):
	def __init__(self, optimizer=SGD, **kwargs):
		self.optimizer=optimizer

check_parameters_default_constructible("default_constructible", MyEstimator)

Expected Results

The test passes since the estimator is default constructible and all arguments are of immutable type (the optimizer itself is of course not immutable, but the type is).

Actual Results

$ python3 example.py
Using TensorFlow backend.
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
Traceback (most recent call last):
  File "tmp.py", line 9, in <module>
    check_parameters_default_constructible("default_constructible", MyEstimator)
  File "/nix/store/isa1ilgb10xpkm6hjxaaw9m7g1xiiqp1-python3-3.7.7-env/lib/python3.7/site-packages/sklearn/utils/estimator_checks.py", line 2533, in check_parameters_default_constructible
    assert init_param.default in [np.float64, np.int64]
AssertionError

Versions

System:
    python: 3.7.7 (default, Mar 10 2020, 06:34:06)  [GCC 9.3.0]
executable: /nix/store/isa1ilgb10xpkm6hjxaaw9m7g1xiiqp1-python3-3.7.7-env/bin/python3.7
   machine: Linux-5.4.46-x86_64-with

Python dependencies:
       pip: None
setuptools: 45.2.0.post20200508
   sklearn: 0.22.2.post1
     numpy: 1.18.3
     scipy: 1.4.1
    Cython: None
    pandas: 1.0.3
matplotlib: 3.2.1
    joblib: 0.14.1

Built with OpenMP: True

(the relevant code hasn’t changed in current sklearn master though).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
thomasjpfancommented, Jul 15, 2020

Okay lets move forward with the suggestion by @rth https://github.com/scikit-learn/scikit-learn/issues/17756#issuecomment-652469631 to be more accepting of defaults.

0reactions
timokaucommented, Jul 16, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python checking __init__ parameter - Stack Overflow
For example, let's say I want to create an object Hand, and initialize a Hand only when I have enough Fingers in the...
Read more >
Check initial estimates and auto init in Monolix - Lixoft
This feature is very useful to find some “good” initial values. Although Monolix is quite robust with respect to initial parameter values, good...
Read more >
param.__init__ — param v1.12.2
Source code for param.__init__. from __future__ import print_function """ Parameters are a kind of class attribute allowing special behavior ...
Read more >
Initialization — The Swift Programming Language (Swift 5.7)
In its simplest form, an initializer is like an instance method with no parameters, written using the init keyword: init() {; // perform...
Read more >
Python Class Constructors: Control Your Object Instantiation
__init__() , which takes the constructor's arguments to initialize the newly created object. To explore how Python's instantiation process ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found