View source on GitHub |
Just your regular densely-connected NN layer.
tf.keras.layers.Dense(
units,
activation=None,
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,
**kwargs
)
Dense
implements the operation:
output = activation(dot(input, kernel) + bias)
where activation
is the element-wise activation function
passed as the activation
argument, kernel
is a weights matrix
created by the layer, and bias
is a bias vector created by the layer
(only applicable if use_bias
is True
). These are all attributes of
Dense
.
Besides, layer attributes cannot be modified after the layer has been called
once (except the trainable
attribute).
When a popular kwarg input_shape
is passed, then keras will create
an input layer to insert before the current layer. This can be treated
equivalent to explicitly defining an InputLayer
.
Example:
# Create a `Sequential` model and add a Dense layer as the first layer.
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(16,)))
model.add(tf.keras.layers.Dense(32, activation='relu'))
# Now the model will take as input arrays of shape (None, 16)
# and output arrays of shape (None, 32).
# Note that after the first layer, you don't need to specify
# the size of the input anymore:
model.add(tf.keras.layers.Dense(32))
model.output_shape
(None, 32)
Input shape | |
---|---|
N-D tensor with shape: (batch_size, ..., input_dim) .
The most common situation would be
a 2D input with shape (batch_size, input_dim) .
|
Output shape | |
---|---|
N-D tensor with shape: (batch_size, ..., units) .
For instance, for a 2D input with shape (batch_size, input_dim) ,
the output would have shape (batch_size, units) .
|