<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nghia Ho</title>
	<atom:link href="http://nghiaho.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://nghiaho.com</link>
	<description>Where boredom, free time, and curiosity meet together</description>
	<lastBuildDate>Fri, 10 May 2013 10:20:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Octave&#8217;s amusing fact() function</title>
		<link>http://nghiaho.com/?p=1881</link>
		<comments>http://nghiaho.com/?p=1881#comments</comments>
		<pubDate>Tue, 19 Mar 2013 11:51:37 +0000</pubDate>
		<dc:creator>nghiaho12</dc:creator>
				<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://nghiaho.com/?p=1881</guid>
		<description><![CDATA[Using Octave today I was taking a guess at the function for factorial might be fact() only to find this amusing snippet: &#8211; Command:  fact &#8211; Function File: T = fact() Display an amazing and random fact about the world&#8217;s &#8230; <a href="http://nghiaho.com/?p=1881">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Using Octave today I was taking a guess at the function for factorial might be fact() only to find this amusing snippet:</p>
<p>&#8211; Command:  fact<br />
&#8211; Function File: T = fact()<br />
Display an amazing and random fact about the world&#8217;s greatest<br />
hacker.</p>
<p>Running it a few time came up with Chuck Norris equivalent jokes for Richard Stallman:</p>
<pre>octave:4&gt; fact

When Richard Stallman executes ps -e, you show up.

octave:5&gt; fact

Richad Stallman's pinky finger is really a USB memory stick. 

octave:6&gt; fact

Richard Stallman discovered extra-terrestrial life but killed them because they
used non-free software.

octave:7&gt; fact

Behind Richard Stallman's beard there is another fist, to code faster.

octave:8&gt; fact

Richard Stallman's doesn't kill a process, he just dares it to stay running.</pre>
<p>That&#8217;s pretty cute.</p>
]]></content:encoded>
			<wfw:commentRss>http://nghiaho.com/?feed=rss2&#038;p=1881</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sparse model denoising in action</title>
		<link>http://nghiaho.com/?p=1871</link>
		<comments>http://nghiaho.com/?p=1871#comments</comments>
		<pubDate>Mon, 11 Mar 2013 13:04:18 +0000</pubDate>
		<dc:creator>nghiaho12</dc:creator>
				<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://nghiaho.com/?p=1871</guid>
		<description><![CDATA[I would like to share some denoising results using sparse modelling based on the SPAMS package. This was inspired by the topic of sparse modeling from the course &#8216;Image and video processing: From Mars to Hollywood with a stop at &#8230; <a href="http://nghiaho.com/?p=1871">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I would like to share some denoising results using sparse modelling based on the <a href="http://spams-devel.gforge.inria.fr/">SPAMS</a> package. This was inspired by the topic of sparse modeling from the course <strong>&#8216;Image and video processing: From Mars to Hollywood with a stop at the hospital&#8217;</strong> at Coursera<strong>. </strong>I highly recommend this course because it presents some state of the art methods in computer vision that I would have missed otherwise.<strong><br />
</strong></p>
<p>I was interested to see how well it would work on real noisy images so I found an old photo I took using a Canon Powershot S3 shot at night on 1600 ISO. Here is a 100% crop of the photo. As you can see, the noise is pretty awful!</p>
<p><img class="alignnone" alt="" src="http://nghiaho.com/uploads/sparse_coding_denoising/crop.jpg" width="640" height="480" /></p>
<p>Since there is a black sky in the background I thought it&#8217;ll be interesting to see what the noise distribution looks like. What do you know, it&#8217;s very Gaussian like! This is great because the square error formulation is well suited to this type of noise.</p>
<p><img class="alignnone" alt="" src="http://nghiaho.com/uploads/sparse_coding_denoising/crop_histogram2.jpg" width="555" height="315" /></p>
<p>There are a few sparse model parameters one can fiddle around with, but in my experiement I&#8217;ve the kept the following fixed and only adjusted lambda since it seems to have the most pronounced effect</p>
<ul>
<li>K = 200, dictionary size (number of atoms)</li>
<li>iterations = 100 &#8211; Ithink it&#8217;s used to optimize the dictionary + sparse vector</li>
<li>patch size = 8 (8&#215;8 patch)</li>
<li>patch stride = 2 (skip every 2 pixels)</li>
</ul>
<p>Here is with lambda = 0.1</p>
<p><img class="alignnone" alt="" src="http://nghiaho.com/uploads/sparse_coding_denoising/denoised_0.1.jpg" width="640" height="480" /></p>
<p>lambda = 0.2</p>
<p><img class="alignnone" alt="" src="http://nghiaho.com/uploads/sparse_coding_denoising/denoised_0.2.jpg" width="640" height="480" /></p>
<p>lambda = 0.9</p>
<p><img class="alignnone" alt="" src="http://nghiaho.com/uploads/sparse_coding_denoising/denoised_0.9.jpg" width="640" height="480" /></p>
<p>It&#8217;s amazing how well it preserves the detail, especially the edges.</p>
<p>Python code can be downloaded here <a href="http://nghiaho.com/uploads/sparse_coding_denoising/denoise.py_">denoise.py_</a> (right click save as and remove the trailing _)</p>
]]></content:encoded>
			<wfw:commentRss>http://nghiaho.com/?feed=rss2&#038;p=1871</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RBM and sparsity penalty</title>
		<link>http://nghiaho.com/?p=1817</link>
		<comments>http://nghiaho.com/?p=1817#comments</comments>
		<pubDate>Tue, 19 Feb 2013 14:37:05 +0000</pubDate>
		<dc:creator>nghiaho12</dc:creator>
				<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://nghiaho.com/?p=1817</guid>
		<description><![CDATA[This is a follow up on my last post about trying to use RBM to extract meaningful visual features. I&#8217;ve been experimenting with a sparsity penalty that encourages the hidden units to rarely activate. I had a look at &#8216;A &#8230; <a href="http://nghiaho.com/?p=1817">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>This is a follow up on my last post about trying to use RBM to extract meaningful visual features. I&#8217;ve been experimenting with a sparsity penalty that encourages the hidden units to rarely activate. I had a look at &#8216;A Practical Guide to Training Restricted Boltzmann Machines&#8217; for some inspiration but had trouble figuring out how their derived formula fits into the training process. I also found some interesting discussions on MetaOptimize pointing to various implementations. But in the end I went for a simple approach and used the tools I learnt from the Coursera course.</p>
<p>The sparsity is the average probability of a unit being active, so they are applicable to sigmoid/logistic units. For my RBM this will be the hidden layer. If you look back in my previous post you can see the weights generate random visual patterns. The hidden units are active about 50% of the time, hence the random looking pattern.</p>
<p>What we want to do is reduce the sparsity so that the units are activated on average a small percentage of the time, which we&#8217;ll call the &#8216;target sparsity&#8217;. Using a typical square error, we can formulate the penalty as:</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=p%20%3D%20K%5Cfrac%7B1%7D%7B2%7D%20%5Cleft%28s%20-%20t%5Cright%29%5E2%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='p = K\frac{1}{2} \left(s - t\right)^2 ' title='p = K\frac{1}{2} \left(s - t\right)^2 ' class='latex' /></p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=s%20%3D%20average%5Cleft%28sigmoid%5Cleft%28wx%20%2B%20b%5Cright%29%5Cright%29%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='s = average\left(sigmoid\left(wx + b\right)\right) ' title='s = average\left(sigmoid\left(wx + b\right)\right) ' class='latex' /></p>
<ul>
<li><strong>K</strong> is a constant multiplier to tune the gradient step.</li>
<li><strong>s</strong> is the current sparsity and is a scalar value, it is the average of all the MxN matrix elements.</li>
<li><strong>t</strong> is the target sparsity, between [0,1].</li>
</ul>
<p>Let the forward pass starting from the visible layer to the hidden layer be:</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=z%20%3D%20wx%20%2B%20b&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='z = wx + b' title='z = wx + b' class='latex' /></p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=h%20%3D%20sigmoid%28z%29%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='h = sigmoid(z) ' title='h = sigmoid(z) ' class='latex' /></p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=s%20%3D%20%5Cfrac%7B1%7D%7BM%7D%20%5Cfrac%7B1%7D%7BN%7D%20%5Csum%5Climits_%7Bi%7D%20%5Csum%5Climits_%7Bj%7D%20h_%7Bij%7D%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='s = \frac{1}{M} \frac{1}{N} \sum\limits_{i} \sum\limits_{j} h_{ij} ' title='s = \frac{1}{M} \frac{1}{N} \sum\limits_{i} \sum\limits_{j} h_{ij} ' class='latex' /></p>
<ul>
<li><strong>w</strong> is the weight matrix</li>
<li><strong>x</strong> is the data matrix</li>
<li><strong>b</strong> is the bias vector</li>
<li><strong>z</strong> is the input to the hidden layer</li>
</ul>
<p style="text-align: left;">The derivative of the sparsity penalty, <strong>p</strong>, with respect to the weights, <strong>w</strong>, using the chain rule is:</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=%5Cfrac%7B%5Cpartial%20p%7D%7B%5Cpartial%20w%7D%20%3D%20%5Cfrac%7B%5Cpartial%20p%7D%7B%5Cpartial%20s%7D%20%5Cfrac%7B%5Cpartial%20s%7D%7B%5Cpartial%20h%7D%20%5Cfrac%7B%5Cpartial%20h%7D%7B%5Cpartial%20z%7D%20%5Cfrac%7B%5Cpartial%20z%7D%7B%5Cpartial%20w%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{\partial p}{\partial w} = \frac{\partial p}{\partial s} \frac{\partial s}{\partial h} \frac{\partial h}{\partial z} \frac{\partial z}{\partial w}' title='\frac{\partial p}{\partial w} = \frac{\partial p}{\partial s} \frac{\partial s}{\partial h} \frac{\partial h}{\partial z} \frac{\partial z}{\partial w}' class='latex' /></p>
<p style="text-align: left;">The derivatives are:</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=%5Cfrac%7B%5Cpartial%20p%7D%7B%5Cpartial%20s%7D%20%3D%20K%5Cleft%28s%20-%20t%5Cright%29%20%5C%3B%5C%3B%20%5Cleftarrow%20scalar%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{\partial p}{\partial s} = K\left(s - t\right) \;\; \leftarrow scalar ' title='\frac{\partial p}{\partial s} = K\left(s - t\right) \;\; \leftarrow scalar ' class='latex' /></p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=%5Cfrac%7B%5Cpartial%20s%7D%7B%5Cpartial%20h%7D%20%3D%20%5Cfrac%7B1%7D%7BNM%7D%20%5C%3B%5C%3B%20%5Cleftarrow%20constant%20%5C%3B%20scalar%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{\partial s}{\partial h} = \frac{1}{NM} \;\; \leftarrow constant \; scalar ' title='\frac{\partial s}{\partial h} = \frac{1}{NM} \;\; \leftarrow constant \; scalar ' class='latex' /></p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=%5Cfrac%7B%5Cpartial%20h%7D%7B%5Cpartial%20z%7D%20%3D%20h%5Cleft%281-h%5Cright%29%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{\partial h}{\partial z} = h\left(1-h\right) ' title='\frac{\partial h}{\partial z} = h\left(1-h\right) ' class='latex' /></p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=%5Cfrac%7B%5Cpartial%20z%7D%7B%5Cpartial%20w%7D%20%3D%20x%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{\partial z}{\partial w} = x ' title='\frac{\partial z}{\partial w} = x ' class='latex' /></p>
<p style="text-align: left;">The derivative of the sparsity penalty with respect to the bias is the same as above except the last partial derivative is replaced with:</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=%5Cfrac%7B%5Cpartial%20z%7D%7B%5Cpartial%20b%7D%20%3D%201%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{\partial z}{\partial b} = 1 ' title='\frac{\partial z}{\partial b} = 1 ' class='latex' /></p>
<p style="text-align: left;">In actual implementation I omitted the constant  <img src='http://s.wordpress.com/latex.php?latex=%5Cfrac%7B%5Cpartial%20s%7D%7B%5Cpartial%20h%7D%20%3D%20%5Cfrac%7B1%7D%7BNM%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\frac{\partial s}{\partial h} = \frac{1}{NM}' title='\frac{\partial s}{\partial h} = \frac{1}{NM}' class='latex' />  because it made the gradients very small, and I had to crank <strong>K</strong> up quite high (in the 100s). If I take it out, good working values of <strong>K</strong> are around 1.0, which is a nicer range.</p>
<h1 style="text-align: left;">Results</h1>
<p>I used an RBM with the following settings:</p>
<ul>
<li>5000 input images, normalized to <img title="\mu=0, \sigma=1" alt="\mu=0, \sigma=1" src="http://s.wordpress.com/latex.php?latex=%5Cmu%3D0%2C%20%5Csigma%3D1&amp;bg=ffffff&amp;fg=000000&amp;s=0" /></li>
<li>no. of visible units (linear) = 64 (16×16 greyscale images from the CIFAR the database)</li>
<li>no. of hidden units (sigmoid) = 100</li>
<li><strong>sparsity target</strong> = 0.01 (1%)</li>
<li><strong>sparsity multiplier K</strong> = 1.0</li>
<li>batch training size = 100</li>
<li>iterations = 1000</li>
<li>momentum = 0.9</li>
<li>learning rate = 0.05</li>
<li>weight refinement using an autoencoder with 500 iterations and a learning rate of 0.01</li>
</ul>
<p>and this is what I got</p>
<p style="text-align: left;"><a href="http://nghiaho.com/?attachment_id=1848" rel="attachment wp-att-1848"><img class="aligncenter size-full wp-image-1848" alt="RBM sparsity" src="http://nghiaho.com/wp-content/uploads/2013/02/weights_sparsity.png" width="720" height="720" /></a>Quite a large portion of the weights are nearly zerod out. The remaining ones have managed to learn a general contrast pattern. It&#8217;s interesting to see how smooth they are. I wonder if there is an implicit L2 weight decay, like we saw in the previous post, from introducing the sparsity penalty. There are also some patterns that look like they&#8217;re still forming but not quite finished.</p>
<p style="text-align: left;">The results are certainly encouraging but there might be an efficiency drawback. Seeing as a lot of the weights are zeroed out, it means we have to use more hidden units in the hope of finding more meaningful patterns. If I keep the same number of units but increase the sparsity target then it approaches the random like patterns.</p>
<h1 style="text-align: left;">Download</h1>
<p><a href="http://nghiaho.com/uploads/code/RBM_Features-0.1.0.tar.gz">RBM_Features-0.1.0.tar.gz</a></p>
<p>Have a look at the README.txt for instructions on obtaining the CIFAR dataset.</p>
]]></content:encoded>
			<wfw:commentRss>http://nghiaho.com/?feed=rss2&#038;p=1817</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RBM, L1 vs L2 weight decay penalty for images</title>
		<link>http://nghiaho.com/?p=1796</link>
		<comments>http://nghiaho.com/?p=1796#comments</comments>
		<pubDate>Wed, 13 Feb 2013 12:04:19 +0000</pubDate>
		<dc:creator>nghiaho12</dc:creator>
				<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://nghiaho.com/?p=1796</guid>
		<description><![CDATA[I&#8217;ve been fascinated for the past months or so on using RBM (restricted Boltzmann machine) to automatically learn visual features, as oppose to hand crafting them. Alex Krizhevsky&#8217;s master thesis, Learning Multiple Layers of Features from Tiny Images, is a &#8230; <a href="http://nghiaho.com/?p=1796">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been fascinated for the past months or so on using RBM (restricted Boltzmann machine) to automatically learn visual features, as oppose to hand crafting them. Alex Krizhevsky&#8217;s master thesis, <a href="http://www.cs.toronto.edu/%7Ekriz/learning-features-2009-TR.pdf">Learning Multiple Layers of Features from Tiny Images</a>, is a good source on this topic. I&#8217;ve been attempting to replicate the results on a much smaller set of data with mix results. However, as a by product of I did manage generate some interesting results.</p>
<p>One of the tunable parameters of an RBM (neural network as well) is a weight decay penalty. This regularisation penalises large weight coefficients to avoid over-fitting (used conjunction with a validation set). Two commonly used penalties are L1 and L2, expressed as follows:</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=weight%5C%3Bdecay%5C%3BL1%20%3D%20%5Cdisplaystyle%5Csum%5Climits_%7Bi%7D%5Cleft%7C%5Ctheta_i%5Cright%7C&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='weight\;decay\;L1 = \displaystyle\sum\limits_{i}\left|\theta_i\right|' title='weight\;decay\;L1 = \displaystyle\sum\limits_{i}\left|\theta_i\right|' class='latex' /></p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=weight%5C%3Bdecay%5C%3BL2%20%3D%20%5Cdisplaystyle%5Csum%5Climits_%7Bi%7D%5Ctheta_i%5E2&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='weight\;decay\;L2 = \displaystyle\sum\limits_{i}\theta_i^2' title='weight\;decay\;L2 = \displaystyle\sum\limits_{i}\theta_i^2' class='latex' /></p>
<p style="text-align: left;">where theta is the coefficents of the weight matrix.</p>
<p style="text-align: left;">L1 penalises the absolute value and L2 the squared value. L1 will generally push a lot of the weights to be exactly zero while allowing some to grow large. L2 on the other hand tends to drive all the weights to smaller values.</p>
<h1 style="text-align: left;">Experiment</h1>
<p style="text-align: left;">To see the effect of the two penalties I&#8217;ll be using a single RBM with the following configuration:</p>
<ul>
<li>5000 input images, normalized to <img src='http://s.wordpress.com/latex.php?latex=%5Cmu%3D0%2C%20%5Csigma%3D1&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mu=0, \sigma=1' title='\mu=0, \sigma=1' class='latex' /></li>
<li>no. of visible units (linear) = 64 (16&#215;16 greyscale images from the CIFAR the database)</li>
<li>no. of hidden units (sigmoid) = 100</li>
<li>batch training size = 100</li>
<li>iterations = 1000</li>
<li>momentum = 0.9</li>
<li>learning rate = 0.01</li>
<li>weight refinement using an autoencoder with 500 iterations and learning rate of 0.01</li>
</ul>
<p>The weight refinement step uses a 64-100-64 autoencoder with standard backpropagation.</p>
<h1 style="text-align: left;">Results</h1>
<p>For reference, here are the 100 hidden layer patterns without any weight decay applied:</p>
<p style="text-align: left;"><a href="http://nghiaho.com/?attachment_id=1807" rel="attachment wp-att-1807"><img class="aligncenter size-full wp-image-1807" title="RBM no weight decay" alt="" src="http://nghiaho.com/wp-content/uploads/2013/02/weights_ref.png" width="720" height="720" /></a>As you can see they&#8217;re pretty random and meaningless There&#8217;s no obvious structure. Though what is amazing is that even with such random patterns you can reconstruct the original 5000 input images quite well using a weighted linear combinations of them.</p>
<p style="text-align: left;">Now applying an L1 weight decay with a weight decay multiplier of 0.01 (which gets multiplied with the learning rate) we get something more interesting:</p>
<p style="text-align: left;"><a href="http://nghiaho.com/?attachment_id=1808" rel="attachment wp-att-1808"><img class="aligncenter size-full wp-image-1808" title="weights_L1_0.01" alt="" src="http://nghiaho.com/wp-content/uploads/2013/02/weights_L1_0.01.png" width="720" height="720" /></a>We get stronger localised &#8220;spot&#8221; like features.</p>
<p style="text-align: left;">And lastly, applying L2 weight decay with a multiplier of 0.1 we get</p>
<p style="text-align: left;"><a href="http://nghiaho.com/?attachment_id=1809" rel="attachment wp-att-1809"><img class="aligncenter size-full wp-image-1809" title="RBM L2 weight decay" alt="" src="http://nghiaho.com/wp-content/uploads/2013/02/weights_L2_0.1.png" width="720" height="720" /></a>which again looks like a bunch of random patterns except smoother. This has the effect of smoothing the reconstruction image.</p>
<p style="text-align: left;">Despite some interesting looking patterns I haven&#8217;t really observed the edge or Gabor like patterns reported in the literature. Maybe my training data is too small? Need to spend some more time &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://nghiaho.com/?feed=rss2&#038;p=1796</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HP Pavilion DM1 + AMD E-450 APU, you both suck!</title>
		<link>http://nghiaho.com/?p=1791</link>
		<comments>http://nghiaho.com/?p=1791#comments</comments>
		<pubDate>Wed, 06 Feb 2013 13:39:17 +0000</pubDate>
		<dc:creator>nghiaho12</dc:creator>
				<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://nghiaho.com/?p=1791</guid>
		<description><![CDATA[Last year I bought myself a small HP Pavilion DM1 for traveling overseas. On paper the specs look great compared to any of Asus EEE PC offering in terms of processing power and consumption. It&#8217;s got a dual core AMD &#8230; <a href="http://nghiaho.com/?p=1791">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Last year I bought myself a small HP Pavilion DM1 for traveling overseas. On paper the specs look great compared to any of Asus EEE PC offering in terms of processing power and consumption. It&#8217;s got a dual core AMD E-450, Radeon graphics card,  2GB RAM and about 4-5 hrs of battery life. In reality? I&#8217;ve had to take this laptop in for warranty repair TWICE in a few short months. First time was a busted graphics chip that only worked if I plugged in an external monitor. The second time was a faulty hard disk singing the click of the death. Both were repaired within a day, so I can&#8217;t get too mad. But when I finally got around to installing all the tools I need to do some number crunching under Linux, this happens &#8230;</p>
<p><a href="http://nghiaho.com/?attachment_id=1792" rel="attachment wp-att-1792"><img class="aligncenter size-full wp-image-1792" title="AMD E450 Octave crash" src="http://nghiaho.com/wp-content/uploads/2013/02/AMD_E450_octave.png" alt="" width="660" height="434" /></a>Yes that&#8217;s right, Octave crashes on a matrix multiply!!! WTF?!?</p>
<p>I ran another program I wrote in GDB and it reveals this interesting snippet</p>
<p><a href="http://nghiaho.com/?attachment_id=1793" rel="attachment wp-att-1793"><img class="aligncenter size-full wp-image-1793" title="AMD_E450_gdb" src="http://nghiaho.com/wp-content/uploads/2013/02/AMD_E450_gdb.png" alt="" width="657" height="383" /></a></p>
<p>My guess is it&#8217;s trying to call some AMD assembly instruction that doesn&#8217;t exist on this CPU. Octave uses /usr/lib/libblas as well, which explains the crash earlier. Oh well, bug report time &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://nghiaho.com/?feed=rss2&#038;p=1791</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RBM Autoencoders</title>
		<link>http://nghiaho.com/?p=1765</link>
		<comments>http://nghiaho.com/?p=1765#comments</comments>
		<pubDate>Sat, 08 Dec 2012 13:37:49 +0000</pubDate>
		<dc:creator>nghiaho12</dc:creator>
				<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://nghiaho.com/?p=1765</guid>
		<description><![CDATA[I&#8217;ve just finished the wonderful &#8220;Neural Networks for Machine Learning&#8221; course on Coursera and wanted to apply what I learnt (or what I think I learnt). One of the topic that I found fascinating was an autoencoder neural network. This &#8230; <a href="http://nghiaho.com/?p=1765">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve just finished the wonderful &#8220;Neural Networks for Machine Learning&#8221; course on Coursera and wanted to apply what I learnt (or what I think I learnt). One of the topic that I found fascinating was an autoencoder neural network. This is a type of neural network that can &#8220;compress&#8221; data similar to PCA. An example of the network topology is shown below.</p>
<p style="text-align: center;"><a href="http://nghiaho.com/?attachment_id=1767" rel="attachment wp-att-1767"><img class="aligncenter size-full wp-image-1767" title="Autoencoder network" src="http://nghiaho.com/wp-content/uploads/2012/12/autoencoder_network1.png" alt="" width="727" height="268" /></a></p>
<p style="text-align: left;">The network is fully connected and symmetrical, but I&#8217;m too lazy to draw all the connections. Given some input data the network will try to reconstruct it as best as it can on the output. The &#8216;compression&#8217; is controlled mainly by the middle bottleneck layer. The above example has 8 input neurons, which gets squashed to 4 then to 2. I will use the notation 8-4-2-4-8 to describe the above autoencoder networks.</p>
<p style="text-align: left;">An autoencoder has the potential to do a better job of PCA for dimensionality reduction, especially for visualisation since it is non-linear.</p>
<h1 style="text-align: left;">My autoencoder</h1>
<p style="text-align: left;">I&#8217;ve implemented a simple autoencoder that uses RBM (restricted Boltzmann machine) to initialise the network to sensible weights and refine it further using standard backpropagation. I also added common improvements like momentum and early termination to speed up training.</p>
<p style="text-align: left;">I used the <a href="http://www.cs.toronto.edu/~kriz/cifar.html">CIFAR-10 dataset</a> to train 100 small images of dogs. The images are 32&#215;32 (1024 vector) colour images, which I converted to grescale. The network I train on is:</p>
<p style="text-align: center;">1024-256-64-8-64-256-1024</p>
<p style="text-align: left;">The input, output and bottleneck are linear with the rest being sigmoid units. I expected this autoencoder to reconstruct the image better than PCA, because it has much more parameters. I&#8217;ll compare the results with PCA using the first 8 principal components.</p>
<h1 style="text-align: left;">Results</h1>
<p>Here are 10 random results from the 100 images I trained on.</p>
<p><img src="http://nghiaho.com/uploads/RBM_dog/results-00.png" alt="" /><br />
<img src="http://nghiaho.com/uploads/RBM_dog/results-01.png" alt="" /><br />
<img src="http://nghiaho.com/uploads/RBM_dog/results-02.png" alt="" /><br />
<img src="http://nghiaho.com/uploads/RBM_dog/results-03.png" alt="" /><br />
<img src="http://nghiaho.com/uploads/RBM_dog/results-04.png" alt="" /><br />
<img src="http://nghiaho.com/uploads/RBM_dog/results-05.png" alt="" /><br />
<img src="http://nghiaho.com/uploads/RBM_dog/results-06.png" alt="" /><br />
<img src="http://nghiaho.com/uploads/RBM_dog/results-07.png" alt="" /><br />
<img src="http://nghiaho.com/uploads/RBM_dog/results-08.png" alt="" /><br />
<img src="http://nghiaho.com/uploads/RBM_dog/results-09.png" alt="" /></p>
<p>The autoencoder does indeed give a better reconstruction than PCA. This gives me confidence that my implementation is somewhat correct.</p>
<p>The RMSE (root mean squared error) for the autoencoder is 9.298, where as for PCA it is 30.716, pixel values range from [0,255].</p>
<p>All the parameters used can be found in the code.</p>
<h1>Download</h1>
<p>You can download the code here</p>
<p><span style="color: #ff0000;">Last update: 10/12/2012</span></p>
<p><a href="http://nghiaho.com/uploads/code/RBM_Autoencoder-0.1.1.tar.gz">RBM_Autoencoder-0.1.1.tar.gz</a></p>
<p>You&#8217;ll need the following libraries installed</p>
<ul>
<li>Armadillo (<a href="http://arma.sourceforge.net">http://arma.sourceforge.net</a>)</li>
<li>OpenBLAS (or any other BLAS alternative, but you&#8217;ll need to edit the Makefile/Codeblocks project)</li>
<li>OpenCV (for display)</li>
</ul>
<p>On Ubuntu 12.10 I use the OpenBLAS package in the repo. Use the latest Armadillo from the website if the Ubuntu one doesn&#8217;t work, I use some newer function introduced recently. I recommend using OpenBLAS over Atlas with Armadillo on Ubuntu 12.10, because multi-core support works straight out of the box. This provides a big speed up.</p>
<p>You&#8217;ll also need the dataset <a href="http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz">http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz</a></p>
<p>Edit <strong>main.cpp</strong> and change <strong>DATASET_FILE</strong> to point to your CIFAR dataset path. Compile via <strong>make</strong> or using CodeBlocks.</p>
<p>All parameter variables can be found in main.cpp near the top of the file.</p>
]]></content:encoded>
			<wfw:commentRss>http://nghiaho.com/?feed=rss2&#038;p=1765</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comments back on</title>
		<link>http://nghiaho.com/?p=1753</link>
		<comments>http://nghiaho.com/?p=1753#comments</comments>
		<pubDate>Tue, 27 Nov 2012 13:26:14 +0000</pubDate>
		<dc:creator>nghiaho12</dc:creator>
				<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://nghiaho.com/?p=1753</guid>
		<description><![CDATA[Looks like I stuffed up big time and forgot to check that comments required a login to post! Settings must have changed from the WordPress update a few months back. Anyway, you should be able to post comments now.]]></description>
				<content:encoded><![CDATA[<p>Looks like I stuffed up big time and forgot to check that comments required a login to post!</p>
<p>Settings must have changed from the WordPress update a few months back. Anyway, you should be able to post comments now.</p>
]]></content:encoded>
			<wfw:commentRss>http://nghiaho.com/?feed=rss2&#038;p=1753</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenCV vs. Armadillo vs. Eigen vs. more! Round 3: pseudoinverse test</title>
		<link>http://nghiaho.com/?p=1726</link>
		<comments>http://nghiaho.com/?p=1726#comments</comments>
		<pubDate>Sun, 25 Nov 2012 08:21:38 +0000</pubDate>
		<dc:creator>nghiaho12</dc:creator>
				<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://nghiaho.com/?p=1726</guid>
		<description><![CDATA[Okay, the title of this post is getting longer and sillier, but this is the 3rd continuation of my last two post on comparing different libraries for everyday matrix operations. The last two posts compared basic operations such as multiplication, &#8230; <a href="http://nghiaho.com/?p=1726">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Okay, the title of this post is getting longer and sillier, but this is the 3rd continuation of my last two post on comparing different libraries for everyday matrix operations. The last two posts compared basic operations such as multiplication, transposition, inversion etc. etc. in isolation, which is probably not a good reflection of real life usage. So I decided to come up with a new test that would combine different matrix operations together. I chose the pseudoinverse because it is something I use every now and then and it combines multiplication, transposition and inversion, which seems like a good test.</p>
<p>For benchmarking I&#8217;m going to be solving the following over determined linear system:</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=AX%20%3D%20B%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='AX = B ' title='AX = B ' class='latex' /></p>
<p>and solve for X using</p>
<p style="text-align: center;"><img src='http://s.wordpress.com/latex.php?latex=X%20%3D%20%5Cleft%28A%5ETA%5Cright%29%5E%7B-1%7DA%5E%7BT%7DB%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='X = \left(A^TA\right)^{-1}A^{T}B ' title='X = \left(A^TA\right)^{-1}A^{T}B ' class='latex' /></p>
<p>A is a NxM matrix, where N is much larger than M. I&#8217;ll be using N=1,000,000 data points and M (dimensions of the data) varying from 2 to 16.</p>
<p>B is a Nx1 matrix.</p>
<p>The matrix values will be randomly generated from 0 to 1 with uniform noise of [-1,1] added to B. They values are kept to a small range to avoid any significant numerical problems that can come about doing the pseudoinverse this way, not that I care too much for this benchmark. Each test is performed for 10 iterations, but not averaged out since I&#8217;m not interested in absolute time but relative to the other libraries.</p>
<p>Just to make the benchmark more interesting I&#8217;ve added GSL and OpenBLAS to the test, since they were just an apt-get away on Ubuntu.</p>
<h1>Results</h1>
<p>The following libraries were used</p>
<ul>
<li>OpenCV 2.4.3 (compiled from source)</li>
<li>Eigen 3.1.2 (C++ headers from website)</li>
<li>Armadillo 3.4.4 (compiled from source)</li>
<li>GSL 1.15 (Ubuntu 12.10 package)</li>
<li>OpenBLAS 1.13 (Ubuntu 12.10 package)</li>
<li>Atlas 3.8.4 (Ubuntu 12.10 package)</li>
</ul>
<p>My laptop has an Intel i7 1.60GHz with 6GB of RAM.</p>
<p>All values reported are in milliseconds. Each psuedoinverse test is performed 10 times but <strong>NOT</strong> averaged out. Lower is better. Just as a reminder each test is dealing with 1,000,000 data points of varying dimensions.</p>
<table border="0" cellspacing="0">
<colgroup width="166"></colgroup>
<colgroup span="8" width="59"></colgroup>
<tbody>
<tr>
<td align="LEFT" height="16"></td>
<td align="RIGHT">2</td>
<td align="RIGHT">3</td>
<td align="RIGHT">4</td>
<td align="RIGHT">5</td>
<td align="RIGHT">6</td>
<td align="RIGHT">7</td>
<td align="RIGHT">8</td>
<td align="RIGHT">9</td>
</tr>
<tr>
<td align="LEFT" height="16">OpenCV</td>
<td align="RIGHT">169.619</td>
<td align="RIGHT">321.204</td>
<td align="RIGHT">376.3</td>
<td align="RIGHT">610.043</td>
<td align="RIGHT">873.379</td>
<td align="RIGHT">1185.82</td>
<td align="RIGHT">1194.12</td>
<td align="RIGHT">1569.16</td>
</tr>
<tr>
<td align="LEFT" height="16">Eigen</td>
<td align="RIGHT">152.159</td>
<td align="RIGHT">258.069</td>
<td align="RIGHT">253.844</td>
<td align="RIGHT">371.627</td>
<td align="RIGHT">423.474</td>
<td align="RIGHT">577.065</td>
<td align="RIGHT">555.305</td>
<td align="RIGHT">744.016</td>
</tr>
<tr>
<td align="LEFT" height="16">Armadillo +  Atlas</td>
<td align="RIGHT">162.332</td>
<td align="RIGHT">184.834</td>
<td align="RIGHT">273.822</td>
<td align="RIGHT">396.629</td>
<td align="RIGHT">528.831</td>
<td align="RIGHT">706.238</td>
<td align="RIGHT">848.51</td>
<td align="RIGHT">1088.47</td>
</tr>
<tr>
<td align="LEFT" height="16">Armadillo + OpenBLAS</td>
<td align="RIGHT"><span style="color: #ff0000;">79.803</span></td>
<td align="RIGHT"><span style="color: #ff0000;">118.718</span></td>
<td align="RIGHT"><span style="color: #ff0000;">147.714</span></td>
<td align="RIGHT"><span style="color: #ff0000;">298.839</span></td>
<td align="RIGHT"><span style="color: #ff0000;">372.235</span></td>
<td align="RIGHT"><span style="color: #ff0000;">484.864</span></td>
<td align="RIGHT"><span style="color: #ff0000;">411.337</span></td>
<td align="RIGHT"><span style="color: #ff0000;">507.84</span></td>
</tr>
<tr>
<td align="LEFT" height="16">GSL</td>
<td align="RIGHT">507.052</td>
<td align="RIGHT">787.429</td>
<td align="RIGHT">1102.07</td>
<td align="RIGHT">1476.67</td>
<td align="RIGHT">1866.33</td>
<td align="RIGHT">2321.66</td>
<td align="RIGHT">2831.36</td>
<td align="RIGHT">3237.67</td>
</tr>
</tbody>
</table>
<table border="0" cellspacing="0">
<colgroup width="166"></colgroup>
<colgroup span="7" width="59"></colgroup>
<tbody>
<tr>
<td align="LEFT" height="16"></td>
<td align="RIGHT">10</td>
<td align="RIGHT">11</td>
<td align="RIGHT">12</td>
<td align="RIGHT">13</td>
<td align="RIGHT">14</td>
<td align="RIGHT">15</td>
<td align="RIGHT">16</td>
</tr>
<tr>
<td align="LEFT" height="16">OpenCV</td>
<td align="RIGHT">1965.95</td>
<td align="RIGHT">2539.57</td>
<td align="RIGHT">2495.63</td>
<td align="RIGHT">2909.9</td>
<td align="RIGHT">3518.22</td>
<td align="RIGHT">4023.67</td>
<td align="RIGHT">4064.92</td>
</tr>
<tr>
<td align="LEFT" height="16">Eigen</td>
<td align="RIGHT">814.683</td>
<td align="RIGHT">1035.96</td>
<td align="RIGHT">993.226</td>
<td align="RIGHT">1254.8</td>
<td align="RIGHT">1362.02</td>
<td align="RIGHT">1632.31</td>
<td align="RIGHT">1615.69</td>
</tr>
<tr>
<td align="LEFT" height="16">Armadillo + Atlas</td>
<td align="RIGHT">1297.01</td>
<td align="RIGHT">1519.04</td>
<td align="RIGHT">1792.74</td>
<td align="RIGHT">2064.77</td>
<td align="RIGHT">1438.16</td>
<td align="RIGHT">1720.64</td>
<td align="RIGHT">1906.79</td>
</tr>
<tr>
<td align="LEFT" height="16">Armadillo + OpenBLAS</td>
<td align="RIGHT"><span style="color: #ff0000;">534.947</span></td>
<td align="RIGHT"><span style="color: #ff0000;">581.294</span></td>
<td align="RIGHT"><span style="color: #ff0000;">639.175</span></td>
<td align="RIGHT"><span style="color: #ff0000;">772.382</span></td>
<td align="RIGHT"><span style="color: #ff0000;">824.971</span></td>
<td align="RIGHT"><span style="color: #ff0000;">825.79</span></td>
<td align="RIGHT"><span style="color: #ff0000;">893.771</span></td>
</tr>
<tr>
<td align="LEFT" height="16">GSL</td>
<td align="RIGHT">3778.44</td>
<td align="RIGHT">4427.47</td>
<td align="RIGHT">4917.54</td>
<td align="RIGHT">6037.29</td>
<td align="RIGHT">6303.08</td>
<td align="RIGHT">7187.5</td>
<td align="RIGHT">7280.27</td>
</tr>
</tbody>
</table>
<p>Ranking from best to worse</p>
<ol>
<li><strong>Armadillo + OpenBLAS</strong></li>
<li>Eigen</li>
<li>Armadillo + Atlas (no multi-core support out of the box???)</li>
<li>OpenCV</li>
<li>GSL</li>
</ol>
<p>All I can say is, holly smokes Batman! Armadillo + OpenBLAS wins out for every single dimension!  Last is GSL, okay no surprise there for me. It never boasted being the fastest car on the track.</p>
<p>The cool thing about Armadillo is switching the BLAS engine only requires a different library to be linked, no recompilation of Armadillo. What is surprising is the Atlas library doesn&#8217;t seem to support multi-core by default. I&#8217;m probably not doing it right. Maybe I&#8217;m missing an environmental variable setting?</p>
<p>OpenBLAS is based on GotoBLAS and is actually a &#8216;made in China&#8217; product, except this time I don&#8217;t get to make any jokes about the quality. It is fast because it takes advantage of multi-core CPU, while the others appear to only use 1 CPU core.</p>
<p>I&#8217;m rather sad OpenCV is not that fast since I use it heavily for computer vision tasks. My compiled version actually uses Eigen, but that doesn&#8217;t explain why it&#8217;s slower than Eigen! Back in the old days OpenCV used to use BLAS/LAPACK, something they might need to consider bringing back.</p>
<h1>Code</h1>
<p><a href="http://nghiaho.com/uploads/code/test_matrix_pseudoinverse.cpp">test_matrix_pseudoinverse.cpp</a> (right click save as)</p>
<p>Edit the code to #define in the libraries you want to test. Make sure you don&#8217;t turn on Armadillo + GSL, because they have conflicting enums. Instructions for compiling is at the top of the cpp file, but here it is again for reference.</p>
<p>To compile using ATLAS:</p>
<p>g++ test_matrix_pseudoinverse.cpp -o test_matrix_pseudoinverse -L/usr/lib/atlas-base -L/usr/lib/openblas-base -lopencv_core -larmadillo -lgomp -fopenmp -lcblas -llapack_atlas -lgsl -lgslcblas -march=native -O3 -DARMA_NO_DEBUG -DNDEBUG -DHAVE_INLINE -DGSL_RANGE_CHECK_OFF</p>
<p>To compile with OpenBLAS:</p>
<p>g++ test_matrix_pseudoinverse.cpp -o test_matrix_pseudoinverse -L/usr/lib/atlas-base -L/usr/lib/openblas-base -lopencv_core -larmadillo -lgomp -fopenmp -lopenblas -llapack_atlas -lgsl -lgslcblas -march=native -O3 -DARMA_NO_DEBUG -DNDEBUG -DHAVE_INLINE -DGSL_RANGE_CHECK_OFF</p>
]]></content:encoded>
			<wfw:commentRss>http://nghiaho.com/?feed=rss2&#038;p=1726</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>New hand SFM dataset</title>
		<link>http://nghiaho.com/?p=1719</link>
		<comments>http://nghiaho.com/?p=1719#comments</comments>
		<pubDate>Fri, 23 Nov 2012 14:59:36 +0000</pubDate>
		<dc:creator>nghiaho12</dc:creator>
				<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://nghiaho.com/?p=1719</guid>
		<description><![CDATA[A few months back I took some pics of my hand to see how well SFM would work on them. The point cloud came out pretty good. It managed to capture the depth of my finger pretty accurately, about 1 &#8230; <a href="http://nghiaho.com/?p=1719">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>A few months back I took some pics of my hand to see how well SFM would work on them. The point cloud came out pretty good. It managed to capture the depth of my finger pretty accurately, about 1 cm in diameter. You can get the dataset from the structure from motion page. Here are some screenshots of the results.</p>
<p><a href="http://nghiaho.com/?attachment_id=1720" rel="attachment wp-att-1720"><img class="aligncenter size-full wp-image-1720" title="hand1" src="http://nghiaho.com/wp-content/uploads/2012/11/hand1.jpg" alt="" width="644" height="501" /></a></p>
<p><a href="http://nghiaho.com/?attachment_id=1721" rel="attachment wp-att-1721"><img class="aligncenter size-full wp-image-1721" title="hand2" src="http://nghiaho.com/wp-content/uploads/2012/11/hand2.jpg" alt="" width="968" height="558" /></a><a href="http://nghiaho.com/?attachment_id=1722" rel="attachment wp-att-1722"><img class="aligncenter size-full wp-image-1722" title="hand3" src="http://nghiaho.com/wp-content/uploads/2012/11/hand3.jpg" alt="" width="1046" height="531" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://nghiaho.com/?feed=rss2&#038;p=1719</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CellCounter 0.2.1 released</title>
		<link>http://nghiaho.com/?p=1716</link>
		<comments>http://nghiaho.com/?p=1716#comments</comments>
		<pubDate>Fri, 16 Nov 2012 13:48:21 +0000</pubDate>
		<dc:creator>nghiaho12</dc:creator>
				<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://nghiaho.com/?p=1716</guid>
		<description><![CDATA[Some users have requested a histogram to make threshold selecting easier. This has been added.]]></description>
				<content:encoded><![CDATA[<p>Some users have requested a histogram to make threshold selecting easier. This has been added.</p>
]]></content:encoded>
			<wfw:commentRss>http://nghiaho.com/?feed=rss2&#038;p=1716</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
