DocsData & AINeural Networks, Layers & Optimizers
Data & AI

Neural Networks, Layers & Optimizers

Dense layers, activation functions, loss calculations, optimizers (SGD, Adam), and model training.

NextViper AI Subsystem API Reference

The ai module provides a comprehensive suite of tools for deep learning and machine learning in NextViper.

main.nv
import ai
import tensor
import data

1

Models (`ai.Sequential`, `ai.Model`)

`ai.Sequential(layers: List[Module]) -> Sequential`

Constructs a sequential feedforward neural network from a list of layers.

#### Methods:

  • `model.compile(optimizer, loss, [metrics])`: Configures training hyperparameters, loss function, and evaluation metrics.
  • `model.fit(x: Tensor, y: Tensor, epochs: int, batch_size: int, [verbose: bool]) -> History`:
  • Trains the model for a fixed number of epochs on input data x and target labels y. Returns a History object with loss trajectory.

  • `model.predict(x: Tensor) -> Tensor`:
  • Runs forward inference in evaluation mode. Returns predicted output tensor.

  • `model.evaluate(x: Tensor, y: Tensor) -> Map[String, Float]`:
  • Computes loss and evaluation metrics over the test dataset.

  • `model.summary()`:
  • Prints layer-by-layer architectural summary and trainable parameter counts.

  • `model.save(path: String)`:
  • Serializes model weights and architecture to safe .nvmodel binary/manifest format.

  • `model.train()`:
  • Sets network mode to training (enables dropout).

  • `model.eval()`:
  • Sets network mode to evaluation (disables dropout).

  • `model.zero_grad()`:
  • Resets all parameter gradients to zero.


    2

    Layers (`ai.Dense`, `ai.Dropout`, `ai.Flatten`)

    `ai.Dense(in_features: int, out_features: int, [activation: String], [bias: bool = true])`

    Fully connected linear layer: $y = x W^T + b$.

  • activation: Optional activation string: "relu", "sigmoid", "tanh", "softmax", or "none".
  • `ai.Dropout(p: float = 0.5)`

    Randomly zeroes out input elements with probability $p$ during training.

    `ai.Flatten()`

    Reshapes tensors of shape [N, D1, D2, ...] to [N, D1 * D2 * ...].

    Activation Layers

  • ai.ReLU(): Rectified linear unit $f(x) = max(0, x)$.
  • ai.Sigmoid(): Logistic sigmoid $f(x) = rac{1}{1 + e^{-x}}$.
  • ai.Tanh(): Hyperbolic tangent $f(x) = anh(x)$.
  • ai.Softmax([dim: int = -1]): Normalized exponential probabilities.

  • 3

    Loss Functions (`ai.losses`)

  • `ai.MSE()` / `ai.MSELoss()`: Mean Squared Error for regression.
  • `ai.MAE()` / `ai.MAELoss()`: Mean Absolute Error for robust regression.
  • `ai.BCE()` / `ai.BCELoss()`: Binary Cross-Entropy for 2-class classification.
  • `ai.CrossEntropy()` / `ai.CrossEntropyLoss()`: Multi-class softmax cross-entropy.

  • 4

    Optimizers (`ai.optimizers`)

  • `ai.SGD(lr: float = 0.01, [momentum: float = 0.0], [weight_decay: float = 0.0])`
  • Stochastic gradient descent with momentum and L2 regularization.

  • `ai.Momentum(lr: float = 0.01, [momentum: float = 0.9])`
  • Classical momentum optimizer.

  • `ai.Adam(lr: float = 0.001, [beta1: float = 0.9], [beta2: float = 0.999], [eps: float = 1e-8], [weight_decay: float = 0.0])`
  • Adaptive moment estimation.

  • `ai.AdamW(lr: float = 0.001, [beta1: float = 0.9], [beta2: float = 0.999], [eps: float = 1e-8], [weight_decay: float = 0.01])`
  • Adam with decoupled weight decay.


    5

    Metrics (`ai.metrics`)

  • `ai.accuracy(y_pred: Tensor, y_true: Tensor) -> float`: Percentage of correct classifications.
  • `ai.precision(y_pred: Tensor, y_true: Tensor) -> float`: True Positives / (True Positives + False Positives).
  • `ai.recall(y_pred: Tensor, y_true: Tensor) -> float`: True Positives / (True Positives + False Negatives).
  • `ai.f1_score(y_pred: Tensor, y_true: Tensor) -> float`: Harmonic mean of precision and recall.
  • `ai.mae(y_pred: Tensor, y_true: Tensor) -> float`: Mean absolute error.
  • `ai.mse(y_pred: Tensor, y_true: Tensor) -> float`: Mean squared error.

  • 6

    Model Serialization (`ai.save`, `ai.load`)

  • `ai.save(model: Sequential, path: String)`:
  • Saves the model to .nvmodel format.

  • `ai.load(path: String) -> Sequential`:
  • Restores model architecture and parameters from .nvmodel format.


    7

    Autograd API (`tensor.autograd`)

  • `tensor.requires_grad: bool`: Checks if tensor tracks gradients.
  • `tensor.set_requires_grad(requires_grad: bool)`: Enables or disables gradient tracking.
  • `tensor.grad: Tensor`: Accesses accumulated gradient tensor.
  • `tensor.backward([grad_output: Tensor])`: Computes reverse-mode derivatives.
  • `tensor.zero_grad()`: Resets gradient to null.
  • `tensor.detach() -> Tensor`: Returns a detached copy that does not participate in autograd graph.

  • Learn NextViper AI in 10 Steps: Complete Beginner Tutorial

    Welcome to machine learning with NextViper! In this tutorial, you will build, train, evaluate, save, and deploy a neural network from scratch using standard NextViper code.


    Step 1: Loading and Inspecting Data

    NextViper provides a robust data subsystem to read structured CSV and tabular datasets:

    main.nv
    import data
    
    // Load tabular training dataset
    let raw_csv = "feature1,feature2,target
    0.0,0.0,0.0
    0.0,1.0,1.0
    1.0,0.0,1.0
    1.0,1.0,0.0
    "
    let df = data.read_csv(raw_csv)
    
    print("Rows:", df.num_rows)
    print("Columns:", df.columns)

    Step 2: Converting Data to Tensors

    Machine learning models operate on multidimensional arrays called Tensors. Convert DataFrame columns into input tensors ($X$) and target tensors ($Y$):

    main.nv
    import tensor
    
    // Extract input features and target labels
    let x_train = df.to_tensor(["feature1", "feature2"])
    let y_train = df.to_tensor(["target"])
    
    print("X Tensor Shape:", x_train.shape) // [4, 2]
    print("Y Tensor Shape:", y_train.shape) // [4, 1]

    Step 3: Building Neural Network Architecture

    Construct a multi-layer perceptron using ai.Sequential and ai.Dense:

    main.nv
    import ai
    
    let model = ai.Sequential([
        ai.Dense(2, 8, "relu"),    // Input Layer (2 features) -> Hidden Layer (8 units, ReLU)
        ai.Dense(8, 1, "sigmoid")  // Hidden Layer (8 units) -> Output Layer (1 probability, Sigmoid)
    ])
    
    model.summary()

    Step 4: Configuring the Loss Function

    The loss function measures the difference between model predictions and true targets:

    main.nv
    // Use Mean Squared Error (or ai.BCE() for binary classification)
    let loss_fn = ai.MSE()

    Step 5: Choosing and Configuring an Optimizer

    The optimizer adjusts model weights using computed gradients during backpropagation:

    main.nv
    // Adam optimizer with learning rate 0.05
    let optimizer = ai.Adam(0.05)

    Step 6: Compiling the Model

    Bind the optimizer and loss function to the model:

    main.nv
    model.compile(optimizer, loss_fn)

    Step 7: Training the Model (`model.fit`)

    Train the network over 200 epochs using mini-batches:

    main.nv
    print("Starting training...")
    let history = model.fit(x_train, y_train, 200, 4)
    
    print("Epochs completed:", history.epochs)

    Step 8: Evaluating Model Performance

    Evaluate your model on test data to inspect accuracy and loss:

    main.nv
    let eval_results = model.evaluate(x_train, y_train)
    print("Evaluation Loss:", eval_results["loss"])

    Step 9: Safe Model Serialization

    Save your trained model to a safe .nvmodel file on disk:

    main.nv
    let model_path = "xor_classifier.nvmodel"
    model.save(model_path)
    
    print("Model saved to:", model_path)

    Step 10: Loading the Model & Running Live Inference

    Load the model in your production environment and run predictions on new inputs:

    main.nv
    // Restore model from disk
    let loaded_model = ai.load("xor_classifier.nvmodel")
    
    // Run inference on new data
    let sample_input = tensor.from([[0.0, 1.0], [1.0, 1.0]])
    let predictions = loaded_model.predict(sample_input)
    
    print("Prediction for [0, 1] (expect ~1.0):", predictions.get(0, 0))
    print("Prediction for [1, 1] (expect ~0.0):", predictions.get(1, 0))

    Congratulations! You have completed the 10-step NextViper AI tutorial.