# Machine Learning in Elixir: error thrown in code snippet (page 44)

**URL:** <https://forum.devtalk.com/t/machine-learning-in-elixir-error-thrown-in-code-snippet-page-44/117271>\
**Category:** PragProg Customers\
**Tags:** book-machine-learning-in-elixir, errata\
**Created:** [21 July 2023 05:22 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-thrown-in-code-snippet-page-44/117271 "2023-07-21T05:22:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdarla](https://forum.devtalk.com/user_avatar/forum.devtalk.com/bdarla/32/860_2.png) [@bdarla](https://forum.devtalk.com/u/bdarla)\
**Post date:** [21 July 2023 05:22 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-thrown-in-code-snippet-page-44/117271/1 "2023-07-21T05:22:56Z")

</div>

Running the code snippet of Chapter 2 on page 44 of version B1.0, I get the following error:

Evaluation process terminated - an exception was raised:  
\*\* (FunctionClauseError) no function clause matching in String.graphemes/1  
(elixir 1.15.2) lib/string.ex:1934: String.graphemes(:ok)  
(benchee 1.1.0) lib/benchee/utility/erlang\_version.ex:100: Benchee.Utility.ErlangVersion.parse\_erlang\_version/1  
(benchee 1.1.0) lib/benchee/utility/erlang\_version.ex:84: Benchee.Utility.ErlangVersion.includes\_fixes\_from?/2  
(benchee 1.1.0) lib/benchee/benchmark/repeated\_measurement.ex:60: Benchee.Benchmark.RepeatedMeasurement.determine\_resolution\_adjustment/2  
(benchee 1.1.0) lib/benchee/benchmark/repeated\_measurement.ex:42: Benchee.Benchmark.RepeatedMeasurement.determine\_n\_times/5  
(benchee 1.1.0) lib/benchee/benchmark/runner.ex:226: Benchee.Benchmark.Runner.measure\_runtimes/4  
(benchee 1.1.0) lib/benchee/benchmark/runner.ex:102: Benchee.Benchmark.Runner.measure\_scenario/2  
(elixir 1.15.2) lib/task/supervised.ex:101: Task.Supervised.invoke\_mfa/2

I run the code on an Apple Silicon M2 and I use Elixir 1.15.4 and Erlang/OTP 26.  
I have not found any solution to this issue yet.

---

<div class="post-metadata">

**Author:** ![bdarla](https://forum.devtalk.com/user_avatar/forum.devtalk.com/bdarla/32/860_2.png) [@bdarla](https://forum.devtalk.com/u/bdarla)\
**Post date:** [21 July 2023 05:47 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-thrown-in-code-snippet-page-44/117271/2 "2023-07-21T05:47:46Z")

</div>

Update:

In the following snippet, each of the Pure Elixir and EXLA based softmax functions seem to work (not sure if I created the tensor correctly though).

The Benchee part still doesn’t work (gives the same error as reported above).

```auto
key = Nx.Random.key(1702)
{tensor, _new_key} = Nx.Random.uniform(key, shape: {1, 1_000_000})
Softmax.softmax(tensor)
apply(EXLA.jit(&Softmax.softmax/1), [tensor])

#Benchee.run(
# %{
# "JIT with EXLA" => fn -> apply(EXLA.jit(&Softmax.softmax/1), [tensor]) end,
# "Regular Elixir" => fn -> Softmax.softmax(tensor) end
# },
# time: 10
#)

```

---

<div class="post-metadata">

**Author:** ![johnmacshea](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@johnmacshea](https://forum.devtalk.com/u/johnmacshea)\
**Post date:** [21 July 2023 07:55 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-thrown-in-code-snippet-page-44/117271/3 "2023-07-21T07:55:03Z")

</div>

I had the same issue with Benchee - I saw someone mention in the elixir forum that Benchee was crashing the LiveBook cells a while back. I’m also on Apple Silicon - LiveBook v0.9.3, Elixir v1.14.2.

I was curious so:  
{exla\_time, exla\_result} = :timer.tc(fn →   
apply(EXLA.jit(&Softmax.softmax/1), [tensor])  
end)

{standard\_time, standard\_result} = :timer.tc(fn →   
Softmax.softmax(tensor)  
end)

IO.puts “exla is #{standard\_time/exla\_time} times faster”  
result (one run):  
exla is 66.25130208333333 times faster

On multiple runs its between 450 and 500 times faster.

---

<div class="post-metadata">

**Author:** ![bdarla](https://forum.devtalk.com/user_avatar/forum.devtalk.com/bdarla/32/860_2.png) [@bdarla](https://forum.devtalk.com/u/bdarla)\
**Post date:** [21 July 2023 09:09 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-thrown-in-code-snippet-page-44/117271/4 "2023-07-21T09:09:11Z")

</div>

Thank you for the feedback and the alternative solution! Indeed, it seems to be an issue with Benchee.

In my local experiments, I get between 250-400 times faster.

---

<div class="post-metadata">

**Author:** ![bdarla](https://forum.devtalk.com/user_avatar/forum.devtalk.com/bdarla/32/860_2.png) [@bdarla](https://forum.devtalk.com/u/bdarla)\
**Post date:** [21 July 2023 19:12 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-thrown-in-code-snippet-page-44/117271/5 "2023-07-21T19:12:17Z")

</div>

Update:  
By visiting the Elixir forum, I discovered that with switching to `Benchee` from the master branch on GitHub, the problem is solved.

Hence, I modified initial setup cell to:

```auto
Mix.install([
  {:nx, "~> 0.5"},
  {:exla, "~> 0.5"},
  {:benchee, github: "bencheeorg/benchee", override: true},
])

```

For the record, the improvement I noted was 337 times faster. I guess this is without using GPU, yet.

---

<div class="post-metadata">

**Author:** ![seanmor5](https://forum.devtalk.com/user_avatar/forum.devtalk.com/seanmor5/32/1169_2.png) [@seanmor5](https://forum.devtalk.com/u/seanmor5)\
**Post date:** [28 August 2023 17:32 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-thrown-in-code-snippet-page-44/117271/6 "2023-08-28T17:32:03Z")

</div>

Just a note that I’ve updated the book to use the master branch as well 🙂
