How to use Keras' evaluate_generator() when “Axis must be specified…”












0















Why is Keras telling me weights and batch sizes are different? How can I fix this? (adding next(...) does not help here).
Thanks in advance; there's something I'm just not getting here.



Error is on evaluate_generator(): TypeError: Axis must be specified when shapes of a and weights differ.



from sklearn.utils import shuffle as identical_shuffle

SAMPLES_PER_BATCH=1
BATCHES_PER_EPOCH=1
BATCHES_PER_VALIDATION=1
EPOCHS_PER_SIMULATION=2

def generate_training(batch_size=64):
validation_length = (int(len(data.train)*0.25) // batch_size) * batch_size
while True:
for i in range(validation_length,len(data.train),batch_size):
x,y,n = identical_shuffle(data.train[i:i+batch_size],data.target[i:i+batch_size],data.context[i:i+batch_size])
yield {'input':x,'target':n}, y

def generate_validation(batch_size=64):
validation_length = (int(len(data.train)*0.25) // batch_size) * batch_size
while True:
for i in range(0,validation_length,batch_size):
x,y,n = identical_shuffle(data.train[i:i+batch_size],data.target[i:i+batch_size],data.context[i:i+batch_size])
yield {'input':x,'target':n}, y

for epoch in range(EPOCHS_PER_SIMULATION):
for batch in range(BATCHES_PER_EPOCH):
result_training = model.train_on_batch( *next(generate_training(batch_size=SAMPLES_PER_BATCH)) )
# <redacted operations on result_training>
result_validation = model.evaluate_generator( generate_validation(batch_size=SAMPLES_PER_BATCH), steps=BATCHES_PER_VALIDATION )


For comparison, the below runs okay with the same generator.



model.fit_generator(generator=generate_training(batch_size=SAMPLES_PER_BATCH),
validation_data=next(generate_validation(batch_size=SAMPLES_PER_BATCH)),
validation_steps=1,
steps_per_epoch=1,
epochs=1)


Full Traceback



TypeError                                 Traceback (most recent call last)
<ipython-input-37-962a23d537c3> in <module>()
79 print(result_training)
80 model.reset_states()
---> 81 result_validation = model.evaluate_generator( generate_validation(batch_size=SAMPLES_PER_BATCH), steps=BATCHES_PER_VALIDATION )
82 #result_validation = model.test_on_batch( *next(generate_validation(batch_size=SAMPLES_PER_BATCH)) )
83 print("VALIDATION OF EPOCH "+str(epoch))

/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in evaluate_generator(self, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
1470 workers=workers,
1471 use_multiprocessing=use_multiprocessing,
-> 1472 verbose=verbose)
1473
1474 @interfaces.legacy_generator_methods_support

/usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py in evaluate_generator(model, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
375 if i not in stateful_metric_indices:
376 averages.append(np.average([out[i] for out in outs_per_batch],
--> 377 weights=batch_sizes))
378 else:
379 averages.append(np.float64(outs_per_batch[-1][i]))

/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in average(a, axis, weights, returned)
1140 if axis is None:
1141 raise TypeError(
-> 1142 "Axis must be specified when shapes of a and weights "
1143 "differ.")
1144 if wgt.ndim != 1:

TypeError: Axis must be specified when shapes of a and weights differ.









share|improve this question

























  • Can you post your generate_training code?

    – Dinari
    Nov 25 '18 at 7:20











  • Please add a full traceback, from just an error message we can't tell you what is wrong. Also it would be useful if you can add more code.

    – Matias Valdenegro
    Nov 25 '18 at 14:21











  • Thanks for the comments; I added constants, generator fns, and full traceback.

    – GGibson
    Nov 25 '18 at 14:28
















0















Why is Keras telling me weights and batch sizes are different? How can I fix this? (adding next(...) does not help here).
Thanks in advance; there's something I'm just not getting here.



Error is on evaluate_generator(): TypeError: Axis must be specified when shapes of a and weights differ.



from sklearn.utils import shuffle as identical_shuffle

SAMPLES_PER_BATCH=1
BATCHES_PER_EPOCH=1
BATCHES_PER_VALIDATION=1
EPOCHS_PER_SIMULATION=2

def generate_training(batch_size=64):
validation_length = (int(len(data.train)*0.25) // batch_size) * batch_size
while True:
for i in range(validation_length,len(data.train),batch_size):
x,y,n = identical_shuffle(data.train[i:i+batch_size],data.target[i:i+batch_size],data.context[i:i+batch_size])
yield {'input':x,'target':n}, y

def generate_validation(batch_size=64):
validation_length = (int(len(data.train)*0.25) // batch_size) * batch_size
while True:
for i in range(0,validation_length,batch_size):
x,y,n = identical_shuffle(data.train[i:i+batch_size],data.target[i:i+batch_size],data.context[i:i+batch_size])
yield {'input':x,'target':n}, y

for epoch in range(EPOCHS_PER_SIMULATION):
for batch in range(BATCHES_PER_EPOCH):
result_training = model.train_on_batch( *next(generate_training(batch_size=SAMPLES_PER_BATCH)) )
# <redacted operations on result_training>
result_validation = model.evaluate_generator( generate_validation(batch_size=SAMPLES_PER_BATCH), steps=BATCHES_PER_VALIDATION )


For comparison, the below runs okay with the same generator.



model.fit_generator(generator=generate_training(batch_size=SAMPLES_PER_BATCH),
validation_data=next(generate_validation(batch_size=SAMPLES_PER_BATCH)),
validation_steps=1,
steps_per_epoch=1,
epochs=1)


Full Traceback



TypeError                                 Traceback (most recent call last)
<ipython-input-37-962a23d537c3> in <module>()
79 print(result_training)
80 model.reset_states()
---> 81 result_validation = model.evaluate_generator( generate_validation(batch_size=SAMPLES_PER_BATCH), steps=BATCHES_PER_VALIDATION )
82 #result_validation = model.test_on_batch( *next(generate_validation(batch_size=SAMPLES_PER_BATCH)) )
83 print("VALIDATION OF EPOCH "+str(epoch))

/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in evaluate_generator(self, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
1470 workers=workers,
1471 use_multiprocessing=use_multiprocessing,
-> 1472 verbose=verbose)
1473
1474 @interfaces.legacy_generator_methods_support

/usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py in evaluate_generator(model, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
375 if i not in stateful_metric_indices:
376 averages.append(np.average([out[i] for out in outs_per_batch],
--> 377 weights=batch_sizes))
378 else:
379 averages.append(np.float64(outs_per_batch[-1][i]))

/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in average(a, axis, weights, returned)
1140 if axis is None:
1141 raise TypeError(
-> 1142 "Axis must be specified when shapes of a and weights "
1143 "differ.")
1144 if wgt.ndim != 1:

TypeError: Axis must be specified when shapes of a and weights differ.









share|improve this question

























  • Can you post your generate_training code?

    – Dinari
    Nov 25 '18 at 7:20











  • Please add a full traceback, from just an error message we can't tell you what is wrong. Also it would be useful if you can add more code.

    – Matias Valdenegro
    Nov 25 '18 at 14:21











  • Thanks for the comments; I added constants, generator fns, and full traceback.

    – GGibson
    Nov 25 '18 at 14:28














0












0








0








Why is Keras telling me weights and batch sizes are different? How can I fix this? (adding next(...) does not help here).
Thanks in advance; there's something I'm just not getting here.



Error is on evaluate_generator(): TypeError: Axis must be specified when shapes of a and weights differ.



from sklearn.utils import shuffle as identical_shuffle

SAMPLES_PER_BATCH=1
BATCHES_PER_EPOCH=1
BATCHES_PER_VALIDATION=1
EPOCHS_PER_SIMULATION=2

def generate_training(batch_size=64):
validation_length = (int(len(data.train)*0.25) // batch_size) * batch_size
while True:
for i in range(validation_length,len(data.train),batch_size):
x,y,n = identical_shuffle(data.train[i:i+batch_size],data.target[i:i+batch_size],data.context[i:i+batch_size])
yield {'input':x,'target':n}, y

def generate_validation(batch_size=64):
validation_length = (int(len(data.train)*0.25) // batch_size) * batch_size
while True:
for i in range(0,validation_length,batch_size):
x,y,n = identical_shuffle(data.train[i:i+batch_size],data.target[i:i+batch_size],data.context[i:i+batch_size])
yield {'input':x,'target':n}, y

for epoch in range(EPOCHS_PER_SIMULATION):
for batch in range(BATCHES_PER_EPOCH):
result_training = model.train_on_batch( *next(generate_training(batch_size=SAMPLES_PER_BATCH)) )
# <redacted operations on result_training>
result_validation = model.evaluate_generator( generate_validation(batch_size=SAMPLES_PER_BATCH), steps=BATCHES_PER_VALIDATION )


For comparison, the below runs okay with the same generator.



model.fit_generator(generator=generate_training(batch_size=SAMPLES_PER_BATCH),
validation_data=next(generate_validation(batch_size=SAMPLES_PER_BATCH)),
validation_steps=1,
steps_per_epoch=1,
epochs=1)


Full Traceback



TypeError                                 Traceback (most recent call last)
<ipython-input-37-962a23d537c3> in <module>()
79 print(result_training)
80 model.reset_states()
---> 81 result_validation = model.evaluate_generator( generate_validation(batch_size=SAMPLES_PER_BATCH), steps=BATCHES_PER_VALIDATION )
82 #result_validation = model.test_on_batch( *next(generate_validation(batch_size=SAMPLES_PER_BATCH)) )
83 print("VALIDATION OF EPOCH "+str(epoch))

/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in evaluate_generator(self, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
1470 workers=workers,
1471 use_multiprocessing=use_multiprocessing,
-> 1472 verbose=verbose)
1473
1474 @interfaces.legacy_generator_methods_support

/usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py in evaluate_generator(model, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
375 if i not in stateful_metric_indices:
376 averages.append(np.average([out[i] for out in outs_per_batch],
--> 377 weights=batch_sizes))
378 else:
379 averages.append(np.float64(outs_per_batch[-1][i]))

/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in average(a, axis, weights, returned)
1140 if axis is None:
1141 raise TypeError(
-> 1142 "Axis must be specified when shapes of a and weights "
1143 "differ.")
1144 if wgt.ndim != 1:

TypeError: Axis must be specified when shapes of a and weights differ.









share|improve this question
















Why is Keras telling me weights and batch sizes are different? How can I fix this? (adding next(...) does not help here).
Thanks in advance; there's something I'm just not getting here.



Error is on evaluate_generator(): TypeError: Axis must be specified when shapes of a and weights differ.



from sklearn.utils import shuffle as identical_shuffle

SAMPLES_PER_BATCH=1
BATCHES_PER_EPOCH=1
BATCHES_PER_VALIDATION=1
EPOCHS_PER_SIMULATION=2

def generate_training(batch_size=64):
validation_length = (int(len(data.train)*0.25) // batch_size) * batch_size
while True:
for i in range(validation_length,len(data.train),batch_size):
x,y,n = identical_shuffle(data.train[i:i+batch_size],data.target[i:i+batch_size],data.context[i:i+batch_size])
yield {'input':x,'target':n}, y

def generate_validation(batch_size=64):
validation_length = (int(len(data.train)*0.25) // batch_size) * batch_size
while True:
for i in range(0,validation_length,batch_size):
x,y,n = identical_shuffle(data.train[i:i+batch_size],data.target[i:i+batch_size],data.context[i:i+batch_size])
yield {'input':x,'target':n}, y

for epoch in range(EPOCHS_PER_SIMULATION):
for batch in range(BATCHES_PER_EPOCH):
result_training = model.train_on_batch( *next(generate_training(batch_size=SAMPLES_PER_BATCH)) )
# <redacted operations on result_training>
result_validation = model.evaluate_generator( generate_validation(batch_size=SAMPLES_PER_BATCH), steps=BATCHES_PER_VALIDATION )


For comparison, the below runs okay with the same generator.



model.fit_generator(generator=generate_training(batch_size=SAMPLES_PER_BATCH),
validation_data=next(generate_validation(batch_size=SAMPLES_PER_BATCH)),
validation_steps=1,
steps_per_epoch=1,
epochs=1)


Full Traceback



TypeError                                 Traceback (most recent call last)
<ipython-input-37-962a23d537c3> in <module>()
79 print(result_training)
80 model.reset_states()
---> 81 result_validation = model.evaluate_generator( generate_validation(batch_size=SAMPLES_PER_BATCH), steps=BATCHES_PER_VALIDATION )
82 #result_validation = model.test_on_batch( *next(generate_validation(batch_size=SAMPLES_PER_BATCH)) )
83 print("VALIDATION OF EPOCH "+str(epoch))

/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in evaluate_generator(self, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
1470 workers=workers,
1471 use_multiprocessing=use_multiprocessing,
-> 1472 verbose=verbose)
1473
1474 @interfaces.legacy_generator_methods_support

/usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py in evaluate_generator(model, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
375 if i not in stateful_metric_indices:
376 averages.append(np.average([out[i] for out in outs_per_batch],
--> 377 weights=batch_sizes))
378 else:
379 averages.append(np.float64(outs_per_batch[-1][i]))

/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in average(a, axis, weights, returned)
1140 if axis is None:
1141 raise TypeError(
-> 1142 "Axis must be specified when shapes of a and weights "
1143 "differ.")
1144 if wgt.ndim != 1:

TypeError: Axis must be specified when shapes of a and weights differ.






numpy keras






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 14:37







GGibson

















asked Nov 25 '18 at 5:50









GGibsonGGibson

11915




11915













  • Can you post your generate_training code?

    – Dinari
    Nov 25 '18 at 7:20











  • Please add a full traceback, from just an error message we can't tell you what is wrong. Also it would be useful if you can add more code.

    – Matias Valdenegro
    Nov 25 '18 at 14:21











  • Thanks for the comments; I added constants, generator fns, and full traceback.

    – GGibson
    Nov 25 '18 at 14:28



















  • Can you post your generate_training code?

    – Dinari
    Nov 25 '18 at 7:20











  • Please add a full traceback, from just an error message we can't tell you what is wrong. Also it would be useful if you can add more code.

    – Matias Valdenegro
    Nov 25 '18 at 14:21











  • Thanks for the comments; I added constants, generator fns, and full traceback.

    – GGibson
    Nov 25 '18 at 14:28

















Can you post your generate_training code?

– Dinari
Nov 25 '18 at 7:20





Can you post your generate_training code?

– Dinari
Nov 25 '18 at 7:20













Please add a full traceback, from just an error message we can't tell you what is wrong. Also it would be useful if you can add more code.

– Matias Valdenegro
Nov 25 '18 at 14:21





Please add a full traceback, from just an error message we can't tell you what is wrong. Also it would be useful if you can add more code.

– Matias Valdenegro
Nov 25 '18 at 14:21













Thanks for the comments; I added constants, generator fns, and full traceback.

– GGibson
Nov 25 '18 at 14:28





Thanks for the comments; I added constants, generator fns, and full traceback.

– GGibson
Nov 25 '18 at 14:28












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%2f53465006%2fhow-to-use-keras-evaluate-generator-when-axis-must-be-specified%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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465006%2fhow-to-use-keras-evaluate-generator-when-axis-must-be-specified%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

Wiesbaden

Marschland

Dieringhausen