To us, here and now, it appears thus

Rants, Ramblings and other things that make life worth living

Working through PAIP

without comments

Paradigms of Artificial Intelligence Programming

Paradigms of Artificial Intelligence Programming

Recently, I convinced myself to buy a copy of the famous PAIP and I started out using common lisp, something I have a passing familiarity with. But with all the buzz around Clojure I have decided to work through the book using clojure.

And, quickly enough, the book leads into a common problem encountered in computational linguistics - language generation. Typically, the whole notion of generative grammars arises from the idea of grammatical production rules which give rise to syntactically correct sentences, colorless green ideas sleep furiously included. As an early result of my efforts, I have a working port of the CFG based language generator presented in chapter 2 of the book. You can get the code directly here or it is also presented with funky syntax highlighting for your viewing pleasure :)

For those, who like to have some real documentation explaining the code, here goes. We define what a production rule is in ProductionRuleProtocol - Production rules have a L.H.S (left-hand side) and one or more R.H.S (right-hand sides). The protocol method some-rhs returns a random R.H.S from the collection of right-hand sides available for a production rule. Next, we define grammar as a collection of rules, where we can add a new rule to a grammar object. We can also use the current rules to generate either a parse tree or a sentence. The generate-tree method considers sequences as non-terminal production rules and recursively calls itself on them. Any atomic types such as strings are considered non-terminals and are directly returned with the L.H.S that generated them.

The generate-sent function is similar, instead of simply mapping over the sequence of symbols on the R.H.S it mapcats over them, effectively flattening the list. We define a simple grammar for english, and then use the rand-sent and rand-tree functions to generate random sentences and trees from the grammar.

Here is the complete code listing

Written by admin

April 5th, 2011 at 9:49 pm

The Zen of Measured Detachment.

without comments

Passion - One of the overused, undervalued words thrown around with careless abandon. You see that everywhere, in job postings, in news articles - even in self-improvement guides. Here is my impassioned take on passion - It is a skill that can be learnt, faked and to a great extent convincingly sold. It’s not something that comes from within, but is as much a product of circumstances as it is an innate tendency to gravitate towards some aspects of the world than others.

A bothersome question, that I can never give an honest answer to - “What is your passion?” - If I were honest in answering the question, it would be  ”Meh”.  But that isn’t one of the acceptable answers. It sounds like the antithesis of the word passion. But given a patient ear, I could explain, that there is more than passion in perfecting a skill, more than passion in making a great cup of latte or writing a beautiful program - and that is skill, which only comes through honest practice. Not the idea of repeating the same thing again and again, but a studied attempt, refined through impassioned criticism, self, or otherwise. And this brings us to the title of the post, my ideal of measured detachment.

Measured Detachment - The ability to be detached from the task at hand enough to objectively analyze what is happening and yet, not too detached to not care about it - detachment in limited qualities, measured detachment. Measured Detachment lets you put yourself in the seat of your worst critic, look at your work in ways that are invisible if you are tied up in your work. It lets you improve upon your previous failures by bringing to light the most obvious defects in your previous attempts. It is a skill, and like all skills can be learnt.

That will be all for my rant today,

Signing Off,
Vishnu Vyas

Written by admin

January 31st, 2011 at 6:40 am

One Liner Fun.

without comments

So, it started with a friends Facebook update lamenting the fact that Gandhi Jayanti (aka, Indian dry day) falls on a Saturday reducing partying opportunities. In an effort to console my friend, I wanted to assure him that the next such unfortunate happenstance was eons away, but I couldn’t - because I didn’t know when was the next time Gandhi Jayanti fell on a Saturday.

This seemed like the perfect opportunity to write a program - so I went ahead and did - a simple one in python, which initially looked something like this

#!/usr/bin/env python
from datetime import date
years = range(2010,2090)

for y in years :
  if date(y,10,2).strftime("%A") == "Saturday" :
    print y

But, that seemed a bit too much - and decided that it should be a one liner, something you can type at the shell and immediately feel gratified that the next time you are forced to go without alcohol on a Saturday is at least a decade away. Its time to pull out the one liners, even though python is supposed to be for done for readability, there are sufficient quirks of the language that suit themselves well to typical one liners, mainly the -c command line option and the use of semicolon to write many lines of code as a single line - Then onwards the above program was a straight forward translation and ended up being something like this

python -c 'import datetime;print "\n".join(["%d"%y for y in range(2010,2090)
if datetime.date(y, 10, 2).strftime("%A") =="Saturday"])'

That seemed to be a solution - so I went ahead and posted it as a comment.

A challenge arises

Now, my friend being the programmer he is decided it wasn’t short enough - as no one liner worth its salt should be that long, so with a bit of php magic and unreadable code - he managed to get it down to something like 80 chars; and a challenge like that can not be unmet. But, here is where I hit a road block - python’s insistence on readable code. Great when writing software, but not so good when writing one liners for things as important as planning for the next dry day. A lot of banging my head against the wall all I could do in python was this

python -c 'import datetime as d;print[y for y in range(2010,2090)
if d.date(y,10,2).strftime("%A")=="Saturday"]'

Standing in at 100 chars, a good contender for the task, but not the best one. Then, I realized something - a better solution, something that uses implicit knowledge - the fact that I know Oct 2nd 2010 falls on a Saturday. Now how could I use this?

A New Hope and A New Solution

The critical trick seems to be that, for dates from Apr 1st, the day moves ahead by 1 on non-leap years and 2 on leap years. There must be some way to use this fact? The gears in my head started turning and I was already thinking of using a accumulator variable to keep track of the day of the week and when it matches another Saturday, print! It seemed so simple, except it wasn’t. Python’s insistence on keeping statements and expressions separate is the bane of writing one liners - so, what do I do?  Enter ruby - Ruby is what you would get if Perl and Jessica Alba had a baby. Something that is elegant, clean, great for writing one liners. And then the solution seemed to fall out of nowhere.

Running at 54 chars, your best companion to planning for dry days in India - the winner is … ding ding ding

ruby -e 'w=0;puts (2011..2090).select{|x|w+=(x%4>0?1:2);w%7==0}'

Now that’s a one liner every one can appreciate.

Signing Off,
Vishnu

Written by admin

October 4th, 2010 at 6:21 am

Random Stuff.

without comments

I just watched the movie Social Network and in two words, highly recommended. Its one of those rare movies that inspires me , brings back memories of marathon coding sessions when I was a younger and those long nights of intense and fruitful discussions. Wish I did it more often.

So, on that inspiration, I’ve been recently stumped with a bunch of problems that are close, but yet a bit out of my grasp - and that has led me to a realization. My knowledge of machine learning is not where it needs to be, so its about time I started with a basic book and started working through. So, in an effort to keep myself honest and to record my progress, I’ve decided to blog about it.

Bayesian Computation with R

Since seems to be a lot of noise on the interwebs about bayesian methods, I consider it a good starting as any other and my choice to get started on it will be this book. But, having tried out R for a few hours makes me both sad and happy at the same time. Its a powerful language, which seems to do the task, however, in a certain sense, its also a horrible language. So, I thought, to make this a little bit more interesting that I shall work through the book, but with python.

As there seem to be no dearth to scientific computation libraries, and prior experience with python - I don’t think python is such a bad choice to start with. So, here is the setup I am planning to use : Python version 2.6, CVXOPT, Numpy, and SciPy for all the computation needs and matplotlib for graphing. With that, and a promise to myself to keep this blog updated with progress

Signing Off,
Vishnu

Written by admin

October 3rd, 2010 at 5:29 pm

Posted in Uncategorized

Some thoughts, on my Mac.

without comments

Its a long time since I’ve written anything here, and I have no excuse for being MIA except that I have been lazy. But to anyway, back to the topic - I bought a fancy new core-i5 mac and the first thing I notice about it is its fast - really fast, like speedy gonzales fast. I am not the one to get into the religious wars between Helvetica and Arial, but I am a sucker for good typography - and OSX has the best fonts I’ve ever seen; it was love at first sight. Menlo seems to hit the spot where Monaco couldn’t.

And, the best part of it all is the terminal - the comfort of the fact that its just another UNIX behind a fancy shell is somehow soothing on such a deep level. Even though my mac seem to come with python and a bunch of other scripting languages pre-installed but doesn’t come with a c compiler by default, which is still not a problem because the install DVD’s they give you has Xcode on it - which is cool because, it comes with a complete development environment with the package.

Signing Off,
Vishnu Vyas

Written by admin

September 16th, 2010 at 1:37 pm

Decisions, Decisions … Bad Decisions.

with one comment

As I’ve spent most of my life in front of glowing CRT screen learning the intricacies of x86 assembly (Oh!.. the horror!) it has only recently come to my attention that I might lack a variety of important survival skills that are essential. For example, eating ice-cream for all 3 meals a day, over the labor day weekend might not keep you in the best of health, and more importantly, if the next day is your first day of officially starting work.

Another horryfying truth I learnt today was I make stupid decisions for reasons other than my own self interest. What is this life altering bad decision you ask? Well, I turned down a job offer that pays 3 times as much as my current job just because I consider my contributions, whatever they may be, important to my current employer and because I enjoy working with people there. And I haven’t even met anyone from my prospective employer I turned down. Sad … Isn’t it.. I probably will never learn.

Signing Off,
Vishnu Vyas.

Written by admin

September 9th, 2009 at 11:17 am

Posted in Free Writing, Rants

For the love of F#

without comments

Enter F#, the ideal middle ground that I’ve always wanted. F# is a functional programming language from Microsoft Research which sits on top of the .NET framework and as all other .NET languages are, can easily integrate with its other less functional cousins. The best feature of F# for me has been it’s a gentle yet intellectually stimulating introduction to the world of .NET programming that I’ve so cautiously eschewed so far.  The thing with learning a new language is not learning new syntax. Anyone can do that over a weekend. The hard part is learning its idiosyncrasies – the libraries that let you do real things, the idiomatic usage of language constructs and the external peculiarities that go with the language be it make files or the umpteen build systems that go with C++ or ant and maven with java or the comfortable world of visual studio which makes you believe building is just one keystroke away.

Learning F#, as obscure as it is, means that I will not be spending a lot of time learning the massive libraries that go with the .NET world again and again, but just once. I can just as easily use them in a different world. Code Reuse is a big deal, but something that’s much bigger is the knowledge reuse you get with platforms like .NET.

So, how does one get started with F#? Well, thanks to Microsoft, it has been made absurdly simple. The first thing for anyone programming within the windows environment is Visual Studio. You can get the free Visual Studio Integrated shell here and F# here.

Once that’s setup, then you can hack away.

Signing off,
Vishnu Vyas

Written by admin

August 22nd, 2009 at 7:58 am

Nothing Intelligent to say today :(

without comments

I am just tired, depressed and down and out - Maybe its december, Maybe its that I am a tad poor than I hoped to be or maybe its just because the future seems bleak and uninspiring. Or maybe its all of these things. I find the motivation in me to do anything has been drained. I am just feeling lazy, cueing up cancer with all those incessant marlboros.

Hoping for a better day tomorrow.

Signing off,
Vishnu Vyas

Written by admin

December 11th, 2008 at 1:54 pm

Posted in Free Writing, General, Rants

Tagged with ,

Claymore!

with one comment

There is anime, and then there is anime. Some of them are just so good that you actually go back and read the manga - and claymore is one of them. If beautiful women kicking ass isn’t enough, it also has a riveting story line and great character development. Its one of those that starts out slow and eventually grows on you.

The story is set in a medieval fantasy world which is inhabited by humans and sentient demonic begins called yoma which feed on human entrails. Normal humans who stand no chance against these demonic beings depend on half-human half-yoma hybrids created by a mysterious and creepy organization, referred to within the series as ‘The Organization’ who send out female warriors wielding large swords (thus the name Claymore). The story starts out with showing these female warriors , the claymores, as beautiful and deadly but rather pale and without any humanity to them. They are feared and ostracized by the people who depend on the claymores to protect themselves from the demonic beings.

As the story progresses, we see another more human side to the claymores and learn that the organizations intentions aren’t so noble after all. The storyline mainly revolves around a single low ranked claymore - Claire, her journey and her growth - both emotionally and as a stronger warrior as she picks up friends, comrades and battles a few powerful adversaries all on the way to finding out much more sinister secrets.

Even though the series focuses on Claire, the protagonist, Other characters are well thought out and are fully developed as the series progresses; they end up playing crucial roles alongside Claire and are important facets of the storyline. A complete and an entertaining anime overall that is completely worth the read!

Signing Off,
Vishnu Vyas

Written by admin

May 12th, 2008 at 1:32 am

Posted in Free Writing, Geeky Stuff

Tagged with ,

Some Java tips

without comments

For every one who comes from the world of dangling pointers and manual resource management of C++, Java deceptively looks very similar, unless you look under the hood. (I’m pretty much a newbie to Java, who is forced to learn its quirks, primarily due to Hadoop). Most of these tips are written for grad students who’s life cycle can be succinctly paraphrased as “program, collect data, evaluate, rinse, repeat”.

Tip 1. Get a good IDE (read Eclipse)

Unlike the world of C++, where the standard library is essentially small and can for the most part be held in your head, Java has a massive standard library which includes features for everything under the sun. Keeping that in your head is not for the light of heart. Thats where a good IDE like Eclipse comes in, especially the content assist features can make your life a lot easier. Also, the IDE can take a lot of pain out of managing your projects and libraries (because Java has a preferred conventions for writing and using libraries). One more amazing thing with Eclipse is that its got a nifty Java debugger which lets you inspect among other things - the threads currently running in your program.

If you think eclipse is massive bloat or don’t have access to a powerful system for doing your development, (some times I do, where I have to ssh into some machine and change, recompile, rinse, repeat if I am running experiments) then get JDEE (an Emacs plug in).

Tip 2. Get Ant for Building

Ant is a simple Java build system based on XML (yeah.. yeah, you’ve to write XML, stop complaining). In fact, you can use their tutorial build file and start from there. It works with very little modifications, unless you have a special project needs (In which case, Internet is your friend). Say good bye to make files and man up and write some XML.

Tip 3. When you have multiple cores, use Java threads.

Java has threading built in, even though there are some pains with the thread synchronization, Java makes things a bit easier and the best part is on any Linux distribution with NPTL (which is almost every distribution out there), the Java threading model is essentially 1-1, which means you can use all those cores to do the hard work. This is great if you are running experiments under different conditions, in which case, each run of the experiment is essentially independent of others. This usually gives you a performance boost of more that 20-25% (well, but then, I have access to a machine with 8cores and 8gigs of RAM, so YMMV).

Here is a really nice tutorial to get started on Java threads

Tip 4. Learn to use Scanner and Console classes.

For those who mostly work with input output formats from text files Scanner is one of your best friends. It essentially encapsulates the work of reading a line, splitting it into various parts and converting them into your favorite data types. And for all your input requirements learn to use the Console class, which provides a nice interactive console. If you the simple readLine method does not satisfy your needs, Java has a complete regex library, and again, the internet is your friend.

Tip 5. Use jconsole

And for the last tip of the day, learn to use jconsole a monitoring tool for java applications which lets you inspect the amount of memory usage and active threads. If you are using this tool locally, then for the most part there is no configuration required - type jconsole at a command prompt and you should see a window with the list of process that are open and their PIDs. connect to one that you want to monitor and you are ready to track your java process.

Things get a bit tricky with remote process and security issues - so my advice is setup a VPN or use ssh tunneling. More information on using jconsole is available here.

So, thats all for now.

Signing off,
Vishnu Vyas.

Written by admin

April 29th, 2008 at 4:00 am