<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>emre şahin's digital garden 🍃 - Machine Learning</title>
    <link>https://emresahin.net/categories/machine-learning/</link>
    <description>Posts in the Machine Learning category</description>
    <language>en</language>
    <managingEditor>contact@emresahin.net (Emre Şahin)</managingEditor>
    <lastBuildDate>Tue, 15 Sep 2026 19:46:32 +0000</lastBuildDate>
    <atom:link href="https://emresahin.net/categories/machine-learning/rss.xml" rel="self" type="application/rss+xml"/>
    <item>
      <title>Graphs for Recommendation Systems</title>
      <published>2024-02-23T19:36:41+00:00</published>
      <updated>2024-02-23T19:36:41+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Fri, 23 Feb 2024 19:36:41 +0000</pubDate>
      <link>https://emresahin.net/graphs-for-recommendation-systems/</link>
      <guid isPermaLink="true">https://emresahin.net/graphs-for-recommendation-systems/</guid>
      <description>This is an older note: I think using graphs for recommendation systems is not a natural choice where temporal patterns and sequences play a role. It is not natural to represent time with nodes that can have multiple edges. Instead, RNNs or Markov chains might be more suitable. Recommendation is a...</description>
      <category>machine-learning</category>
      <category>data-science</category>
      <category>graphs</category>
      <category>recommendation-systems</category>
      <category>sequential-modeling</category>
      <content:encoded><![CDATA[<p>This is an older note:</p>
<blockquote>
<p>I think using graphs for recommendation systems is not a natural choice where temporal patterns and sequences play a role. It is not natural to represent time with nodes that can have multiple edges. Instead, RNNs or Markov chains might be more suitable. Recommendation is almost always sequential. Otherwise, it is about finding embeddings between elements. If so, graphs might not fit these problems. If graphs are not suitable, why do we use them?</p>
</blockquote>
<p>The answer is simple: People don’t buy things in a strict sequence; they have options, multiple choices, and various factors that play a role in these choices. So, it is actually natural to model these interactions with graphs.</p>]]></content:encoded>
    </item>
    <item>
      <title>Converting MNIST and Fashion-MNIST IDX format to NumPy</title>
      <published>2021-07-01T20:46:12+00:00</published>
      <updated>2021-07-01T20:46:12+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Thu, 01 Jul 2021 20:46:12 +0000</pubDate>
      <link>https://emresahin.net/convert-mnist-data-to-numpy/</link>
      <guid isPermaLink="true">https://emresahin.net/convert-mnist-data-to-numpy/</guid>
      <description>MNIST and the newer Fashion-MNIST datasets are among the most well-known datasets for testing Machine Learning models. Although the original MNIST dataset is considered solved, it will likely remain a staple for a long time. These datasets are provided in a binary format. There are four files—two...</description>
      <category>Python</category>
      <category>Machine Learning</category>
      <category>MNIST</category>
      <category>Fashion-MNIST</category>
      <category>IDX</category>
      <category>IDX3</category>
      <category>NumPy</category>
      <content:encoded><![CDATA[<p>MNIST and the newer Fashion-MNIST datasets are among the most well-known datasets for testing Machine Learning models. Although the original MNIST dataset is considered solved, it will likely remain a staple for a long time.</p>
<p>These datasets are provided in a binary format. There are four files—two for the training set and two for the test set—in gzipped <code>IDX</code> and <code>IDX3</code> formats. Although it is a simple format, it is non-standard and requires custom code to parse.</p>
<p>For a professional project, I wrote the following two functions to load these files into NumPy arrays.</p>
<pre><code class="language-python">
import struct
import gzip
import numpy as np

def mnist_images_idx_to_array(images_filename):
    images_f = gzip.open(images_filename, mode="rb")
    images_f.seek(0)
    magic = struct.unpack('&gt;I', images_f.read(4))[0]
    if magic != 0x00000803:
        raise Exception(f"Format error: Need an IDX3 file: {images_filename}")
    n_images = struct.unpack('&gt;I', images_f.read(4))[0]
    n_row = struct.unpack('&gt;I', images_f.read(4))[0]
    n_col = struct.unpack('&gt;I', images_f.read(4))[0]

    n_bytes = n_images * n_row * n_col  # each pixel is 1 byte

    images_data = struct.unpack(
        '&gt;' + str(n_bytes) + 'B', images_f.read(n_bytes))

    images_array = np.asarray(images_data, dtype='uint8')
    images_array.shape = (n_images, n_row, n_col)

    return images_array


def mnist_labels_idx_to_array(labels_filename):
    labels_f = gzip.open(labels_filename, mode="rb")
    labels_f.seek(0)
    magic = struct.unpack('&gt;I', labels_f.read(4))[0]
    if magic != 0x00000801:
        raise Exception(f"Format error: Need an IDX file: {labels_filename}")
    n_labels = struct.unpack('&gt;I', labels_f.read(4))[0]
    labels_data = struct.unpack(
        '&gt;' + str(n_labels) + 'B', labels_f.read(n_labels))
    labels_array = np.asarray(labels_data, dtype='uint8')
    return labels_array


</code></pre>
<p>You can use these functions by passing the appropriate filenames, as shown below:</p>
<pre><code class="language-python">

training_images = mnist_images_idx_to_array(
    os.path.join(input_dir, "train-images-idx3-ubyte.gz"))
training_labels = mnist_labels_idx_to_array(
    os.path.join(input_dir, "train-labels-idx1-ubyte.gz"))
testing_images = mnist_images_idx_to_array(
    os.path.join(input_dir, "t10k-images-idx3-ubyte.gz"))
testing_labels = mnist_labels_idx_to_array(
    os.path.join(input_dir, "t10k-labels-idx1-ubyte.gz"))

</code></pre>]]></content:encoded>
    </item>
    <item>
      <title>Types of regularization in ML</title>
      <published>2020-12-26T01:07:49+00:00</published>
      <updated>2020-12-26T01:07:49+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Sat, 26 Dec 2020 01:07:49 +0000</pubDate>
      <link>https://emresahin.net/regularization-types/</link>
      <guid isPermaLink="true">https://emresahin.net/regularization-types/</guid>
      <description>What is regularization? Regularization is a technique used to reduce the complexity of a model, thereby preventing overfitting. There are three common types of regularization used in Deep Neural Networks (DNN): L2 Regularization: We define the complexity of a model by the sum of the squares of it...</description>
      <category>AI</category>
      <category>Machine Learning</category>
      <category>deep learning</category>
      <category>regularization</category>
      <category>ml</category>
      <category>neural networks</category>
      <category>l1 regularization</category>
      <category>l2 regularization</category>
      <category>dropout</category>
      <content:encoded><![CDATA[<p>What is regularization?</p>
<p>Regularization is a technique used to reduce the complexity of a model, thereby preventing overfitting. There are three common types of regularization used in Deep Neural Networks (DNN):</p>
<p><strong>L2 Regularization:</strong> We define the complexity of a model by the sum of the squares of its weights: $W = w_0^2 + w_1^2 + … + w_n^2$. We add this term to the loss function to obtain:</p>
<p>$L(\text{data}, \text{model}) = \text{loss}(\text{data}, \text{model}) + \lambda \sum w_i^2$</p>
<p>We then aim to minimize this total loss. As the derivative of $W$ with respect to each weight $w_i$ is $2w_i$, backpropagation reduces the weights by penalizing larger values, effectively “decaying” them.</p>
<p><strong>L1 Regularization:</strong> This is similar to L2 regularization, but $W$ is defined as the sum of the absolute values of the weights:</p>
<p>$W = \sum |w_i|$</p>
<p>The derivative of $W$ with respect to $w_i$ is a constant ($\pm 1$) this time, so weights can be reduced exactly to zero, unlike in L2 regularization. This often leads to sparse models.</p>
<p><strong>Dropout:</strong> Unlike the previous two methods, dropout is implemented as a layer within the neural network rather than a modification to the loss function.</p>
<p>A dropout layer randomly sets a subset of activations to zero during training. For example, a dropout layer with a rate of 0.3 will randomly deactivate 30% of the neurons in that layer for each training step.</p>]]></content:encoded>
    </item>
    <item>
      <title>Numpy ValueError while using dlib's face detector</title>
      <published>2018-10-24T21:41:18+00:00</published>
      <updated>2018-10-24T21:41:18+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Wed, 24 Oct 2018 21:41:18 +0000</pubDate>
      <link>https://emresahin.net/value-error-dlib-14346-27722/</link>
      <guid isPermaLink="true">https://emresahin.net/value-error-dlib-14346-27722/</guid>
      <description>For two days, I was trying to find a bug in my code because an assertion in the code that uses numpy.max was throwing an error: ValueError: zero-size array to reduction operation maximum which has no identity , which didn’t seem reasonable. I’m building a face recognizer with dlib ’s frontal face...</description>
      <category>Machine Learning</category>
      <category>Python</category>
      <category>Debugging</category>
      <category>dlib</category>
      <category>NumPy</category>
      <category>Face Detection</category>
      <category>Python</category>
      <category>Computer Vision</category>
      <category>ValueError</category>
      <content:encoded><![CDATA[<p>For two days, I was trying to find a bug in my code because an assertion in the
code that uses <code>numpy.max</code> was throwing an error: <code>ValueError: zero-size array to reduction operation maximum which has no identity</code>, which didn’t seem
reasonable.</p>
<p>I’m building a face recognizer with <a href="https://github.com/davisking/dlib/">dlib</a>’s
frontal face detector, and today, I noticed that some of the results return
<em>negative</em> coordinates in face detection. This means the detected face is
partial, although it’s a bit of a stretch to use negative coordinates for this.</p>
<p>My code wasn’t checking for negative coordinates and was building the NumPy
array incorrectly. However, after reading <a href="https://github.com/davisking/dlib/issues/767">the
issue</a>, although I’m still not convinced
that it’s a good approach, I added a few <code>if</code> statements and the problem was solved.</p>]]></content:encoded>
    </item>
    <item>
      <title>Recurrent Neural Networks</title>
      <published>2014-10-09T21:00:00+00:00</published>
      <updated>2014-10-09T21:00:00+00:00</updated>
      <author>Emre Şahin</author>
      <pubDate>Thu, 09 Oct 2014 21:00:00 +0000</pubDate>
      <link>https://emresahin.net/rnn-notes/</link>
      <guid isPermaLink="true">https://emresahin.net/rnn-notes/</guid>
      <description>These notes have been gathered from various sources. I provide credits and links whenever possible, but even where omitted, these are certainly not original ideas. Sequence Learning in RNNs An example of a sequence is a set of words in a sentence. Sequence learning and transformation allow comput...</description>
      <category>AI</category>
      <category>Machine Learning</category>
      <category>rnn</category>
      <category>recurrent neural networks</category>
      <category>deep learning</category>
      <category>sequence learning</category>
      <category>hmm</category>
      <category>linear dynamical systems</category>
      <content:encoded><![CDATA[<p>These notes have been gathered from various sources. I provide credits and links
whenever possible, but even where omitted, these are certainly not original
ideas.</p>
<h1 id="sequence-learning-in-rnns">Sequence Learning in RNNs</h1>
<p>An example of a sequence is a set of words in a sentence. Sequence learning and
transformation allow computers to translate one sequence into another language.</p>
<p>Alternatively, if no explicit target exists, RNNs can predict the next element
in a sequence. This type of prediction often blurs the line between supervised
and unsupervised learning.</p>
<h2 id="models-with-state">Models with State</h2>
<p>Autoregressive models calculate the current value based on previous ones:</p>
<p>$$x_t = f(x_{t-1}, x_{t-2}, \ldots)$$</p>
<p>By incorporating hidden states, it becomes much easier to perform complex
tasks:</p>
<p>$$x_t = f(h_t, x_{t-1}, x_{t-2}, \ldots)$$</p>
<p>These hidden states are typically nonlinear.</p>
<h2 id="similarity-to-quantum-mechanics">Similarity to Quantum Mechanics</h2>
<p>In Feed-Forward Neural Networks (FFNNs), the hidden state is not directly
observable. Is this similar to a quantum state?</p>
<h2 id="two-earlier-models">Two Earlier Models</h2>
<p>There are two general types of models worth mentioning.</p>
<h3 id="linear-dynamical-systems">Linear Dynamical Systems</h3>
<p>Used extensively in engineering. The system state is always linear; therefore,
Kalman filtering is utilized.</p>
<h3 id="hidden-markov-models">Hidden Markov Models</h3>
<p>Stochastic models with discrete states that store $\log(N)$ bits for $N$ states.
HMMs have efficient learning and prediction algorithms.</p>
<p>An important <strong>limitation</strong> of HMMs is their <strong>memory.</strong> They can only keep
$\log(N)$ bits of information. For a full-fledged linguistic application, we
might need at least 100 bits of state, which would require $2^{100}$ states—an
infeasible number.</p>
<h3 id="differences-between-rnns-hmms-and-ldss">Differences Between RNNs, HMMs, and LDSs</h3>
<p>Unlike HMMs, RNNs feature a distributed hidden state and complex, nonlinear
hidden units. Furthermore, they are typically deterministic.</p>
<p>RNN state behaviors can include:</p>
<ul>
<li><strong>Oscillation:</strong> Potentially useful for motor control.</li>
<li><strong>Settling to point attractors:</strong> Potentially useful for retrieving memories.</li>
<li><strong>Chaos:</strong> Generally undesirable for information processing.</li>
</ul>
<p>RNNs can learn to implement many small programs that run in parallel.</p>
<p>A significant disadvantage of RNNs: <strong>RNNs are hard to train.</strong></p>]]></content:encoded>
    </item>
  </channel>
</rss>
