- From: Dave Raggett <dsr@w3.org>
- Date: Wed, 19 Nov 2025 14:29:37 +0000
- To: public-cogai <public-cogai@w3.org>
- Message-Id: <88BBA839-5AE2-4089-9B0E-47F843F2C770@w3.org>
WebNN is W3C's platform neutral API for neural networks, with backends for NPUs, GPUs and CPUs. Current frameworks like TensorFlow and ONNX have huge libraries. This motivates the development of much smaller web-based frameworks with easy to understand representations for network models. I’ve now got a couple of examples working for WebNN. The first one is very simple and just shows how to use the WebNN API. See: https://www.w3.org/2025/webnn/example1/
The second introduces the neural network model format, NNM, see: https://www.w3.org/2025/webnn/example2/
model:input shape=[1, 784];
model:output shape=[1, 10];
model:layers
dense(shape=[1, 128], activation=relu, foo=fred);
dense(shape=[1, 10], activation=softmax);
dense:layers
matmul(w),
add(b, foo=bar),
activation();
It is common place for the tensor input to have the shape [batchSize, featureSize] or [batchSize, sequenceLength, featureSize] for models that handle sequential input. Here I included foo=fred as a test of macro instantiation for layer options. Each layer can have zero or more operands, i.e. model parameters, and zero or more options. Options are used for initialisation, inference and training. This toy example uses softmax for the final layer to compute the probabilities for each of ten classes based upon the input data, which could be a 28x28 pixel monochrome image.
The above model expands to the following pipeline:
matmul(w1, init=he, shape=[1, 128])
add(b1, bias=0.1, foo=fred, shape=[1, 128])
relu(shape=[1, 128])
matmul(w2, init=glorot, shape=[1, 10])
add(b2, bias=0, foo=bar, shape=[1, 10])
softmax(shape=[1, 10])
Where “he” and “glorot” refer to common algorithms for parameter initialisation, and are selected based upon the activation operator. The framework uses shape inference to deduce the tensor shape of the model parameters:
input shape=[1,784]
w1 shape=[784,128]
b1 shape=[1,128]
w2 shape=[128,10]
b2 shape=[1,10]
output shape=[1,10]
For this example, I’ve used random input and random values for the model parameters.
WebNN is designed for inference, not training. For my next example, I will extend the NNM framework to transform the inference model to a training model that WebNN can run forward. This involves automatic differentiation and the chain rule to determine how each parameter varies with the loss function. The framework will also address the learning rate schedule and related optimisation strategies. I plan further examples for a variety of different kinds of small models that can be trained in a modest amount of time with some widely used free datasets. I will then take a look at supporting federated learning and start work on real-time estimates of head pose and facial blendshapes for use in the immersive web.
p.s. I expect to refine the NNM framework API as I gain experience and will prepare a CG Report documenting it.
Enjoy!
Dave Raggett <dsr@w3.org>
Received on Wednesday, 19 November 2025 14:29:49 UTC