# `ExBurn.Model`
[🔗](https://github.com/ohhi-vn/ex_burn/blob/main/lib/ex_burn/model.ex#L1)

Model definition and training orchestration for ExBurn.

This module provides a high-level API for defining, compiling, and
training neural network models using Axon with the ExBurn backend.

## Usage

    # Define a model
    model =
      Axon.input("input", shape: {nil, 784})
      |> Axon.dense(256)
      |> Axon.relu()
      |> Axon.dropout(rate: 0.2)
      |> Axon.dense(128)
      |> Axon.relu()
      |> Axon.dense(10)

    # Compile with ExBurn backend
    compiled = ExBurn.Model.compile(model, loss: :cross_entropy, optimizer: :adam)

    # Train
    ExBurn.Model.fit(compiled, train_data, epochs: 10, batch_size: 32)

# `t`

```elixir
@type t() :: %ExBurn.Model{
  axon_model: Axon.ModelState.t(),
  compiled: boolean(),
  loss_fn: atom(),
  optimizer: atom(),
  optimizer_state: map(),
  params: map()
}
```

# `compile`

```elixir
@spec compile(
  Axon.ModelState.t(),
  keyword()
) :: t()
```

Compiles an Axon model for training with the ExBurn backend.

## Options

  * `:loss` — Loss function: `:cross_entropy`, `:mse`, `:binary_cross_entropy` (default: `:cross_entropy`)
  * `:optimizer` — Optimizer: `:adam`, `:sgd`, `:rmsprop` (default: `:adam`)
  * `:learning_rate` — Learning rate (default: 0.001)
  * `:device` — Device: `:cpu` or `:gpu` (default: `:gpu`)

## Returns

  An `ExBurn.Model` struct ready for training.

# `compute_loss`

```elixir
@spec compute_loss(t(), Nx.Tensor.t(), Nx.Tensor.t()) ::
  {:ok, Nx.Tensor.t()} | {:error, String.t()}
```

Computes the loss between predictions and targets.

Supports `:cross_entropy` (with log-softmax numerical stability) and `:mse`.

# `deserialize_params`

```elixir
@spec deserialize_params(binary()) :: {:ok, map()} | {:error, String.t()}
```

Deserializes parameters from a binary.

# `load`

```elixir
@spec load(t(), Path.t()) :: {:ok, t()} | {:error, String.t()}
```

Loads model parameters from a file.

# `loss_function`

```elixir
@spec loss_function(t()) :: atom()
```

Returns the model's loss function.

# `optimizer`

```elixir
@spec optimizer(t()) :: atom()
```

Returns the model's optimizer.

# `parameters`

```elixir
@spec parameters(t()) :: map()
```

Returns the current model parameters.

# `predict`

```elixir
@spec predict(t(), Nx.Tensor.t()) :: {:ok, Nx.Tensor.t()} | {:error, String.t()}
```

Runs a forward pass through the model.

Returns the output tensor as an Nx tensor.

# `save`

```elixir
@spec save(t(), Path.t()) :: :ok | {:error, String.t()}
```

Saves the model parameters to a file using compressed Erlang term format.

# `serialize_params`

```elixir
@spec serialize_params(t()) :: binary()
```

Serializes parameters to a binary for network transfer or storage.
Uses compressed Erlang term format.

# `summary`

```elixir
@spec summary(t()) :: String.t()
```

Returns a summary of the model architecture including parameter count.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
