<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: -",
    "macOS: 26.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 26.0.0 -",
    "watchOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/BNNSGraph/Builder/Tensor/rnn(initialHiddenStates:inputHiddenWeight:hiddenHiddenWeight:bias:direction:activation:outputSequence:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate9BNNSGraphO7BuilderV6TensorV3rnn19initialHiddenStates05inputG6Weight06hiddengJ04bias9direction10activation14outputSequenceAGy__xG0O0_AP0kH0tAP_A3pE9DirectionOAE10ActivationOSbtF"
  },
  "title" : "rnn(initialHiddenStates:inputHiddenWeight:hiddenHiddenWeight:bias:direction:activation:outputSequence:)"
}
-->

# rnn(initialHiddenStates:inputHiddenWeight:hiddenHiddenWeight:bias:direction:activation:outputSequence:)

Adds an RNN operation to the current graph.

```
func rnn(initialHiddenStates: BNNSGraph.Builder.Tensor<T>, inputHiddenWeight: BNNSGraph.Builder.Tensor<T>, hiddenHiddenWeight: BNNSGraph.Builder.Tensor<T>, bias: BNNSGraph.Builder.Tensor<T>, direction: BNNSGraph.Builder.Direction, activation: BNNSGraph.Builder.Activation, outputSequence: Bool) -> (output: BNNSGraph.Builder.Tensor<T>, hiddenStates: BNNSGraph.Builder.Tensor<T>)
```

## Parameters

`initialHiddenStates`

The initial hidden states, with the shape `(N, Hout)`,
that the operation uses in the second matrix multiplication above when computing `h[0, ...]`.

`inputHiddenWeight`

The input-hidden weight with the shape `(Hout, Hin)`.

`hiddenHiddenWeight`

The hidden-hidden weight with the shape `(Hout, Hout)`.

`bias`

The bias (the sum of input-hidden and hidden-hidden biases) with the shape  `(Hout,)`.

`direction`

An enumeration that specifies a forward or backward RNN.

`activation`

An enumeration that controls the output activation function.

`outputSequence`

When `true`, `output` is of shape `(L, N, Hout)` and
contains hidden states from every step, `h[:, ...]`. When `false`, `output` is of shape `(1, N, Hout)`
and contains hidden states from the last step, `h[-1, ...]`.

## Discussion

For each time `t`, from `0` to `L-1`, this operation performs the following:

```
h[t, ...] = activation(matmul(x[t, ...], inputHiddenWeight^T) +
                       matmul(h[t-1, ...], hiddenHiddenWeight^T) +
                       bias)
```

The input tensor `x` is of shape `(L, N, Hin)`.

`hiddenStates` is of shape `(N, Hout)` and contains hidden states from the last step, `h[-1, ...]`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)