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

DOC: Add a page to help users transition from tf.keras.wrappers.scikit_learn #124

Closed
adriangb opened this issue Nov 5, 2020 · 5 comments · Fixed by #138
Closed

DOC: Add a page to help users transition from tf.keras.wrappers.scikit_learn #124

adriangb opened this issue Nov 5, 2020 · 5 comments · Fixed by #138

Comments

@adriangb
Copy link
Owner

adriangb commented Nov 5, 2020

We should have a page noting the differences (not features) vs. the old wrappers. I think so far we only have:

  • fit/predict arguments should be passed to the constructor instead of as kwargs to fit/predict.
  • Renamed build_fn to model
  • No longer need to declare defaults in build_fn, instead they should be declared in the SciKeras constructor
  • To get automatic one-hot encoding with categorical_crossentropy loss="categorical_crossentropy" (or loss=CategoricalCrossentropy, etc.) needs to be passed to the SciKeras constructor (even for user-compiled models) since we no longer introspect compiled models.
@stsievert
Copy link
Collaborator

I thought SciKeras aimed to be compatible with tf.keras.wrappers.scikit_learn?

@adriangb
Copy link
Owner Author

adriangb commented Nov 5, 2020

Yes, these are minor changes. I think all of these we've discussed together previously. An example with all of these changes (copied from #112):

def build_model(optimizer='adam'):
    model = Sequential()
    model.add(Flatten(input_shape=(28, 28)))
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(10, activation='softmax'))
    model.compile(optimizer=optimizer,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

    return model
    
model = KerasClassifier(build_fn=build_model)

New:

def build_model(optimizer):
    model = Sequential()
    model.add(Flatten(input_shape=(28, 28)))
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(10, activation='softmax'))
    model.compile(optimizer=optimizer,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

    return model
    
model = KerasClassifier(model=build_model, optimizer="rmsprop", loss="categorical_crossentropy")

@stsievert
Copy link
Collaborator

To get automatic one-hot encoding with categorical_crossentropy loss="categorical_crossentropy" (or loss=CategoricalCrossentropy, etc.) needs to be passed to the SciKeras constructor (even for user-compiled models) since we no longer introspect compiled models.

Why not specify a default of loss="categorical_crossentropy" for KerasClassifier? That'd also resolve #112, correct? Does it have any side effects?

  • fit/predict arguments should be passed to the constructor instead of as kwargs to fit/predict.
  • Renamed build_fn to model

I like the implementation around build_fn: it still works, but issues a warning if specified. Could the same be done for parameters passed to fit/predict?

def fit(self, X, y, classes=None, ..., **kwargs):
    for k, v in kwargs.items():
        warn(f"Set fit arguments at initialization (i.e., `BaseWrapper({k}={v})`)")
    self.set_parmas(**kwargs)

@adriangb
Copy link
Owner Author

adriangb commented Nov 6, 2020

Could the same be done for parameters passed to fit/predict?

def fit(self, X, y, classes=None, ..., **kwargs):
    for k, v in kwargs.items():
        warn(f"Set fit arguments at initialization (i.e., `BaseWrapper({k}={v})`)")
    self.set_parmas(**kwargs)

We could do that, I don't see why not. But I'd like to deprecate that relatively soon.

@adriangb
Copy link
Owner Author

def fit(self, X, y, classes=None, ..., **kwargs):
    for k, v in kwargs.items():
        warn(f"Set fit arguments at initialization (i.e., `BaseWrapper({k}={v})`)")
    self.set_params(**kwargs)

The main problem with this approach is that subsequent calls to fit/predict will use the **kwargs, even if they are not passed the second call. But maybe this is enough of an edge case to not worry about.

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 a pull request may close this issue.

2 participants