Plotting functions with LaTeX & Tikz

I’ve been working on some notes for my students on Neural Networks. I am using Pandoc to write the text, and convert it back to LaTeX for PDF generation, and to HTML, for making the documents available online. In a future post I might talk about Pandoc, but for now, I want to share something I found on TeX Stack Exchange to plot functions. To make it clear how to use, I am presenting four simple examples: four common activation functions used in Neural Networks.

To start with, the well known sigmoid function: \[a = \frac{1}{1+e^{-z}}\]:

This can be achieved with the following tikzpicture environment:

\begin{tikzpicture}
\begin{axis}[
    axis lines=middle,
    xmax=10,
    xmin=-10,
    ymin=-0.05,
    ymax=1.05,
    xlabel={$x$},
    ylabel={$y$}
]
\addplot [domain=-9.5:9.5, samples=100,
          thick, blue] {1/(1+exp(-x)};
\end{axis}
\end{tikzpicture}

Now, for the tanh, defined as \[a=\frac{e^z – e^{-z}}{e^z + e^{-z}}\], you get:

With the following code:

\begin{tikzpicture}
\begin{axis}[
    axis lines=middle,
    xmax=10,
    xmin=-10,
    ymin=-1.05,
    ymax=1.05,
    xlabel={$x$},
    ylabel={$y$}]
\addplot [domain=-9.5:9.5, samples=100,
     thick, blue] {(exp(x) - exp(-x))/(exp(x) + exp(-x))};
\end{axis}
\end{tikzpicture}

I was already loving it, but it was awesome when I tested the ReLU math, and it worked as expected: \[a=\max(0, z)\]

This one can be obtained using the simple…

\begin{tikzpicture}
\begin{axis}[
    axis lines=middle,
    xmax=6,
    xmin=-6,
    ymin=-0.05,
    ymax=5.05,
    xlabel={$x$},
    ylabel={$y$}]
\addplot [domain=-5.5:5.5, samples=100, thick, blue] {max(0, x)};
\end{axis}
\end{tikzpicture}

And now, although not being extremely interesting, the Leaky ReLU (well, not implemented exactly as the definition, just to get it a little more easy to read): \[a=max(0.001 \times z, z)\]

With the cheating…

\begin{tikzpicture}
\begin{axis}[
    axis lines=middle,
    xmax=6,
    xmin=-6,
    ymin=-1.05,
    ymax=5.05,
    xlabel={$x$},
    ylabel={$y$}]
\addplot [domain=-5.5:5.5, samples=100,
          thick, blue] {max(0.1 * x, x)};
\end{axis}
\end{tikzpicture}

And to conclude, and just for reference, do not forget to load the required packages:

\usepackage{tikz}
\usepackage{pgfplots}

Arango::Tango Perl 5 Module

(cross posted from blogs.perl.org)

I am planning on a new project, and a friend suggested me to look to Arango, as an alternative do Mongo, specially because it includes a graphs query language integrated. As I am not really used to Mongo at all, decided to give it a try.

Unfortunately I did not find a proper module to use Arango from Perl. Therefore I decided to start one from scratch. Probably not a great idea as my free time is likely non-existent. But nevertheless, I did it. Arango::DB was born!

I am trying to abstract major entities (database, collection, document, cursor) in order to use them directly as proper objects in Perl, and at the same time, try to keep a low code profile, delegating most options directly to Arango REST interface. In order not to be too relaxed, I am using JSON::Schema::Fit to validate the parameters sent to Arango API.

The main problem is that there are too many options and endpoints to use, and too few time to do it. Anyway, there is a working version, that already allows the storage of documents, and querying them using AQL. Unfortunately all the user-related functionalities are not yet implemented.

While I would love to keep maintaining and defining the direction of the module, I am very grateful to any patches, bug fixes of added functionalities, keeping the same simple approach in mind.

Regarding using a module for objects, I might decide to use Moo, but for now, I am still happy using Perl blessed hashes directly.

Hope to post more news very soon.

GIT repository: https://gitlab.com/ambs/perl5-arango-tango/
Latest release: https://metacpan.org/pod/Arango::Tango

Hawaii Five-0 (2010) – S01

I watched some random episodes in the TV, and recently decided to give it a watch. As my sister also likes, it allows some nice time at the night.

The series is not something formidable. But it is fun, it has some action, and it runs in Hawaii, with nice landscapes, great beaches and bikini babes. What else can we ask for in a TV series?

As any other series, some episodes are great, some good, some acceptable. But I must say that Season 1 ends with high interest. Looking forward to start watching Season 2 today.

Star Trek: The Motion Picture (1979)

Enterprise shipyard

I know this is an old movie. After watching it I remember I did it before. Well, regarding it, what can I say? Of course I can’t comment the special effects. But while some images look cool (see the one above) some other are quite, quite bad (as an example, the small ship used to travel between Starfleet quarters to enterprise. So, not sure why some are not that bad, and some are awful. But then, in 1979 I was one year old.

About the story, acceptable. I found V-GER story interesting. Always nice to see some cross-over between sci-fi and some reality.

Also, I just discovered that the series is prior to the movie. So, now I have in my queue to watch at least one episode from it, and see if I want to see them all.

The Devil’s Violinist (2013)

David Garrett

Really bad movie for what I was expected. It is not a documentary (just like Bohemiam Raphsody isn’t a documentary). Thus, we expect a minimal story. And this story is… well, mostly non existent.

Other than that, I was expecting better acting. I know David Garrett is not an actor. But I suppose there was a casting for that character. And choosing him was a bad idea. Even being a violinist player, he exaggerated on his movements. Paganini was a virtuoso, and played some dissonant musics. But you should not move like player a rock music to mimic Paganini. Really.

The nice part of the movie, as usual, is a girl. Andrea Deck is gorgeous. And her acting was quite good. Kudos to her.

August Rush (2007)

Freddie Highmore

Someone told me and my sister about this movie, as being nice, and interesting to present to music students (10 yo). I took the chance to watch it too. And although there is a lot I can argue about (namely to someone learn how to write music from a single explanation of the position of notes in a keyboard), the movie is interesting enough. Of course it is a Drama, something I usually prefer not to watch. But the story is nice, and ends well, as expected for Hollywood movies.

The most interesting thing (other than having the nice and cute Keri Russell, is to listen my sister saying, about the main character (above in the picture) that he resembles the actor for Good Doctor. And then, when looking at the information at IMDB, find out that she is right. I wouldn’t get there myself.

Chef (2014)

Jon Favreau and Emjay Anthony
Jon Favreau and Emjay Anthony

Another movie that was in the TV, and that I started watching just because I like the title. I will not talk about the story. It is nothing really new, and it is quite simple (I could explain the whole movie in two lines). But the movie is enjoyable, fun, and results on some good time. What I did not understand is why we have Scarlett Johansson, Dustin Hoffman or Robert Downey Jr. They are all well known actors, and their characters in this movie are less than secondary. And in 2014 they were already well known for Marvel (at least Scarlett and Robert) and I do not think they were needing the money. And this movie production could be quite cheaper with other actors. Just saying.

Other than that, kudos to Emjay Anthony. I really enjoyed his acting in this movie.

Astérix: Le secret de la potion magique (2018)

Gaulish Team
Gaulish Team

It would be hard to write this post to talk about the story and the characters. I am a fan of Asterix, but I am used to read it in Portuguese, and writing about it in English would be hard. And for those wondering, no, I did not watch the original (French) version, but the dubbed version. And although I usually hate Portuguese dubbed versions, it wasn’t that bad. Acceptable enough.

About the story, well it focus the usual ingredients of Asterix stories: the magic potion, the Romans, the pirates, the wild boars, etc. It isn’t as good as the first books. It focus some ideas from recent movies that I will not refer so I do not give any spoilers. Ah… and it has a religion reference that, although I find it humorous, I think some other Catholics might not like.

Mr. & Mrs. Smith (2005)

Angelina Jolie & Brad Pitt @ Mr. & Mrs. Smith
Angelina Jolie & Brad Pitt @ Mr. & Mrs. Smith

Again, taking the Christmas holidays to see some movies in the TV. I had this in my to-see list for some time, as I found the plot idea interesting. And I can say the movie delivers it. Of course it is a typical Hollywood plot, with all ending well, and with the main characters surviving everything. But you are expecting it from the beginning. Some details remember me of Get Smart series, like the gun locker hidden in the oven. Nice to watch and laugh.

Now You See Me (2013)

Isla Fisher @ Now You See Me
Isla Fisher @ Now You See Me

While the fact of having Isla Fisher or Mélanie Laurent could make me rate this movie high, the truth is that I can’t. I can’t see any interest in the movie. There is a story, but so vacuous… And if one wants a movie about magic, or it makes it really about magic, and has interesting details, that are plausible, or they want a movie about fiction, and then you can make what you want. Doing a movie somewhat in the middle doesn’t make sense. At least for me. If you want an example, explain how Isla Fisher can fly in a soap bubble, or how the… oops, spoilers, or how the last movie scene is possible. Rated 5 out of 10 in IMDB.