Machine Learning in Elixir: Chapter 1 - Unable to train model (page 19)

Solution is found at:

https://devtalk.com/t/machine-learning-in-elixir-chapter-1-doesnt-work-with-axon-0-7-page-26/173984

Explicitly converting the training and test sets to :f32 corrects the issue and the simulation can run.

feature_columns = [
  "sepal_length",
  "sepal_width",
  "petal_length",
  "petal_width"
]

label_column = "species"

x_train = Nx.stack(train_df[feature_columns], axis: 1)
|> Nx.as_type(:f32)

y_train =
  train_df
  |> DF.pull(label_column)
  |> Explorer.Series.to_list()
  |> Enum.map(fn
    "Iris-setosa" -> 0
    "Iris-versicolor" -> 1
    "Iris-virginica" -> 2
  end)
  |> Nx.tensor(type: :u8)
  |> Nx.new_axis(-1)
  |> Nx.equal(Nx.iota({1, 3}, axis: -1))
  |> Nx.as_type(:f32)

x_test = Nx.stack(test_df[feature_columns], axis: 1)
|> Nx.as_type(:f32)

y_test =
  test_df
  |> DF.pull(label_column)
  |> Explorer.Series.to_list()
  |> Enum.map(fn
    "Iris-setosa" -> 0
    "Iris-versicolor" -> 1
    "Iris-virginica" -> 2
  end)
  |> Nx.tensor(type: :u8)
  |> Nx.new_axis(-1)
  |> Nx.equal(Nx.iota({1, 3}, axis: -1))
  |> Nx.as_type(:f32)