In Quest of Making a Master Poet

You might have noticed, I titled this post “... of Making ...” instead of “... of Being ...” and you'll get to know why. So, I thought, who would write poetry like me after I die? To address the issue at hand, I started thinking of ways I could use my knowledge of Machine Learning to build a bot that could be trained by my poems and the way I write, and be smart enough to spit out rhymes when required.

The simplest and the most trivial step in this quest is to write a Markov chain text generator. A Markov chain (in layman language) is a chain in which state changes are probabilistic and a future state depends just on the present state and is independent of past states.

So, how will this work?

The first step will be to get a collection of poems (or any text for that matter) which will be used as the corpus. Now, using our training database (the corpus) we will look for all triplets (three words in succession) and will make a map of all words which can come after two words. The current state of our Markov chain will be represented by the last two words in our sentence.

To begin, choose two successive words from the database at random. Look into the map and check which possible words can come after those two words. Choose one of them at random and continue.

An example of the method follows.

Input text: Hope is a good thing, maybe the best of things, and no good thing ever dies.


Triplets:
[['Hope', 'is', 'a'],
 ['is', 'a', 'good'],
 ['a', 'good', 'thing,'],
 ['good', 'thing,', 'maybe'],
 ['thing,', 'maybe', 'the'],
 ['maybe', 'the', 'best'],
 ['the', 'best', 'of'],
 ['best', 'of', 'things,'],
 ['of', 'things,', 'and'],
 ['things,', 'and', 'no'],
 ['and', 'no', 'good'],
 ['no', 'good', 'thing'],
 ['good', 'thing', 'ever'],
 ['thing', 'ever', 'dies.']]

Map:
{('maybe', 'the'): ['best'],
 ('no', 'good'): ['thing'],
 ('thing', 'ever'): ['dies.'],
 ('good', 'thing,'): ['maybe'],
 ('the', 'best'): ['of'],
 ('best', 'of'): ['things,'],
 ('things,', 'and'): ['no'],
 ('and', 'no'): ['good'],
 ('thing,', 'maybe'): ['the'],
 ('Hope', 'is'): ['a'],
 ('is', 'a'): ['good'], 
 ('a', 'good'): ['thing,'],
 ('good', 'thing'): ['ever'],
 ('of', 'things,'): ['and']}

As I did not have a huge collection of my own poems, I used Sir Robert Frost's poetry collection North of Boston as the corpus. It will generate gibberish most of the time but can generate sensible statements once in a while. A kind of sensible one generated by the algorithm follows.

Who else will harbour him At his age for pair,
the pair, you know. We sha'n't have the of
art of what mean
I mean by home. Of the
course the easy job For the next forty it
summers--call it forty. But not
I'm not so drunk I can't here:
stay here: Estelle's take
to take it as much wishing
as wishing him good-night. He went on: sure--I'm
'I'm sure--I'm sure'--as polite as be.
could be. He spoke to his door.

Yes, that's how a drunk Robert Frost writes.

Furthur work includes the use of Backus–Naur context-free grammar and Natural Language Processing techniques to make the outputs more meaningful.

Fork on Github.

Mosaic using Facebook profile pictures

We can easily download profile pictures of people using Facebook's Graph API. For instance, to download the profile picture of Mark Zuckerburg (Facebook ID - 4) you can simply use this link. Following the pattern we can download a lot of profile pictures by simply increasing the Facebook ID sequentially beginning at 4 (1,2 and 3 aren't used). You cannot however download the profile pictures of all users using this method because at this point of time Facebook ID naming pattern has changed but that pattern is also not too tough to figure out.

A scaled version of the mosaic of first 3249 profile pictures. The original file was 61 MB large.

Here are the steps I followed to create a mosaic of first 3249 profile pictures on Facebook:

  1. For each fb-id beginning at 4, check if it is the default silhouette of Facebook using json data obtained from this link.
  2. If the Image is not silhouette then download it.
  3. Resize the Image to 100x100 pixels.
  4. Join the resized images to form a mosaic.

Check out the python script here: https://github.com/abdulfatir/fb-mosaic

A walk unto Infinity (the one which leads to a negative fraction)

Have you ever wondered about divergent series? These are the series which do not converge to a finite sum. Take for instance the following infinite series:

1+(1/2)+(1/4)+(1/8) …

We know that this series converges to 2 at infinity. Such series are known as convergent series. The series which we are interested in, however, are the ones called divergent series.

N.H. Abel wrote

❝The divergent series are the invention of the devil, and it is a shame to base on them any demonstration whatsoever.❞

Divergent series cannot be summed by usual methods but they can be ❛summed❜ (read: associated) to a number using other rigorous methods.

One famous example of a divergent series is the sum of all natural numbers upto infinity.

S = 1+2+3+4+5 ...

This series is particularly important because it has applications in other fields like Bosonic String Theory, Quantum Field Theory et cetera.

There are many methods to ❛sum❜ this series which require different amounts of rigor. I'm going to discuss two methods, the one using Cesaro Summation of Grandi's Series and Euler's Method. Other methods include Zeta Function Regularization, Ramanujan Summation, Exponential Regulation Method et cetera.

  1. The first method uses the following two series, the first one is called Grandi's Series

    S0 = 1-1+1-1+1-1 ... S1 = 1-2+3-4+5...

    Now, the Grandi's series is mildly divergent as it can either be summed to 1 or 0. The graph of Grandi's series is a zig-zag 'oscillating' between 0 and 1 which can be thought as a straight line parallel to X-axis at y=1/2 or in simple terms an average of the two possibilities i.e. (0+1)/2 = 1/2. Cesaro Summation also sums Grandi's series to 1/2.

    Now,

    S0 = 1-1+1-1+1-1 ... = 1/2 S1 = 1-2+3-4+5 ... +S1 =  +1-2+3-4+5 ... 2S1 = 1-1+1-1+1-1 ... = S0 = 1/2

    Therefore, S1 = 1/4.

    Now,

    S-S1 = [1+2+3+4 ...] - [1-2+3-4 ...]

    As we can see all the odd terms will cancel out and even terms will add up resulting in:

    S-S1 = 4+8+12+16 ... = 4 (1+2+3+4 ...) = 4S

    Now, -3S = S1 = 1/4.

    Therefore, S= -1/12 or 1+2+3+4 ... = -1/12.

  2. Euler's Method

    This method uses Taylor series expansion of 1/(1+x)2 to prove that S1 or 1-2+3-4 ... is equal to 1/4.

    1/(1+x)2 = 1-2x+3x2-4x3 ...

    Putting x=1 in the above equation reduces it to the following:

    1/22 = 1-2+3-4 ... = S1

    Now that we have proved S1=1/4 we can easily continue as above to prove S=-1/12.

    It might seem bizarre to associate -1/12 to 1+2+3+4 ... but it has serious physical implications and is a well established fact with other rigorous proofs.

    Here is an excerpt from the letter of S. Ramanujan to G.H. Hardy on the topic:

    ❝Dear Sir, I am very much gratified on perusing your letter of the 8th February 1913. I was expecting a reply from you similar to the one which a Mathematics Professor at London wrote asking me to study carefully Bromwich's Infinite Series and not fall into the pitfalls of divergent series. … I told him that the sum of an infinite number of terms of the series: 1 + 2 + 3 + 4 + · · · = −1/12 under my theory. If I tell you this you will at once point out to me the lunatic asylum as my goal. I dilate on this simply to convince you that you will not be able to follow my methods of proof if I indicate the lines on which I proceed in a single letter.❞

Further Reading