<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>JAX on Christian Hotz-Behofsits</title><link>https://hotzbehofsits.com/tags/jax/</link><description>Recent content in JAX on Christian Hotz-Behofsits</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Fri, 19 Aug 2022 00:00:00 +0000</lastBuildDate><atom:link href="https://hotzbehofsits.com/tags/jax/index.xml" rel="self" type="application/rss+xml"/><item><title>Multi-hot encoding in JAX</title><link>https://hotzbehofsits.com/post/jax_multi_hot/</link><pubDate>Fri, 19 Aug 2022 00:00:00 +0000</pubDate><guid>https://hotzbehofsits.com/post/jax_multi_hot/</guid><description>&lt;p&gt;JAX offers a range of practical functions, also for data preparation. One of these is &lt;a href="https://jax.readthedocs.io/en/latest/_autosummary/jax.nn.one_hot.html"&gt;jax.nn.one_hot&lt;/a&gt;, which performs a classic &lt;a href="https://en.wikipedia.org/wiki/One-hot"&gt;one-hot encoding&lt;/a&gt;. Unfortunately, I was not able to find a suitable multi-hot equivalent for multi-label applications. However, it is also quite easy to implement the functionality directly in JAX:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;import&lt;/span&gt; jax
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;import&lt;/span&gt; jax.numpy &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; jnp
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; functools &lt;span style="color:#f92672"&gt;import&lt;/span&gt; partial
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;@partial&lt;/span&gt;(jax&lt;span style="color:#f92672"&gt;.&lt;/span&gt;jit, static_argnames&lt;span style="color:#f92672"&gt;=&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;num_classes&amp;#34;&lt;/span&gt;))
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;multi_hot&lt;/span&gt;(labels, num_classes: int):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; jnp&lt;span style="color:#f92672"&gt;.&lt;/span&gt;take(jnp&lt;span style="color:#f92672"&gt;.&lt;/span&gt;eye(num_classes), jnp&lt;span style="color:#f92672"&gt;.&lt;/span&gt;array(labels), axis&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;)&lt;span style="color:#f92672"&gt;.&lt;/span&gt;sum(axis&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>Multinomial Logistic Regression in JAX</title><link>https://hotzbehofsits.com/post/jax_first_steps/</link><pubDate>Fri, 29 Jul 2022 00:00:00 +0000</pubDate><guid>https://hotzbehofsits.com/post/jax_first_steps/</guid><description>&lt;p&gt;Classifications are a classic machine learning problem we can tackle using &lt;a href="https://en.wikipedia.org/wiki/Logistic_regression"&gt;logistic regression&lt;/a&gt;. If we distinguish between more than two classes, we call it a &lt;a href="https://en.wikipedia.org/wiki/Multinomial_logistic_regression"&gt;multinomial logistic regression&lt;/a&gt;. In this post, I will show how this can be done using &lt;a href="https://jax.readthedocs.io/en/latest/"&gt;JAX&lt;/a&gt; based on the well-known &lt;a href="https://en.wikipedia.org/wiki/Iris_flower_data_set"&gt;Fisher&amp;rsquo;s Iris dataset&lt;/a&gt; (every R user should be familiar with this one).&lt;/p&gt;
&lt;p&gt;First, we have to load the required libraries and load the data. Since this is a classification, we have a set of predictors (aka. features) and a label for each sample.&lt;/p&gt;</description></item></channel></rss>