<br />

<button value="stable" default="">See Stable</button> <button value="nightly">See Nightly</button>

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/layers/core/einsum_dense.py#L26-L1611) |

A layer that uses `einsum` as the backing computation.

Inherits From: [`Layer`](https://www.tensorflow.org/api_docs/python/tf/keras/Layer.md.txt), [`Operation`](https://www.tensorflow.org/api_docs/python/tf/keras/Operation.md.txt)

```python
tf.keras.layers.EinsumDense(
    equation,
    output_shape,
    activation=None,
    bias_axes=None,
    kernel_initializer='glorot_uniform',
    bias_initializer='zeros',
    kernel_regularizer=None,
    bias_regularizer=None,
    kernel_constraint=None,
    bias_constraint=None,
    lora_rank=None,
    lora_alpha=None,
    gptq_unpacked_column_size=None,
    quantization_config=None,
    **kwargs
)
```

This layer can perform einsum calculations of arbitrary dimensionality.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `equation` | An equation describing the einsum to perform. This equation must be a valid einsum string of the form `ab,bc->ac`, `...ab,bc->...ac`, or `ab...,bc->ac...` where 'ab', 'bc', and 'ac' can be any valid einsum axis expression sequence. |
| `output_shape` | The expected shape of the output tensor (excluding the batch dimension and any dimensions represented by ellipses). You can specify `None` for any dimension that is unknown or can be inferred from the input shape. |
| `activation` | Activation function to use. If you don't specify anything, no activation is applied (that is, a "linear" activation: `a(x) = x`). |
| `bias_axes` | A string containing the output dimension(s) to apply a bias to. Each character in the `bias_axes` string should correspond to a character in the output portion of the `equation` string. |
| `kernel_initializer` | Initializer for the `kernel` weights matrix. |
| `bias_initializer` | Initializer for the bias vector. |
| `kernel_regularizer` | Regularizer function applied to the `kernel` weights matrix. |
| `bias_regularizer` | Regularizer function applied to the bias vector. |
| `kernel_constraint` | Constraint function applied to the `kernel` weights matrix. |
| `bias_constraint` | Constraint function applied to the bias vector. |
| `lora_rank` | Optional integer. If set, the layer's forward pass will implement LoRA (Low-Rank Adaptation) with the provided rank. LoRA sets the layer's kernel to non-trainable and replaces it with a delta over the original kernel, obtained via multiplying two lower-rank trainable matrices (the factorization happens on the last dimension). This can be useful to reduce the computation cost of fine-tuning large dense layers. You can also enable LoRA on an existing `EinsumDense` layer by calling `layer.enable_lora(rank)`. lora_alpha: Optional integer. If set, this parameter scales the low-rank adaptation delta (computed as the product of two lower-rank trainable matrices) during the forward pass. The delta is scaled by `lora_alpha / lora_rank`, allowing you to fine-tune the strength of the LoRA adjustment independently of `lora_rank`. |
| `**kwargs` | Base layer keyword arguments, such as `name` and `dtype`. |

<br />

#### Examples:

**Biased dense layer with einsums**

This example shows how to instantiate a standard Keras dense layer using
einsum operations. This example is equivalent to
[`keras.layers.Dense(64, use_bias=True)`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense.md.txt).

```python
layer = keras.layers.EinsumDense("ab,bc->ac",
                                      output_shape=64,
                                      bias_axes="c")
input_tensor = keras.Input(shape=[32])
output_tensor = layer(input_tensor)
output_tensor.shape
(None, 64)
```

**Applying a dense layer to a sequence**

This example shows how to instantiate a layer that applies the same dense
operation to every element in a sequence. Here, the `output_shape` has two
values (since there are two non-batch dimensions in the output); the first
dimension in the `output_shape` is `None`, because the sequence dimension
`b` has an unknown shape.

```python
layer = keras.layers.EinsumDense("abc,cd->abd",
                                      output_shape=(None, 64),
                                      bias_axes="d")
input_tensor = keras.Input(shape=[32, 128])
output_tensor = layer(input_tensor)
output_tensor.shape
(None, 32, 64)
```

**Applying a dense layer to a sequence using ellipses**

This example shows how to instantiate a layer that applies the same dense
operation to every element in a sequence, but uses the ellipsis notation
instead of specifying the batch and sequence dimensions.

Because we are using ellipsis notation and have specified only one axis, the
`output_shape` arg is a single value. When instantiated in this way, the
layer can handle any number of sequence dimensions - including the case
where no sequence dimension exists.

```python
layer = keras.layers.EinsumDense("...x,xy->...y",
                                      output_shape=64,
                                      bias_axes="y")
input_tensor = keras.Input(shape=[32, 128])
output_tensor = layer(input_tensor)
output_tensor.shape
(None, 32, 64)
```

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `input` | Retrieves the input tensor(s) of a symbolic operation. <br /> Only returns the tensor(s) corresponding to the *first time* the operation was called. |
| `kernel` | <br /> <br /> |
| `output` | Retrieves the output tensor(s) of a layer. <br /> Only returns the tensor(s) corresponding to the *first time* the operation was called. |
| `variable_serialization_spec` | Returns a dict mapping quantization modes to variable names in order. <br /> This spec is used by `save_own_variables` and `load_own_variables` to determine the correct ordering of variables during serialization for each quantization mode. `None` means no quantization. |

<br />

## Methods

### `enable_lora`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/layers/core/einsum_dense.py#L320-L378)

```python
enable_lora(
    rank,
    lora_alpha=None,
    a_initializer='he_uniform',
    b_initializer='zeros'
)
```

### `from_config`

[View source](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/layers/core/einsum_dense.py#L481-L489)

```python
@classmethod
from_config(
    config
)
```

Creates an operation from its config.

This method is the reverse of `get_config`, capable of instantiating the
same operation from the config dictionary.

> [!NOTE]
> **Note:** If you override this method, you might receive a serialized dtype config, which is a `dict`. You can deserialize it as follows:

```python
if "dtype" in config and isinstance(config["dtype"], dict):
    policy = dtype_policies.deserialize(config["dtype"])
```

<br />

<br />

<br />

| Args ||
|---|---|
| `config` | A Python dictionary, typically the output of `get_config`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An operation instance. ||

<br />