Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

# Chapter 1 ```elixir Mix.install([ {:axon, "~> 0.7"}, {:nx, "~> 0.9"}, {:explorer, "~> 0.10.0"}, {:kino, "~> 0.14.2"} ]) ``` ## Section ```elixir require Explorer.DataFrame, as: DF iris = Explorer.Datasets.iris() ``` ```elixir cols = ~w(sepal_width sepal_length petal_length petal_width) normalized_iris = DF.mutate( iris, for col <- across(^cols) do # Needed to cast result to :f32 due to {col.name, cast((col - mean(col)) / standard_deviation(col), :f32)} end ) ``` ```elixir normalized_iris = DF.mutate(normalized_iris, [ species: Explorer.Series.cast(species, :category) ]) ``` ```elixir shuffled_normalized_iris = DF.shuffle(normalized_iris) ``` ```elixir train_df = DF.slice(shuffled_normalized_iris, 0..119) test_df = DF.slice(shuffled_normalized_iris, 120..149) ``` ```elixir feature_columns = [ "sepal_length", "sepal_width", "petal_length", "petal_width" ] x_train = Nx.stack(train_df[feature_columns], axis: -1) y_train = train_df["species"] |> Nx.stack(axis: -1) |> Nx.equal(Nx.iota({1, 3}, axis: -1)) x_test = Nx.stack(test_df[feature_columns], axis: -1) y_test = test_df["species"] |> Nx.stack(axis: -1) |> Nx.equal(Nx.iota({1, 3}, axis: -1)) ``` ```elixir model = Axon.input("iris_features", shape: {nil, 4}) |> Axon.dense(3, activation: :softmax) ``` ```elixir Axon.Display.as_graph(model, Nx.template({1, 4}, :f64)) ``` ```elixir data_stream = Stream.repeatedly(fn -> {x_train, y_train} end) ``` ```elixir trained_model_state = model |> Axon.Loop.trainer(:categorical_cross_entropy, :sgd) |> Axon.Loop.metric(:accuracy) |> Axon.Loop.run(data_stream, Axon.ModelState.new(%{}), iterations: 500, epochs: 10) ``` ```elixir data = [{x_test, y_test}] model |> Axon.Loop.evaluator() |> Axon.Loop.metric(:accuracy) |> Axon.Loop.run(data, trained_model_state) ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run on your machine

with Livebook Desktop

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More ×