# concrete.ml.sklearn.qnn\_module.md

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/qnn_module.py#L0)

## module `concrete.ml.sklearn.qnn_module`

Sparse Quantized Neural Network torch module.

### **Global Variables**

* **MAX\_BITWIDTH\_BACKWARD\_COMPATIBLE**

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/qnn_module.py#L14)

### class `SparseQuantNeuralNetwork`

Sparse Quantized Neural Network.

This class implements an MLP that is compatible with FHE constraints. The weights and activations are quantized to low bit-width and pruning is used to ensure accumulators do not surpass an user-provided accumulator bit-width. The number of classes and number of layers are specified by the user, as well as the breadth of the network

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/qnn_module.py#L24)

#### method `__init__`

```python
__init__(
    input_dim: int,
    n_layers: int,
    n_outputs: int,
    n_hidden_neurons_multiplier: int = 4,
    n_w_bits: int = 3,
    n_a_bits: int = 3,
    n_accum_bits: int = 8,
    n_prune_neurons_percentage: float = 0.0,
    activation_function: Type = <class 'torch.nn.modules.activation.ReLU'>,
    quant_narrow: bool = False,
    quant_signed: bool = True
)
```

Sparse Quantized Neural Network constructor.

**Args:**

* `input_dim` (int): Number of dimensions of the input data.
* `n_layers` (int): Number of linear layers for this network.
* `n_outputs` (int): Number of output classes or regression targets.
* `n_w_bits` (int): Number of weight bits.
* `n_a_bits` (int): Number of activation and input bits.
* `n_accum_bits` (int): Maximal allowed bit-width of intermediate accumulators.
* `n_hidden_neurons_multiplier` (int): The number of neurons on the hidden will be the number of dimensions of the input multiplied by `n_hidden_neurons_multiplier`. Note that pruning is used to adjust the accumulator size to attempt to keep the maximum accumulator bit-width to `n_accum_bits`, meaning that not all hidden layer neurons will be active. The default value for `n_hidden_neurons_multiplier` is chosen for small dimensions of the input. Reducing this value decreases the FHE inference time considerably but also decreases the robustness and accuracy of model training.
* `n_prune_neurons_percentage` (float): The percentage of neurons to prune in the hidden layers. This can be used when setting `n_hidden_neurons_multiplier` with a high number (3-4), once good accuracy is obtained, in order to speed up the model in FHE.
* `activation_function` (Type): The activation function to use in the network (e.g., torch.ReLU, torch.SELU, torch.Sigmoid, ...).
* `quant_narrow` (bool): Whether this network should quantize the values using narrow range (e.g a 2-bits signed quantization uses \[-1, 0, 1] instead of \[-2, -1, 0, 1]).
* `quant_signed` (bool): Whether this network should quantize the values using signed integers.

**Raises:**

* `ValueError`: If the parameters have invalid values or the computed accumulator bit-width is zero.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/qnn_module.py#L228)

#### method `enable_pruning`

```python
enable_pruning() → None
```

Enable pruning in the network. Pruning must be made permanent to recover pruned weights.

**Raises:**

* `ValueError`: If the quantization parameters are invalid.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/qnn_module.py#L284)

#### method `forward`

```python
forward(x: Tensor) → Tensor
```

Forward pass.

**Args:**

* `x` (torch.Tensor): network input

**Returns:**

* `x` (torch.Tensor): network prediction

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/qnn_module.py#L158)

#### method `make_pruning_permanent`

```python
make_pruning_permanent() → None
```

Make the learned pruning permanent in the network.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/qnn_module.py#L138)

#### method `max_active_neurons`

```python
max_active_neurons() → int
```

Compute the maximum number of active (non-zero weight) neurons.

The computation is done using the quantization parameters passed to the constructor. Warning: With the current quantization algorithm (asymmetric) the value returned by this function is not guaranteed to ensure FHE compatibility. For some weight distributions, weights that are 0 (which are pruned weights) will not be quantized to 0. Therefore the total number of active quantized neurons will not be equal to max\_active\_neurons.

**Returns:**

* `int`: The maximum number of active neurons.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.zama.org/concrete-ml/1.1/developer-guide/api/concrete.ml.sklearn.qnn_module.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
