本文整理汇总了Python中keras.engine.topology.Input方法的典型用法代码示例。如果您正苦于以下问题:Python topology.Input方法的具体用法?Python topology.Input怎么用?Python topology.Input使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.engine.topology
的用法示例。
在下文中一共展示了topology.Input方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_trainable_argument
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def test_trainable_argument():
x = np.random.random((5, 3))
y = np.random.random((5, 2))
model = Sequential()
model.add(Dense(2, input_dim=3, trainable=False))
model.compile('rmsprop', 'mse')
out = model.predict(x)
model.train_on_batch(x, y)
out_2 = model.predict(x)
assert_allclose(out, out_2)
# test with nesting
inputs = Input(shape=(3,))
outputs = model(inputs)
model = Model(inputs, outputs)
model.compile('rmsprop', 'mse')
out = model.predict(x)
model.train_on_batch(x, y)
out_2 = model.predict(x)
assert_allclose(out, out_2)
示例2: test_specify_initial_state_keras_tensor
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def test_specify_initial_state_keras_tensor(layer_class):
num_states = 2 if layer_class is recurrent.LSTM else 1
# Test with Keras tensor
inputs = Input((timesteps, embedding_dim))
initial_state = [Input((units,)) for _ in range(num_states)]
layer = layer_class(units)
if len(initial_state) == 1:
output = layer(inputs, initial_state=initial_state[0])
else:
output = layer(inputs, initial_state=initial_state)
assert initial_state[0] in layer._inbound_nodes[0].input_tensors
model = Model([inputs] + initial_state, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
inputs = np.random.random((num_samples, timesteps, embedding_dim))
initial_state = [np.random.random((num_samples, units))
for _ in range(num_states)]
targets = np.random.random((num_samples, units))
model.fit([inputs] + initial_state, targets)
示例3: test_specify_initial_state_non_keras_tensor
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def test_specify_initial_state_non_keras_tensor(layer_class):
num_states = 2 if layer_class is recurrent.LSTM else 1
# Test with non-Keras tensor
inputs = Input((timesteps, embedding_dim))
initial_state = [K.random_normal_variable((num_samples, units), 0, 1)
for _ in range(num_states)]
layer = layer_class(units)
output = layer(inputs, initial_state=initial_state)
model = Model(inputs, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
inputs = np.random.random((num_samples, timesteps, embedding_dim))
targets = np.random.random((num_samples, units))
model.fit(inputs, targets)
示例4: test_specify_state_with_masking
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def test_specify_state_with_masking(layer_class):
''' This test based on a previously failing issue here:
https://github.com/keras-team/keras/issues/1567
'''
num_states = 2 if layer_class is recurrent.LSTM else 1
inputs = Input((timesteps, embedding_dim))
_ = Masking()(inputs)
initial_state = [Input((units,)) for _ in range(num_states)]
output = layer_class(units)(inputs, initial_state=initial_state)
model = Model([inputs] + initial_state, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
inputs = np.random.random((num_samples, timesteps, embedding_dim))
initial_state = [np.random.random((num_samples, units))
for _ in range(num_states)]
targets = np.random.random((num_samples, units))
model.fit([inputs] + initial_state, targets)
示例5: test_stacked_rnn_attributes
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def test_stacked_rnn_attributes():
cells = [recurrent.LSTMCell(3),
recurrent.LSTMCell(3, kernel_regularizer='l2')]
layer = recurrent.RNN(cells)
layer.build((None, None, 5))
# Test regularization losses
assert len(layer.losses) == 1
# Test weights
assert len(layer.trainable_weights) == 6
cells[0].trainable = False
assert len(layer.trainable_weights) == 3
assert len(layer.non_trainable_weights) == 3
# Test `get_losses_for`
x = keras.Input((None, 5))
y = K.sum(x)
cells[0].add_loss(y, inputs=x)
assert layer.get_losses_for(x) == [y]
示例6: test_initial_states_as_other_inputs
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def test_initial_states_as_other_inputs(layer_class):
num_states = 2 if layer_class is recurrent.LSTM else 1
# Test with Keras tensor
main_inputs = Input((timesteps, embedding_dim))
initial_state = [Input((units,)) for _ in range(num_states)]
inputs = [main_inputs] + initial_state
layer = layer_class(units)
output = layer(inputs)
assert initial_state[0] in layer._inbound_nodes[0].input_tensors
model = Model(inputs, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
main_inputs = np.random.random((num_samples, timesteps, embedding_dim))
initial_state = [np.random.random((num_samples, units))
for _ in range(num_states)]
targets = np.random.random((num_samples, units))
model.train_on_batch([main_inputs] + initial_state, targets)
示例7: create_model
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def create_model(gpu):
with tf.device(gpu):
input = Input((1280, 1918, len(dirs)))
x = Lambda(lambda x: K.mean(x, axis=-1, keepdims=True))(input)
model = Model(input, x)
model.summary()
return model
示例8: get_simple_unet
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def get_simple_unet(input_shape):
img_input = Input(input_shape)
conv1 = conv_block_simple(img_input, 32, "conv1_1")
conv1 = conv_block_simple(conv1, 32, "conv1_2")
pool1 = MaxPooling2D((2, 2), strides=(2, 2), padding="same", name="pool1")(conv1)
conv2 = conv_block_simple(pool1, 64, "conv2_1")
conv2 = conv_block_simple(conv2, 64, "conv2_2")
pool2 = MaxPooling2D((2, 2), strides=(2, 2), padding="same", name="pool2")(conv2)
conv3 = conv_block_simple(pool2, 128, "conv3_1")
conv3 = conv_block_simple(conv3, 128, "conv3_2")
pool3 = MaxPooling2D((2, 2), strides=(2, 2), padding="same", name="pool3")(conv3)
conv4 = conv_block_simple(pool3, 256, "conv4_1")
conv4 = conv_block_simple(conv4, 256, "conv4_2")
conv4 = conv_block_simple(conv4, 256, "conv4_3")
up5 = concatenate([UpSampling2D()(conv4), conv3], axis=-1)
conv5 = conv_block_simple(up5, 128, "conv5_1")
conv5 = conv_block_simple(conv5, 128, "conv5_2")
up6 = concatenate([UpSampling2D()(conv5), conv2], axis=-1)
conv6 = conv_block_simple(up6, 64, "conv6_1")
conv6 = conv_block_simple(conv6, 64, "conv6_2")
up7 = concatenate([UpSampling2D()(conv6), conv1], axis=-1)
conv7 = conv_block_simple(up7, 32, "conv7_1")
conv7 = conv_block_simple(conv7, 32, "conv7_2")
conv7 = SpatialDropout2D(0.2)(conv7)
prediction = Conv2D(1, (1, 1), activation="sigmoid", name="prediction")(conv7)
model = Model(img_input, prediction)
return model
示例9: build_model
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def build_model(args):
cnn_filter_num = args['cnn_filter_num']
cnn_filter_size = args['cnn_filter_size']
l2_reg = args['l2_reg']
in_x = x = Input(args['input_dim'])
# (batch, channels, height, width)
x = Conv2D(filters=cnn_filter_num, kernel_size=cnn_filter_size, padding="same",
data_format="channels_first", kernel_regularizer=l2(l2_reg))(x)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
for _ in range(args['res_layer_num']):
x = _build_residual_block(args, x)
res_out = x
# for policy output
x = Conv2D(filters=2, kernel_size=1, data_format="channels_first", kernel_regularizer=l2(l2_reg))(res_out)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
x = Flatten()(x)
policy_out = Dense(args['policy_dim'], kernel_regularizer=l2(l2_reg), activation="softmax", name="policy")(x)
# for value output
x = Conv2D(filters=1, kernel_size=1, data_format="channels_first", kernel_regularizer=l2(l2_reg))(res_out)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
x = Flatten()(x)
x = Dense(256, kernel_regularizer=l2(l2_reg), activation="relu")(x)
value_out = Dense(1, kernel_regularizer=l2(l2_reg), activation="tanh", name="value")(x)
return Model(in_x, [policy_out, value_out], name="model")
示例10: build
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def build(self):
mc = self.config.model
in_x = x = Input((2, 6, 7)) # [own(8x8), enemy(8x8)]
# (batch, channels, height, width)
x = Conv2D(filters=mc.cnn_filter_num, kernel_size=mc.cnn_filter_size, padding="same",
data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(x)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
for _ in range(mc.res_layer_num):
x = self._build_residual_block(x)
res_out = x
# for policy output
x = Conv2D(filters=2, kernel_size=1, data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(res_out)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
x = Flatten()(x)
# no output for 'pass'
policy_out = Dense(self.config.n_labels, kernel_regularizer=l2(mc.l2_reg), activation="softmax", name="policy_out")(x)
# for value output
x = Conv2D(filters=1, kernel_size=1, data_format="channels_first", kernel_regularizer=l2(mc.l2_reg))(res_out)
x = BatchNormalization(axis=1)(x)
x = Activation("relu")(x)
x = Flatten()(x)
x = Dense(mc.value_fc_size, kernel_regularizer=l2(mc.l2_reg), activation="relu")(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="connect4_model")
示例11: test_weighted_masked_objective
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def test_weighted_masked_objective():
a = Input(shape=(3,), name='input_a')
# weighted_masked_objective
def mask_dummy(y_true=None, y_pred=None, weight=None):
return K.placeholder(y_true.shape)
weighted_function = _weighted_masked_objective(losses.categorical_crossentropy)
weighted_function(a, a, None)
示例12: test_warnings
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [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.'
示例13: test_sparse_inputs_targets
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def test_sparse_inputs_targets():
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,))
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)
示例14: test_sparse_placeholder_fit
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
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)
示例15: test_trainable_weights_count_consistency
# 需要导入模块: from keras.engine import topology [as 别名]
# 或者: from keras.engine.topology import Input [as 别名]
def test_trainable_weights_count_consistency():
"""Tests the trainable weights consistency check of Model.
This verifies that a warning is shown if model.trainable is modified
and the model is summarized/run without a new call to .compile()
Reproduce issue #8121
"""
a = Input(shape=(3,), name='input_a')
model1 = Model(inputs=a, outputs=Dense(1)(a))
model1.trainable = False
b = Input(shape=(3,), name='input_b')
y = model1(b)
model2 = Model(inputs=b, outputs=Dense(1)(y))
model2.compile(optimizer='adam', loss='mse')
model1.trainable = True
# Should warn on .summary()
with pytest.warns(UserWarning) as w:
model2.summary()
warning_raised = any(['Discrepancy' in str(w_.message) for w_ in w])
assert warning_raised, 'No warning raised when trainable is modified without .compile.'
# And on .fit()
with pytest.warns(UserWarning) as w:
model2.fit(x=np.zeros((5, 3)), y=np.zeros((5, 1)))
warning_raised = any(['Discrepancy' in str(w_.message) for w_ in w])
assert warning_raised, 'No warning raised when trainable is modified without .compile.'
# And shouldn't warn if we recompile
model2.compile(optimizer='adam', loss='mse')
with pytest.warns(None) as w:
model2.summary()
assert len(w) == 0, "Warning raised even when .compile() is called after modifying .trainable"