agentleFS
G1JoshiG1Joshi/Agent-Skills/skills/ai-ml/keras/SKILL.md

keras

Skill15 starsChanged 44 days agoMITView on GitHub

Expert Keras 3 assistance covering multi-backend execution (JAX, PyTorch, TensorFlow), Sequential/Functional APIs, and custom training loops. Use when building deep learning models with clean, modular Python abstractions.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use keras.ops instead of backend-specific tensor libraries (torch.* or tf.*) in custom layers.

    line 166
  2. Very specific:

    Always include EarlyStopping with restore_best_weights=True to prevent overfitting.

    line 168
  3. Very specific:

    Use legacy keras 2.x patterns tied exclusively to tf.keras.

    line 172
  4. Very specific:

    Write custom training loops unless necessary.

    line 173

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Keras

Keras 3 is a game changer: it is now multi-backend. You can write Keras code and run it on top of JAX, PyTorch, or TensorFlow.

When to Use

  • Multi-Backend Deep Learning (Keras 3): Writing models that run interchangeably on JAX, PyTorch, or TensorFlow.
  • Rapid Neural Network Prototyping: Developing vision, NLP, and tabular architectures with clean, intuitive APIs.
  • Production Model Deployment: Exporting models to ONNX, TensorRT, or TensorFlow Lite.
  • Custom Layers & Loss Functions: Subclassing keras.layers.Layer with backend-agnostic keras.ops.

Quick Start

import os
os.environ["KERAS_BACKEND"] = "torch" # Choose: jax, torch, or tensorflow
import keras
from keras import layers

# Build classifier using Functional API
inputs = keras.Input(shape=(28, 28, 1))
x = layers.Conv2D(32, kernel_size=(3, 3), activation="relu")(inputs)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)
x = layers.Flatten()(x)
outputs = layers.Dense(10, activation="softmax")(x)

model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

Core Concepts

Multi-Backend Functional Model Architecture

Building deep learning architectures compatible with PyTorch, JAX, and TensorFlow:

import os
# Configure backend before importing keras: 'jax', 'torch', or 'tensorflow'
os.environ["KERAS_BACKEND"] = "jax"

import keras
from keras import layers, ops

# Functional API model definition
def create_residual_classifier(input_shape, num_classes):
    inputs = layers.Input(shape=input_shape)

    # Feature extraction block
    x = layers.Dense(128, activation="relu")(inputs)
    x = layers.BatchNormalization()(x)
    x = layers.Dropout(0.2)(x)

    # Residual skip connection
    residual = x
    x = layers.Dense(128, activation="relu")(x)
    x = layers.BatchNormalization()(x)
    x = layers.add([x, residual])

    outputs = layers.Dense(num_classes, activation="softmax")(x)
    return keras.Model(inputs=inputs, outputs=outputs, name="res_classifier")

model = create_residual_classifier(input_shape=(64,), num_classes=10)
model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=1e-3),
    loss="categorical_crossentropy",
    metrics=["accuracy"]
)
model.summary()

Custom Layer with Backend-Agnostic ops

Writing custom layers using universal keras.ops:

import keras
from keras import layers, ops

class ScaleAndShiftLayer(layers.Layer):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def build(self, input_shape):
        self.scale = self.add_weight(
            shape=(input_shape[-1],),
            initializer="ones",
            trainable=True,
            name="scale"
        )
        self.shift = self.add_weight(
            shape=(input_shape[-1],),
            initializer="zeros",
            trainable=True,
            name="shift"
        )

    def call(self, inputs):
        # ops works transparently across JAX, PyTorch, and TensorFlow
        return ops.add(ops.multiply(inputs, self.scale), self.shift)

Robust Training with Modern Callbacks

Automating early stopping, learning rate reduction, and model checkpointing:

callbacks = [
    keras.callbacks.EarlyStopping(
        monitor="val_loss",
        patience=5,
        restore_best_weights=True
    ),
    keras.callbacks.ReduceLROnPlateau(
        monitor="val_loss",
        factor=0.2,
        patience=3,
        min_lr=1e-6
    ),
    keras.callbacks.ModelCheckpoint(
        filepath="best_model.keras",
        monitor="val_accuracy",
        save_best_only=True
    )
]

# model.fit(x_train, y_train, validation_split=0.2, epochs=50, callbacks=callbacks)

Common Patterns

Custom Layer Subclassing with Backend-Agnostic Operations

Problem: Writing custom layers that run seamlessly across JAX, PyTorch, and TensorFlow backends.

Solution: Subclass keras.layers.Layer using keras.ops:

import keras
from keras import ops

class SimpleDense(keras.layers.Layer):
    def __init__(self, units=32):
        super().__init__()
        self.units = units

    def build(self, input_shape):
        self.w = self.add_weight(shape=(input_shape[-1], self.units), initializer="glorot_uniform")
        self.b = self.add_weight(shape=(self.units,), initializer="zeros")

    def call(self, inputs):
        return ops.matmul(inputs, self.w) + self.b

Best Practices

Do:

  • Target Keras 3 with multi-backend compatibility (os.environ["KERAS_BACKEND"] = "jax" or "torch").
  • Use keras.ops instead of backend-specific tensor libraries (torch.* or tf.*) in custom layers.
  • Save models in the native .keras zip-based format (model.save("model.keras")).
  • Always include EarlyStopping with restore_best_weights=True to prevent overfitting.

Don't:

  • Use legacy keras 2.x patterns tied exclusively to tf.keras.
  • Write custom training loops unless necessary; model.compile() and model.fit() provide high optimization.
  • Save models using legacy H5 (.h5) format; use modern .keras.

Troubleshooting

ErrorCauseSolution
Backend mismatch error in Keras 3Changing KERAS_BACKEND after importing Keras.Set os.environ["KERAS_BACKEND"] before importing keras.
ValueError: Shapes (None, 1) and (None, 10) are incompatibleLoss function mismatch (e.g. categorical_crossentropy with integer labels).Use sparse_categorical_crossentropy for integer class labels.
Layer ... was called on an input with incompatible shapeInput dimension does not match input_shape of the first layer.Inspect model architecture using model.summary() to verify layer shapes.

References

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.