echo and printenv in x86 Assembly

This post contains implementations of echo and printenv in 32-bit x86 assembly for Linux.

echo is a Unix utility that prints its arguments to standard output.

printenv is a Unix utility that prints the environment to standard output.

The core functionality of these programs can be written in a few lines of C, where program arguments and the environment are passed as function arguments to main.

When a process is executed on Linux (or other Unix-like systems), its stack contains pointers to the program arguments and the environment, as shown below.

        |--------------------------|     Low
0(%esp) |      Argument Count      |  Addresses
        |--------------------------|
4(%esp) |     Argument Pointers    |
        |           ...            |
        |--------------------------|
        |            0             |
        |--------------------------|
        |   Environment Pointers   |
        |           ...            |
        |--------------------------|
        |            0             |
        |--------------------------|
        |     Additional Data      |
        |           ...            |     High
        |--------------------------|  Addresses

Continue reading

Tagged , , , , | Leave a comment

k-means Image Color Quantization

I implemented a web page that can apply color quantization to images using k-means clustering. Here’s the link:
https://dstein64.github.io/k-means-quantization-js/

The JavaScript source code is available on GitHub:
https://github.com/dstein64/k-means-quantization-js

Continue reading

Tagged , , , , | Leave a comment

Factorization Machines with Theano

A Factorization Machine (FM) is a predictive model that can be used for regression and classification (Rendle 2010). FMs efficiently incorporate pairwise interactions by using factorized parameters.

PyFactorizationMachines is a Theano-based Python implementation of factorization machines.

Update 4/20/2017: The library is now available on PyPI, the Python Package Index. It can be installed with pip.

$ pip install pyfms

For documentation, see documentation.md.

For example usage, see example.py.

Tagged , , , | Leave a comment

Conway’s Game of Life

Here’s a quick-and-dirty implementation of Conway’s Game of Life.

Cells can be selected/deselected by clicking and dragging your mouse. The interface and display were designed for use with a desktop/laptop computer, not a touchscreen mobile device. That is, cells can’t be selected by swiping (but tapping works).

The default selected cells spell my first name, daniel. After selecting cells, click Start to begin the game of life.

The source code is available here (use your browser’s view source).

Tagged | Leave a comment

Matrix Factorization with Theano

Matrix factorization algorithms factorize a matrix D into two matrices P and Q, such that D ≈ PQ. By limiting the dimensionality of P and Q, PQ provides a low-rank approximation of D. While singular value decomposition (SVD) can also be used for this same task, the matrix factorization algorithms considered in this post accommodate missing data in matrix D, unlike SVD.

For an overview of matrix factorization, I recommend Albert Au Yeung’s tutorial. That post describes matrix factorization, motivates the problem with a ratings prediction task, derives the gradients used by stochastic gradient descent, and implements the algorithm in Python.

For exploratory work, it would be great if the algorithm could be implemented in such a way that the gradients could be automatically derived. With such an approach, gradients would not have to be re-derived when e.g., a change is made to the loss function (either the error term and/or the regularization term). In general, automatically derived gradients for machine learning problems facilitate increased exploration of ideas by removing a time-consuming step.

Theano is a Python library that allows users to specify their problem symbolically using NumPy-based syntax. The expressions are compiled to run efficiently on actual data. Theano’s webpage provides documentation and a tutorial.

The following code includes a Theano-based implementation of matrix factorization using batch gradient descent. The parameters are similar to those in the quuxlabs matrix factorization implementation. D is a second-order masked numpy.ndarray (e.g., a ratings matrix, where the mask indicates missing ratings), and P and Q are the initial matrix factors. The elements of P and Q are the parameters of the model, which are initialized by the function’s caller. The rank of the factorization is specified by the dimensions of P and Q. For a rank-k factorization, P must be m \times k and Q must be k \times n (where D is an m \times n matrix). Additional parameters specify the number of iterations, the learning rate, and the regularization importance.

The code doesn’t contain any derived gradients. It specifies the loss function and the parameters that the loss function will be minimized with respect to. Theano figures out the rest!

Continue reading

Tagged , , , | Leave a comment

Article Highlighter

Auto Highlight is a Chrome extension that automatically highlights the important content on article pages.

Here’s a link to the extension:
https://chrome.google.com/webstore/detail/highlight/dnkdpcbijfnmekbkchfjapfneigjomhh

The source code is on GitHub:
https://github.com/dstein64/highlight

After installing the extension, a highlighter icon appears in the location bar. Clicking that icon highlights important content on the page.

1

Continue reading

Tagged , , | Leave a comment

Anchor Graph Hashing in Python

I was recently collaborating on a project that relied on hashing (where I’m referring to “hashing” the same way it’s used in locality sensitive hashing, as opposed to its more conventional usage). One of my contributions was an implementation of Anchor Graph Hashing (AGH) [1] in Python. The code was integrated into the project, but I’ve uploaded the AGH module to its own separate GitHub repository.

https://github.com/dstein64/PyAnchorGraphHasher

[1] Liu, Wei, Jun Wang, Sanjiv Kumar, and Shih-Fu Chang. 2011. “Hashing with Graphs.” In Proceedings of the 28th International Conference on Machine Learning (ICML-11), edited by Lise Getoor and Tobias Scheffer, 1–8. ICML ’11. New York, NY, USA: ACM.

Tagged , , | Leave a comment

Color Coded Bash Prompt

This post explains what I’m referring to by color coded bash prompt, why it is useful to have one, and presents an implementation.

By color coded bash prompt, I am referring to the use of colors to represent aspects of the environment running bash. An ordinary bash prompt may display a username and hostname. Color coding can be used to represent whether the user is root, whether the session is running on a local or remote machine, and other aspects of the environment (not covered in this post).

When running multiple terminals at the same time, with some connected to remote machines and/or running as root, color coding makes it easier to keep track of the sessions, and may help prevent inadvertently entering a command as root or on the wrong machine.

I have configured my bash prompt’s colors to be a function of whether I am running as a root or non-root user, and whether I am connected to a local or remote machine. The username is displayed in green for a non-root user and in red for the root user. The hostname is displayed in blue for a local bash session and in cyan for a remote session.

The following image shows the four possible scenarios, 1) on a local machine as a non-root user, 2) on a local machine as root, 3) on a remote machine as a non-root user, and 4) on a remote machine as root.

bash

Continue reading

Tagged , , , | Leave a comment

recrun Chrome Extension

recrun is a Chrome extension I developed to provide a clean interface for reading articles on the web. recrun is an acronym that stands for retain essential content, remove unwanted noise. It uses the Diffbot Article API to extract relevant content from article sites (I work at Diffbot :-)).

The extension can be downloaded at the Chrome Web Store and the source code is available at Github.

A Diffbot token is required to use the extension. A free token can be obtained by signing up at https://www.diffbot.com/plans/free.

Usage

  1. Install the extension
  2. Sign up for a Diffbot API token
  3. Navigate to an article web site
  4. Click the eyeglasses icon in the Chrome toolbar (see arrow in image below)

screenshot

Tagged , | Leave a comment

Bar Charts for Hacker News Polls

Update 10/14/2013: The tool is now available as an extension on the Chrome Web Store.

I was recently viewing a poll on Hacker News and thought it would be useful to visualize the results of the poll, so I wrote a script for generating bar charts. The image below shows an example.

Bar Chart Example

Continue reading

Tagged , , | Leave a comment