class_docstr: Simple train state for the common case with a single Optax optimizer.
  
  Example usage::
  
    >>> import flax.linen as nn
    >>> from flax.training.train_state import TrainState
    >>> import jax, jax.numpy as jnp
    >>> import optax
  
    >>> x = jnp.ones((1, 2))
    >>> y = jnp.ones((1, 2))
    >>> model = nn.Dense(2)
    >>> variables = model.init(jax.random.key(0), x)
    >>> tx = optax.adam(1e-3)
  
    >>> state = TrainState.create(
    ...     apply_fn=model.apply,
    ...     params=variables['params'],
    ...     tx=tx)
  
    >>> def loss_fn(params, x, y):
    ...   predictions = state.apply_fn({'params': params}, x)
    ...   loss = optax.l2_loss(predictions=predictions, targets=y).mean()
    ...   return loss
    >>> loss_fn(state.params, x, y)
    Array(3.3514676, dtype=float32)
  
    >>> grads = jax.grad(loss_fn)(state.params, x, y)
    >>> state = state.apply_gradients(grads=grads)
    >>> loss_fn(state.params, x, y)
    Array(3.343844, dtype=float32)
  
  Note that you can easily extend this dataclass by subclassing it for storing
  additional data (e.g. additional variable collections).
  
  For more exotic usecases (e.g. multiple optimizers) it's probably best to
  fork the class and modify it.
  
  Args:
    step: Counter starts at 0 and is incremented by every call to
      ``.apply_gradients()``.
    apply_fn: Usually set to ``model.apply()``. Kept in this dataclass for
      convenience to have a shorter params list for the ``train_step()`` function
      in your training loop.
    params: The parameters to be updated by ``tx`` and used by ``apply_fn``.
    tx: An Optax gradient transformation.
    opt_state: The state for ``tx``.
docstr: Creates a new instance with ``step=0`` and initialized ``opt_state``.
docstr_tok: ["Creates","a","new","instance","with","``","step=0","``","and","initialized","``","opt_state","``","."]
code: def create(cls, *, apply_fn, params, tx, **kwargs):
      """Creates a new instance with ``step=0`` and initialized ``opt_state``."""
      # We exclude OWG params when present because they do not need opt states.
      params_with_opt = (
          params["params"]
          if OVERWRITE_WITH_GRADIENT in params
          else params
      )
      opt_state = tx.init(params_with_opt)
      return cls(
          step=0,
          apply_fn=apply_fn,
          params=params,
          tx=tx,
          opt_state=opt_state,
          **kwargs,
      )
  
imports: from typing import Any, Callable
  import optax
  from flax import core, struct
  from flax.linen.fp8_ops import OVERWRITE_WITH_GRADIENT
label_desc: Use image node_id 2 for calling the TrainState obj's underlying member method code with example usage: obj.create(cls) and returns: cls