Tuesday, 23 January 2018

Sigmoid Function and Softmax Function, Are They Related?

Sigmoid function is an activation function that gives value in the range of 0 to 1.  Values very much less than 0 gives activation value close to 0 and values very much greater than 0 gives activation value close to 1.
It can be expressed mathematically as:
$$ \sigma (x) = \cfrac {1} {1+{e}^{-x}}$$

Sigmoid function is used as the final activation function when we need to find if something is True.  It gives the probability that something is True or False as the truth we define.  For example, if we are to find if the given picture is cat, we may use sigmoid function as the final activating function.  But if we are to do multi-class problem, shall we use sigmoid function for all the classes or is there any other function?

Softmax function is widely used for multi-class classification problem.  But how does it compare with sigmoid function applied to all the classes?

Block diagram of logistic regression


It turns out that having sigmoid function for all the classes is same as having softmax function. It does not matter which activation function we would use.  We would be doing same thing.  Training using either of the functions is mathematically identical.  Let's see it how for single-class classification problem.

We employ sigmoid function giving result the probability of some picture being a cat.  That can be said as the two class classification problem having labels as:
1) Being a cat
2) Not being a cat

So, using sigmoid function as final activation function, we would get equivalent two-classes final activation as:
$$ \left( {a}_{1} \quad {a}_{2}\right)=\left( \cfrac {1} {1 + {e}^{-{z}_{1}}} \quad \cfrac {{e}^{-{z}_{1}}} {1+{e}^{-{z}_{1}}} \right)\quad \dots \quad (1)$$
Note that we got ${a}_{2}$ by subtracting ${a}_{1}$ from $1$.

That is same as applying sigmoid function on each classes output.  In that case we would get:
$$ \left( { a }_{ 1 }\quad { a }_{ 2 } \right) =\left( \cfrac { 1 }{ 1+{ e }^{ -{ z }_{ 1 } } } \quad \cfrac { 1 }{ 1+{ e }^{ -{ z }_{ 2 } } }  \right)  $$

Equating the value of ${a}_{2}$ and simplifying, we would get:
$$ {z}_{1}=-{z}_{2}\quad\dots\quad(2)$$

Putting $(2)$ into $(1)$,
$$\left( {a}_{1} \quad {a}_{2}\right)=\left( \cfrac {1} {1 + {e}^{-{z}_{1}}} \quad \cfrac {{e}^{{z}_{2}}} {1+{e}^{{z}_{2}}} \right)$$

Now, multiplying ${a}_{1}$ by ${e}^{\frac{{z}_1} {2}}$ and ${a}_{2}$ by ${e}^{-\frac{{z}_2} {2}}$ in both numerator and denominator, we would have:
$$\left( {a}_{1} \quad {a}_{2}\right)=\left( \cfrac {{e}^{\frac{{z}_1} {2}}} {{e}^{\frac{{z}_1} {2}} + {e}^{\frac{-{z}_{1}} {2}}} \quad \cfrac {{e}^{\frac{{z}_{2}}{2}}} {{e}^{-\frac{{z}_{2}}{2}}+{e}^{\frac{{z}_{2}}{2}}} \right)\quad\dots\quad(3)$$

Using $(2)$ in $(3)$,
$$\left( {a}_{1} \quad {a}_{2}\right)=\left( \cfrac {{e}^{\frac{{z}_1} {2}}} {{e}^{\frac{{z}_1} {2}} + {e}^{\frac{{z}_{2}} {2}}} \quad \cfrac {{e}^{\frac{{z}_{2}}{2}}} {{e}^{\frac{{z}_{1}}{2}}+{e}^{\frac{{z}_{2}}{2}}} \right)\quad\dots\quad(3)$$

$(3)$ is same as using final activation as softmax function where the $z$ values are $\frac{{z}_{1}}{2}$ and $\frac{{z}_{2}}{2}$.  It is no different than using softmax function on the $z$ values ${z}_{1}$ and ${z}_{2}$ as the two methods differ in just scaling of $z$ values.

So, training using sigmoid function for single class classification as final activation function is just a case for training using softmax function using two classes.  We can see the same true for more than two classes classification cases too.

Monday, 4 July 2016

Secret to Solving the Square Puzzle

During my childhood days, I was very much interested in math tricks though I didn't learn much of them.  One of the puzzle that I got to know is square puzzle where numbers from certain range are arranged in such a way that the sum of every rows and columns would be same.  Also, the sum of numbers in both the diagonals would also be the same.  I learned one method to solve such puzzle(probably from my father) but what bugged me was another solution done by XYZ which was different from what I had learned.  I tried to learn how XYZ did that solution but could not.  I even asked XYZ how s/he would do it, but s/he never taught the trick.  Failed after my few attempts, I didn't try and eventually forgot that thing.

This puzzle came up again when I was browsing Quora.  I was looking a thread of Mathematics trick and happened to see the trick.  With all excitement, I crammed that technique and was delighted.  That very technique is today's recipe.  However, it only applies to odd-numbered-boxes squares.

Monday, 27 June 2016

Four Pauses

  1. The shivering cold has long since gone.  I am still to take off my jackets.  The heat has already started to generate in my body.  I feel every cells carrying exothermic reactions inside me.  I am starting to feel cooked.  Oh, please, no scorching sun now!  No!  No!
  2. The dazzling reflection coming from the papers have teared me, not as a result of emotions but as a result of biological response.  I can't open my eyes, neither can I flush the beam.  With corrugated eyebrow, I close my eyes as hard as I could, as quickly as I could.
  3. The lines I get to write sporadically when the sun fades are not still to find some rhythm.  Rhythm lies in reader, argues one.  Rhythm is a must thing to writer, I insist. 
  4. Each breath creates a freshness in me as my breath resonates with flowing air.  The nitrogen dominated air, though odorless, is pleasant and fresh on its own right; though colorless, colorful on its own reflections; though tasteless, delicious on its own flavor.

Saturday, 25 June 2016

Cosine Cup, an Untalked Mathematical Surface and Its Area

Everyone must have seen and used umbrella once in their lifetime.  In the rainy days, umbrella is a must-have thing while going somewhere.  Monsoon has already started in Nepal and many villagers have already started sowing rice seeds.  However, very few people wonder about the structure and shape of the umbrella.  Looks like the truncate of sphere, umbrella is beautiful in its own structure and design, thanks to different colors and designs that makes it beautiful as a lotus flower.

Now, I would like you to wonder one thing.
Have you ever heard of cosine cup?



Most probably, not.  Even before I came up with this term, I hadn't listened.  I was doing some mathematics on surfaces obtained by revolving things about some axis and this thing, cosine cup, a surface of revolution came up.  There may even be umbrella designed on this shape.  Who knows?!


Saturday, 4 June 2016

Mathematical Analysis of Tower of Hanoi Problem

Tower of Hanoi is a mathematical puzzle invented by French Mathematician Ă‰douard Lucas in 1883.  There is a story about it being associated with Indian temple named Kashi Viswanath temple about which you can read here.

Now, let's jump into the problem.

Picture source: Wikipedia

There are three rods as shown in the figure above.  One of the rods has the discs arranged in the order of their size.  The discs are arranged upwards in the decreasing-size manner.  Our job is to shift whole set of discs to any other rod following the following rules.

  • Only one disc can be moved at a time.
  • Never can a larger disc sit above smaller disc.
  • Only uppermost disc can be moved from the set of discs.
Complying to these rules, we should shift the whole set of discs from first rod to any other rod.  There can be any number of discs.  Our task is to come for general solution for any 'n' number of discs.


Tuesday, 31 May 2016

Secret to Happiness


This article is my Quora answer to the question "How can I be happy every day?"  



Following these three rules makes me happy every time, not just every day. They are just my subjective thoughts.
  1. Stop over-thinking: Most people are unhappy as a result of over thinking. People over-think about their career, others career, future and so on. Thinking should be done at moderate level . We can’t plan and frame everything about our life at once. Life should be lived on its flow. Trying to go and plan everything of future is likely to disturb your tranquility.

  2. Live your life at present: What I have seen most unhappy people do is time traveling. They time travel past and future now and again. Just live your present moment. Forget about past. Stop worrying about future. Just think about what you have at present. Count every moments and respect it, enjoy it. Happiness is in the present moment.
  3. Enjoy small things: The secret to happiness is enjoying small things. I had read on Quora, Harsha Bhogle saying, big things don’t come often; so we need to learn to enjoy small things. Counting on those small moments is going to stop making you unhappy. Even it adds on our productivity. Dhoni breaks down the chase method.
All those three points may seem to mean same thing. Going in depth, they all mean same but expressing in three points, as I have done above, may help to get what I am trying to state.
Let talk taking real life examples.
  • People tend to think more when they are approaching pressure situations like examinations. Make plans on how are you going to handle the situation and work on those plans. Make sure the plans don’t get disturbed or changed. Sticking to the made plans is the most important thing you need to do. Execute the plans one by one. Don’t overdo, don’t under do.
  • People think about their future very much. Where I may go in future, what may happen to my life in future, am I going to achieve my dreams or not, what if s/he surpasses me, what if I fail, etc are the questions that haunt us again and again. FORGET ALL THAT. Worrying is not going to change anything. It is only making your efficiency decrease in present. Just stop thinking about future.
  • Some people can’t forget their past and the past haunts again and again. We can’t go in the past and rectify our mistakes. It is worthless worrying and repenting about what wrong we have done in our past. Do the things like apologizing, minimizing the damage of the past mistake, learning the lesson and not repeating the mistake.
  • While doing something, don’t do keeping some expectation from them or having framework of how something would be. The preconceived picture about a situation, event or anything is going to hurt us if preconceived picture does not come out to be real. Just be empty minded.
  • You are sitting in your room alone. Instead of being sad of how your friends dumped you or how no one is there by your side, enjoy the rumbling sound the fan is making. Enjoy the rays of sun the window is reflecting. Just be the observer and enjoy little things at the present.
Happy, happy !

Friday, 18 March 2016

30-seconds Vedic Method to Square a Number

One of the perks of knowing Mathematical tricks is you get to tell the answer before someone takes calculator out.  Squaring of two digit number is what we(especially people related to STEM field) encounter commonly.  I am going to tell a trick regarding squaring of 2-digits number which is from Vedic Mathematics.

Courtesy: Indiatoday