WebNN is W3C's platform neutral API for neural networks, with backends for NPUs, GPUs and CPUs. Current frameworks like TensorFlow and ONNX are huge. This motivates the development of much smaller frameworks with easy to understand representations for network models.
This web page provides an example of how you can use the neural network model syntax (NNM) to load a model and run it forward. In this case, we initialise the model input and model parameters with random numbers. The model uses a batch size of 1 and a feature size of 784 which corresponds to a monochrome image of 28x28 pixels. The batch size dimension is automatically prepended to the tensor shape when the model is flattended to a pipeline. The same applies to sequence length for models involving sequences of data.
The nnm.js library allows you to define layer prototypes, e.g. dense as a set of sub-layers. Each layer has zero or more operands as well as zero or more named options. The options either relate to initialisation, inference or training. The framework handles defaults, e.g. to ensure that model parameter initialisation is sensitive to the chosen activation function. You can see what was selected in the flattened sequence of layers generated from the model.
The framework uses shape inference to infer the shape of the model's parameters. Most layers leave the shape unchanged, but a few, e.g. matmul and reshape change the shape. If the model is under or over determined an exception will be thrown. Look at the JavaScript console to see the error message. Note that most neural network models process data with the tensor shape [batchSize, featureSize] or [batchSize, sequenceLength, featureSize].
The next example will focus on training. WebNN is designed for inference, not training. The NNM framework will 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.