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

fit_generator does not expose a sample_weight parameter like fit #11800

Closed
loretoparisi opened this issue Dec 5, 2018 · 4 comments
Closed

fit_generator does not expose a sample_weight parameter like fit #11800

loretoparisi opened this issue Dec 5, 2018 · 4 comments
Labels
type:feature The user is asking for a new feature.

Comments

@loretoparisi
Copy link

loretoparisi commented Dec 5, 2018

While the signature of fit is

fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)

the fit_generator is

fit_generator(generator, steps_per_epoch=None, epochs=1, verbose=1, callbacks=None, validation_data=None, validation_steps=None, class_weight=None, max_queue_size=10, workers=1, use_multiprocessing=False, shuffle=True, initial_epoch=0)

so class_weight is defined for both, the validation_data can have optionally sample weights in the form of tuple (x_val, y_val, val_sample_weights), that will work on validation set only, but the fit_generator has no sample_weight option, i.e. the array of weights for the training samples.

Why training the model for a given number of epochs (i.e. fit) support sample weighting while training the model on data generated batch-by-batch (i.e. fit_generator) does not?

At the end of the day fit_generator will use the model.train_on_batch api:

def fit_generator(model,
                  generator,
                  steps_per_epoch=None,
                  epochs=1,
                  verbose=1,
                  callbacks=None,
                  validation_data=None,
                  validation_steps=None,
                  class_weight=None,
                  max_queue_size=10,
                  workers=1,
                  use_multiprocessing=False,
                  shuffle=True,
                  initial_epoch=0):

outs = model.train_on_batch(x, y,

Now the method train_on_batch support both class_weight than sample_weight parameters:

def train_on_batch(self, x, y,

def train_on_batch(self, x, y,
                       sample_weight=None,
                       class_weight=None):

Now what happens internally here

if len(generator_output) == 2:
is like

if len(generator_output) == 2:
                x, y = generator_output
                sample_weight = None
            elif len(generator_output) == 3:
                x, y, sample_weight = generator_output
            else:
                raise ValueError('Output of generator should be a tuple '
                                 '(x, y, sample_weight) '
                                 'or (x, y). Found: ' +
                                 str(generator_output))

so a sample_weight is mapped from the generator output when your return the tuple (x_val, y_val, val_sample_weights), so apparently it will be used on the training batch, depending on your generator.
That said, why the fit_generator is not exposing the sample_weight argument then?

@gabrieldemarmiesse
Copy link
Contributor

I see you opened two issue related to the same problem. Would it be possible for you to close one and follow everything on one single thread? thank you.

@gabrieldemarmiesse gabrieldemarmiesse added the type:feature The user is asking for a new feature. label Dec 5, 2018
@loretoparisi
Copy link
Author

@gabrieldemarmiesse correct, l have realized later that those issues could be related to the same problem. Which one do you suggest to close? Thank you.

@brge17
Copy link
Contributor

brge17 commented Dec 13, 2018

Read the docs on fit_generator.

The output of the generator must be either

a tuple (inputs, targets)
a tuple (inputs, targets, sample_weights).

The generator for train or validation simply needs to

 yield X, y, w

@loretoparisi
Copy link
Author

@brge17 thanks, as for #11805 (comment) the fit_generator and fit will be soon merged into one taking in account the api signature of both methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature The user is asking for a new feature.
Projects
None yet
Development

No branches or pull requests

3 participants