⚒️ Train and run a deep neural network via webhooks using pure code nodes
⚡ 3 views · ⚒️ Engineering
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
/trainwith 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
/predictcontaining one or more 8‑dimensional feature vectors (and optionaltrueClasslabels). - 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 /trainandPOST /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
/predictas JSON containingx: [8 numbers](optionally addsampleIdandtrueClass: 1..4) and review the JSON response for probabilities, predictions, loss, and accuracy.
🔗 Nodes Used
Webhook
📥 Import
Download workflow.json and import into n8n:
Workflow menu → Import from File