本文整理汇总了Python中keras.engine.training.Model.train_on_batch方法的典型用法代码示例。如果您正苦于以下问题:Python Model.train_on_batch方法的具体用法?Python Model.train_on_batch怎么用?Python Model.train_on_batch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.engine.training.Model
的用法示例。
在下文中一共展示了Model.train_on_batch方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_model_multiple_calls
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_model_multiple_calls():
x1 = Input(shape=(20,))
y1 = sequential([
Dense(10),
Dense(1),
])(x1)
m1 = Model(x1, y1)
x2 = Input(shape=(25,))
y2 = sequential([
Dense(20),
m1
])(x2)
m2 = Model(x2, y2)
m2.compile('adam', 'mse')
x3 = Input(shape=(20,))
y3 = sequential([
Dense(25),
m2
])(x3)
m3 = Model(x3, y3)
m3.compile('adam', 'mse')
m3.train_on_batch(np.zeros((32, 20)), np.zeros((32, 1)))
示例2: test_model_custom_target_tensors
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_model_custom_target_tensors():
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)
y = K.placeholder([10, 4], name='y')
y1 = K.placeholder([10, 3], name='y1')
y2 = K.placeholder([7, 5], name='y2')
model = Model([a, b], [a_2, b_2])
optimizer = 'rmsprop'
loss = 'mse'
loss_weights = [1., 0.5]
# test list of target tensors
with pytest.raises(ValueError):
model.compile(optimizer, loss, metrics=[], loss_weights=loss_weights,
sample_weight_mode=None, target_tensors=[y, y1, y2])
model.compile(optimizer, loss, metrics=[], loss_weights=loss_weights,
sample_weight_mode=None, target_tensors=[y, y1])
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np],
{y: np.random.random((10, 4)),
y1: np.random.random((10, 3))})
# test dictionary of target_tensors
with pytest.raises(ValueError):
model.compile(optimizer, loss,
metrics=[],
loss_weights=loss_weights,
sample_weight_mode=None,
target_tensors={'does_not_exist': y2})
# test dictionary of target_tensors
model.compile(optimizer, loss,
metrics=[],
loss_weights=loss_weights,
sample_weight_mode=None,
target_tensors={'dense_1': y, 'dropout': y1})
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np],
{y: np.random.random((10, 4)),
y1: np.random.random((10, 3))})
if K.backend() == 'tensorflow':
import tensorflow as tf
# test with custom TF placeholder as target
pl_target_a = tf.placeholder('float32', shape=(None, 4))
model.compile(optimizer='rmsprop', loss='mse',
target_tensors={'dense_1': pl_target_a})
model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np])
示例3: test_render_gan_builder_generator_extended
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_render_gan_builder_generator_extended():
labels_shape = (27,)
z_dim_offset = 50
builder = RenderGAN(lambda x: tag3d_network_dense(x, nb_units=4),
generator_units=4, discriminator_units=4,
z_dim_offset=z_dim_offset,
labels_shape=(27,))
bs = 19
z, z_offset, labels = data(builder, bs)
real = np.zeros((bs,) + builder.data_shape)
labels_input = Input(shape=labels_shape)
z = Input(shape=(z_dim_offset,))
fake = builder.generator_given_z_and_labels([z, labels_input])
m = Model([z, labels_input], [fake])
m.compile('adam', 'mse')
m.train_on_batch([z_offset, labels], real)
示例4: test_model_with_partial_loss
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_model_with_partial_loss():
a = Input(shape=(3,), name='input_a')
a_2 = Dense(4, name='dense_1')(a)
dp = Dropout(0.5, name='dropout')
a_3 = dp(a_2)
model = Model(a, [a_2, a_3])
optimizer = 'rmsprop'
loss = {'dropout': 'mse'}
model.compile(optimizer, loss, metrics=['mae'])
input_a_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
# test train_on_batch
out = model.train_on_batch(input_a_np, output_a_np)
out = model.test_on_batch(input_a_np, output_a_np)
# fit
out = model.fit(input_a_np, [output_a_np])
# evaluate
out = model.evaluate(input_a_np, [output_a_np])
# Same without dropout.
a = Input(shape=(3,), name='input_a')
a_2 = Dense(4, name='dense_1')(a)
a_3 = Dense(4, name='dense_2')(a_2)
model = Model(a, [a_2, a_3])
optimizer = 'rmsprop'
loss = {'dense_2': 'mse'}
model.compile(optimizer, loss, metrics={'dense_1': 'mae'})
# test train_on_batch
out = model.train_on_batch(input_a_np, output_a_np)
out = model.test_on_batch(input_a_np, output_a_np)
# fit
out = model.fit(input_a_np, [output_a_np])
# evaluate
out = model.evaluate(input_a_np, [output_a_np])
示例5: train_f_enc
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def train_f_enc(self, steps_list, epoch=50):
print("training f_enc")
f_add0 = Sequential(name='f_add0')
f_add0.add(self.f_enc)
f_add0.add(Dense(FIELD_DEPTH))
f_add0.add(Activation('softmax', name='softmax_add0'))
f_add1 = Sequential(name='f_add1')
f_add1.add(self.f_enc)
f_add1.add(Dense(FIELD_DEPTH))
f_add1.add(Activation('softmax', name='softmax_add1'))
env_model = Model(self.f_enc.inputs, [f_add0.output, f_add1.output], name="env_model")
env_model.compile(optimizer='adam', loss=['categorical_crossentropy']*2)
for ep in range(epoch):
losses = []
for idx, steps_dict in enumerate(steps_list):
prev = None
for step in steps_dict['steps']:
x = self.convert_input(step.input)[:2]
env_values = step.input.env.reshape((4, -1))
in1 = np.clip(env_values[0].argmax() - 1, 0, 9)
in2 = np.clip(env_values[1].argmax() - 1, 0, 9)
carry = np.clip(env_values[2].argmax() - 1, 0, 9)
y_num = in1 + in2 + carry
now = (in1, in2, carry)
if prev == now:
continue
prev = now
y0 = to_one_hot_array((y_num % 10)+1, FIELD_DEPTH)
y1 = to_one_hot_array((y_num // 10)+1, FIELD_DEPTH)
y = [yy.reshape((self.batch_size, -1)) for yy in [y0, y1]]
loss = env_model.train_on_batch(x, y)
losses.append(loss)
print("ep %3d: loss=%s" % (ep, np.average(losses)))
if np.average(losses) < 1e-06:
break
示例6: test_model_methods
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_model_methods():
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)
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
# test train_on_batch
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np])
out = model.train_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np])
out = model.train_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np})
# test fit
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np], nb_epoch=1, batch_size=4)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np], nb_epoch=1, batch_size=4)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np},
nb_epoch=1, batch_size=4)
# test validation_split
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np],
nb_epoch=1, batch_size=4, validation_split=0.5)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np],
nb_epoch=1, batch_size=4, validation_split=0.5)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np},
nb_epoch=1, batch_size=4, validation_split=0.5)
# test validation data
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np],
nb_epoch=1, batch_size=4,
validation_data=([input_a_np, input_b_np], [output_a_np, output_b_np]))
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np],
nb_epoch=1, batch_size=4, validation_split=0.5,
validation_data=({'input_a': input_a_np, 'input_b': input_b_np}, [output_a_np, output_b_np]))
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np},
nb_epoch=1, batch_size=4, validation_split=0.5,
validation_data=({'input_a': input_a_np, 'input_b': input_b_np}, {'dense_1': output_a_np, 'dropout': output_b_np}))
# test_on_batch
out = model.test_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np])
out = model.test_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np])
out = model.test_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np})
# predict_on_batch
out = model.predict_on_batch([input_a_np, input_b_np])
out = model.predict_on_batch({'input_a': input_a_np, 'input_b': input_b_np})
# predict, evaluate
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
out = model.evaluate([input_a_np, input_b_np], [output_a_np, output_b_np], batch_size=4)
out = model.predict([input_a_np, input_b_np], batch_size=4)
# with sample_weight
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
sample_weight = [None, np.random.random((10,))]
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np],
sample_weight=sample_weight)
out = model.test_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np],
#.........这里部分代码省略.........
示例7: test_model_methods
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_model_methods():
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]
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
input_a_df = pd.DataFrame(input_a_np)
input_b_df = pd.DataFrame(input_b_np)
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
output_a_df = pd.DataFrame(output_a_np)
output_b_df = pd.DataFrame(output_b_np)
# training/testing doesn't work before compiling.
with pytest.raises(RuntimeError):
model.train_on_batch([input_a_np, input_b_np], [output_a_np, output_b_np])
model.compile(optimizer, loss, metrics=[], loss_weights=loss_weights,
sample_weight_mode=None)
# test train_on_batch
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np])
out = model.train_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np])
out = model.train_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np})
out = model.train_on_batch([input_a_df, input_b_df],
[output_a_df, output_b_df])
# test fit
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np], epochs=1, batch_size=4)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np], epochs=1, batch_size=4)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np},
epochs=1, batch_size=4)
out = model.fit([input_a_df, input_b_df],
[output_a_df, output_b_df], epochs=1, batch_size=4)
# test validation_split
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np],
epochs=1, batch_size=4, validation_split=0.5)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np],
epochs=1, batch_size=4, validation_split=0.5)
# test validation data
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np],
epochs=1, batch_size=4,
validation_data=([input_a_np, input_b_np], [output_a_np, output_b_np]))
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np],
epochs=1, batch_size=4, validation_split=0.5,
validation_data=({'input_a': input_a_np, 'input_b': input_b_np}, [output_a_np, output_b_np]))
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np},
epochs=1, batch_size=4, validation_split=0.5,
validation_data=(
{'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np}))
# test_on_batch
out = model.test_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np])
out = model.test_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np])
out = model.test_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np})
out = model.test_on_batch([input_a_df, input_b_df],
[output_a_df, output_b_df])
# predict_on_batch
out = model.predict_on_batch([input_a_np, input_b_np])
out = model.predict_on_batch({'input_a': input_a_np, 'input_b': input_b_np})
out = model.predict_on_batch([input_a_df, input_b_df])
# predict, evaluate
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
out = model.evaluate([input_a_np, input_b_np], [output_a_np, output_b_np], batch_size=4)
out = model.evaluate([input_a_df, input_b_df], [output_a_df, output_b_df], batch_size=4)
#.........这里部分代码省略.........
示例8: test_model_with_external_loss
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_model_with_external_loss():
# None loss, only regularization loss.
a = Input(shape=(3,), name='input_a')
a_2 = Dense(4, name='dense_1',
kernel_regularizer='l1',
bias_regularizer='l2')(a)
dp = Dropout(0.5, name='dropout')
a_3 = dp(a_2)
model = Model(a, [a_2, a_3])
optimizer = 'rmsprop'
loss = None
model.compile(optimizer, loss, metrics=['mae'])
input_a_np = np.random.random((10, 3))
# test train_on_batch
out = model.train_on_batch(input_a_np, None)
out = model.test_on_batch(input_a_np, None)
# fit
out = model.fit(input_a_np, None)
# evaluate
out = model.evaluate(input_a_np, None)
# No dropout, external loss.
a = Input(shape=(3,), name='input_a')
a_2 = Dense(4, name='dense_1')(a)
a_3 = Dense(4, name='dense_2')(a)
model = Model(a, [a_2, a_3])
model.add_loss(K.mean(a_3 + a_2))
optimizer = 'rmsprop'
loss = None
model.compile(optimizer, loss, metrics=['mae'])
# test train_on_batch
out = model.train_on_batch(input_a_np, None)
out = model.test_on_batch(input_a_np, None)
# fit
out = model.fit(input_a_np, None)
# evaluate
out = model.evaluate(input_a_np, None)
# Test fit with no external data at all.
if K.backend() == 'tensorflow':
import tensorflow as tf
a = Input(tensor=tf.Variable(input_a_np, dtype=tf.float32))
a_2 = Dense(4, name='dense_1')(a)
a_2 = Dropout(0.5, name='dropout')(a_2)
model = Model(a, a_2)
model.add_loss(K.mean(a_2))
model.compile(optimizer='rmsprop',
loss=None,
metrics=['mean_squared_error'])
# test train_on_batch
out = model.train_on_batch(None, None)
out = model.test_on_batch(None, None)
out = model.predict_on_batch(None)
# test fit
with pytest.raises(ValueError):
out = model.fit(None, None, epochs=1, batch_size=10)
out = model.fit(None, None, epochs=1, steps_per_epoch=1)
# test fit with validation data
with pytest.raises(ValueError):
out = model.fit(None, None,
epochs=1,
steps_per_epoch=None,
validation_steps=2)
out = model.fit(None, None,
epochs=1,
steps_per_epoch=2,
validation_steps=2)
# test evaluate
with pytest.raises(ValueError):
out = model.evaluate(None, None, batch_size=10)
out = model.evaluate(None, None, steps=3)
# test predict
with pytest.raises(ValueError):
out = model.predict(None, batch_size=10)
out = model.predict(None, steps=3)
assert out.shape == (10 * 3, 4)
# Test multi-output model without external data.
a = Input(tensor=tf.Variable(input_a_np, dtype=tf.float32))
a_1 = Dense(4, name='dense_1')(a)
a_2 = Dropout(0.5, name='dropout')(a_1)
model = Model(a, [a_1, a_2])
model.add_loss(K.mean(a_2))
model.compile(optimizer='rmsprop',
loss=None,
metrics=['mean_squared_error'])
#.........这里部分代码省略.........
示例9: test_model_with_input_feed_tensor
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_model_with_input_feed_tensor():
"""We test building a model with a TF variable as input.
We should be able to call fit, evaluate, predict,
by only passing them data for the placeholder inputs
in the model.
"""
import tensorflow as tf
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
a = Input(tensor=tf.Variable(input_a_np, dtype=tf.float32))
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])
model.summary()
optimizer = 'rmsprop'
loss = 'mse'
loss_weights = [1., 0.5]
model.compile(optimizer, loss, metrics=['mean_squared_error'],
loss_weights=loss_weights,
sample_weight_mode=None)
# test train_on_batch
out = model.train_on_batch(input_b_np,
[output_a_np, output_b_np])
out = model.train_on_batch({'input_b': input_b_np},
[output_a_np, output_b_np])
out = model.test_on_batch({'input_b': input_b_np},
[output_a_np, output_b_np])
out = model.predict_on_batch({'input_b': input_b_np})
# test fit
out = model.fit({'input_b': input_b_np},
[output_a_np, output_b_np], epochs=1, batch_size=10)
out = model.fit(input_b_np,
[output_a_np, output_b_np], epochs=1, batch_size=10)
# test evaluate
out = model.evaluate({'input_b': input_b_np},
[output_a_np, output_b_np], batch_size=10)
out = model.evaluate(input_b_np,
[output_a_np, output_b_np], batch_size=10)
# test predict
out = model.predict({'input_b': input_b_np}, batch_size=10)
out = model.predict(input_b_np, batch_size=10)
assert len(out) == 2
# Now test a model with a single input
# i.e. we don't pass any data to fit the model.
a = Input(tensor=tf.Variable(input_a_np, dtype=tf.float32))
a_2 = Dense(4, name='dense_1')(a)
a_2 = Dropout(0.5, name='dropout')(a_2)
model = Model(a, a_2)
model.summary()
optimizer = 'rmsprop'
loss = 'mse'
model.compile(optimizer, loss, metrics=['mean_squared_error'])
# test train_on_batch
out = model.train_on_batch(None,
output_a_np)
out = model.train_on_batch(None,
output_a_np)
out = model.test_on_batch(None,
output_a_np)
out = model.predict_on_batch(None)
out = model.train_on_batch([],
output_a_np)
out = model.train_on_batch({},
output_a_np)
# test fit
out = model.fit(None,
output_a_np, epochs=1, batch_size=10)
out = model.fit(None,
output_a_np, epochs=1, batch_size=10)
# test evaluate
out = model.evaluate(None,
output_a_np, batch_size=10)
out = model.evaluate(None,
output_a_np, batch_size=10)
# test predict
out = model.predict(None, steps=3)
out = model.predict(None, steps=3)
assert out.shape == (10 * 3, 4)
# Same, without learning phase
#.........这里部分代码省略.........
示例10: test_pandas_dataframe
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_pandas_dataframe():
input_a = Input(shape=(3,), name='input_a')
input_b = Input(shape=(3,), name='input_b')
x = Dense(4, name='dense_1')(input_a)
y = Dense(3, name='desne_2')(input_b)
model_1 = Model(inputs=input_a, outputs=x)
model_2 = Model(inputs=[input_a, input_b], outputs=[x, y])
optimizer = 'rmsprop'
loss = 'mse'
model_1.compile(optimizer=optimizer, loss=loss)
model_2.compile(optimizer=optimizer, loss=loss)
input_a_df = pd.DataFrame(np.random.random((10, 3)))
input_b_df = pd.DataFrame(np.random.random((10, 3)))
output_a_df = pd.DataFrame(np.random.random((10, 4)))
output_b_df = pd.DataFrame(np.random.random((10, 3)))
model_1.fit(input_a_df,
output_a_df)
model_2.fit([input_a_df, input_b_df],
[output_a_df, output_b_df])
model_1.fit([input_a_df],
[output_a_df])
model_1.fit({'input_a': input_a_df},
output_a_df)
model_2.fit({'input_a': input_a_df, 'input_b': input_b_df},
[output_a_df, output_b_df])
model_1.predict(input_a_df)
model_2.predict([input_a_df, input_b_df])
model_1.predict([input_a_df])
model_1.predict({'input_a': input_a_df})
model_2.predict({'input_a': input_a_df, 'input_b': input_b_df})
model_1.predict_on_batch(input_a_df)
model_2.predict_on_batch([input_a_df, input_b_df])
model_1.predict_on_batch([input_a_df])
model_1.predict_on_batch({'input_a': input_a_df})
model_2.predict_on_batch({'input_a': input_a_df, 'input_b': input_b_df})
model_1.evaluate(input_a_df,
output_a_df)
model_2.evaluate([input_a_df, input_b_df],
[output_a_df, output_b_df])
model_1.evaluate([input_a_df],
[output_a_df])
model_1.evaluate({'input_a': input_a_df},
output_a_df)
model_2.evaluate({'input_a': input_a_df, 'input_b': input_b_df},
[output_a_df, output_b_df])
model_1.train_on_batch(input_a_df,
output_a_df)
model_2.train_on_batch([input_a_df, input_b_df],
[output_a_df, output_b_df])
model_1.train_on_batch([input_a_df],
[output_a_df])
model_1.train_on_batch({'input_a': input_a_df},
output_a_df)
model_2.train_on_batch({'input_a': input_a_df, 'input_b': input_b_df},
[output_a_df, output_b_df])
model_1.test_on_batch(input_a_df,
output_a_df)
model_2.test_on_batch([input_a_df, input_b_df],
[output_a_df, output_b_df])
model_1.test_on_batch([input_a_df],
[output_a_df])
model_1.test_on_batch({'input_a': input_a_df},
output_a_df)
model_2.test_on_batch({'input_a': input_a_df, 'input_b': input_b_df},
[output_a_df, output_b_df])
示例11: test_model_methods
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_model_methods():
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)
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
# test train_on_batch
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np])
out = model.train_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np])
out = model.train_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np})
# test fit
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np], nb_epoch=1, batch_size=4)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np], nb_epoch=1, batch_size=4)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np},
nb_epoch=1, batch_size=4)
# test validation_split
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np],
nb_epoch=1, batch_size=4, validation_split=0.5)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np],
nb_epoch=1, batch_size=4, validation_split=0.5)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np},
nb_epoch=1, batch_size=4, validation_split=0.5)
# test validation data
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np],
nb_epoch=1, batch_size=4,
validation_data=([input_a_np, input_b_np], [output_a_np, output_b_np]))
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np],
nb_epoch=1, batch_size=4, validation_split=0.5,
validation_data=({'input_a': input_a_np, 'input_b': input_b_np}, [output_a_np, output_b_np]))
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np},
nb_epoch=1, batch_size=4, validation_split=0.5,
validation_data=({'input_a': input_a_np, 'input_b': input_b_np}, {'dense_1': output_a_np, 'dropout': output_b_np}))
# test_on_batch
out = model.test_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np])
out = model.test_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np])
out = model.test_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np})
# predict_on_batch
out = model.predict_on_batch([input_a_np, input_b_np])
out = model.predict_on_batch({'input_a': input_a_np, 'input_b': input_b_np})
# predict, evaluate
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
out = model.evaluate([input_a_np, input_b_np], [output_a_np, output_b_np], batch_size=4)
out = model.predict([input_a_np, input_b_np], batch_size=4)
# with sample_weight
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
sample_weight = [None, np.random.random((10,))]
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np],
sample_weight=sample_weight)
out = model.test_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np],
#.........这里部分代码省略.........
示例12: __init__
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
class SeqGAN:
def __init__(self, g, d, m, g_optimizer, d_optimizer):
self.g = g
self.d = d
self.m = m
self.z, self.seq_input = self.g.inputs
self.fake_prob, = self.g.outputs
with trainable(m, False):
m_input = merge([self.seq_input, self.fake_prob], mode='concat', concat_axis=1)
self.m_realness = self.m(m_input)
self.model_fit_g = Model([self.z, self.seq_input], [self.m_realness])
self.model_fit_g.compile(g_optimizer, K.binary_crossentropy)
self.d.compile(d_optimizer, loss=K.binary_crossentropy)
def z_shape(self, batch_size=64):
layer, _, _ = self.z._keras_history
return (batch_size,) + layer.output_shape[1:]
def sample_z(self, batch_size=64):
shape = self.z_shape(batch_size)
return np.random.uniform(-1, 1, shape)
def generate(self, z, seq_input, batch_size=32):
return self.g.predict([z, seq_input], batch_size=batch_size)
def train_on_batch(self, seq_input, real, d_target=None):
nb_real = len(real)
nb_fake = len(seq_input)
if d_target is None:
d_target = np.concatenate([
np.zeros((nb_fake, 1)),
np.ones((nb_real, 1))
])
fake_prob = self.generate(self.sample_z(nb_fake), seq_input)
fake = np.concatenate([seq_input, prob_to_sentence(fake_prob)], axis=1)
fake_and_real = np.concatenate([fake, real], axis=0)
d_loss = self.d.train_on_batch(fake_and_real, d_target)
d_realness = self.d.predict(fake)
m_loss = self.m.train_on_batch(
np.concatenate([seq_input, fake_prob], axis=1), d_realness)
g_loss = self.model_fit_g.train_on_batch([self.sample_z(nb_fake), seq_input],
np.ones((nb_fake, 1)))
return g_loss, d_loss, m_loss
def fit_generator(self, generator, nb_epoch, nb_batches_per_epoch, callbacks=[],
batch_size=None,
verbose=False):
if batch_size is None:
batch_size = 2*len(next(generator)[0])
out_labels = ['g', 'd', 'm']
self.history = cbks.History()
callbacks = [cbks.BaseLogger()] + callbacks + [self.history]
if verbose:
callbacks += [cbks.ProgbarLogger()]
callbacks = cbks.CallbackList(callbacks)
callbacks._set_model(self)
callbacks._set_params({
'nb_epoch': nb_epoch,
'nb_sample': nb_batches_per_epoch*batch_size,
'verbose': verbose,
'metrics': out_labels,
})
callbacks.on_train_begin()
for e in range(nb_epoch):
callbacks.on_epoch_begin(e)
for batch_index, (seq_input, real) in enumerate(generator):
callbacks.on_batch_begin(batch_index)
batch_logs = {}
batch_logs['batch'] = batch_index
batch_logs['size'] = len(real) + len(seq_input
)
outs = self.train_on_batch(seq_input, real)
for l, o in zip(out_labels, outs):
batch_logs[l] = o
callbacks.on_batch_end(batch_index, batch_logs)
if batch_index + 1 == nb_batches_per_epoch:
break
callbacks.on_epoch_end(e)
callbacks.on_train_end()
示例13: test_model_methods
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
def test_model_methods():
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]
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
# training/testing doesn't work before compiling.
with pytest.raises(RuntimeError):
model.train_on_batch([input_a_np, input_b_np], [output_a_np, output_b_np])
model.compile(optimizer, loss, metrics=[], loss_weights=loss_weights,
sample_weight_mode=None)
# test train_on_batch
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np])
out = model.train_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np])
out = model.train_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np})
# test fit
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np], epochs=1, batch_size=4)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np], epochs=1, batch_size=4)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np},
epochs=1, batch_size=4)
# test validation_split
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np],
epochs=1, batch_size=4, validation_split=0.5)
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np],
epochs=1, batch_size=4, validation_split=0.5)
# test validation data
out = model.fit([input_a_np, input_b_np],
[output_a_np, output_b_np],
epochs=1, batch_size=4,
validation_data=([input_a_np, input_b_np], [output_a_np, output_b_np]))
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np],
epochs=1, batch_size=4, validation_split=0.5,
validation_data=({'input_a': input_a_np, 'input_b': input_b_np}, [output_a_np, output_b_np]))
out = model.fit({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np},
epochs=1, batch_size=4, validation_split=0.5,
validation_data=(
{'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np}))
# test_on_batch
out = model.test_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np])
out = model.test_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
[output_a_np, output_b_np])
out = model.test_on_batch({'input_a': input_a_np, 'input_b': input_b_np},
{'dense_1': output_a_np, 'dropout': output_b_np})
# predict_on_batch
out = model.predict_on_batch([input_a_np, input_b_np])
out = model.predict_on_batch({'input_a': input_a_np, 'input_b': input_b_np})
# predict, evaluate
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
out = model.evaluate([input_a_np, input_b_np], [output_a_np, output_b_np], batch_size=4)
out = model.predict([input_a_np, input_b_np], batch_size=4)
# with sample_weight
input_a_np = np.random.random((10, 3))
input_b_np = np.random.random((10, 3))
output_a_np = np.random.random((10, 4))
output_b_np = np.random.random((10, 3))
sample_weight = [None, np.random.random((10,))]
out = model.train_on_batch([input_a_np, input_b_np],
[output_a_np, output_b_np],
#.........这里部分代码省略.........
示例14: AdditionNPIModel
# 需要导入模块: from keras.engine.training import Model [as 别名]
# 或者: from keras.engine.training.Model import train_on_batch [as 别名]
#.........这里部分代码省略.........
ok_rate = []
np.random.shuffle(steps_list)
for idx, steps_dict in enumerate(steps_list):
question = copy(steps_dict['q'])
question_key = self.dict_to_str(question)
if self.question_test(addition_env, npi_runner, question):
if correct_count[question_key] == 0:
correct_new += 1
correct_count[question_key] += 1
print("GOOD!: ep=%2d idx=%3d :%s CorrectCount=%s" % (ep, idx, self.dict_to_str(question), correct_count[question_key]))
ok_rate.append(1)
if skip_correct or int(math.sqrt(correct_count[question_key])) ** 2 != correct_count[question_key]:
continue
else:
ok_rate.append(0)
if correct_count[question_key] > 0:
print("Degraded: ep=%2d idx=%3d :%s CorrectCount=%s -> 0" % (ep, idx, self.dict_to_str(question), correct_count[question_key]))
correct_count[question_key] = 0
wrong_new += 1
steps = steps_dict['steps']
xs = []
ys = []
ws = []
for step in steps:
xs.append(self.convert_input(step.input))
y, w = self.convert_output(step.output)
ys.append(y)
ws.append(w)
self.reset()
for i, (x, y, w) in enumerate(zip(xs, ys, ws)):
loss = self.model.train_on_batch(x, y, sample_weight=w)
if not np.isfinite(loss):
print("Loss is not finite!, Last Input=%s" % ([i, (x, y, w)]))
self.print_weights(last_weights, detail=True)
raise RuntimeError("Loss is not finite!")
losses.append(loss)
last_weights = self.model.get_weights()
if losses:
cur_loss = np.average(losses)
print("ep=%2d: ok_rate=%.2f%% (+%s -%s): ave loss %s (%s samples)" %
(ep, np.average(ok_rate)*100, correct_new, wrong_new, cur_loss, len(steps_list)))
# self.print_weights()
if correct_new + wrong_new == 0:
no_change_count += 1
else:
no_change_count = 0
if math.fabs(1 - cur_loss/last_loss) < 0.001 and no_change_count > 5:
print("math.fabs(1 - cur_loss/last_loss) < 0.001 and no_change_count > 5:")
return False
last_loss = cur_loss
print("=" * 80)
self.save()
if np.average(ok_rate) >= pass_rate:
return True
return False
def update_learning_rate(self, learning_rate, arg_weight=1.):
print("Re-Compile Model lr=%s aw=%s" % (learning_rate, arg_weight))
self.compile_model(learning_rate, arg_weight=arg_weight)
def train_f_enc(self, steps_list, epoch=50):
print("training f_enc")