Index of every page on this site: llms.txt
All skills
yonesuke avatar

/training-mlps

@1e23d34
Run it yourself

A skill for defining and training Multi-Layer Perceptrons (MLPs) using Flax NNX.

Use this Skill: https://skilld.dev/gh/yonesuke/skills/training-mlps

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

examples.md

≈849 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Examples

MultiLayerPerceptron

A configurable MLP with support for different normalization types.

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "flax",
#     "jax",
#     "jaxlib",
# ]
# ///
import jax.numpy as jnp
from flax import nnx
from typing import Callable, Optional

class MultiLayerPerceptron(nnx.Module):
    def __init__(
        self,
        in_features: int,
        out_features: int,
        hidden_layers: int,
        hidden_units: int,
        *,
        rngs: nnx.Rngs,
        activation: Callable = nnx.relu,
        dropout_rate: float = 0.0,
        normalization: Optional[str] = None,
    ):
        layers = []
        self.dropout_rate = dropout_rate
        
        # Build hidden layers
        current_dim = in_features
        for _ in range(hidden_layers):
            # Dense Layer
            layers.append(nnx.Linear(current_dim, hidden_units, rngs=rngs))
            current_dim = hidden_units
            
            # Normalization (optional)
            if normalization == "layernorm":
                layers.append(nnx.LayerNorm(current_dim, rngs=rngs))
            elif normalization == "rmsnorm":
                layers.append(nnx.RMSNorm(current_dim, rngs=rngs))
            elif normalization is not None:
                raise ValueError(f"Unknown normalization: {normalization}")
            
            # Activation
            layers.append(activation)
            
            # Dropout (if rate > 0)
            if dropout_rate > 0:
                layers.append(nnx.Dropout(dropout_rate, rngs=rngs))
        
        # Output Layer
        layers.append(nnx.Linear(current_dim, out_features, rngs=rngs))
        
        # Store as nnx.List to allow iteration and module tracking
        self.layers = nnx.List(layers)

    def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
        for layer in self.layers:
            x = layer(x)
        return x

if __name__ == "__main__":
    import jax
    
    # Setup
    key = jax.random.key(0)
    rngs = nnx.Rngs(0)
    
    # Parameters
    IN_DIM = 10
    OUT_DIM = 2
    HIDDEN_LAYERS = 2
    HIDDEN_UNITS = 32
    DROPOUT = 0.5
    
    # Test 1: LayerNorm
    print("Initializing MultiLayerPerceptron (LayerNorm)...")
    model_ln = MultiLayerPerceptron(
        in_features=IN_DIM,
        out_features=OUT_DIM,
        hidden_layers=HIDDEN_LAYERS,
        hidden_units=HIDDEN_UNITS,
        dropout_rate=DROPOUT,
        normalization="layernorm",
        rngs=rngs
    )
    
    dummy_input = jnp.ones((1, IN_DIM))
    print(f"LayerNorm Output: {model_ln(dummy_input)}")

    # Test 2: RMSNorm
    print("\nInitializing MultiLayerPerceptron (RMSNorm)...")
    model_rms = MultiLayerPerceptron(
        in_features=IN_DIM,
        out_features=OUT_DIM,
        hidden_layers=HIDDEN_LAYERS,
        hidden_units=HIDDEN_UNITS,
        dropout_rate=DROPOUT,
        normalization="rmsnorm",
        rngs=rngs
    )
    print(f"RMSNorm Output: {model_rms(dummy_input)}")
    
    # Validation
    model_ln.train()
    out_train_1 = model_ln(dummy_input)
    out_train_2 = model_ln(dummy_input)
    is_diff = not jnp.allclose(out_train_1, out_train_2)
    print(f"\nDropout Check (LayerNorm): {is_diff}")

    print("\nSuccess! MultiLayerPerceptron created and verified with normalization switching.")

Source: scripts/mlp.py

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts5mo3 checks · Risk SAFE
  • Gen Agent Trust Hub5mo

    The skill provides a standard implementation for Multi-Layer Perceptrons using the Flax NNX library. It follows best practices for machine learning code and contains no security risks.

  • Socket5mo

    No alerts

  • Snyk5mo

    Risk: LOW · No issues

Signed by skilld at 1e23d34. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 10 months ago

README badge

README badge for yonesuke/skills/training-mlps