Monthly Archives: March 2017

Weekly ML drop #4 – self-driving cars

I’ve become more and more interested in machine learning during last year. This is my way of collecting and sharing interesting reads on the topic I stumble upon. Those posts are published each Friday, they are divided into few categories and the format is constantly evolving.

This week I gathered a lot of articles on the topic of AI for self-driving cars and this will be a theme for this edition.

News
In this part, I share interesting news from machine learning and artificial intelligence world. Those are mostly not very scientific articles about interesting applications,  predictions and controversies that AI causes.

Some of those are not “news” anymore, as I have a long backlog of them saved. They are all very interesting, though!

Would you buy a car that’s programmed to kill you?
In this article, the author discusses some moral issues related to how self-driving cars will behave in death-threatening situations. If you want to test how your set of morals compare to others, check out this survey.

How Drive.ai Is Mastering Autonomous Driving With Deep Learning
This interesting piece takes a ride with drive.ai. This Silicon Valley company has a different approach than most others autonomous drive providers. Instead of using AI just for vision and understanding surrounding and using a rule-based approach to making decisions, they apply deep learning to the whole process of driving.

Self-driving trucks
I recently learned, that truck driver is the most common occupation in the United States. Over 1.7 million people drive all sizes of trucks to deliver goods across the country. The article looks into how knowledge gathered by testing self-driving cars translate to trucks and how the rise of this technology will affect the truckers.

Tesla Drivers Are Paying Big Bucks to Test Flawed Self-Driving Software
Looks like the reality of Tesla’s autopilot is slightly more complicated than promotional videos show. And you need to pay extra to take part in the test program. It may be due to fact that Tesla opted out from using Lidar technology and their autopilot is based solely on cameras. Also check out this youtube channel, that documents evolution of the Autopilot on the same local road.

Intel, Mobileye, and Smiling Curves
Read how recent Intel’s acquisition of Israeli self-driving technology startup fits into technology economy theory called “Smiling Curves”.

Learning materials
Here I’m sharing material for learning ML that I found useful – online courses, blogs, books etc. This is usually rather technical stuff.

Deep Learning for Self-driving Cars @ MIT
This winter MIT offered open lectures about Deep Learning for self-driving cars. If you were in the area, you could sign-up and come for free. Those lectures were recorded and successively published. Now the whole series is complete and available online.

This is it for today, thanks for reading. If you liked the post, let me know and please check other parts of the series.

Adding new routes in Phoenix

This is another part of my series on learning Elixir.

Last week I mixed new Phoenix project and looked how different parts of framework fit together. This week I started adding my own routes and pages. I followed the official guide but altered it a bit to better fit my application.

Adding a Route
First things to change, when you’re adding new pages, is the router.ex file, that sits in the web folder in your project. All of the work today will be contained within web folder. To add a new route, simply add a new line within the existing scope. We’ll talk more about scopes in one of the future episodes.

  scope "/", Flightlog do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    get "/flights", FlightsController, :index
  end

What the new Route says is, when browser hits /flights path, index action of FlightsController controller will be executed. Or in other words, this routes maps GET request for /flights to the index action of FlightsConroller. This won’t work because this controller doesn’t exist yet. Let’s fix it!

Adding a Controller
The Controller is very similar to the one I created last week. Nothing new here. We have index function that renders index.html view.

defmodule Flightlog.FlightsController do
  use Flightlog.Web, :controller

  def index(conn, _params) do
    render conn, "index.html"
  end
end


Adding a View and a Template
As you can see below, I added some code to the Template. I’m quite sure that it should be handled in View, but for now, I hard coded it this way. Below flights_view.ex and index.html.eex:

defmodule Flightlog.FlightsView do
  use Flightlog.Web, :view
end

You can also see reference to the path /flights/1 that has not been declared. Let’s do it now.

Adding another Route, Controller and Template
I pasted below the code for all three files, marking changes in bold.

defmodule Flightlog.Router do
  use Flightlog.Web, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", Flightlog do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    get "/flights", FlightsController, :index
    get "/flights/:id", FlightsController, :show
  end

  # Other scopes may use custom stacks.
  # scope "/api", Flightlog do
  #   pipe_through :api
  # end
end
defmodule Flightlog.FlightsController do
  use Flightlog.Web, :controller

  def index(conn, _params) do
    render conn, "index.html"
  end

  def show(conn, %{"id" => id}) do
    render conn, "show.html", id: id
  end
end

There were no changes in flights_view.ex.

There are some important things happening here. In Router, I pattern match against the params passed into the show function so that the id variable is bound to the value we put in the :id position in the URL. For example, if our URL is http://localhost:4000/flights/1, the id variable would be bound to 1.

In Template, I again hard coded some logic. There are for sure better ways to do it, but it works for now. It’s worth mentioning, that id passed is a string! I debugged this for an hour while comparing to integer 1 and couldn’t figure out why it doesn’t work ;). Everyday you learn something new. And the effects:

http://localhost:4000/flights/1

Screen Shot 2017-03-21 at 23.51.09.png

http://localhost:4000/flights/2

Screen Shot 2017-03-21 at 23.51.29.png

As you can see it’s all rendered within parent template. I’m not gonna get bothered by this at this point.

If you want to read more about Templates, again official guide is fantastic. I also stumbled upon this very interesting blog post that explains that Templates are just functions.

Thanks for reading this another part of my adventures with Elixir and Phoenix. Come next week for more. And if you’re interested in machine learning, look into my weekly link drop. on that topic.

First look at Phoenix

This week I decided to push some things in the project. Instead of looking into the internals of Elixir and Erlang VM, let’s actually do something. My project for DSP competition is a web application, and most popular framework for web development in Elixir is called Phoenix. Phoenix use as it’s foundation Erlang’s HTTP server – Cowboy. I’m not gonna look into it right now, I don’t think it will be necessary at this point.

More important for now will be knowledge what is Plug. It is a specification for constructing modules, that can be composed together and configure how web applications behave. Modules and functions that are built to this specification are called Plugs. Plugs can be chained together to create pipelines and can handle nearly everything, i.e. authentication or parameter pre-processing. To learn more look into this guide.

Thanks to Plugs, Phoenix doesn’t have a need for a monolithic middleware. And you can customise every step of handling a request. Let’s look at the elements that handle a request up to rendering, step by step.

The Endpoint is responsible for handling request until the moment that routes take over. Which includes starting server, applying the configuration and also applying a set of plugs that are common for all requests.

The Router is responsible for a few things. First of all, it parses incoming requests and sends them to appropriate Controller and Action. You can also create Pipelines here, which are sequences of Plugs, that can be easily applied to routes. So on top of common plugs, you can add your own. For example, to differently handle requests that will be rendered in a browser, vs. API requests.

Controllers do what they usually do in MVC model. They provide actions, which may prepare data for views, render stuff (through Views) on screen or do redirects.

Views are presentation layer. They’re rendered based on Templates. Templates in Elixir are precompiled, which makes them very fast.


Code
Armed with those basics, let’s look at some code that’s generated when you start Phoenix project.

First what I did is mix new Phoenix project instead of previous one.

$ mix phoenix.new hello_phoenix

This caused some havoc in my repository but  I managed to clean it up. After this and few other steps, like setting up a database, I could start it up.

$ mix phoenix.server

After that I could navigate to http://localhost:4000/ to see my first Phoenix application:

phoenix after start.png

Let’s take a look, how default Router looks like:

defmodule Flightlog.Router do
  use Flightlog.Web, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", Flightlog do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
  end

  # Other scopes may use custom stacks.
  # scope "/api", Flightlog do
  #   pipe_through :api
  # end
end

You can notice declared separate pipelines for browser and API. The one for browsers has accept-headers for HTML, handles session and adds anti-forgery tokens. One for API is much simpler. Then inside of scope (which is out of the scope of this post :P) you can see this pipeline applied and route for “/” declared that will trigger PageController’s action named :index.

defmodule Flightlog.PageController do
  use Flightlog.Web, :controller

  def index(conn, _params) do
    render conn, "index.html"
  end
end


This renders a view based on template located in file index.html.eex.

Next week I’ll start working on the code and adjust the routes for my own needs.


More resources

If you interested into looking Phoenix yourself, their documentation is very good, especially the guides. I also watched this video from 2015 NDC Oslo by the creator of Phoenix, Chris McCord. It gave me good overview how things work in Phoenix.

Thanks for reading this another part of my adventures with Elixir and Phoenix. Come next week for more. And if you’re interested in machine learning, look into my weekly link drop.

Weekly Machine Learning drop #3

I’ve become more and more interested in machine learning during last year. This is my way of collecting and sharing interesting reads on the topic I stumble upon. Those posts are published each Friday, they are divided into few categories and the format is constantly evolving.

News
In this part, I share interesting news from machine learning and artificial intelligence world. Those are mostly not very scientific articles about interesting applications,  predictions and controversies that AI causes.

Some of those are not “news” anymore, as I have a long backlog of them saved. They are all very interesting, though!

Google acquires Kaggle
Kaggle is a platform for solving machine learning problems. As a company, you can publish your problem with sample dataset and pay people the or best solution of your problem. There are also practice tasks, for example analysing Titanic data. Last week Google bought Kaggle, most likely to access 800’000 community of machine learning developers.

Poker is another game, AI can beat humans in
Two independent researcher groups’ AIs managed to beat professional players in Texas Hold’em Poker, the most complicated version of poker. It’s another game at which computers are getting better.  Will those teams now play against each other?

Facebook wants to predict and prevent suicide attempts
Using machine learning and tens of thousands of reported posts, facebook build a model to predict people who plan to commit suicide. On a similar note, there is a map, that tries to predict crimes in London. Are we getting closer to Minority Report?

Is AI going to kill us all?
When you see a movie about AI, it usually tries to whipe out humanity. But there are also people thinking otherwise with solid philosophical arguments.

JPMorgan Software Does in Seconds What Took Lawyers 360,000 Hours
Big banks feeling more and more competition from fintech sector, are turning into ML to streamline their processes and help replace layers of redundant software.

Video
I pick one or two videos every week that touches an interesting subject in AI and ML field. Sometimes it’s more scientific and the other it’s about real life applications.

AI experts panel from Beneficial AI conference
Watch Elon Musk, Ray Kurzweil, Nick Bostrom and few others discuss their outlooks for incoming artificial intelligence.

Learning materials
Here I’m sharing material for learning ML that I found useful – online courses, blogs, books etc. This is usually rather technical stuff.

Google’s Depp Learning course on Udacity
This 3 months course goes through fundamentals of Deep Learning. Starting from building simple neural networks from a linear regression model, up to exploring Convolutional Neural Networks for image recognition and Long Term Short Memory algorithms for text analysis. It’s based on Tensorflow so basic knowledge of htis library will be helpful. It also assumes some basic knowledge in Machine Learning.

Pytorch tutorial on Github
Pytorch is another tensor-based python library for machine learning. It has its fans among the academic world and some upsides over Tensorflow.

This is it for today, thanks for reading. If you liked the post, let me know and please check other parts of the series.

Elixir vs F# – opinionated syntax comparison

Note: There are some awesome comments to this post that add a lot of value. Please check them below.

This is the second part of my Elixir adventures and another post for “Get Noticed” competition.

I was planning to start furiously coding on my project for this second post and start building some web API with Phoenix. But Gutek suggested, that first I should really dig into some internals that will help me understand how Phoenix works – thanks for this advice. On top of that, I didn’t really have time to dig properly, but I managed to look a bit into syntax and I have mixed feelings.

I won’t be doing an introduction to Elixir post here. You can find a lot of resources on that, for example, Gutek’s series in Polish, official documentation or this short article. Instead, I’m gonna compare it to something that I’m familiar with –  F# syntax. And it’s nowhere near comprehensive comparison. Just a few things that I found interesting and worth noting.

Comparing stuff

Right from the start, there are few differences here. F# uses just ‘=’
to compare if two things are equal. Elixir has two comparison operators ‘==’ and ‘===’. First one is standard compare operator. Second, from my current understanding, is useful mostly for comparing if numbers are of the same type. To explain, look at this example:

// F#
1 = 2             // false
1 = 1             // true

1 = 1.0           // This yields compile error
float(1) = 1.0    // true

Although we didn’t declare any types in F#, it will infer them during compilation. And as a strongly typed language, will not allow comparing values of two different types.

# Elixir
1 == 2            # false
1 == 1            # true

1 == 1.0          # true
1 === 1.0         # false

Elixir is dynamically typed. It means, that it will also infer types, but this will happen in the runtime and also will do casts for you.

For not-equal F# uses ‘<>’ and Elixir ‘!=’ and ‘!==’. Generally, Elixir is here more consistent with most programming languages, so I’ll give it a point here, but I appreciate type safety of F# also. You can also notice that those languages use a different convention for comments.

In Elixir ‘=’ is also used for matching which is quite powerful.

Immutability

Although both languages are immutable by default, there are some differences in approach.

In Elixir, value is immutable so you cannot change it, but you can assign the “label” to some other value.

# Elixir
a = 1             # value "1" is now labelled "a"
a = a+1           # label "a" is changed: now "2" is labelled "a"
a = a*5           # value "10" is now labelled "a"

But if you want to refer to the current value of, i.e. when using match operator, you can do it this way:

# Elixir
b = 1
b = 2             # rebinding variable to 2
^b = 3            # matching: 2 = 3 -> error

First thing that came to my mind when saw it, were C language pointers 🙂

F# allows mutability, but it has to be openly declared, and then you need a different operator to change the value. Mutability is mostly allowed for compatibility reasons with .NET libraries, so you shouldn’t abuse it.

// F#
let a = 1         // binding value "1"" to label "a"
a = 2             // returns false (it is just comparing)
a <- 2            // compile error

let mutable b = 1 // binding value "1"" to mutable variable "b""
b <- 2            // changing value of variable "b""
b = 2             // returns true

In this part, F# is for me clear winner. You cannot change value bind to a label. It is much less confusing and makes more readable code.

List operations

List operations are generally very similar. What I found interesting in Elixir, you can match not only head and tail, like in F# but several first elements:
EDIT: As anonguy pointed out in the comments, that’s also possible in F#. Updated the code sample.

# Elixir
[ a, b, c | tail ]
// F#
head::tail
a::b::c::tail // that also works

There are two things worth mentioning while we’re on lists. Pipe operator (|>) works pretty much the same in both languages. In Elixir it binds the first parameter of the function, and in F# last one, but that’s the main difference. It’s a matter of convention and doesn’t really matter in the end. Just worth knowing.
EDIT: as Chris and Paul Blair pointed out, this has a tremendous impact on how currying and partial application works and makes F# much easier in that regard. Check out the comments for details.

The classic approach to lists is that you usually iterate through them with for loop. It’s possible in F#, but Elixir doesn’t have “for” loop. You have to do it in a more functional way, i.e. through recursion. For me, that’s a huge plus on Elixir side, because it forces you to use proper functional approach. In F# for loops are a gateway drug to imperative programming :).

Functions and modules

The first thing that I find annoying in Elixir is that every ‘def’ and ‘defp’ must be paired with ‘end’. It’s like curly braces all over again. Or Visual Basic. It makes code dirty and is excessive. In F#, blocks of code are delimited by the level of whitespace, similar to Python.

In Elixir, functions must be wrapped in Modules. It doesn’t create a big pain, but again – something I don’t have to do in F#. On the other hand, Elixir allows you to do multilevel Modules, which may be convenient in some situations.
EDIT: Anil Mujagic mentioned in the comments, that it also works in F#.

Pattern matching

A bit about Elixir pattern matching was mentioned in the first paragraph. “=” parameter has some impressive qualities. You can also pattern match on function parameters, like shown below in the second example. And you can further simplify it with guards.

# Elixir
# case statement
def blank?(value) do
    case value do
        nil    -> true
        false  -> true
        ""     -> true
        _other -> false
    end
end

# pattern matching on function parameters
def blank?(nil),    do: true
def blank?(false),  do: true
def blank?(""),     do: true
def blank?(_other), do: false

# pattern matching on function parameters with guards
def blank?(value) when value in [nil, false, ""], do: true
def blank?(_other), do: false

In F# it looks similar to the case statement in Elixir. You can also use guards with it and much more.

// F#
let x = 
    match 1 with 
    | 1 -> "a"
    | 2 -> "b"  
    | _ -> "z" 

I couldn’t recreate the same example easily, because of strong typing of F#.  The Same variable cannot have values of different types, and nulls are non-existent in this language. You could have something similar using discriminated unions.

I’m not a fan of Elixir’s approach to this problem with declaring several functions. I prefer F# way again.

Summary

As mentioned in the beginning, I have mixed feelings. For the last couple of years, I’ve been hearing a lot how beautiful Elixir is. And I can imagine for a lot of folks coming from other languages it is. But I’ve been spoiled with F# for last 5 years and I must admit, it’s still my number one. That being said, Elixir lands on the strong second position in terms of beauty. I do appreciate some big uncompromising design decisions that José made to make Elixir much more functional. F# has some “gateway drugs” to imperative programming, as they wanted to leave that option open too and be compatible with the rest of .NET. Big points for Elixir for that. There are some features of F# like discriminated unions or units of measures, that I haven’t found a good replacement in Elixir, but I’m also at the beginning of my journey. I also like F# more for strong typing.

Additional resources

F# has an abundance of operators. Some of them are really crazy. Check this Microsoft document to see all of them.

Quick guides on Elixir and F# syntax. The second one comes from the excellent blog of Scott Wlaschin. If you want to dive into F# more, I highly recommend it.

Next week I’ll be diving into internals. Hopefully, I will find time for that. Come back next week for more Elixir, and if you’re interested in Machine Learning, check my subjective drop of interesting articles in that area.

This post was edited to fix inaccuracies that were pointed out in the comments. Thank you for kind, constructive and informative comments!

Weekly Machine Learning drop #2

I’ve become more and more interested in machine learning in last year. This is my way of collecting and sharing interesting reads on topic I stumble upon. They will be arranged in few categories. Those posts will be published each Friday and format will evolve.

News
In this part I share interesting news from machine learning and artificial intelligence world. Those are mostly not very scientific articles about interesting applications,  predictions and controversies that AI causes.

Some of those are not “news” anymore, as I have a long backlog of them saved. They are all very interesting though!

The Great A.I. awakening.
Fascinating (and long) read on how Machine Learning drastically changed backends of Google services. If you are to read only one article from this week’s drop, pick this one.

Future of Finance
AI and Bitcoin are driving next generation of hedge funds. Read about how technology changes financial market.

“Ross” is AI Lawyer
Lawyers are expected to be one of the jobs to be replaced by AI. Algorightms are much better in going through complicated laws and precedences and drawing logical conclusions from set of rules. Ross is already doing great job as paralegal.

The AI Takeover Is Coming
Author is exploring more positives sides of incoming AI revolution. Instead of fearing the machine, let’s Embrace It.

Video
I pick one or two videos every week that touches interesting subject in AI and ML field. Some time it’s more scientific and the other it’s about real life applications.

Tensorflow Dev Summit 2017
This is whole playlist for this year’s Tensorflow Summit. It took place in mid February in Mountain View, CA. Talks on ecosystem, distributing calculation, building models or art generation. Pick what interests you 🙂

Learning materials
Here I’m sharing material for learning machine learning that I found useful – online courses, blogs, books etc. This is gonna be rather technical stuff.

MIT 6.034 Artificial Intelligence lectures
23 lectures from MIT, going through many AI algorithms and methodologies. Most of them are from Fall 2010, but some have been updated with versions from later years to include new advances in areas like Neural Networks.

Rules of ML
Google’s guidelines for developing Machine Learning systems. Slightly technical (but on high level) document presenting how Google approaches designing AI products.

Weekly Machine Learning drop #1

I’ve become more and more interested in machine learning in last year. This is my way of collecting and sharing interesting reads on topic I stumble upon. They will be arranged in few categories. Those posts will be published each Friday and format will evolve.

News
In this part I share interesting news from machine learning and artificial intelligence world. Those are mostly not very scientific articles about interesting applications,  predictions and controversies that AI causes.

Some of those are not “news” anymore, as I have a long backlog of them saved. They are all very interesting though!

AI builds it’s own encryption algorithm
Google wanted to see if AI can build encryption algorithm, that will be harder to break for humans. Looks like they succeeded.

Blizzard and DeepMind work on API for Starcraft II to build bots to play the game
It’s supposed to be research environment to build AI algorithms to play in complex game. DeepMind are authors of the AlphaGo that beat Lee Sedol in GO few months ago. Also similar concept of environment for testing AI algorithms on games is available through OpenAI Gym.

Does AI has First Amendment rights?
Amazon is claiming that Alexa has right to claim First Amendment rights in murder case to fight an order to hand over recordings from an Amazon Echo.

Bringing dead back to life with machine learning
Fascinating article about creating chatbot based on dead’s person text communication. If you’re into TV series, similar concept was presented in S02E01 episode of Black Mirror.

How to win election with machine learning
A bit scary article presenting how machine learning was used to to gather information and target individuals with customized advertisements to bias their views and alter their voting choices.

Video
I pick one or two videos every week that touches interesting subject in AI and ML field. Some time it’s more scientific and the other it’s about real life applications.

TED Talk – incredible inventions of intuitive AI
Interesting video about how helpful AI algorithms already are in engineering and design.

The Next Rembrandt
By analyzing previous painting of Rembrandt, group of data scientists and museum workers created new “Rembrandt’s” painting over 300 years after his death.

Learning materials
Here I’m sharing material for learning machine learning that I found useful – online courses, blogs, books etc. This is gonna be rather technical stuff.

Stanford University Machine Learning
What else could have landed in the first episode? This is a classic course on machine learning. Many people in the field recommend it for a start. It’s led by Baidu’s chief scientist, Coursera’s co-founder and Stanford professor – Andrew Ng. It’s quite intensive 3 months course, that will give you basics of mathematical concepts in ML and walk through basic algorithms. Highly recommended.