---
title: "ClikaRT::nn::InstanceNorm"
sidebar_label: "InstanceNorm"
description: "Instance normalization. \\[ y = \\frac{x - \\mu}{\\sqrt{\\sigma^2 + \\varepsilon}} \\cdot \\mathrm{weight} + \\mathrm{bias} \\] with mu / sigma^2 taken per (sample, channel) over the spatial dimensions, or per…"
---

<!-- Generated by tools/doxygen2mdx from Doxygen XML. Do not edit. -->

<span class="api-kind api-kind--class">class</span>

**Header:** `ClikaRT/nn/instance_norm.h`  
**Inherits:** [`ClikaRT::nn::Module`](./Module.md)

Instance normalization.

$$
y = \frac{x - \mu}{\sqrt{\sigma^2 + \varepsilon}} \cdot \mathrm{weight}
+ \mathrm{bias}
$$

with `mu` / `sigma^2` taken per `(sample, channel)` over the spatial dimensions, or per channel from the stored statistics when the module tracks them. The channel axis is the last one. The module form of [`ops::instance_norm`](../ops/instance_norm.md#instance_norm).

```cpp
ClikaRT::nn::InstanceNormOptions options;
options.affine = true;
auto norm = ClikaRT::nn::InstanceNorm::make(64, options);
norm->set_weights(gamma, beta);
auto y = (*norm)(x);               // [N, H, W, 64] -> [N, H, W, 64]
```

## Static member functions

### make() {#make}

<div class="api-signature">

`static` `std::shared_ptr<InstanceNorm>` **`make`**(`std::int64_t` `num_features`, [`InstanceNormOptions`](./InstanceNormOptions.md) `options` `=` `{}`)

</div>

Construct from config with storage-free slots: registers the `[`[`num_features`](./InstanceNorm.md#num_features)`]` parameter slots (`weight`, `bias`, when `options.affine`) and, when `options.track_running_stats`, the `running_mean` / `running_var` buffer slots plus the `num_batches_tracked` counter, on `options.dtype` / `options.device`; no weight memory is allocated. The first [`forward`](./InstanceNorm.md#forward) builds. Raises [`ClikaRT::Error`](../Error.md) on a channel count or `eps` that is not positive.

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 95

</div>

## Member functions

### set\_weights() {#set_weights}

<div class="api-signature">

`void` **`set_weights`**([`Tensor`](../Tensor.md) `weight`, [`OptionalTensor`](../../index.md#OptionalTensor) `bias` `=` `{}`)

</div>

Bind the affine parameters positionally: `weight` (scale, `[C]`) and `bias` (shift, `[C]`). The declared placement wins; shapes must match the declaration; the next [`forward`](./InstanceNorm.md#forward) rebuilds. Raises [`ClikaRT::Error`](../Error.md) on a geometry mismatch or on a module made with `affine = false`.

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 105

</div>

### set\_running\_stats() {#set_running_stats}

<div class="api-signature">

`void` **`set_running_stats`**([`Tensor`](../Tensor.md) `running_mean`, [`Tensor`](../Tensor.md) `running_var`)

</div>

Bind the statistics positionally: `running_mean` and `running_var`, both `[C]`. Raises [`ClikaRT::Error`](../Error.md) on a geometry mismatch or on a module made without `track_running_stats`.

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 113

</div>

### \~InstanceNorm() {#InstanceNorm}

<div class="api-signature">

**`~InstanceNorm`**() `override`

</div>

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 117

</div>

### to\_impl(StreamOrDevice) {#to_impl}

<div class="api-signature">

`virtual` [`Result`](../Result.md)`<void>` **`to_impl`**([`StreamOrDevice`](../StreamOrDevice.md) `where`) `override`

</div>

Move to a placement ([`Device`](../Device.md) / [`Stream`](../Stream.md)) or cast to a dtype. Rebuilds over the moved slots on the next forward.

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 121

</div>

### to\_impl(DataType) {#to_impl-2}

<div class="api-signature">

`virtual` [`Result`](../Result.md)`<void>` **`to_impl`**([`DataType`](../../index.md#DataType) `dtype`) `override`

</div>

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 122

</div>

### forward() {#forward}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`forward`**([`Tensor`](../Tensor.md) `input`) `const`

</div>

Normalizes `input` `[N, D1..Dn, C]` per sample and channel (or with the stored statistics), applies the affine parameters when declared and the fused activation when set; output shape and dtype follow `input`. The first call builds (once, thread-safe). Raises [`ClikaRT::Error`](../Error.md) when a slot is still unbound or the channel count does not match.

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 131

</div>

### operator()() {#operator-call}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`operator()`**([`Tensor`](../Tensor.md) `input`) `const`

</div>

Same as [`forward`](./InstanceNorm.md#forward).

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 135

</div>

### num\_features() {#num_features}

<div class="api-signature">

`std::int64_t` **`num_features`**() `const noexcept`

</div>

The channel count the module was made with.

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 140

</div>

### options() {#options}

<div class="api-signature">

`const` [`InstanceNormOptions`](./InstanceNormOptions.md)`&` **`options`**() `const noexcept`

</div>

The settings the module was made with.

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 142

</div>

## Protected member functions

### initialize\_impl() {#initialize_impl}

<div class="api-signature">

`virtual` [`Result`](../Result.md)`<void>` **`initialize_impl`**() `override`

</div>

The pack hook ([`Module::initialize_impl`](./Module.md#initialize_impl)): builds the normalization over the bound slots (idempotent, thread-safe). Every declared slot must hold a real (loaded) tensor; a still-unbound slot is a clean error naming it.

<div class="api-declared">

Declared in `ClikaRT/nn/instance_norm.h`, line 153

</div>
