本文整理汇总了Python中keras.engine.training.Model.predict_generator方法的典型用法代码示例。如果您正苦于以下问题:Python Model.predict_generator方法的具体用法?Python Model.predict_generator怎么用?Python Model.predict_generator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.engine.training.Model
的用法示例。
在下文中一共展示了Model.predict_generator方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_model_methods
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import predict_generator [as 别名]
#.........这里部分代码省略.........
model.compile(optimizer, loss='mse', sample_weight_mode={'lstm': 'temporal'})
# `sample_weight_mode` does not match output_names.
with pytest.raises(ValueError):
model.compile(optimizer, loss='mse', sample_weight_mode=['temporal'])
# `sample_weight_mode` matches output_names partially.
with pytest.raises(ValueError):
model.compile(optimizer, loss='mse', sample_weight_mode={'dense_1': 'temporal'})
# `loss` does not exist.
with pytest.raises(ValueError):
model.compile(optimizer, loss=[])
model.compile(optimizer, loss=['mse', 'mae'])
model.compile(optimizer, loss='mse', loss_weights={'dense_1': 0.2, 'dropout': 0.8})
model.compile(optimizer, loss='mse', loss_weights=[0.2, 0.8])
# the rank of weight arrays should be 1.
with pytest.raises(ValueError):
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np],
sample_weight=[None, np.random.random((10, 20, 30))])
model.compile(optimizer, loss='mse', sample_weight_mode={'dense_1': None, 'dropout': 'temporal'})
model.compile(optimizer, loss='mse', sample_weight_mode=[None, 'temporal'])
# the rank of output arrays should be at least 3D.
with pytest.raises(ValueError):
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np],
sample_weight=sample_weight)
model.compile(optimizer, loss, metrics=[], loss_weights=loss_weights,
sample_weight_mode=None)
trained_epochs = []
trained_batches = []
out = model.fit_generator(generator=RandomSequence(3), steps_per_epoch=3, epochs=5,
initial_epoch=0, validation_data=RandomSequence(4),
validation_steps=3, callbacks=[tracker_cb])
assert trained_epochs == [0, 1, 2, 3, 4]
assert trained_batches == list(range(3)) * 5
# steps_per_epoch will be equal to len of sequence if it's unspecified
trained_epochs = []
trained_batches = []
out = model.fit_generator(generator=RandomSequence(3), epochs=5,
initial_epoch=0, validation_data=RandomSequence(4),
callbacks=[tracker_cb])
assert trained_epochs == [0, 1, 2, 3, 4]
assert trained_batches == list(range(12)) * 5
# fit_generator will throw an exception if steps is unspecified for regular generator
with pytest.raises(ValueError):
def gen_data():
while True:
yield (np.asarray([]), np.asarray([]))
out = model.fit_generator(generator=gen_data(), epochs=5,
initial_epoch=0, validation_data=gen_data(),
callbacks=[tracker_cb])
# predict_generator output shape behavior should be consistent
def expected_shape(batch_size, n_batches):
return (batch_size * n_batches, 4), (batch_size * n_batches, 3)
# Multiple outputs and one step.
batch_size = 5
sequence_length = 1
shape_0, shape_1 = expected_shape(batch_size, sequence_length)
out = model.predict_generator(RandomSequence(batch_size,
sequence_length=sequence_length))
assert np.shape(out[0]) == shape_0 and np.shape(out[1]) == shape_1
# Multiple outputs and multiple steps.
batch_size = 5
sequence_length = 2
shape_0, shape_1 = expected_shape(batch_size, sequence_length)
out = model.predict_generator(RandomSequence(batch_size,
sequence_length=sequence_length))
assert np.shape(out[0]) == shape_0 and np.shape(out[1]) == shape_1
# Create a model with a single output.
single_output_model = Model([a, b], a_2)
single_output_model.compile(optimizer, loss, metrics=[], sample_weight_mode=None)
# Single output and one step.
batch_size = 5
sequence_length = 1
shape_0, _ = expected_shape(batch_size, sequence_length)
out = single_output_model.predict_generator(RandomSequence(batch_size,
sequence_length=sequence_length))
assert np.shape(out) == shape_0
# Single output and multiple steps.
batch_size = 5
sequence_length = 2
shape_0, _ = expected_shape(batch_size, sequence_length)
out = single_output_model.predict_generator(RandomSequence(batch_size,
sequence_length=sequence_length))
assert np.shape(out) == shape_0