本文整理汇总了Python中keras.engine.training.Model类的典型用法代码示例。如果您正苦于以下问题:Python Model类的具体用法?Python Model怎么用?Python Model使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Model类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_warnings
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.'
示例2: test_sparse_input_validation_split
def test_sparse_input_validation_split():
test_input = sparse.random(6, 3, density=0.25).tocsr()
in1 = Input(shape=(3,), sparse=True)
out1 = Dense(4)(in1)
test_output = np.random.random((6, 4))
model = Model(in1, out1)
model.compile('rmsprop', 'mse')
model.fit(test_input, test_output, epochs=1, batch_size=2, validation_split=0.2)
示例3: test_resnet
def test_resnet():
n = 4
x = Input(shape=(1, 8, 8))
y = sequential([
conv2d_block(n),
resnet(n)
])(x)
model = Model(x, y)
assert model.get_output_shape_for((None, 1, 8, 8)) == (None, n, 8, 8)
示例4: m
def m():
x = Input(shape=(input_size + output_size, nb_chars))
m_realness = sequential([
LSTM(14),
Dense(1, activation='sigmoid'),
])(x)
m = Model([x], [m_realness])
m.compile(Adam(), 'mse')
return m
示例5: decoder_dummy
def decoder_dummy(label_sizes, nb_filter=16, data_shape=(1, 64, 64), nb_bits=12,
optimizer='adam'):
input = Input(shape=data_shape)
x = input
outputs, losses = decoder_end_block(x, label_sizes, nb_bits,
activation=lambda: ELU())
model = Model(input, list(outputs.values()))
model.compile(optimizer, loss=list(losses.values()),
loss_weights={k: decoder_loss_weights(k) for k in losses.keys()})
return model
示例6: test_model_multiple_calls
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)))
示例7: decoder_baseline
def decoder_baseline(label_sizes, nb_bits=12, data_shape=(1, 64, 64),
depth=1, nb_filter=16, optimizer='adam'):
n = nb_filter
input = Input(shape=data_shape)
x = sequential([
conv2d_block(n, depth=depth, pooling='max'), # 32x32
conv2d_block(2*n, depth=depth, pooling='max'), # 16x16
conv2d_block(4*n, depth=depth, pooling='max'), # 8x8
conv2d_block(8*n, depth=depth, pooling='max'), # 4x4
])(input)
outputs, losses = decoder_end_block(x, label_sizes, nb_bits,
activation=lambda: ELU())
model = Model(input, list(outputs.values()))
model.compile(optimizer, loss=list(losses.values()),)
return model
示例8: test_sparse_placeholder_fit
def test_sparse_placeholder_fit():
test_inputs = [sparse.random(6, 3, density=0.25).tocsr() for _ in range(2)]
test_outputs = [sparse.random(6, i, density=0.25).tocsr() for i in range(3, 5)]
in1 = Input(shape=(3,))
in2 = Input(shape=(3,), sparse=True)
out1 = Dropout(0.5, name='dropout')(in1)
out2 = Dense(4, name='dense_1')(in2)
model = Model([in1, in2], [out1, out2])
model.predict(test_inputs, batch_size=2)
model.compile('rmsprop', 'mse')
model.fit(test_inputs, test_outputs, epochs=1, batch_size=2, validation_split=0.5)
model.evaluate(test_inputs, test_outputs, batch_size=2)
示例9: build
def build(self):
mc = self.config.model
in_x = x = Input((14, 10, 9)) # 14 x 10 x 9
# (batch, channels, height, width)
x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_first_filter_size, padding="same",
data_format="channels_first", use_bias=False, kernel_regularizer=l2(mc.l2_reg),
name="input_conv-"+str(mc.cnn_first_filter_size)+"-"+str(mc.cnn_filter_num))(x)
x = BatchNormalization(axis=1, name="input_batchnorm")(x)
x = Activation("relu", name="input_relu")(x)
for i in range(mc.res_layer_num):
x = self._build_residual_block(x, i + 1)
res_out = x
# for policy output
x = Conv2D(filters=2, kernel_size=1, data_format="channels_first", use_bias=False,
kernel_regularizer=l2(mc.l2_reg), name="policy_conv-1-2")(res_out)
x = BatchNormalization(axis=1, name="policy_batchnorm")(x)
x = Activation("relu", name="policy_relu")(x)
x = Flatten(name="policy_flatten")(x)
policy_out = Dense(self.n_labels, kernel_regularizer=l2(mc.l2_reg), activation="softmax", name="policy_out")(x)
# for value output
x = Conv2D(filters=4, kernel_size=1, data_format="channels_first", use_bias=False,
kernel_regularizer=l2(mc.l2_reg), name="value_conv-1-4")(res_out)
x = BatchNormalization(axis=1, name="value_batchnorm")(x)
x = Activation("relu",name="value_relu")(x)
x = Flatten(name="value_flatten")(x)
x = Dense(mc.value_fc_size, kernel_regularizer=l2(mc.l2_reg), activation="relu", name="value_dense")(x)
value_out = Dense(1, kernel_regularizer=l2(mc.l2_reg), activation="tanh", name="value_out")(x)
self.model = Model(in_x, [policy_out, value_out], name="cchess_model")
self.graph = tf.get_default_graph()
示例10: create_policy_value_net
def create_policy_value_net(self):
"""create the policy value network """
in_x = network = Input((4, self.board_width, self.board_height))
# conv layers
network = Conv2D(filters=32, kernel_size=(3, 3), padding="same", data_format="channels_first", activation="relu", kernel_regularizer=l2(self.l2_const))(network)
network = Conv2D(filters=64, kernel_size=(3, 3), padding="same", data_format="channels_first", activation="relu", kernel_regularizer=l2(self.l2_const))(network)
network = Conv2D(filters=128, kernel_size=(3, 3), padding="same", data_format="channels_first", activation="relu", kernel_regularizer=l2(self.l2_const))(network)
# action policy layers
policy_net = Conv2D(filters=4, kernel_size=(1, 1), data_format="channels_first", activation="relu", kernel_regularizer=l2(self.l2_const))(network)
policy_net = Flatten()(policy_net)
self.policy_net = Dense(self.board_width*self.board_height, activation="softmax", kernel_regularizer=l2(self.l2_const))(policy_net)
# state value layers
value_net = Conv2D(filters=2, kernel_size=(1, 1), data_format="channels_first", activation="relu", kernel_regularizer=l2(self.l2_const))(network)
value_net = Flatten()(value_net)
value_net = Dense(64, kernel_regularizer=l2(self.l2_const))(value_net)
self.value_net = Dense(1, activation="tanh", kernel_regularizer=l2(self.l2_const))(value_net)
self.model = Model(in_x, [self.policy_net, self.value_net])
def policy_value(state_input):
state_input_union = np.array(state_input)
results = self.model.predict_on_batch(state_input_union)
return results
self.policy_value = policy_value
示例11: create_network
def create_network(self):
x_in = Input((3, 8, 8))
x = Conv2D(filters=128, kernel_size=(3,3), padding="same", data_format="channels_first")(x_in)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
for _ in range(10):
x = self._build_residual_block(x)
res_out = x
x = Conv2D(filters=2, kernel_size=1, data_format="channels_first")(res_out)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
x = Flatten()(x)
policy_out = Dense(8*8+1, activation="softmax", name="policy_out")(x)
x = Conv2D(filters=1, kernel_size=1, data_format="channels_first")(res_out)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
x = Flatten()(x)
x = Dense(64, activation="relu")(x)
value_out = Dense(1, activation="tanh", name="value_out")(x)
self.network = Model(x_in, [policy_out, value_out], name="reversi_model")
self.compile()
示例12: test_render_gan_builder_generator_extended
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)
示例13: build
def build(self):
dim_data = self.size_of_input_data_dim
nb_time_step = self.size_of_input_timesteps
news_input = Input(shape=(nb_time_step, dim_data))
lstm = LSTM(output_dim=nb_hidden_units, dropout_U=dropout, dropout_W=dropout,
W_regularizer=l2(l2_norm_alpha), b_regularizer=l2(l2_norm_alpha), activation='tanh')
bi_lstm = Bidirectional(lstm, input_shape=(nb_time_step, dim_data), merge_mode='concat')
all_news_rep = bi_lstm(news_input)
news_predictions = Dense(1, activation='linear')(all_news_rep)
self.model = Model(news_input, news_predictions, name="deep rnn for financial news analysis")
示例14: build
def build(self):
enc_size = self.size_of_env_observation()
argument_size = IntegerArguments.size_of_arguments
input_enc = InputLayer(batch_input_shape=(self.batch_size, enc_size), name='input_enc')
input_arg = InputLayer(batch_input_shape=(self.batch_size, argument_size), name='input_arg')
input_prg = Embedding(input_dim=PROGRAM_VEC_SIZE, output_dim=PROGRAM_KEY_VEC_SIZE, input_length=1,
batch_input_shape=(self.batch_size, 1))
f_enc = Sequential(name='f_enc')
f_enc.add(Merge([input_enc, input_arg], mode='concat'))
f_enc.add(MaxoutDense(128, nb_feature=4))
self.f_enc = f_enc
program_embedding = Sequential(name='program_embedding')
program_embedding.add(input_prg)
f_enc_convert = Sequential(name='f_enc_convert')
f_enc_convert.add(f_enc)
f_enc_convert.add(RepeatVector(1))
f_lstm = Sequential(name='f_lstm')
f_lstm.add(Merge([f_enc_convert, program_embedding], mode='concat'))
f_lstm.add(LSTM(256, return_sequences=False, stateful=True, W_regularizer=l2(0.0000001)))
f_lstm.add(Activation('relu', name='relu_lstm_1'))
f_lstm.add(RepeatVector(1))
f_lstm.add(LSTM(256, return_sequences=False, stateful=True, W_regularizer=l2(0.0000001)))
f_lstm.add(Activation('relu', name='relu_lstm_2'))
# plot(f_lstm, to_file='f_lstm.png', show_shapes=True)
f_end = Sequential(name='f_end')
f_end.add(f_lstm)
f_end.add(Dense(1, W_regularizer=l2(0.001)))
f_end.add(Activation('sigmoid', name='sigmoid_end'))
f_prog = Sequential(name='f_prog')
f_prog.add(f_lstm)
f_prog.add(Dense(PROGRAM_KEY_VEC_SIZE, activation="relu"))
f_prog.add(Dense(PROGRAM_VEC_SIZE, W_regularizer=l2(0.0001)))
f_prog.add(Activation('softmax', name='softmax_prog'))
# plot(f_prog, to_file='f_prog.png', show_shapes=True)
f_args = []
for ai in range(1, IntegerArguments.max_arg_num+1):
f_arg = Sequential(name='f_arg%s' % ai)
f_arg.add(f_lstm)
f_arg.add(Dense(IntegerArguments.depth, W_regularizer=l2(0.0001)))
f_arg.add(Activation('softmax', name='softmax_arg%s' % ai))
f_args.append(f_arg)
# plot(f_arg, to_file='f_arg.png', show_shapes=True)
self.model = Model([input_enc.input, input_arg.input, input_prg.input],
[f_end.output, f_prog.output] + [fa.output for fa in f_args],
name="npi")
self.compile_model()
plot(self.model, to_file='model.png', show_shapes=True)
示例15: load
def load(self, config_path, weight_path):
if os.path.exists(config_path) and os.path.exists(weight_path):
logger.debug(f"loading model from {config_path}")
with open(config_path, "rt") as f:
self.model = Model.from_config(json.load(f))
self.model.load_weights(weight_path)
self.digest = self.fetch_digest(weight_path)
self.graph = tf.get_default_graph()
logger.debug(f"loaded model digest = {self.digest}")
return True
else:
logger.debug(f"model files does not exist at {config_path} and {weight_path}")
return False