本文整理匯總了Python中tensorflow.python.keras.layers.Dense方法的典型用法代碼示例。如果您正苦於以下問題:Python layers.Dense方法的具體用法?Python layers.Dense怎麽用?Python layers.Dense使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.python.keras.layers
的用法示例。
在下文中一共展示了layers.Dense方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create_model
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def create_model(node_size, hidden_size=[256, 128], l1=1e-5, l2=1e-4):
A = Input(shape=(node_size,))
L = Input(shape=(None,))
fc = A
for i in range(len(hidden_size)):
if i == len(hidden_size) - 1:
fc = Dense(hidden_size[i], activation='relu',
kernel_regularizer=l1_l2(l1, l2), name='1st')(fc)
else:
fc = Dense(hidden_size[i], activation='relu',
kernel_regularizer=l1_l2(l1, l2))(fc)
Y = fc
for i in reversed(range(len(hidden_size) - 1)):
fc = Dense(hidden_size[i], activation='relu',
kernel_regularizer=l1_l2(l1, l2))(fc)
A_ = Dense(node_size, 'relu', name='2nd')(fc)
model = Model(inputs=[A, L], outputs=[A_, Y])
emb = Model(inputs=A, outputs=Y)
return model, emb
示例2: crnn_model
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def crnn_model(width=100, n_vars=6, n_classes=7, conv_kernel_size=5,
conv_filters=3, lstm_units=3):
input_shape = (width, n_vars)
model = Sequential()
model.add(Conv1D(filters=conv_filters, kernel_size=conv_kernel_size,
padding='valid', activation='relu', input_shape=input_shape))
model.add(Conv1D(filters=conv_filters, kernel_size=conv_kernel_size,
padding='valid', activation='relu'))
model.add(LSTM(units=lstm_units, dropout=0.1, recurrent_dropout=0.1))
model.add(Dense(n_classes, activation="softmax"))
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
return model
# load the data
示例3: crnn_model
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def crnn_model(width=100, n_vars=6, n_classes=7, conv_kernel_size=5,
conv_filters=2, lstm_units=2):
# create a crnn model with keras with one cnn layers, and one rnn layer
input_shape = (width, n_vars)
model = Sequential()
model.add(Conv1D(filters=conv_filters, kernel_size=conv_kernel_size,
padding='valid', activation='relu', input_shape=input_shape))
model.add(LSTM(units=lstm_units, dropout=0.1, recurrent_dropout=0.1))
model.add(Dense(n_classes, activation="softmax"))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
# load the data
示例4: crnn_model
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def crnn_model(width=100, n_vars=6, n_classes=7, conv_kernel_size=5,
conv_filters=3, lstm_units=3):
input_shape = (width, n_vars)
model = Sequential()
model.add(Conv1D(filters=conv_filters, kernel_size=conv_kernel_size,
padding='valid', activation='relu', input_shape=input_shape))
model.add(LSTM(units=lstm_units, dropout=0.1, recurrent_dropout=0.1))
model.add(Dense(n_classes, activation="softmax"))
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
return model
##############################################
# Setup
##############################################
# load the data
示例5: build
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def build(self, input_layer):
last_layer = input_layer
input_shape = K.int_shape(input_layer)
if self.with_embedding:
if input_shape[-1] != 1:
raise ValueError("Only one feature (the index) can be used with embeddings, "
"i.e. the input shape should be (num_samples, length, 1). "
"The actual shape was: " + str(input_shape))
last_layer = Lambda(lambda x: K.squeeze(x, axis=-1),
output_shape=K.int_shape(last_layer)[:-1])(last_layer) # Remove feature dimension.
last_layer = Embedding(self.embedding_size, self.embedding_dimension,
input_length=input_shape[-2])(last_layer)
for _ in range(self.num_layers):
last_layer = Dense(self.num_units, activation=self.activation)(last_layer)
if self.with_bn:
last_layer = BatchNormalization()(last_layer)
if not np.isclose(self.p_dropout, 0):
last_layer = Dropout(self.p_dropout)(last_layer)
return last_layer
示例6: build
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def build(self, input_shapes):
self.dense_layers = [Dense(
self.input_dim, activation='relu', use_bias=True, kernel_regularizer=l2(self.l2_reg))]
self.neigh_weights = self.add_weight(
shape=(self.input_dim * 2, self.output_dim),
initializer=glorot_uniform(
seed=self.seed),
regularizer=l2(self.l2_reg),
name="neigh_weights")
if self.use_bias:
self.bias = self.add_weight(shape=(self.output_dim,),
initializer=Zeros(),
name='bias_weight')
self.built = True
示例7: _mock_logits_layer
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def _mock_logits_layer(kernel, bias):
"""Sets initialization values to dense `logits` layers used in context."""
class _MockDenseLayer(keras_layers.Dense):
def __init__(self, units, activation, name):
kwargs = {}
if name == 'logits':
kwargs = {
'kernel_initializer': tf.compat.v1.initializers.constant(kernel),
'bias_initializer': tf.compat.v1.initializers.constant(bias)
}
super(_MockDenseLayer, self).__init__(
units=units, name=name, activation=activation, **kwargs)
return tf.compat.v1.test.mock.patch.object(keras_layers, 'Dense',
_MockDenseLayer)
示例8: _build_dense
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def _build_dense(units, activation, use_bias=True, kernel_initializer="glorot_uniform",
bias_initializer="zeros", kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None,
kernel_constraint=None, bias_constraint=None, seed=None, **kwargs):
return layers.Dense(units=units,
activation=activation,
use_bias=use_bias,
kernel_initializer=_get_initializer(kernel_initializer, seed),
bias_initializer=_get_initializer(bias_initializer, seed),
kernel_regularizer=kernel_regularizer,
bias_regularizer=bias_regularizer,
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs)
示例9: test_mnist_unet_with_shape_valid
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def test_mnist_unet_with_shape_valid(self):
num_subsamples = 100
(x_train, y_train), (x_test, y_test) = TestUtil.get_mnist(flattened=False, num_subsamples=num_subsamples)
explained_model_builder = MLPModelBuilder(num_layers=2, num_units=64, activation="relu", p_dropout=0.2,
verbose=0, batch_size=256, learning_rate=0.001, num_epochs=2,
early_stopping_patience=128)
input_shape = x_train.shape[1:]
input_layer = Input(shape=input_shape)
last_layer = Flatten()(input_layer)
last_layer = explained_model_builder.build(last_layer)
last_layer = Dense(y_train.shape[-1], activation="softmax")(last_layer)
explained_model = Model(input_layer, last_layer)
explained_model.compile(loss="categorical_crossentropy",
optimizer="adam")
explained_model.fit(x_train, y_train)
masking_operation = ZeroMasking()
loss = categorical_crossentropy
downsample_factors = [(2, 2), (4, 4), (4, 7), (7, 4), (7, 7)]
with_bns = [True if i % 2 == 0 else False for i in range(len(downsample_factors))]
for downsample_factor, with_bn in zip(downsample_factors, with_bns):
model_builder = UNetModelBuilder(downsample_factor, num_layers=2, num_units=64, activation="relu",
p_dropout=0.2, verbose=0, batch_size=256, learning_rate=0.001,
num_epochs=2, early_stopping_patience=128, with_bn=with_bn)
explainer = CXPlain(explained_model, model_builder, masking_operation, loss,
downsample_factors=downsample_factor)
explainer.fit(x_train, y_train)
eval_score = explainer.score(x_test, y_test)
train_score = explainer.get_last_fit_score()
median = explainer.predict(x_test)
self.assertTrue(median.shape == x_test.shape)
示例10: build
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def build(self, input_layer):
last_layer = input_layer
for _ in range(self.num_layers):
last_layer = Dense(self.num_units, activation=self.activation)(last_layer)
if self.with_bn:
last_layer = BatchNormalization()(last_layer)
if not np.isclose(self.p_dropout, 0):
last_layer = Dropout(self.p_dropout)(last_layer)
return last_layer
示例11: trivial_model
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def trivial_model(num_classes):
"""Trivial model for ImageNet dataset."""
input_shape = (224, 224, 3)
img_input = layers.Input(shape=input_shape)
x = layers.Lambda(lambda x: backend.reshape(x, [-1, 224 * 224 * 3]),
name='reshape')(img_input)
x = layers.Dense(1, name='fc1')(x)
x = layers.Dense(num_classes, name='fc1000')(x)
x = layers.Activation('softmax', dtype='float32')(x)
return models.Model(img_input, x, name='trivial')
開發者ID:ShivangShekhar,項目名稱:Live-feed-object-device-identification-using-Tensorflow-and-OpenCV,代碼行數:15,代碼來源:trivial_model.py
示例12: create_tf_keras_model
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def create_tf_keras_model():
model = tf.keras.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(32,)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.002,
epsilon=1e-08,
name='Eve'),
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
示例13: create_tf_keras_model
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def create_tf_keras_model():
model = tf.keras.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(32,)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
示例14: architecture
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def architecture(inputs):
""" Architecture of model """
conv1 = Conv2D(32, kernel_size=(3, 3),
activation='relu')(inputs)
max1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(32, (3, 3), activation='relu')(max1)
max2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(64, (3, 3), activation='relu')(max2)
max3 = MaxPooling2D(pool_size=(2, 2))(conv3)
flat1 = Flatten()(max3)
dense1 = Dense(64, activation='relu')(flat1)
drop1 = Dropout(0.5)(dense1)
return drop1
示例15: WDL
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Dense [as 別名]
def WDL(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(128, 128), l2_reg_linear=1e-5,
l2_reg_embedding=1e-5, l2_reg_dnn=0, seed=1024, dnn_dropout=0, dnn_activation='relu',
task='binary'):
"""Instantiates the Wide&Deep Learning architecture.
:param linear_feature_columns: An iterable containing all the features used by linear part of the model.
:param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
:param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of DNN
:param l2_reg_linear: float. L2 regularizer strength applied to wide part
:param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
:param l2_reg_dnn: float. L2 regularizer strength applied to DNN
:param seed: integer ,to use as random seed.
:param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
:param dnn_activation: Activation function to use in DNN
:param task: str, ``"binary"`` for binary logloss or ``"regression"`` for regression loss
:return: A Keras model instance.
"""
features = build_input_features(
linear_feature_columns + dnn_feature_columns)
inputs_list = list(features.values())
linear_logit = get_linear_logit(features, linear_feature_columns, seed=seed, prefix='linear',
l2_reg=l2_reg_linear)
sparse_embedding_list, dense_value_list = input_from_feature_columns(features, dnn_feature_columns,
l2_reg_embedding, seed)
dnn_input = combined_dnn_input(sparse_embedding_list, dense_value_list)
dnn_out = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout,
False, seed)(dnn_input)
dnn_logit = Dense(
1, use_bias=False, activation=None)(dnn_out)
final_logit = add_func([dnn_logit, linear_logit])
output = PredictionLayer(task)(final_logit)
model = Model(inputs=inputs_list, outputs=output)
return model