alpaca0984.log

A Minimal Environment for Octave with VSCode

alpaca0984

If you're interested in Data Science or Machine Learning, probably you heard about Andrew Ng Machine learning at Coursera. Yes, I'm one of them. One thing which I think is interesting is that it uses Octave, which isn't popular compared to Python or R. The course provides free MATLAB online accounts so you can edit and run Octave files there, but some of you might want to just try out running Octave locally without any rich IDE or online tools. Here I will show how we do that.


Requirements

  • Visual Studio Code with Matlab support extension — Octave has the same file format as Matlab. I found some Octave extensions for VSCode but seems they aren't updated anymore. I will recommend installing the Matlab one
  • Docker Desktop — We install the Octave image later

Set up and run

Let's download files first. I assume you have a Coursera account and are enrolled in Andrew's machine learning course. We go to week 2's programming assignment page and download the zip file following the instruction. When you extract and open it with VSCode, it looks like this:

file structure

The "ex1/ex1.m" is the main file. To run it, install mtmiller/octave docker image (that's a semi-official one).

$ docker pull mtmiller/octave

Now we're ready for executing octave files. Go to the assignment folder and run the docker container as mounting those files:

$ cd machine-learning-ex1
$ docker container run --rm -v "$PWD:/home" -w "/home/ex1" -it mtmiller/octave

Since I didn't find explicit the workdir in mtmiller/octave, I just mount the entire folder to the home directory. After you run the "docker container run …", you're in an octave cli process. The current directory is "/home/ex1" and you can see that assignment files are mounted to the container.

octave-files

The entry point of the program is "ex1.m" so we run:

octave:1> run ex1

then it starts showing the output. The visualization doesn't look much though.

octave-exec-m1

The last topic is debugging. How do we set breakpoints and inspect variables? This official page shows the debug features in Octave. It seems to have nice features and here I will show how to put a breakpoint at a certain line in a certain file. Let's try it out with an example use case: I want to put a breakpoint in "gradientDescent.m" at line 10 so that I can inspect the variable "m" and "J_history".

gradient-descent-m

The syntax is "dbstop in at ". We call it before we run the program.

octave:1> dbstop in gradientDescent at 10
ans =  10
octave:2> run ex1

debug

They are how we leave debug mode:

# Leave command-line debugging mode
debug> dbcont

# Quit debugging mode immediately
debug> dbquit

We finished setting up a minimal Octave development environment. Of course, you will get more rich experience if you use other graphical user interface tools, but I think this is enough for a quick start :)