micropyML is a lightweight embedded machine-learning utility module for MicroPython,
built on top of ulab.numpy.ndarray. It provides common neural-network operations
optimized for microcontrollers and resource-constrained environments.
micropyML functions:
ndarray as the first argument for almost all functionsndarray for almost all functions
relu(x)
Rectified Linear Unit activation, element-wise.
reluN(x, N=6)
ReLU activation with configurable upper bound. ReLU6 is widely used.
softmax(x)
Fixed-point softmax for integer input arrays.
confidence(x)
Convert fixed-point softmax output to percent confidence.
dropout(x, p=0.5)
Apply dropout regularization.
maxpool1d(x, k=2)
1D max pooling over a 3D input tensor.
conv1d(x, kernel, bias=0)
Floating-point 1D convolution layer.
qconvrelu1d(x, kernel, bias=0, quant_params=(1, 0, 0))
Quantized 1D convolution with ReLU activation fused in.
Apply the ReLU (Rectified Linear Unit) activation function element-wise.
x (ndarray): Input array of any numeric dtype and shape.ndarray: Output array with the same shape and dtype as x,
where negative values are clipped to zero.
Apply a clipped ReLU activation function element-wise, where the output
is limited to an upper bound N. ReLU6 is most commonly used in
mobile and edge applications. The upper bound of 6 is based on tests performed in
Convolutional Deep Belief Networks on CIFAR-10.
x (ndarray): Input array of dtype int8 and any shape.
N (int8, Optional): Upper bound for the activation
(default 6).
ndarray: Output array with the same shape and dtype as x,
where values are clipped to the range [0, N].
Compute the softmax activation for integer-valued input arrays.
x (ndarray): array of shape (B, N) with dtype 8-bit uint/int.
Softmax is computed along the last dimension (N). Only guaranteed to be safe from overflow
if N < 1024.tuple(tuple(int), ndarray): First element is tuple of size B that has indices of max probabilities (argmax).
Second element is fixed-point Q7.7 output array with dtype uint16 and shape (B, N).100 * 27, representing probability 100%.Convert fixed-point softmax output into confidence values suitable for display.
x (uint16 or ndarray): Fixed-point softmax output for a single class or multiple classes.
If x is an ndarray, it must be 1D and have dtype uint16.tuple(uint8) or tuple(tuple(uint8)): If a single scalar uint16 is supplied, a 2 element tuple is returned.
First element is the integer part of the confidence (0-100). Second element is the fractional part of confidence (0-99).
This makes it easy to use with python f strings. For example, print(f"{conf[0]}.{conf[1]}%") displays confidence as percentage.
If a ndarray of uint16 is supplied, then a tuple is returned of the same length. Each entry in the returned tuple is a 2 element tuple
with the same format mentioned above.Apply dropout regularization to an input array.
x (ndarray): Input array of any numeric dtype.p (float, Optional): Dropout probability (default 0.5).ndarray: Output array with randomly zeroed elements. Same shape and dtype as x.Perform 1D max pooling over a 3D input tensor.
x (ndarray): Input array of shape (B, C, N).k (int, Optional): Kernel/Pool size as well as Stride(default 2).ndarray: Pooled output array of shape (B, C, N//k) with dtype same as x.
Since last dim is of size N//k, if N % k ≠ 0, the last element(s) are ignored when pooling.Perform a floating-point 1D convolution, meaning all inputs must be float. Function doesn't cast automatically. Stride and dilation are 1, and padding is 'same' for this implementation.
x(ndarray): Shape (B, Cin, N)kernel(ndarray): Shape (Cout, Cin, K)bias(ndarray, Optional): Shape (Cout)ndarray: Output of shape (B, Cout, N) and dtype floatQuantized 1D convolution with fused ReLU activation for integer-only inference. Stride and dilation are 1, and padding is 'same' for this implementation.
x(ndarray): Quantized input array of shape (B, Cin, N) and dtype (8- or 16-bit uint or int).kernel(ndarray): Quantized kernel array of shape (Cout, Cin, K) and dtype (8- or 16-bit uint or int).bias(ndarray, Optional): Bias array of shape (Cout,) and dtype int32.quant_params(tuple, Optional): 3 integer tuple of quantization parameters
(multiplier, shift, output_zero_point). Uses the quantization scheme described by Google in
Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference.
Multiplier is the final int32 result of multiplying the effective scale (S1 × S2⁄S3)
by 2shift + 31. Both the multiplier and shift are expected to be non-negative.
Output zero point corresponds to Z3 in equation (4).ndarray: Quantized output array of same dtype and shape as x.x and kernel scales are wrapped into the multiplier, but if they have non-zero zero points, it is expected that
the ndarrays will have this value subtracted before calling this function.