本文整理匯總了Python中tensorflow.keras.Sequential方法的典型用法代碼示例。如果您正苦於以下問題:Python keras.Sequential方法的具體用法?Python keras.Sequential怎麽用?Python keras.Sequential使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.keras
的用法示例。
在下文中一共展示了keras.Sequential方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def __init__(self, out_features,**kwargs):
super(_DenseLayer, self).__init__(**kwargs)
k_reg = None if w_decay is None else l2(w_decay)
self.layers = []
self.layers.append(tf.keras.Sequential(
[
layers.ReLU(),
layers.Conv2D(
filters=out_features, kernel_size=(3,3), strides=(1,1), padding='same',
use_bias=True, kernel_initializer=weight_init,
kernel_regularizer=k_reg),
layers.BatchNormalization(),
layers.ReLU(),
layers.Conv2D(
filters=out_features, kernel_size=(3,3), strides=(1,1), padding='same',
use_bias=True, kernel_initializer=weight_init,
kernel_regularizer=k_reg),
layers.BatchNormalization(),
])) # first relu can be not needed
示例2: __call__
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def __call__(self, model):
"""
:param model: Keras model to be accelerated
:type model: Union[keras.Model, keras.Sequential]
:return: Accelerated Keras model
:rtype: Union[keras.Model, keras.Sequential]
"""
if isinstance(model, tfk.Model) or isinstance(model, tfk.Sequential):
self.model = model
else:
raise TypeError(f'FastMCInference expects tensorflow.keras Model, you gave {type(model)}')
new_input = tfk.layers.Input(shape=(self.model.input_shape[1:]), name='input')
mc_model = tfk.models.Model(inputs=self.model.inputs, outputs=self.model.outputs)
mc = FastMCInferenceMeanVar()(tfk.layers.TimeDistributed(mc_model)(FastMCRepeat(self.n)(new_input)))
new_mc_model = tfk.models.Model(inputs=new_input, outputs=mc)
return new_mc_model
示例3: build
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def build(self, input_shape):
assert isinstance(input_shape, list)
layer_kwargs = dict(
kernel_initializer=self.kernel_initializer,
bias_initializer=self.bias_initializer,
kernel_regularizer=self.kernel_regularizer,
bias_regularizer=self.bias_regularizer,
kernel_constraint=self.kernel_constraint,
bias_constraint=self.bias_constraint
)
mlp_layers = []
for i, channels in enumerate(self.mlp_hidden):
mlp_layers.append(
Dense(channels, self.mlp_activation, **layer_kwargs)
)
mlp_layers.append(
Dense(self.k, 'softmax', **layer_kwargs)
)
self.mlp = Sequential(mlp_layers)
super().build(input_shape)
示例4: build_nn_model
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def build_nn_model(input_shape, nn_define, loss, optimizer, metrics,
is_supported_layer=has_builder,
default_layer=None) -> KerasNNModel:
model = Sequential()
is_first_layer = True
for layer_config in nn_define:
layer = layer_config.get("layer", default_layer)
if layer and is_supported_layer(layer):
del layer_config["layer"]
if is_first_layer:
layer_config["input_shape"] = input_shape
is_first_layer = False
builder = get_builder(layer)
model.add(builder(**layer_config))
else:
raise ValueError(f"dnn not support layer {layer}")
return from_keras_sequential_model(model=model,
loss=loss,
optimizer=optimizer,
metrics=metrics)
示例5: build_model
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def build_model(input_shape):
"""Build a logistic regression model with tf.keras."""
model = keras.Sequential(
[
layers.Dense(
1, use_bias=False, activation="sigmoid", input_shape=[input_shape]
),
]
)
model.compile(
loss="binary_crossentropy",
optimizer=tf.train.AdamOptimizer(),
metrics=["accuracy"],
)
return model
示例6: _build_model
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def _build_model(self, num_classes, image_size):
units = self._knobs['hidden_layer_units']
layers = self._knobs['hidden_layer_count']
lr = self._knobs['learning_rate']
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(image_size, image_size, 3)))
model.add(keras.layers.BatchNormalization())
for _ in range(layers):
model.add(keras.layers.Dense(units, activation=tf.nn.relu))
model.add(keras.layers.Dense(
num_classes,
activation=tf.nn.softmax
))
model.compile(
optimizer=keras.optimizers.Adam(lr=lr),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
return model
示例7: ConvLayer
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def ConvLayer(conv_function=Conv2D,
filters=[32, 64, 64],
kernels=[[8, 8], [4, 4], [3, 3]],
strides=[[4, 4], [2, 2], [1, 1]],
padding='valid',
activation='relu'):
'''
Params:
conv_function: the convolution function
filters: list of flitter of all hidden conv layers
kernels: list of kernel of all hidden conv layers
strides: list of stride of all hidden conv layers
padding: padding mode
activation: activation function
Return:
A sequential of multi-convolution layers, with Flatten.
'''
layers = Sequential([conv_function(filters=f, kernel_size=k, strides=s, padding=padding, activation=activation) for f, k, s in zip(filters, kernels, strides)])
layers.add(Flatten())
return layers
示例8: create_keras_model
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def create_keras_model(input_dim, learning_rate, window_size):
"""Creates Keras model for regression.
Args:
input_dim: How many features the input has
learning_rate: Learning rate for training
Returns:
The compiled Keras model (still needs to be trained)
"""
model = keras.Sequential([
layers.LSTM(4, dropout = 0.2, input_shape = (input_dim, window_size)),
layers.Dense(1)
])
model.compile(loss='mean_squared_error', optimizer=tf.train.AdamOptimizer(
learning_rate=learning_rate))
return model
示例9: __init__
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def __init__(self, num_layers=12, filters=24, num_classes=10, dropout_rate=0.0):
super().__init__()
self.num_layers = num_layers
self.stem = Sequential([
Conv2D(filters, kernel_size=3, padding='same', use_bias=False),
BatchNormalization()
])
labels = ['layer_{}'.format(i) for i in range(num_layers)]
self.enas_layers = []
for i in range(num_layers):
layer = ENASLayer(labels[i], labels[:i], filters)
self.enas_layers.append(layer)
pool_num = 2
self.pool_distance = num_layers // (pool_num + 1)
self.pool_layers = [FactorizedReduce(filters) for _ in range(pool_num)]
self.gap = GlobalAveragePooling2D()
self.dropout = Dropout(dropout_rate)
self.dense = Dense(num_classes)
示例10: create_keras_model
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def create_keras_model():
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation="relu", input_shape=(32, )))
# Add another:
model.add(layers.Dense(64, activation="relu"))
# Add a softmax layer with 10 output units:
model.add(layers.Dense(10, activation="softmax"))
model.compile(
optimizer=keras.optimizers.RMSprop(0.01),
loss=keras.losses.categorical_crossentropy,
metrics=[keras.metrics.categorical_accuracy])
return model
# __tf_model_end__
# yapf: enable
# yapf: disable
# __ray_start__
示例11: test_load_persist
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def test_load_persist(self):
# define the model.
model = Sequential()
model.add(Dense(16, input_shape=(10,)))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy')
# fetch activations.
x = np.ones((2, 10))
activations = get_activations(model, x)
# persist the activations to the disk.
output = 'activations.json'
persist_to_json_file(activations, output)
# read them from the disk.
activations2 = load_activations_from_json_file(output)
for a1, a2 in zip(list(activations.values()), list(activations2.values())):
np.testing.assert_almost_equal(a1, a2)
示例12: eval_batch
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def eval_batch(o, ims, allow_input_layer = False):
layer_functions, has_input_layer = (
get_layer_functions (o) if isinstance (o, (keras.Sequential, keras.Model))
# TODO: Check it's sequential? --------------------------------------^
else o)
having_input_layer = allow_input_layer and has_input_layer
activations = []
for l, func in enumerate(layer_functions):
if not having_input_layer:
if l==0:
activations.append(func([ims])[0])
else:
activations.append(func([activations[l-1]])[0])
else:
if l==0:
activations.append([]) #activations.append(func([ims])[0])
elif l==1:
activations.append(func([ims])[0])
else:
activations.append(func([activations[l-1]])[0])
return activations
示例13: build_model
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def build_model(hp):
model = keras.Sequential()
model.add(layers.Flatten(input_shape=(28, 28)))
min_layers = 2
max_layers = 5
for i in range(hp.Int('num_layers', min_layers, max_layers)):
model.add(layers.Dense(units=hp.Int('units_' + str(i),
32,
256,
32,
parent_name='num_layers',
parent_values=list(range(i + 1, max_layers + 1))),
activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(
optimizer=keras.optimizers.Adam(1e-4),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
示例14: test_checkpoint_removal
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def test_checkpoint_removal(tmp_dir):
def build_model(hp):
model = keras.Sequential([
keras.layers.Dense(hp.Int('size', 5, 10)),
keras.layers.Dense(1)])
model.compile('sgd', 'mse', metrics=['accuracy'])
return model
tuner = kerastuner.Tuner(
oracle=kerastuner.tuners.randomsearch.RandomSearchOracle(
objective='val_accuracy',
max_trials=1,
seed=1337),
hypermodel=build_model,
directory=tmp_dir,
)
x, y = np.ones((1, 5)), np.ones((1, 1))
tuner.search(x,
y,
validation_data=(x, y),
epochs=21)
trial = list(tuner.oracle.trials.values())[0]
trial_id = trial.trial_id
assert tf.io.gfile.exists(tuner._get_checkpoint_fname(trial_id, 20))
assert not tf.io.gfile.exists(tuner._get_checkpoint_fname(trial_id, 10))
示例15: __init__
# 需要導入模塊: from tensorflow import keras [as 別名]
# 或者: from tensorflow.keras import Sequential [as 別名]
def __init__(self, filters):
super(Attention_block, self).__init__()
self.W_g = Sequential([
Conv2D(filters, kernel_size=1, strides=1, padding='same'),
BatchNormalization()
])
self.W_x = Sequential([
Conv2D(filters, kernel_size=1, strides=1, padding='same'),
BatchNormalization()
])
self.psi = Sequential([
Conv2D(filters, kernel_size=1, strides=1, padding='same'),
BatchNormalization(),
Activation('sigmoid')
])
self.relu = Activation('relu')