# tf.summary.SummaryWriter

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/summary_ops_v2.py#L235-L303) |

Interface representing a stateful summary writer object.

## Methods

### `as_default`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/summary_ops_v2.py#L259-L291)

    as_default(
        step=None
    )

Returns a context manager that enables summary writing.

For convenience, if `step` is not None, this function also sets a default
value for the `step` parameter used in summary-writing functions elsewhere
in the API so that it need not be explicitly passed in every such
invocation. The value can be a constant or a variable.

> [!NOTE]
> **Note:** when setting `step` in a @tf.function, the step value will be captured at the time the function is traced, so changes to the step outside the function will not be reflected inside the function unless using a [`tf.Variable`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/Variable) step.

For example, `step` can be used as:

    with writer_a.as_default(step=10):
      tf.summary.scalar(tag, value)   # Logged to writer_a with step 10
      with writer_b.as_default(step=20):
        tf.summary.scalar(tag, value) # Logged to writer_b with step 20
      tf.summary.scalar(tag, value)   # Logged to writer_a with step 10

<br />

<br />

<br />

| Args ||
|---|---|
| `step` | An `int64`-castable default step value, or `None`. When not `None`, the current step is captured, replaced by a given one, and the original one is restored when the context manager exits. When `None`, the current step is not modified (and not restored when the context manager exits). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The context manager. ||

<br />

### `close`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/summary_ops_v2.py#L301-L303)

    close()

Flushes and closes the summary writer.

### `flush`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/summary_ops_v2.py#L297-L299)

    flush()

Flushes any buffered data.

### `init`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/summary_ops_v2.py#L293-L295)

    init()

Initializes the summary writer.

### `set_as_default`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/summary_ops_v2.py#L239-L257)

    set_as_default(
        step=None
    )

Enables this summary writer for the current thread.

For convenience, if `step` is not None, this function also sets a default
value for the `step` parameter used in summary-writing functions elsewhere
in the API so that it need not be explicitly passed in every such
invocation. The value can be a constant or a variable.

> [!NOTE]
> **Note:** when setting `step` in a @tf.function, the step value will be captured at the time the function is traced, so changes to the step outside the function will not be reflected inside the function unless using a [`tf.Variable`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/Variable) step.

<br />

<br />

<br />

| Args ||
|---|---|
| `step` | An `int64`-castable default step value, or `None`. When not `None`, the current step is modified to the given value. When `None`, the current step is not modified. |

<br />