Skip to main content

Deep neural networks: DNN / MLP

Train a small neural network on a nonlinear classification fixture and interpret its probabilities.

The practical problem and model​

Consider a sensor classifier where a fault depends on the interaction between two measurements. Neither measurement alone cleanly separates the outcomes. A multilayer perceptron (MLP) can combine inputs through hidden layers and nonlinear activations to represent such interactions.

The tutorial's twelve points form an XOR-style pattern: opposite signs are class one; matching signs are class zero. Although the table is named spiral, these are quadrant examples rather than a sampled spiral dataset.

The model has two hidden layers of eight units and uses ReLU activations. With task = 'binary', the returned scalar represents the model's class-one probability. The example uses 0.5 to turn that score into a class decision.

Before you run​

Complete the shared setup. This walkthrough uses synthetic data and recreates its tutorial objects. Use a sandbox tenant or schema, and run the steps in order.

Studio: Tutorial: Deep Neural Network (DNN / MLP). Download the complete SQL.

Step 1: Create the nonlinear fixture​

The two features are already small numeric values. There are six examples of each class.

CREATE CATALOG IF NOT EXISTS tutorial;

CREATE SCHEMA IF NOT EXISTS tutorial.dnn;

CREATE OR REPLACE TABLE tutorial.dnn.spiral (
x DOUBLE, y DOUBLE, class INT
);

INSERT INTO tutorial.dnn.spiral VALUES
(-1.0, -1.0, 0), (-0.9, -1.1, 0), (-1.1, -0.8, 0),
( 1.0, 1.0, 0), ( 1.1, 0.9, 0), ( 0.8, 1.2, 0),
(-1.0, 1.0, 1), (-0.9, 1.1, 1), (-1.1, 0.9, 1),
( 1.0, -1.0, 1), ( 1.1, -0.9, 1), ( 0.9, -1.1, 1);

Step 2: Train and explain the network​

hidden_layers = '8,8' is the supported option form in this tutorial. The training SELECT supplies the label, then x and y. EXPLAIN MODEL confirms the registered model and active version.

CREATE OR REPLACE MODEL tutorial.dnn.xor_clf
(DOUBLE, DOUBLE)
RETURNS DOUBLE
TYPE 'dnn'
OPTIONS (
task = 'binary',
hidden_layers = '8,8',
activation = 'relu',
learning_rate = 0.05,
max_iters = 2000,
tolerance = 1e-5
)
AS SELECT
CAST(class AS DOUBLE) AS label,
x, y
FROM tutorial.dnn.spiral;

EXPLAIN MODEL tutorial.dnn.xor_clf;

Step 3: Compare probabilities and evaluation​

Inspect all twelve rows before looking at aggregate metrics. The evaluation query uses the same feature ordering and label cast.

SELECT x, y, class AS actual, tutorial.dnn.xor_clf(x, y) AS predicted
FROM tutorial.dnn.spiral
ORDER BY class, x;

EVALUATE MODEL tutorial.dnn.xor_clf
ON SELECT CAST(class AS DOUBLE) AS label, x, y FROM tutorial.dnn.spiral;

What to check in the results​

All twelve supplied examples were classified correctly at a 0.5 threshold in the recorded run. Class-zero scores ranged from about 0.000225 to 0.011827; class-one scores were above 0.9989.

The recorded accuracy, precision, recall, and F1 were all 1.0 on this training fixture. RMSE was about 0.005944 and MAE about 0.003441. These values check the example's behavior; they do not demonstrate generalization beyond the twelve points.

Adapt it to real data​

Compare the network with a simpler baseline and use a separate validation set. Normalize inputs consistently, tune network size and training settings on validation data, and check calibration before using scores as probabilities in a decision system.

Monitor errors on meaningful input groups, not just the global average. A larger network can fit a small dataset very well while performing poorly on new data.

DNN algorithm reference

All tutorials · Setup and troubleshooting