本文整理汇总了Python中tensorflow.keras.models.Model方法的典型用法代码示例。如果您正苦于以下问题:Python models.Model方法的具体用法?Python models.Model怎么用?Python models.Model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.models
的用法示例。
在下文中一共展示了models.Model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_single_ddpg_input
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def test_single_ddpg_input():
nb_actions = 2
actor = Sequential()
actor.add(Flatten(input_shape=(2, 3)))
actor.add(Dense(nb_actions))
action_input = Input(shape=(nb_actions,), name='action_input')
observation_input = Input(shape=(2, 3), name='observation_input')
x = Concatenate()([action_input, Flatten()(observation_input)])
x = Dense(1)(x)
critic = Model(inputs=[action_input, observation_input], outputs=x)
memory = SequentialMemory(limit=10, window_length=2)
agent = DDPGAgent(actor=actor, critic=critic, critic_action_input=action_input, memory=memory,
nb_actions=2, nb_steps_warmup_critic=5, nb_steps_warmup_actor=5, batch_size=4)
agent.compile('sgd')
agent.fit(MultiInputTestEnv((3,)), nb_steps=10)
示例2: test_single_continuous_dqn_input
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def test_single_continuous_dqn_input():
nb_actions = 2
V_model = Sequential()
V_model.add(Flatten(input_shape=(2, 3)))
V_model.add(Dense(1))
mu_model = Sequential()
mu_model.add(Flatten(input_shape=(2, 3)))
mu_model.add(Dense(nb_actions))
L_input = Input(shape=(2, 3))
L_input_action = Input(shape=(nb_actions,))
x = Concatenate()([Flatten()(L_input), L_input_action])
x = Dense(((nb_actions * nb_actions + nb_actions) // 2))(x)
L_model = Model(inputs=[L_input_action, L_input], outputs=x)
memory = SequentialMemory(limit=10, window_length=2)
agent = NAFAgent(nb_actions=nb_actions, V_model=V_model, L_model=L_model, mu_model=mu_model,
memory=memory, nb_steps_warmup=5, batch_size=4)
agent.compile('sgd')
agent.fit(MultiInputTestEnv((3,)), nb_steps=10)
示例3: make_densenet121_resisc_model
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def make_densenet121_resisc_model(**model_kwargs) -> tf.keras.Model:
# Load ImageNet pre-trained DenseNet
model_notop = DenseNet121(
include_top=False, weights=None, input_shape=(224, 224, 3)
)
# Add new layers
x = GlobalAveragePooling2D()(model_notop.output)
predictions = Dense(num_classes, activation="softmax")(x)
# Create graph of new model and freeze pre-trained layers
new_model = Model(inputs=model_notop.input, outputs=predictions)
for layer in new_model.layers[:-1]:
layer.trainable = False
if "bn" == layer.name[-2:]: # allow batchnorm layers to be trainable
layer.trainable = True
# compile the model
new_model.compile(
optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]
)
return new_model
示例4: build_model
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def build_model(self):
inputs = Input(shape=(self.max_len,))
x = Embedding(len(self.embeddings),
300,
weights=[self.embeddings],
trainable=False)(inputs)
x = Lambda(lambda t: tf.reduce_mean(t, axis=1))(x)
x = Dense(128, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(16, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
model.summary()
return model
示例5: __init__
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
with graph.as_default():
if sess is not None:
set_session(sess)
inp = None
output = None
if self.shared_network is None:
inp = Input((self.input_dim,))
output = self.get_network_head(inp).output
else:
inp = self.shared_network.input
output = self.shared_network.output
output = Dense(
self.output_dim, activation=self.activation,
kernel_initializer='random_normal')(output)
self.model = Model(inp, output)
self.model.compile(
optimizer=SGD(lr=self.lr), loss=self.loss)
示例6: get_network_head
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def get_network_head(inp):
output = Dense(256, activation='sigmoid',
kernel_initializer='random_normal')(inp)
output = BatchNormalization()(output)
output = Dropout(0.1)(output)
output = Dense(128, activation='sigmoid',
kernel_initializer='random_normal')(output)
output = BatchNormalization()(output)
output = Dropout(0.1)(output)
output = Dense(64, activation='sigmoid',
kernel_initializer='random_normal')(output)
output = BatchNormalization()(output)
output = Dropout(0.1)(output)
output = Dense(32, activation='sigmoid',
kernel_initializer='random_normal')(output)
output = BatchNormalization()(output)
output = Dropout(0.1)(output)
return Model(inp, output)
示例7: construct_q_network
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def construct_q_network(self):
# replacement of the Convolution layers by Dense layers, and change the size of the input space and output space
# Uses the network architecture found in DeepMind paper
self.model = Sequential()
input_layer = Input(shape=(self.observation_size * self.training_param.NUM_FRAMES,))
layer1 = Dense(self.observation_size * self.training_param.NUM_FRAMES)(input_layer)
layer1 = Activation('relu')(layer1)
layer2 = Dense(self.observation_size)(layer1)
layer2 = Activation('relu')(layer2)
layer3 = Dense(self.observation_size)(layer2)
layer3 = Activation('relu')(layer3)
layer4 = Dense(2 * self.action_size)(layer3)
layer4 = Activation('relu')(layer4)
output = Dense(self.action_size)(layer4)
self.model = Model(inputs=[input_layer], outputs=[output])
self.model.compile(loss='mse', optimizer=Adam(lr=self.lr_))
self.target_model = Model(inputs=[input_layer], outputs=[output])
self.target_model.compile(loss='mse', optimizer=Adam(lr=self.lr_))
self.target_model.set_weights(self.model.get_weights())
示例8: _build_q_NN
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def _build_q_NN(self):
input_states = Input(shape=(self.observation_size,))
input_action = Input(shape=(self.action_size,))
input_layer = Concatenate()([input_states, input_action])
lay1 = Dense(self.observation_size)(input_layer)
lay1 = Activation('relu')(lay1)
lay2 = Dense(self.observation_size)(lay1)
lay2 = Activation('relu')(lay2)
lay3 = Dense(2*self.action_size)(lay2)
lay3 = Activation('relu')(lay3)
advantage = Dense(1, activation = 'linear')(lay3)
model = Model(inputs=[input_states, input_action], outputs=[advantage])
model.compile(loss='mse', optimizer=Adam(lr=self.lr_))
return model
示例9: image_model
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def image_model(lr=0.0001):
input_1 = Input(shape=(None, None, 3))
base_model = ResNet50(weights='imagenet', include_top=False)
x1 = base_model(input_1)
x1 = GlobalMaxPool2D()(x1)
dense_1 = Dense(vec_dim, activation="linear", name="dense_image_1")
x1 = dense_1(x1)
_norm = Lambda(lambda x: K.l2_normalize(x, axis=-1))
x1 = _norm(x1)
model = Model([input_1], x1)
model.compile(loss="mae", optimizer=Adam(lr))
model.summary()
return model
示例10: text_model
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def text_model(vocab_size, lr=0.0001):
input_2 = Input(shape=(None,))
embed = Embedding(vocab_size, 50, name="embed")
gru = Bidirectional(GRU(256, return_sequences=True), name="gru_1")
dense_2 = Dense(vec_dim, activation="linear", name="dense_text_1")
x2 = embed(input_2)
x2 = gru(x2)
x2 = GlobalMaxPool1D()(x2)
x2 = dense_2(x2)
_norm = Lambda(lambda x: K.l2_normalize(x, axis=-1))
x2 = _norm(x2)
model = Model([input_2], x2)
model.compile(loss="mae", optimizer=Adam(lr))
model.summary()
return model
示例11: get_model
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def get_model():
"""Returns sample model."""
xi = Input((28, 28, 1), name="input") # pylint: disable=undefined-variable
x = Conv2D(32, 3, strides=1, padding="same", name="c1")(xi) # pylint: disable=undefined-variable
x = BatchNormalization(name="b1")(x) # pylint: disable=undefined-variable
x = Activation("relu", name="a1")(x) # pylint: disable=undefined-variable
x = MaxPooling2D(2, 2, name="mp1")(x) # pylint: disable=undefined-variable
x = QConv2D(32, 3, kernel_quantizer="binary", bias_quantizer="binary", # pylint: disable=undefined-variable
strides=1, padding="same", name="c2")(x)
x = QBatchNormalization(name="b2")(x) # pylint: disable=undefined-variable
x = QActivation("binary", name="a2")(x) # pylint: disable=undefined-variable
x = MaxPooling2D(2, 2, name="mp2")(x) # pylint: disable=undefined-variable
x = QConv2D(32, 3, kernel_quantizer="ternary", bias_quantizer="ternary", # pylint: disable=undefined-variable
strides=1, padding="same", activation="binary", name="c3")(x)
x = Flatten(name="flatten")(x) # pylint: disable=undefined-variable
x = Dense(1, name="dense", activation="softmax")(x) # pylint: disable=undefined-variable
model = Model(inputs=xi, outputs=x)
return model
示例12: build_model
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def build_model(dist: Union[Distribution, PixelCNN], input_shape: tuple = None, filepath: str = None) \
-> Tuple[tf.keras.Model, Union[Distribution, PixelCNN]]:
"""
Create tf.keras.Model from TF distribution.
Parameters
----------
dist
TensorFlow distribution.
input_shape
Input shape of the model.
Returns
-------
TensorFlow model.
"""
x_in = Input(shape=input_shape)
log_prob = dist.log_prob(x_in)
model = Model(inputs=x_in, outputs=log_prob)
model.add_loss(-tf.reduce_mean(log_prob))
if isinstance(filepath, str):
model.load_weights(filepath)
return model, dist
示例13: __init__
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def __init__(self, model: tf.keras.Model, hidden_layer: int, output_dim: int, hidden_dim: int = None) -> None:
"""
Dense layer that extracts the feature map of a hidden layer in a model and computes
output probabilities over that layer.
Parameters
----------
model
tf.keras classification model.
hidden_layer
Hidden layer from model where feature map is extracted from.
output_dim
Output dimension for softmax layer.
hidden_dim
Dimension of optional additional dense layer.
"""
super(DenseHidden, self).__init__()
self.partial_model = Model(inputs=model.inputs, outputs=model.layers[hidden_layer].output)
for layer in self.partial_model.layers: # freeze model layers
layer.trainable = False
self.hidden_dim = hidden_dim
if hidden_dim is not None:
self.dense_layer = Dense(hidden_dim, activation=tf.nn.relu)
self.output_layer = Dense(output_dim, activation=tf.nn.softmax)
示例14: build_model
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def build_model(self, n_inputs, n_outputs):
"""Q Network is 256-256-256 MLP
Arguments:
n_inputs (int): input dim
n_outputs (int): output dim
Return:
q_model (Model): DQN
"""
inputs = Input(shape=(n_inputs, ), name='state')
x = Dense(256, activation='relu')(inputs)
x = Dense(256, activation='relu')(x)
x = Dense(256, activation='relu')(x)
x = Dense(n_outputs,
activation='linear',
name='action')(x)
q_model = Model(inputs, x)
q_model.summary()
return q_model
示例15: compile
# 需要导入模块: from tensorflow.keras import models [as 别名]
# 或者: from tensorflow.keras.models import Model [as 别名]
def compile(self, optimizer, metrics=[]):
metrics += [mean_q] # register default metrics
# We never train the target model, hence we can set the optimizer and loss arbitrarily.
self.target_model = clone_model(self.model, self.custom_model_objects)
self.target_model.compile(optimizer='sgd', loss='mse')
self.model.compile(optimizer='sgd', loss='mse')
# Compile model.
if self.target_model_update < 1.:
# We use the `AdditionalUpdatesOptimizer` to efficiently soft-update the target model.
updates = get_soft_target_model_updates(self.target_model, self.model, self.target_model_update)
optimizer = AdditionalUpdatesOptimizer(optimizer, updates)
def clipped_masked_error(args):
y_true, y_pred, mask = args
loss = huber_loss(y_true, y_pred, self.delta_clip)
loss *= mask # apply element-wise mask
return K.sum(loss, axis=-1)
# Create trainable model. The problem is that we need to mask the output since we only
# ever want to update the Q values for a certain action. The way we achieve this is by
# using a custom Lambda layer that computes the loss. This gives us the necessary flexibility
# to mask out certain parameters by passing in multiple inputs to the Lambda layer.
y_pred = self.model.output
y_true = Input(name='y_true', shape=(self.nb_actions,))
mask = Input(name='mask', shape=(self.nb_actions,))
loss_out = Lambda(clipped_masked_error, output_shape=(1,), name='loss')([y_true, y_pred, mask])
ins = [self.model.input] if type(self.model.input) is not list else self.model.input
trainable_model = Model(inputs=ins + [y_true, mask], outputs=[loss_out, y_pred])
assert len(trainable_model.output_names) == 2
combined_metrics = {trainable_model.output_names[1]: metrics}
losses = [
lambda y_true, y_pred: y_pred, # loss is computed in Lambda layer
lambda y_true, y_pred: K.zeros_like(y_pred), # we only include this for the metrics
]
trainable_model.compile(optimizer=optimizer, loss=losses, metrics=combined_metrics)
self.trainable_model = trainable_model
self.compiled = True