本文整理汇总了Python中keras.models.Sequential.train_on_batch方法的典型用法代码示例。如果您正苦于以下问题:Python Sequential.train_on_batch方法的具体用法?Python Sequential.train_on_batch怎么用?Python Sequential.train_on_batch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.models.Sequential
的用法示例。
在下文中一共展示了Sequential.train_on_batch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_sequential_model_saving_2
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_sequential_model_saving_2():
# test with funkier config
model = Sequential()
model.add(Dense(2, input_dim=3))
model.add(RepeatVector(3))
model.add(TimeDistributed(Dense(3)))
model.compile(loss=objectives.MSE,
optimizer=optimizers.RMSprop(lr=0.0001),
metrics=[metrics.categorical_accuracy],
sample_weight_mode='temporal')
x = np.random.random((1, 3))
y = np.random.random((1, 3, 3))
model.train_on_batch(x, y)
out = model.predict(x)
fname = 'tmp_' + str(np.random.randint(10000)) + '.h5'
save_model(model, fname)
new_model = load_model(fname)
os.remove(fname)
out2 = new_model.predict(x)
assert_allclose(out, out2, atol=1e-05)
# test that new updates are the same with both models
x = np.random.random((1, 3))
y = np.random.random((1, 3, 3))
model.train_on_batch(x, y)
new_model.train_on_batch(x, y)
out = model.predict(x)
out2 = new_model.predict(x)
assert_allclose(out, out2, atol=1e-05)
示例2: test_nested_sequential
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_nested_sequential():
(X_train, y_train), (X_test, y_test) = _get_test_data()
inner = Sequential()
inner.add(Dense(nb_hidden, input_shape=(input_dim,)))
inner.add(Activation("relu"))
inner.add(Dense(nb_class))
middle = Sequential()
middle.add(inner)
model = Sequential()
model.add(middle)
model.add(Activation("softmax"))
model.compile(loss="categorical_crossentropy", optimizer="rmsprop")
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, y_test))
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_split=0.1)
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)
model.train_on_batch(X_train[:32], y_train[:32])
loss = model.evaluate(X_test, y_test, verbose=0)
model.predict(X_test, verbose=0)
model.predict_classes(X_test, verbose=0)
model.predict_proba(X_test, verbose=0)
fname = "test_nested_sequential_temp.h5"
model.save_weights(fname, overwrite=True)
inner = Sequential()
inner.add(Dense(nb_hidden, input_shape=(input_dim,)))
inner.add(Activation("relu"))
inner.add(Dense(nb_class))
middle = Sequential()
middle.add(inner)
model = Sequential()
model.add(middle)
model.add(Activation("softmax"))
model.compile(loss="categorical_crossentropy", optimizer="rmsprop")
model.load_weights(fname)
os.remove(fname)
nloss = model.evaluate(X_test, y_test, verbose=0)
assert loss == nloss
# test serialization
config = model.get_config()
new_model = Sequential.from_config(config)
model.summary()
json_str = model.to_json()
new_model = model_from_json(json_str)
yaml_str = model.to_yaml()
new_model = model_from_yaml(yaml_str)
示例3: test_sequential_model_saving
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_sequential_model_saving():
model = Sequential()
model.add(Dense(2, input_shape=(3,)))
model.add(RepeatVector(3))
model.add(TimeDistributed(Dense(3)))
model.compile(loss=losses.MSE,
optimizer=optimizers.RMSprop(lr=0.0001),
metrics=[metrics.categorical_accuracy],
sample_weight_mode='temporal')
x = np.random.random((1, 3))
y = np.random.random((1, 3, 3))
model.train_on_batch(x, y)
out = model.predict(x)
_, fname = tempfile.mkstemp('.h5')
save_model(model, fname)
new_model = load_model(fname)
os.remove(fname)
out2 = new_model.predict(x)
assert_allclose(out, out2, atol=1e-05)
# test that new updates are the same with both models
x = np.random.random((1, 3))
y = np.random.random((1, 3, 3))
model.train_on_batch(x, y)
new_model.train_on_batch(x, y)
out = model.predict(x)
out2 = new_model.predict(x)
assert_allclose(out, out2, atol=1e-05)
示例4: test_nested_sequential
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_nested_sequential(in_tmpdir):
(x_train, y_train), (x_test, y_test) = _get_test_data()
inner = Sequential()
inner.add(Dense(num_hidden, input_shape=(input_dim,)))
inner.add(Activation('relu'))
inner.add(Dense(num_class))
middle = Sequential()
middle.add(inner)
model = Sequential()
model.add(middle)
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2, validation_split=0.1)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=0)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, shuffle=False)
model.train_on_batch(x_train[:32], y_train[:32])
loss = model.evaluate(x_test, y_test, verbose=0)
model.predict(x_test, verbose=0)
model.predict_classes(x_test, verbose=0)
model.predict_proba(x_test, verbose=0)
fname = 'test_nested_sequential_temp.h5'
model.save_weights(fname, overwrite=True)
inner = Sequential()
inner.add(Dense(num_hidden, input_shape=(input_dim,)))
inner.add(Activation('relu'))
inner.add(Dense(num_class))
middle = Sequential()
middle.add(inner)
model = Sequential()
model.add(middle)
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.load_weights(fname)
os.remove(fname)
nloss = model.evaluate(x_test, y_test, verbose=0)
assert(loss == nloss)
# test serialization
config = model.get_config()
Sequential.from_config(config)
model.summary()
json_str = model.to_json()
model_from_json(json_str)
yaml_str = model.to_yaml()
model_from_yaml(yaml_str)
示例5: test_sequential_model_saving_2
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_sequential_model_saving_2():
# test with custom optimizer, loss
custom_opt = optimizers.rmsprop
custom_loss = losses.mse
model = Sequential()
model.add(Dense(2, input_shape=(3,)))
model.add(Dense(3))
model.compile(loss=custom_loss, optimizer=custom_opt(), metrics=['acc'])
x = np.random.random((1, 3))
y = np.random.random((1, 3))
model.train_on_batch(x, y)
out = model.predict(x)
load_kwargs = {'custom_objects': {'custom_opt': custom_opt,
'custom_loss': custom_loss}}
_, fname = tempfile.mkstemp('.h5')
save_model(model, fname)
new_model_disk = load_model(fname, **load_kwargs)
os.remove(fname)
with tf_file_io_proxy('keras.engine.saving.tf_file_io') as file_io_proxy:
gcs_filepath = file_io_proxy.get_filepath(filename=fname)
save_model(model, gcs_filepath)
file_io_proxy.assert_exists(gcs_filepath)
new_model_gcs = load_model(gcs_filepath, **load_kwargs)
file_io_proxy.delete_file(gcs_filepath) # cleanup
for new_model in [new_model_disk, new_model_gcs]:
new_out = new_model.predict(x)
assert_allclose(out, new_out, atol=1e-05)
示例6: test_sequential_model_saving
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_sequential_model_saving():
model = Sequential()
model.add(Dense(2, input_dim=3))
model.add(Dense(3))
model.compile(loss='mse', optimizer='rmsprop', metrics=['acc'])
x = np.random.random((1, 3))
y = np.random.random((1, 3))
model.train_on_batch(x, y)
out = model.predict(x)
fname = 'tmp_' + str(np.random.randint(10000)) + '.h5'
save_model(model, fname)
new_model = load_model(fname)
out2 = new_model.predict(x)
assert_allclose(out, out2, atol=1e-05)
# test that new updates are the same with both models
x = np.random.random((1, 3))
y = np.random.random((1, 3))
model.train_on_batch(x, y)
new_model.train_on_batch(x, y)
out = model.predict(x)
out2 = new_model.predict(x)
assert_allclose(out, out2, atol=1e-05)
# test load_weights on model file
model.load_weights(fname)
os.remove(fname)
示例7: test_merge_overlap
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_merge_overlap():
left = Sequential()
left.add(Dense(nb_hidden, input_shape=(input_dim,)))
left.add(Activation('relu'))
model = Sequential()
model.add(Merge([left, left], mode='sum'))
model.add(Dense(nb_class))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test))
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test))
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1)
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1)
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)
model.train_on_batch(X_train[:32], y_train[:32])
loss = model.evaluate(X_train, y_train, verbose=0)
assert(loss < 0.7)
model.predict(X_test, verbose=0)
model.predict_classes(X_test, verbose=0)
model.predict_proba(X_test, verbose=0)
model.get_config(verbose=0)
fname = 'test_merge_overlap_temp.h5'
model.save_weights(fname, overwrite=True)
model.load_weights(fname)
os.remove(fname)
nloss = model.evaluate(X_train, y_train, verbose=0)
assert(loss == nloss)
示例8: test_dynamic_behavior
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_dynamic_behavior(layer_class):
layer = layer_class(units, input_shape=(None, embedding_dim))
model = Sequential()
model.add(layer)
model.compile('sgd', 'mse')
x = np.random.random((num_samples, timesteps, embedding_dim))
y = np.random.random((num_samples, units))
model.train_on_batch(x, y)
示例9: test_dynamic_behavior
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_dynamic_behavior(layer_class):
layer = layer_class(output_dim, input_dim=embedding_dim)
model = Sequential()
model.add(layer)
model.compile('sgd', 'mse')
x = np.random.random((nb_samples, timesteps, embedding_dim))
y = np.random.random((nb_samples, output_dim))
model.train_on_batch(x, y)
示例10: test_with_list_as_targets
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_with_list_as_targets():
model = Sequential()
model.add(Dense(1, input_dim=3, trainable=False))
model.compile('rmsprop', 'mse')
x = np.random.random((2, 3))
y = [0, 1]
model.train_on_batch(x, y)
示例11: test_hierarchical_softmax
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_hierarchical_softmax(timesteps = 15, input_dim = 50, batch_size = 32,
output_dim = 3218, batches = 300, epochs = 30):
model = Graph()
model.add_input(name='real_input', batch_input_shape=(batch_size, timesteps, input_dim))
model.add_input(name='train_input', batch_input_shape=(batch_size, timesteps), dtype='int32')
model.add_node(HierarchicalSoftmax(output_dim, input_dim = input_dim, input_length = timesteps),
name = 'hs', inputs=['real_input','train_input'],
merge_mode = 'join', create_output=True)
model.compile(loss={'hs':hs_categorical_crossentropy}, optimizer='adam')
print "hs model compiled"
model2 = Sequential()
model2.add(TimeDistributedDense(output_dim,
batch_input_shape=(batch_size, timesteps, input_dim)))
model2.add(Activation('softmax'))
model2.compile(loss='categorical_crossentropy', optimizer='adam')
print "softmax model compiled"
learn_f = np.random.normal(size = (input_dim, output_dim))
learn_f = np.divide(learn_f, norm(learn_f, axis=1)[:,None])
print "learn_f generated"
for j in range(epochs):
batch_data= generate_batch(learn_f, batch_size,
timesteps, input_dim, output_dim, batches)
print "Epoch", j, "data genrated"
p = Progbar(batches * batch_size)
for b in batch_data:
data_train = {'real_input': b[0], 'train_input': b[1], 'hs':b[2]}
loss = float(model.train_on_batch(data_train)[0])
p.add(batch_size,[('hs_loss', loss)])
p2 = Progbar(batches * batch_size)
for b in batch_data:
loss, acc = model2.train_on_batch(b[0], b[3], accuracy=True)
p2.add(batch_size,[('softmax_loss', loss),('softmax_acc', acc)])
test_data = generate_batch(learn_f, batch_size,
timesteps, input_dim, output_dim, batches)
p = Progbar(batches * batch_size)
for b in test_data:
data_test = {'real_input': b[0], 'train_input': b[1], 'hs':b[3]}
loss = float(model.test_on_batch(data_test)[0])
p.add(batch_size,[('hs__test_loss', loss)])
p2 = Progbar(batches * batch_size)
for b in batch_data:
loss = float(model2.train_on_batch(b[0], b[3])[0])
p2.add(batch_size,[('softmax_loss', loss)])
示例12: test_loading_weights_by_name_2
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_loading_weights_by_name_2():
"""
test loading model weights by name on:
- both sequential and functional api models
- different architecture with shared names
"""
# test with custom optimizer, loss
custom_opt = optimizers.rmsprop
custom_loss = losses.mse
# sequential model
model = Sequential()
model.add(Dense(2, input_shape=(3,), name='rick'))
model.add(Dense(3, name='morty'))
model.compile(loss=custom_loss, optimizer=custom_opt(), metrics=['acc'])
x = np.random.random((1, 3))
y = np.random.random((1, 3))
model.train_on_batch(x, y)
out = model.predict(x)
old_weights = [layer.get_weights() for layer in model.layers]
_, fname = tempfile.mkstemp('.h5')
model.save_weights(fname)
# delete and recreate model using Functional API
del(model)
data = Input(shape=(3,))
rick = Dense(2, name='rick')(data)
jerry = Dense(3, name='jerry')(rick) # add 2 layers (but maintain shapes)
jessica = Dense(2, name='jessica')(jerry)
morty = Dense(3, name='morty')(jessica)
model = Model(inputs=[data], outputs=[morty])
model.compile(loss=custom_loss, optimizer=custom_opt(), metrics=['acc'])
# load weights from first model
model.load_weights(fname, by_name=True)
os.remove(fname)
out2 = model.predict(x)
assert np.max(np.abs(out - out2)) > 1e-05
rick = model.layers[1].get_weights()
jerry = model.layers[2].get_weights()
jessica = model.layers[3].get_weights()
morty = model.layers[4].get_weights()
assert_allclose(old_weights[0][0], rick[0], atol=1e-05)
assert_allclose(old_weights[0][1], rick[1], atol=1e-05)
assert_allclose(old_weights[1][0], morty[0], atol=1e-05)
assert_allclose(old_weights[1][1], morty[1], atol=1e-05)
assert_allclose(np.zeros_like(jerry[1]), jerry[1]) # biases init to 0
assert_allclose(np.zeros_like(jessica[1]), jessica[1]) # biases init to 0
示例13: test_unitnorm_constraint
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_unitnorm_constraint(self):
lookup = Sequential()
lookup.add(Embedding(3, 2, weights=[self.W1], W_constraint=unitnorm(), input_length=1))
lookup.add(Flatten())
lookup.add(Dense(1))
lookup.add(Activation('sigmoid'))
lookup.compile(loss='binary_crossentropy', optimizer='sgd', class_mode='binary')
lookup.train_on_batch(self.X1, np.array([[1], [0]], dtype='int32'))
norm = np.linalg.norm(lookup.params[0].get_value(), axis=1)
self.assertTrue(np.allclose(norm, np.ones_like(norm).astype('float32')))
示例14: _runner
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def _runner(layer_class):
"""
All the recurrent layers share the same interface,
so we can run through them with a single function.
"""
for ret_seq in [True, False]:
layer = layer_class(output_dim, return_sequences=ret_seq,
weights=None, input_shape=(timesteps, input_dim))
layer.input = K.variable(np.ones((nb_samples, timesteps, input_dim)))
layer.get_config()
for train in [True, False]:
out = K.eval(layer.get_output(train))
# Make sure the output has the desired shape
if ret_seq:
assert(out.shape == (nb_samples, timesteps, output_dim))
else:
assert(out.shape == (nb_samples, output_dim))
mask = layer.get_output_mask(train)
# check statefulness
layer = layer_class(output_dim, return_sequences=False,
stateful=True,
weights=None,
batch_input_shape=(nb_samples, timesteps, input_dim))
model = Sequential()
model.add(layer)
model.compile(optimizer='sgd', loss='mse')
out1 = model.predict(np.ones((nb_samples, timesteps, input_dim)))
assert(out1.shape == (nb_samples, output_dim))
# train once so that the states change
model.train_on_batch(np.ones((nb_samples, timesteps, input_dim)),
np.ones((nb_samples, output_dim)))
out2 = model.predict(np.ones((nb_samples, timesteps, input_dim)))
# if the state is not reset, output should be different
assert(out1.max() != out2.max())
# check that output changes after states are reset
# (even though the model itself didn't change)
layer.reset_states()
out3 = model.predict(np.ones((nb_samples, timesteps, input_dim)))
assert(out2.max() != out3.max())
# check that container-level reset_states() works
model.reset_states()
out4 = model.predict(np.ones((nb_samples, timesteps, input_dim)))
assert_allclose(out3, out4, atol=1e-5)
# check that the call to `predict` updated the states
out5 = model.predict(np.ones((nb_samples, timesteps, input_dim)))
assert(out4.max() != out5.max())
示例15: test_linear_in_bounds_regularizer
# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import train_on_batch [as 别名]
def test_linear_in_bounds_regularizer():
model = Sequential()
model.add(LinearInBounds(-1, 1, clip=True, input_shape=(1,)))
model.compile('adam', 'mse')
loss = model.train_on_batch(np.array([[0]]), np.array([[0]]))
assert float(loss) == 0
loss_on_2 = model.train_on_batch(np.array([[2]]), np.array([[1]]))
assert float(loss_on_2) > 0
loss_on_100 = model.train_on_batch(np.array([[100]]), np.array([[1]]))
assert float(loss_on_2) < float(loss_on_100)