本文整理匯總了Python中keras.utils.Sequence方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.Sequence方法的具體用法?Python utils.Sequence怎麽用?Python utils.Sequence使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類keras.utils
的用法示例。
在下文中一共展示了utils.Sequence方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from keras import utils [as 別名]
# 或者: from keras.utils import Sequence [as 別名]
def __init__(self, X, y, batch_size, process_fn=None):
"""A `Sequence` implementation that returns balanced `y` by undersampling majority class.
Args:
X: The numpy array of inputs.
y: The numpy array of targets.
batch_size: The generator mini-batch size.
process_fn: The preprocessing function to apply on `X`
"""
self.X = X
self.y = y
self.batch_size = batch_size
self.process_fn = process_fn or (lambda x: x)
self.pos_indices = np.where(y == 1)[0]
self.neg_indices = np.where(y == 0)[0]
self.n = min(len(self.pos_indices), len(self.neg_indices))
self._index_array = None
示例2: test_warnings
# 需要導入模塊: from keras import utils [as 別名]
# 或者: from keras.utils import Sequence [as 別名]
def test_warnings():
a = Input(shape=(3,), name='input_a')
b = Input(shape=(3,), name='input_b')
a_2 = Dense(4, name='dense_1')(a)
dp = Dropout(0.5, name='dropout')
b_2 = dp(b)
model = Model([a, b], [a_2, b_2])
optimizer = 'rmsprop'
loss = 'mse'
loss_weights = [1., 0.5]
model.compile(optimizer, loss, metrics=[], loss_weights=loss_weights,
sample_weight_mode=None)
def gen_data(batch_sz):
while True:
yield ([np.random.random((batch_sz, 3)), np.random.random((batch_sz, 3))],
[np.random.random((batch_sz, 4)), np.random.random((batch_sz, 3))])
with pytest.warns(Warning) as w:
out = model.fit_generator(gen_data(4), steps_per_epoch=10, use_multiprocessing=True, workers=2)
warning_raised = any(['Sequence' in str(w_.message) for w_ in w])
assert warning_raised, 'No warning raised when using generator with processes.'
with pytest.warns(None) as w:
out = model.fit_generator(RandomSequence(3), steps_per_epoch=4, use_multiprocessing=True, workers=2)
assert all(['Sequence' not in str(w_.message) for w_ in w]), 'A warning was raised for Sequence.'
示例3: test_context_switch
# 需要導入模塊: from keras import utils [as 別名]
# 或者: from keras.utils import Sequence [as 別名]
def test_context_switch():
enqueuer = OrderedEnqueuer(DummySequence([3, 200, 200, 3]), use_multiprocessing=True)
enqueuer2 = OrderedEnqueuer(DummySequence([3, 200, 200, 3], value=15), use_multiprocessing=True)
enqueuer.start(3, 10)
enqueuer2.start(3, 10)
gen_output = enqueuer.get()
gen_output2 = enqueuer2.get()
acc = []
for i in range(100):
acc.append(next(gen_output)[0, 0, 0, 0])
assert acc[-1] == 99
# One epoch is completed so enqueuer will switch the Sequence
acc = []
for i in range(100):
acc.append(next(gen_output2)[0, 0, 0, 0])
assert acc[-1] == 99 * 15
# One epoch has been completed so enqueuer2 will switch
# Be sure that both Sequence were updated
assert next(gen_output)[0, 0, 0, 0] == 0
assert next(gen_output)[0, 0, 0, 0] == 5
assert next(gen_output2)[0, 0, 0, 0] == 0
assert next(gen_output2)[0, 0, 0, 0] == 15 * 5
# Tear down everything
enqueuer.stop()
enqueuer2.stop()
示例4: __len__
# 需要導入模塊: from keras import utils [as 別名]
# 或者: from keras.utils import Sequence [as 別名]
def __len__(self):
"""Number of batches in the Sequence."""
return int(np.ceil(len(self.samples) / self.batch_size))