# Path: enn/base.py
# class OutputWithPrior(NamedTuple):
# class EpistemicModule(abc.ABC, hk.Module):
# class ApplyFn(typing_extensions.Protocol):
# class InitFn(typing_extensions.Protocol):
# class EpistemicIndexer(typing_extensions.Protocol):
# class EpistemicNetwork:
# class Batch(NamedTuple):
# class LossFn(typing_extensions.Protocol):
# class EpistemicNetworkWithState:
# class LossFnWithState(typing_extensions.Protocol):
#   def preds(self) -> Array:
#   def __call__(self, inputs: Array, index: Index) -> Output:
#   def __call__(self, params: hk.Params, inputs: Array, index: Index) -> Output:
#   def __call__(self, rng_key: RngKey, inputs: Array, index: Index) -> hk.Params:
#   def __call__(self, key: RngKey) -> Index:
#   def __call__(self,
#                enn: EpistemicNetwork,
#                params: hk.Params,
#                batch: Batch,
#                key: RngKey) -> Tuple[Array, LossMetrics]:
#   def __call__(self,
#                enn: EpistemicNetworkWithState,
#                params: hk.Params,
#                state: hk.State,
#                batch: Batch,
#                key: RngKey) -> Tuple[Array, Tuple[hk.State, LossMetrics]]:
# 
# Path: enn/supervised/base.py
# class BaseExperiment(abc.ABC):
#   def train(self, num_batches: int):
#   def predict(
#       self, inputs: base.Array, key: base.RngKey) -> base.Array:
#   def loss(self, batch: base.Batch, key: base.RngKey) -> base.Array:


import functools
import dataclasses
import haiku as hk
import jax
import optax
from typing import Callable, Dict, NamedTuple, Optional, Sequence, Tuple
from acme.utils import loggers
from enn import base
from enn.supervised import base as supervised_base
# ============================================================================

"""An SGD experiment with facility for multiple losses."""




class TrainingState(NamedTuple):
  params: hk.Params
  opt_state: optax.OptState


@dataclasses.dataclass
class MultilossTrainer:
  """Specify the training schedule for a given loss/dataset.

  For step=1,2,...:
    If should_train(step):
      Apply one step of loss_fn on a batch = next(dataset).
  """
  loss_fn: base.LossFn  # Loss function
  dataset: base.BatchIterator  # Dataset to pull batch from
  should_train: Callable[[int], bool] = lambda _: True  # Which steps to train
  name: str = 'loss'  # Name used for logging


# Type definition for loss function after internalizing the ENN
PureLoss = Callable[[hk.Params, base.Batch, base.RngKey], base.Array]



# Path: enn/base.py
# class OutputWithPrior(NamedTuple):
# class EpistemicModule(abc.ABC, hk.Module):
# class ApplyFn(typing_extensions.Protocol):
# class InitFn(typing_extensions.Protocol):
# class EpistemicIndexer(typing_extensions.Protocol):
# class EpistemicNetwork:
# class Batch(NamedTuple):
# class LossFn(typing_extensions.Protocol):
# class EpistemicNetworkWithState:
# class LossFnWithState(typing_extensions.Protocol):
#   def preds(self) -> Array:
#   def __call__(self, inputs: Array, index: Index) -> Output:
#   def __call__(self, params: hk.Params, inputs: Array, index: Index) -> Output:
#   def __call__(self, rng_key: RngKey, inputs: Array, index: Index) -> hk.Params:
#   def __call__(self, key: RngKey) -> Index:
#   def __call__(self,
#                enn: EpistemicNetwork,
#                params: hk.Params,
#                batch: Batch,
#                key: RngKey) -> Tuple[Array, LossMetrics]:
#   def __call__(self,
#                enn: EpistemicNetworkWithState,
#                params: hk.Params,
#                state: hk.State,
#                batch: Batch,
#                key: RngKey) -> Tuple[Array, Tuple[hk.State, LossMetrics]]:
# 
# Path: enn/supervised/base.py
# class BaseExperiment(abc.ABC):
#   def train(self, num_batches: int):
#   def predict(
#       self, inputs: base.Array, key: base.RngKey) -> base.Array:
#   def loss(self, batch: base.Batch, key: base.RngKey) -> base.Array:


# Path: enn/supervised/multiloss_experiment.py
import functools
import dataclasses
import haiku as hk
import jax
import optax
from typing import Callable, Dict, NamedTuple, Optional, Sequence, Tuple
from acme.utils import loggers
from enn import base
from enn.supervised import base as supervised_base

# ============================================================================

"""An SGD experiment with facility for multiple losses."""




class TrainingState(NamedTuple):
  params: hk.Params
  opt_state: optax.OptState


@dataclasses.dataclass
class MultilossTrainer:
  """Specify the training schedule for a given loss/dataset.

  For step=1,2,...:
    If should_train(step):
      Apply one step of loss_fn on a batch = next(dataset).
  """
  loss_fn: base.LossFn  # Loss function
  dataset: base.BatchIterator  # Dataset to pull batch from
  should_train: Callable[[int], bool] = lambda _: True  # Which steps to train
  name: str = 'loss'  # Name used for logging


# Type definition for loss function after internalizing the ENN
PureLoss = Callable[[hk.Params, base.Batch, base.RngKey], base.Array]



class MultilossExperiment(supervised_base.BaseExperiment):