Chance Coble

Functional Programming in Austin, Texas

Archive for November, 2007

Starting an F# Project

Posted by Chance Coble on November 30, 2007

A number of people have asked me recently to write something at a very basic level on coding in F#. A lot of people really just can’t wait to get their hands in the code. That’s great, let’s do it.
First download F# and install the msi package. You will get a good deal of sample code, but for the time being we are just going to keep it simple and write some code up bit by bit.
Now, F# at the moment doesn’t add a source file by default when you add a new project. Also if you start VS, add an F# project and add a source file now, you will get all sorts of tutorial code. I recommend you do that and have a look at the tutorial code at some point because it is great for a beginner. However, here we are going to set up your environment to automatically add source to new source files (as would be done in other projects) on startup. The startup file will be comprised of some basic boilerplate items most of your projects will need.

I am assuming you installed 1.9.2.9 which is what I have. For later editions just change the version number on the directory. Navigate windows explorer to

[root]:\Program Files\FSharp-1.9.2.9\bin\FsPrjProjectItems\Source_Files

Open “SourceFile.fs” with notepad and you should see a file starting with

 //F# Visual Studio Sample File

Here you see the tutorial code. Delete the whole caboodle. Now insert in its place anything you like. Mine includes a little starter code (which languages like F# make practically pointless). There is one thing I have come to enjoy in my files and that is predefined references. In F# you do get the basic references you can expect in a C# project automatically (e.g. you can just import System.Drawing you do not have to add the reference). However, when you want to add something out of the ordinary you have to go hunt down the dll file yourself and then add the text in. Rather than doing this every time, when I hunt something down I add it into my template. Consequently my template currently has references to directX dlls and remoting apis. Then I take the approach of deleting them when I don’t want them, rather than hunting them down when I do. As you will see below F# allows new references through precompilation tags starting with ‘#’. #I includes a directory to search for imported libraries, #r references a file, and #R references a file and copies it over at compile time to the local directory. Note the paths are for my system, your system may be different. My SourceFile.fs looks like:

namespace Org.Subject
#light
#I @"C:\Windows\Microsoft.NET\Framework\v2.0.50727"
#I @"C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0"
#R "Microsoft.DirectX.dll"
#R "Microsoft.Direct3D.dll"
#R "System.Runtime.Remoting.dll"</span></code>

// A description of the organization of this file.
module Main = begin

// A description of this module 

end

Open up Visual Studio (I use 2005 here) and start a new project.Under File->New->Project the standard new project dialog window pops up. Expand the tree under Other Project Types. You will see F# is available. Select it and name your project “FPlayground”. Click OK.
New FSharp Project

With that complete add a new item to your project by right-clicking on your project FPlayground and selecting Add->New Item.

New FSharp Project Item

Select “F# Source File” and name it fplayground.fs. Click “Add”. You should now see the template source code in your new file. You can completely remove the references today. Above the Main module replace the comment with:

// This module prints “Hello World”

Then under the declaration of the module replace the comment with

 //The function go prints “Hello World”

Then add a function “go” which prints “Hello World” making your final code file:

namespace Org.Subject
#light

// A description of the organization of this file.

module Main=begin
// The function go prints "Hello World"
 let go() = printf "Hello World\n"
end

Note the indentation of the method “go”. Now before we run it, let’s quickly review what we have typed. The first line declares a namespace (note this does not open any previous contributions to the namespace). The second line declares the “#light” syntax which indicates we will be using an indentation based coding style (rather than using “;;” to indicate statement termination and such as we will do in interactive mode). Next is a comment. We could also have used (* *) or ///. Next we open a module, and add a function go. The type signature of go is

go:unit->unit


That means go takes a unit (something like void) type and returns a void type. The type signature of this function was inferred, we did not have to explicitly state any type information. If you mouse over go, you will see the popup indicating the inferred type. Using this information before compile time is a very powerful tool for sanity checking on your program, and I recommend you do it frequently. We will talk a lot about type inference in the future, but for today we just want to get started.

Type Inference Example

Now we want to start the interactive session. Under the Visual Studio menu go Tools-> Add-in Manager …

Check the “F# Interactive for Visual Studio” option and click okay. The interactive session will popup, but it can sometimes popup as a very small window. If you don’t see it right away do a little looking to make sure it didn’t popup in the bottom corner of the screen. I like to embed the interactive session in the same place as my output, error and task lists as a tab. You can do this by dragging it to the appropriate place.

Interactive Location Example

Now you can type all of the code you want in the interactive session, but a very helpful technique in visual studio allows you to send code from the display to the interactive session. Starting from below the #light statement highlight the rest of the file going down (that is, skip the namespace declaration and the #light statement). Press Alt-Enter. You will see the code binding to the interactive session.

> >

module Main : begin

val go : unit -> unit

end

Now you can test your code by typing in the interactive window

> Main.go();;

Hello World

val it : unit = ()
>

You will find in the future the interactive mode, combined with intellisense give you a very powerful tool for the checking your code out pre-compile time. These tools facilitate a kind of pre-compilation opportunity to catch errors, which when combined with the concise nature of F# provide a powerful toolset for building more reliable software under very fast-paced conditions.

To wrap up; we have gone through the installation of F#, The basic setup of project source templates, starting a project, and running the code in interactive mode. In the future we will expand on this work by looking at how to effectively manipulate modules, create automated unit tests by saving interactive sessions, and doing some other basic F# coding. So be sure to save this project somewhere so you can revisit it.

Posted in Basics | Leave a Comment »

Implementing the Self-Organizing Map in F#

Posted by Chance Coble on November 7, 2007

In neural networks, one of the most common applications is the Self Organizing Map. The self organizing map is a beautiful tool when you have a lot of complex noisy data and you want to reduce it to a few characteristic points. If you are curious about the technique a little googling will go a long way , as it is one of the most heavily used and deeply researched topics in neural networks.

The algorithm is fairly simple, and can be reduced to a brief description. In this post I am going to write a self organizing map compositionally using F#. I believe this will be a fun, geeky and alien topic for some while at the same time creating yet another example of the expressiveness of programming in a functional style.

We will implement this by having a set of coordinates (representing the network geometry) and a set of points that will be mapped (our data). We will pick a point in our data at random, find the closest coordinate in our network to that point (its nearest neighbor) and move it a little closer to the selected data point. We iterate over this process many times and the points will begin to look like a reduced representation of our data.

First I implement two small helper functions: distance (the euclidean distance between two points) and a function which folds an array into its minimum value and its minimum value’s index. These functions will be used together to help us find the closest point from an array to a given x,y coordinate.

let distance (x,y) (x',y') = sqrt (float (x - x') ** 2.0 + float (y - y') ** 2.0);;   

let min_point (xs:float[]) =

     Array.fold_left                   

      (fun (index,v) i -> if(xs.[i]<=v) then (i,xs.[i]) else (index,v))

      (-1,System.Double.MaxValue)

      [|0..(xs.Length-1)|];;

The function som_step below takes an array of network coordinates, a single data point and an alpha value. This function represents one step of the Self Organizing Network. In one step, the closest network coordinate is selected and it is moved slightly closer to the given data point. The alpha value allows us to describe a learning rate. What’s more, in the function below we will reduce the alpha at every step so the points move slower and slower as time moves on (under the assumption that they are approaching good regions). This is a technique known as annealing.

 let som_step (coords:(int*int) array) ((px,py):(int*int)) (alpha:float) =

     let times_a = let mul x = int (alpha * float x) in mul in

     let (c,v) = min_point (Array.map (fun (a,b) -> distance (px,py) (a,b)) coords) in

     let (cx,cy) = coords.[c] in

     let (dx,dy) = (times_a (px - cx),times_a (py-cy)) in

     let (x,y) = (cx + dx, cy + dy) in

     coords.[c] <- (x,y);

     coords;;

Finally, the iterating function is implemented. The iterating function is responsible for executing the function a set number of times (alternative implementations might stop when an error rate is sufficiently small). The function takes an integer representing the number of iterations, the initialized array of x,y coordinates and an array of the x,y data points to which the network will adapt. Note the annealing is implemented in the last line of the lambda expression.

let som_iterate iterations (coords:(int*int) array) (points:(int*int) array) step =

    let r = new System.Random() in

    let (falpha,fcoords) = List.fold_left

                            (fun (alpha,coords') iterating ->

                                 let v = r.Next(0,points.Length) in

                                 let coords'' = step coords' (points.[v]) alpha in

                                 (alpha * 0.999,coords'')) (0.25,coords) [0 .. iterations] in

    fcoords;;


som2.jpgsom1.jpg

The images above visualize the result of the network (red dots) on data (black regions) resulting from the code above after a quick run. The network points were started in random places. It is easy to see they tend towards the groups, or clusters, of the data. This reduced representation of data has vast applications in machine vision and data mining.

What’s more the functional style of programming allowed us to create a very simple and readable representation of the SOM (the C style version is generally not amenable to a brief treatment). We were able to do this entire thing in a couple of dozen lines of code. Because the som_step is passed in we can take advantage of this compositional approach and implement other versions which update multiple nodes, or ignore alpha.

Posted in Coding | 2 Comments »