# tf.distribute.experimental_set_strategy

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/distribute/distribute_lib.py#L582-L628) |

Set a [`tf.distribute.Strategy`](https://www.tensorflow.org/versions/r2.14/api_docs/python/tf/distribute/Strategy) as current without `with strategy.scope()`.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.distribute.experimental_set_strategy`](https://www.tensorflow.org/api_docs/python/tf/distribute/experimental_set_strategy)

<br />

    tf.distribute.experimental_set_strategy(
        strategy
    )

    tf.distribute.experimental_set_strategy(strategy1)
    f()
    tf.distribute.experimental_set_strategy(strategy2)
    g()
    tf.distribute.experimental_set_strategy(None)
    h()

is equivalent to:

    with strategy1.scope():
      f()
    with strategy2.scope():
      g()
    h()

In general, you should use the `with strategy.scope():` API, but this
alternative may be convenient in notebooks where you would have to put
each cell in a `with strategy.scope():` block.

> [!NOTE]
> **Note:** This should only be called outside of any TensorFlow scope to avoid improper nesting.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `strategy` | A [`tf.distribute.Strategy`](https://www.tensorflow.org/versions/r2.14/api_docs/python/tf/distribute/Strategy) object or None. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `RuntimeError` | If called inside a `with strategy.scope():`. |

<br />