Train and run a deep neural network via webhooks using pure code nodes

Go to Workflow
0 views
Built by Alex Raj Alex Raj
Created on August 25, 2026

Description

Quick overview
This workflow exposes two webhooks to train and run inference for a 3-hidden-layer neural network built entirely with n8n Code and Merge nodes, persisting learned weights in workflow static data and returning class probabilities, predictions, and evaluation metrics.

How it works
Receives a POST request on /train with epochs, learning rate, and an optional dataset to train the network.
Runs a vectorized forward and backpropagation pass in JavaScript, updates weights and biases with gradient descent, and stores them in n8n workflow static data.
Receives a POST request on /predict containing one or more 8‑dimensional feature vectors (and optional trueClass labels).
Computes a feedforward pass through three ReLU hidden layers (9 neurons per layer) using the stored weights (or deterministic fallback weights if none are trained).
Produces 4 output logits, applies a temperature-scaled softmax to return probabilities, predicted class, and confidence.
Calculates per-sample cross-entropy loss and batch accuracy using trueClass (when provided) and responds with the prediction results as JSON.

Setup
Activate the workflow and copy the production webhook URLs for POST /train and POST /predict.
Send training data to /train (or rely on the default dataset) to initialize and persist weights in workflow static data.
Send inference requests to /predict as JSON containing x: [8 numbers] (optionally add sampleId and trueClass: 1..4) and review the JSON response for probabilities, predictions, loss, and accuracy.

Nodes Used (1)

Code
n8n-nodes-base.code