Skip to content
TensorFlow

Layers

Use core layers and build a custom one.

By EZ4Code Team
layerscustom

Code

import tensorflow as tf

# Common built-in layers
dense = tf.keras.layers.Dense(64, activation="relu", kernel_regularizer="l2")
conv = tf.keras.layers.Conv2D(32, 3, padding="same", activation="relu")
pool = tf.keras.layers.MaxPooling2D(2)
flat = tf.keras.layers.Flatten()
dropout = tf.keras.layers.Dropout(0.5)
batchnorm = tf.keras.layers.BatchNormalization()
lstm = tf.keras.layers.LSTM(64, return_sequences=False)
embed = tf.keras.layers.Embedding(input_dim=10000, output_dim=128)

# Custom layer
class ScaleLayer(tf.keras.layers.Layer):
    def __init__(self, factor=2.0, **kwargs):
        super().__init__(**kwargs)
        self.factor = factor

    def build(self, input_shape):
        self.bias = self.add_weight("bias", shape=(1,), initializer="zeros")
        super().build(input_shape)

    def call(self, inputs):
        return inputs * self.factor + self.bias

layer = ScaleLayer(factor=3.0)
print(layer(tf.constant([1.0, 2.0])))

Explanation

Keras ships with Dense, Conv2D, LSTM, Embedding, BatchNormalization, and Dropout covering most architectures. A custom layer subclasses Layer, creates weights in build, and defines the forward computation in call. Regularizers and initializers are passed as strings or objects for reuse.

More TensorFlow Snippets