I'd like to call 'on_epoch_end()' at each end of epoch. [keras custom generator]












0














I use keras and tried to define custom generator.



In generator, I expect that the function "on_epoch_end()" is called at each end of epoch, But "on_epoch_end()" never be called anytime.



Could you tell me why? Please.



from pathlib import Path
import math

from tensorflow.keras.utils import Sequence
from keras.utils import np_utils

class ImageSequence(Sequence):
def __init__(self, x, batch_size=512):
self.x_positive = x[0]
self.x_negative = x[1]
self.batch_size = batch_size

def __getitem__(self, idx):
hbs = self.batch_size//2
idx_p = np.random.randint(0, self.x_positive.shape[0], hbs)
batch_x_positive = self.x_positive[idx_p]
#
idx_n = np.random.randint(0, self.x_negative.shape[0], hbs)
batch_x_negative = self.x_negative[idx_n]
#batch_x_negative = self.x_negative[idx*hbs : (idx+1)*hbs]
#
batch_x = np.r_[batch_x_positive, batch_x_negative]
#
batch_y = np.r_[np.ones(len(batch_x_positive)), np.zeros(len(batch_x_negative))]
return batch_x, batch_y

def __len__(self):
return math.ceil(2 * len(self.x_negative) / self.batch_size)

def _shuffle(self):
self.x_negative = shuffle(self.x_negative)

def on_epoch_end(self):
self._shuffle()


data_gen = ImageSequence([train_positive, train_negative], batch_size=BATCH_SIZE)

history = model.fit_generator(
generator=data_gen,
use_multiprocessing=True,
validation_data=(x_valid, y_valid),
steps_per_epoch=2 * len(train_positive) / BATCH_SIZE,
epochs=30,
verbose=2,
callbacks=)


I wrote my environment(version info )



import tensorflow.keras
print(tensorflow.keras.__version__)
2.1.6-tf









share|improve this question






















  • you are picking your batch using random indexes with the line idx_p = np.random.randint(0, self.x_positive.shape[0], hbs) what are you trying to shuffle with on_epoch_end() ?
    – Mete Han Kahraman
    Nov 13 '18 at 10:17












  • Thank you for your reply. and I'm sorry for confusing you. At first I tried to shuffle at "on_epoch_end()", but it was not called. so then I define "idx_n = np.random.randint(0, self.x_negative.shape[0], hbs)". I would like to shuffle x.negative at the end of epoch if I can, in order to get completeness of train data in each epoch.
    – RoadRoller
    Nov 13 '18 at 13:09










  • This might help: github.com/keras-team/keras/issues/11122
    – Mete Han Kahraman
    Nov 14 '18 at 5:09












  • Perhaps you should change math.ceil to math.floor in the __len__function.
    – Mete Han Kahraman
    Nov 14 '18 at 5:15












  • Thank you very much!! the problem has benn solved. I replaced math.ceil to math.floor. And I found that there was a difference of definition between steps_per_epoch and __len__.
    – RoadRoller
    Nov 15 '18 at 1:17


















0














I use keras and tried to define custom generator.



In generator, I expect that the function "on_epoch_end()" is called at each end of epoch, But "on_epoch_end()" never be called anytime.



Could you tell me why? Please.



from pathlib import Path
import math

from tensorflow.keras.utils import Sequence
from keras.utils import np_utils

class ImageSequence(Sequence):
def __init__(self, x, batch_size=512):
self.x_positive = x[0]
self.x_negative = x[1]
self.batch_size = batch_size

def __getitem__(self, idx):
hbs = self.batch_size//2
idx_p = np.random.randint(0, self.x_positive.shape[0], hbs)
batch_x_positive = self.x_positive[idx_p]
#
idx_n = np.random.randint(0, self.x_negative.shape[0], hbs)
batch_x_negative = self.x_negative[idx_n]
#batch_x_negative = self.x_negative[idx*hbs : (idx+1)*hbs]
#
batch_x = np.r_[batch_x_positive, batch_x_negative]
#
batch_y = np.r_[np.ones(len(batch_x_positive)), np.zeros(len(batch_x_negative))]
return batch_x, batch_y

def __len__(self):
return math.ceil(2 * len(self.x_negative) / self.batch_size)

def _shuffle(self):
self.x_negative = shuffle(self.x_negative)

def on_epoch_end(self):
self._shuffle()


data_gen = ImageSequence([train_positive, train_negative], batch_size=BATCH_SIZE)

history = model.fit_generator(
generator=data_gen,
use_multiprocessing=True,
validation_data=(x_valid, y_valid),
steps_per_epoch=2 * len(train_positive) / BATCH_SIZE,
epochs=30,
verbose=2,
callbacks=)


I wrote my environment(version info )



import tensorflow.keras
print(tensorflow.keras.__version__)
2.1.6-tf









share|improve this question






















  • you are picking your batch using random indexes with the line idx_p = np.random.randint(0, self.x_positive.shape[0], hbs) what are you trying to shuffle with on_epoch_end() ?
    – Mete Han Kahraman
    Nov 13 '18 at 10:17












  • Thank you for your reply. and I'm sorry for confusing you. At first I tried to shuffle at "on_epoch_end()", but it was not called. so then I define "idx_n = np.random.randint(0, self.x_negative.shape[0], hbs)". I would like to shuffle x.negative at the end of epoch if I can, in order to get completeness of train data in each epoch.
    – RoadRoller
    Nov 13 '18 at 13:09










  • This might help: github.com/keras-team/keras/issues/11122
    – Mete Han Kahraman
    Nov 14 '18 at 5:09












  • Perhaps you should change math.ceil to math.floor in the __len__function.
    – Mete Han Kahraman
    Nov 14 '18 at 5:15












  • Thank you very much!! the problem has benn solved. I replaced math.ceil to math.floor. And I found that there was a difference of definition between steps_per_epoch and __len__.
    – RoadRoller
    Nov 15 '18 at 1:17
















0












0








0







I use keras and tried to define custom generator.



In generator, I expect that the function "on_epoch_end()" is called at each end of epoch, But "on_epoch_end()" never be called anytime.



Could you tell me why? Please.



from pathlib import Path
import math

from tensorflow.keras.utils import Sequence
from keras.utils import np_utils

class ImageSequence(Sequence):
def __init__(self, x, batch_size=512):
self.x_positive = x[0]
self.x_negative = x[1]
self.batch_size = batch_size

def __getitem__(self, idx):
hbs = self.batch_size//2
idx_p = np.random.randint(0, self.x_positive.shape[0], hbs)
batch_x_positive = self.x_positive[idx_p]
#
idx_n = np.random.randint(0, self.x_negative.shape[0], hbs)
batch_x_negative = self.x_negative[idx_n]
#batch_x_negative = self.x_negative[idx*hbs : (idx+1)*hbs]
#
batch_x = np.r_[batch_x_positive, batch_x_negative]
#
batch_y = np.r_[np.ones(len(batch_x_positive)), np.zeros(len(batch_x_negative))]
return batch_x, batch_y

def __len__(self):
return math.ceil(2 * len(self.x_negative) / self.batch_size)

def _shuffle(self):
self.x_negative = shuffle(self.x_negative)

def on_epoch_end(self):
self._shuffle()


data_gen = ImageSequence([train_positive, train_negative], batch_size=BATCH_SIZE)

history = model.fit_generator(
generator=data_gen,
use_multiprocessing=True,
validation_data=(x_valid, y_valid),
steps_per_epoch=2 * len(train_positive) / BATCH_SIZE,
epochs=30,
verbose=2,
callbacks=)


I wrote my environment(version info )



import tensorflow.keras
print(tensorflow.keras.__version__)
2.1.6-tf









share|improve this question













I use keras and tried to define custom generator.



In generator, I expect that the function "on_epoch_end()" is called at each end of epoch, But "on_epoch_end()" never be called anytime.



Could you tell me why? Please.



from pathlib import Path
import math

from tensorflow.keras.utils import Sequence
from keras.utils import np_utils

class ImageSequence(Sequence):
def __init__(self, x, batch_size=512):
self.x_positive = x[0]
self.x_negative = x[1]
self.batch_size = batch_size

def __getitem__(self, idx):
hbs = self.batch_size//2
idx_p = np.random.randint(0, self.x_positive.shape[0], hbs)
batch_x_positive = self.x_positive[idx_p]
#
idx_n = np.random.randint(0, self.x_negative.shape[0], hbs)
batch_x_negative = self.x_negative[idx_n]
#batch_x_negative = self.x_negative[idx*hbs : (idx+1)*hbs]
#
batch_x = np.r_[batch_x_positive, batch_x_negative]
#
batch_y = np.r_[np.ones(len(batch_x_positive)), np.zeros(len(batch_x_negative))]
return batch_x, batch_y

def __len__(self):
return math.ceil(2 * len(self.x_negative) / self.batch_size)

def _shuffle(self):
self.x_negative = shuffle(self.x_negative)

def on_epoch_end(self):
self._shuffle()


data_gen = ImageSequence([train_positive, train_negative], batch_size=BATCH_SIZE)

history = model.fit_generator(
generator=data_gen,
use_multiprocessing=True,
validation_data=(x_valid, y_valid),
steps_per_epoch=2 * len(train_positive) / BATCH_SIZE,
epochs=30,
verbose=2,
callbacks=)


I wrote my environment(version info )



import tensorflow.keras
print(tensorflow.keras.__version__)
2.1.6-tf






tensorflow keras






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 9:41









RoadRollerRoadRoller

1




1












  • you are picking your batch using random indexes with the line idx_p = np.random.randint(0, self.x_positive.shape[0], hbs) what are you trying to shuffle with on_epoch_end() ?
    – Mete Han Kahraman
    Nov 13 '18 at 10:17












  • Thank you for your reply. and I'm sorry for confusing you. At first I tried to shuffle at "on_epoch_end()", but it was not called. so then I define "idx_n = np.random.randint(0, self.x_negative.shape[0], hbs)". I would like to shuffle x.negative at the end of epoch if I can, in order to get completeness of train data in each epoch.
    – RoadRoller
    Nov 13 '18 at 13:09










  • This might help: github.com/keras-team/keras/issues/11122
    – Mete Han Kahraman
    Nov 14 '18 at 5:09












  • Perhaps you should change math.ceil to math.floor in the __len__function.
    – Mete Han Kahraman
    Nov 14 '18 at 5:15












  • Thank you very much!! the problem has benn solved. I replaced math.ceil to math.floor. And I found that there was a difference of definition between steps_per_epoch and __len__.
    – RoadRoller
    Nov 15 '18 at 1:17




















  • you are picking your batch using random indexes with the line idx_p = np.random.randint(0, self.x_positive.shape[0], hbs) what are you trying to shuffle with on_epoch_end() ?
    – Mete Han Kahraman
    Nov 13 '18 at 10:17












  • Thank you for your reply. and I'm sorry for confusing you. At first I tried to shuffle at "on_epoch_end()", but it was not called. so then I define "idx_n = np.random.randint(0, self.x_negative.shape[0], hbs)". I would like to shuffle x.negative at the end of epoch if I can, in order to get completeness of train data in each epoch.
    – RoadRoller
    Nov 13 '18 at 13:09










  • This might help: github.com/keras-team/keras/issues/11122
    – Mete Han Kahraman
    Nov 14 '18 at 5:09












  • Perhaps you should change math.ceil to math.floor in the __len__function.
    – Mete Han Kahraman
    Nov 14 '18 at 5:15












  • Thank you very much!! the problem has benn solved. I replaced math.ceil to math.floor. And I found that there was a difference of definition between steps_per_epoch and __len__.
    – RoadRoller
    Nov 15 '18 at 1:17


















you are picking your batch using random indexes with the line idx_p = np.random.randint(0, self.x_positive.shape[0], hbs) what are you trying to shuffle with on_epoch_end() ?
– Mete Han Kahraman
Nov 13 '18 at 10:17






you are picking your batch using random indexes with the line idx_p = np.random.randint(0, self.x_positive.shape[0], hbs) what are you trying to shuffle with on_epoch_end() ?
– Mete Han Kahraman
Nov 13 '18 at 10:17














Thank you for your reply. and I'm sorry for confusing you. At first I tried to shuffle at "on_epoch_end()", but it was not called. so then I define "idx_n = np.random.randint(0, self.x_negative.shape[0], hbs)". I would like to shuffle x.negative at the end of epoch if I can, in order to get completeness of train data in each epoch.
– RoadRoller
Nov 13 '18 at 13:09




Thank you for your reply. and I'm sorry for confusing you. At first I tried to shuffle at "on_epoch_end()", but it was not called. so then I define "idx_n = np.random.randint(0, self.x_negative.shape[0], hbs)". I would like to shuffle x.negative at the end of epoch if I can, in order to get completeness of train data in each epoch.
– RoadRoller
Nov 13 '18 at 13:09












This might help: github.com/keras-team/keras/issues/11122
– Mete Han Kahraman
Nov 14 '18 at 5:09






This might help: github.com/keras-team/keras/issues/11122
– Mete Han Kahraman
Nov 14 '18 at 5:09














Perhaps you should change math.ceil to math.floor in the __len__function.
– Mete Han Kahraman
Nov 14 '18 at 5:15






Perhaps you should change math.ceil to math.floor in the __len__function.
– Mete Han Kahraman
Nov 14 '18 at 5:15














Thank you very much!! the problem has benn solved. I replaced math.ceil to math.floor. And I found that there was a difference of definition between steps_per_epoch and __len__.
– RoadRoller
Nov 15 '18 at 1:17






Thank you very much!! the problem has benn solved. I replaced math.ceil to math.floor. And I found that there was a difference of definition between steps_per_epoch and __len__.
– RoadRoller
Nov 15 '18 at 1:17














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278007%2fid-like-to-call-on-epoch-end-at-each-end-of-epoch-keras-custom-generator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278007%2fid-like-to-call-on-epoch-end-at-each-end-of-epoch-keras-custom-generator%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine