{"id":8626,"date":"2026-08-09T15:59:29","date_gmt":"2026-08-09T14:59:29","guid":{"rendered":"https:\/\/sinatootoonian.com\/?p=8626"},"modified":"2026-08-09T16:00:02","modified_gmt":"2026-08-09T15:00:02","slug":"how-to-pick-activation-function","status":"publish","type":"post","link":"https:\/\/sinatootoonian.com\/index.php\/2026\/08\/09\/how-to-pick-activation-function\/","title":{"rendered":"Activation Functions aren&#8217;t Arbitrary"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this note I&#8217;m going to re-explain canonical link functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose that we have a set of input-output observations $(\\xx_1, t_1), \\dots (\\xx_N, t_N).$ We&#8217;re interested finding a mapping from the inputs to the outputs. In particular, we&#8217;ll consider the form $ y = f(\\ww^T \\xx).$ For a given activation function $f$, we may decide to fit $\\ww$ by computed a squared loss $$ L(\\ww) = \\sum_n {1 \\over 2} (t_n &#8211; y_n)^2 $$ and then minimizing the loss down the gradient, $$ \\nabla_\\ww L = \\sum_n (t_n &#8211; y_n) f'(\\ww^T\\xx_n) \\xx_n.$$<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But why is the squared loss the right one to use? And how should we pick $f$ and fit $\\ww$? <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can begin by ignoring the inputs and focusing on the outputs.  We want to assign a probability to each observation. It seems reasonable to assume that all of the observations would come from the same family of probability distributions, but which family?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A fruitful guess is the exponential family. In particular, the probability we assign to  each output is $$ p(t|\\eta, s) = h(t\/s) g(\\eta) \\exp(\\eta t\/s)\/s,$$ where we assume the scale hyperparameter  $s$ is fixed throughout but that the parameter $\\eta$ can vary with each observation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The intuition is that when e.g. modelling the data as Gaussian, $\\eta$ can set the mean of the distribution, and that is in turn determined by the inputs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now suppose we wanted to find the parameter  $\\eta$ for a given observation $t$ using maximum likelihood. The log likelihood up to constant terms is $ \\log p(t|\\eta, s) = \\log g(\\eta) + \\eta t\/s + &#8230;,$ so $$ {d \\log p \\over d \\eta} = {1 \\over g(\\eta)} g'(\\eta) + {t \\over s}.$$ By virtue of the exponential family, this is equal to \\begin{align*} {d \\log p \\over d \\eta} &amp;= -{1 \\over s} \\mu(\\eta,s) + {t\\over s} = {1 \\over s} (t -\\mu(\\eta,s)),\\end{align*} where $$\\mu(\\eta, s) \\triangleq \\mathbb \\EE(t|\\eta, s).$$<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So if we were able to adjust $\\eta$ independently for every observation, we would set it so that the mean of the distribution determined by $\\eta$ matched the observation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The simple form of this update suggests having our output variable $y$ model the mean of the distribution, $$ y(\\eta) = \\mu(\\eta, s) = -s {g'(\\eta) \\over g(\\eta)}.$$ <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Assuming some well-behaved-ness, we can invert this relationship and write $\\eta$ as a function $\\psi$ of $y$, which is in turn determined by the drive $a$ to $f$, which is determined by the weights: $$ \\eta = \\psi(y) = \\psi(f(a)) = \\psi(f(\\ww^T\\xx)).$$<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We now use the chain rule to compute the gradient of likelihood, \\begin{align*} {dL \\over d\\ww} &amp;= {dL \\over d\\eta} {d\\eta \\over dy} {dy \\over da} {da \\over d\\ww} \\\\ &amp;=(t &#8211; y)\\psi'(y) f'(a) \\xx \\end{align*}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This still doesn&#8217;t determine $f$: this expression is valid for any (well-behaved) $f$. However, we can appeal to the difficulty of the computation: each weight update would have to be scaled by its own specific factor $$h(y) = f'(a(y)) \\psi'(y) = f'(f^{-1} (y)) \\psi'(y).$$ This is a static function of values available at each synapse, so it&#8217;s even neurally plausible, but it is complicated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can get a simpler update if we pick $f$ appropriately. In particular,  if  we pick $f$ such that $$ \\psi'(y) f'(a)= 1 \\implies \\psi'(y) = [f'(a)]^{-1} = {da \\over df} = {d f^{-1} \\over dy}.$$ <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One solution to this is $$  f^{-1} (y) = \\eta(y) \\implies f \\circ \\eta = \\text{identity}, $$ i.e. $f$ is the inverse of $\\eta$.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example: Logistic Regression<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s work this out for the case of logistic regression. Here our observations consist of inputs $\\xx_i$ mapped to binary targets $t_i \\in \\{0,1\\}$.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since the observations are binary, it&#8217;s natural to model them as coming from a Bernoulli distribution with parameter $\\theta$. So, $$ t_i \\sim p(t_i|\\theta_i) = \\text{Ber}(\\theta_i) = \\begin{cases} 1 &amp; \\quad \\text{with probability } &amp; \\theta_i \\\\ 0 &amp; \\quad \\text{with probability } &amp; 1 &#8211; \\theta_i \\end{cases}.$$ Notice that $\\theta_i$ is also the mean. The idea is that each sample $\\xx_i$ sets its own corresponding mean $\\theta_i$, and that in turn determines the binary observation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To map this into the exponential family, let&#8217;s first drop the index $i$ for clarity. We can write the Bernoulli distribution as \\begin{align*} p(t|\\theta) &amp;= \\text{Ber}(\\theta)\\\\  &amp;= \\theta^t (1 &#8211; \\theta)^{1 &#8211; t}\\\\ &amp;= \\left({\\theta \\over 1 &#8211; \\theta}\\right)^t (1 &#8211; \\theta) \\\\ &amp;= e^{t \\ln(\\theta\/1-\\theta)} (1 &#8211; \\theta).\\end{align*}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Comparing this to the form we need for the exponential family, we see that our &#8220;natural parameter&#8221; $\\eta$ is the log odds: $$ \\eta = \\ln {\\theta \\over 1 &#8211; \\theta}.$$ <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The remaining term $(1 &#8211; \\theta)$ is our $g(\\eta)$, we just have to express it in terms of $\\eta$. From our expression for $\\eta$ we see that $$ \\theta = {e^\\eta \\over 1 + e^\\eta} \\implies 1 &#8211; \\theta = {1 \\over 1 + e^\\eta} = g(\\eta).$$<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We don&#8217;t have a scale parameter, so $$y(\\eta) = &#8211; {g'(\\eta) \\over g(\\eta)} = {e^\\eta \\over 1 + e^\\eta} = {1 \\over 1 + e^{-\\eta}}.$$ <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So we see that the sigmoidal activation function that we frequently encounter is not arbitrary, but is determined by our probabilistic model of the observations!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can also check that it $y(\\eta)$ is indeed the mean parameter: \\begin{align*} y(\\eta) &amp;= {e^\\eta \\over 1 + e^\\eta}\\\\ &amp;= {e^{\\ln(\\theta\/1-\\theta)} \\over 1 + e^{\\ln(\\theta\/1-\\theta)}}\\\\ &amp;= {{\\theta \\over 1 &#8211; \\theta}  \\over 1 + {\\theta \\over 1 &#8211; \\theta}}\\\\ &amp;= {{\\theta \\over 1 &#8211; \\theta} \\over {1 \\over 1 -\\theta}}\\\\ &amp;= \\theta,\\end{align*} as expected.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$$\\blacksquare$$<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this note I&#8217;m going to re-explain canonical link functions. Suppose that we have a set of input-output observations $(\\xx_1, t_1), \\dots (\\xx_N, t_N).$ We&#8217;re interested finding a mapping from the inputs to the outputs. In particular, we&#8217;ll consider the form $ y = f(\\ww^T \\xx).$ For a given activation function $f$, we may decide [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8626","post","type-post","status-publish","format-standard","hentry","category-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/sinatootoonian.com\/index.php\/wp-json\/wp\/v2\/posts\/8626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sinatootoonian.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sinatootoonian.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sinatootoonian.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sinatootoonian.com\/index.php\/wp-json\/wp\/v2\/comments?post=8626"}],"version-history":[{"count":81,"href":"https:\/\/sinatootoonian.com\/index.php\/wp-json\/wp\/v2\/posts\/8626\/revisions"}],"predecessor-version":[{"id":8709,"href":"https:\/\/sinatootoonian.com\/index.php\/wp-json\/wp\/v2\/posts\/8626\/revisions\/8709"}],"wp:attachment":[{"href":"https:\/\/sinatootoonian.com\/index.php\/wp-json\/wp\/v2\/media?parent=8626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sinatootoonian.com\/index.php\/wp-json\/wp\/v2\/categories?post=8626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sinatootoonian.com\/index.php\/wp-json\/wp\/v2\/tags?post=8626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}