<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by M Nassar on Medium]]></title>
        <description><![CDATA[Stories by M Nassar on Medium]]></description>
        <link>https://medium.com/@mnassar1?source=rss-2e017808cd98------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7PRPO2lt9xd2VuruCHGFEg.jpeg</url>
            <title>Stories by M Nassar on Medium</title>
            <link>https://medium.com/@mnassar1?source=rss-2e017808cd98------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 15:23:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mnassar1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Turning Lime into Lemna]]></title>
            <link>https://medium.com/@mnassar1/turning-lime-into-lemna-07bbb8f48664?source=rss-2e017808cd98------2</link>
            <guid isPermaLink="false">https://medium.com/p/07bbb8f48664</guid>
            <category><![CDATA[gaussian]]></category>
            <category><![CDATA[lasso]]></category>
            <category><![CDATA[xai]]></category>
            <category><![CDATA[lime]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[M Nassar]]></dc:creator>
            <pubDate>Fri, 12 Apr 2024 01:43:15 GMT</pubDate>
            <atom:updated>2024-04-12T16:02:44.171Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6RqOqSTnOd-CBFQrN0epFg.png" /></figure><p>This is about XAI (eXplainable AI).</p><p>Lime stands for Local Interpretable Model-Agnostic Explanations. <a href="https://github.com/marcotcr/lime/tree/master">https://github.com/marcotcr/lime/tree/master</a></p><p>The idea of Lime is to replace a global model with a simpler local model for explaining the global model’s prediction for a given data instance. The simpler local model is usually linear regression and is trained over a set of artificial samples which are in the vicinity of the original data point. This can be generated by replacing some pixels in an image or some words in a sentence. The weights (coefficients) of the obtained linear regression model reflect the importance of their corresponding features.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*2f_FTkgPkJ78RcKYynpBSg.png" /><figcaption>Lime approach — Image source: [1]</figcaption></figure><p>Lemna stands for Local Explanation Method using Nonlinear Approximation.</p><blockquote><em>[1] Guo, Wenbo, et al. “Lemna: Explaining deep learning based security applications.” proceedings of the 2018 ACM SIGSAC conference on computer and communications security. 2018.</em></blockquote><p>Lemna replaces Lime’s linear model with a Gaussian mixture of linear models:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*EUDrsseQKxvf2WdMiR5OtA.png" /><figcaption>Mixture of components — Image source: [1]</figcaption></figure><p>The other idea introduced in Lemna is to replace linear regression with a fused Lasso linear regression where subsequent features have a constrained weight difference:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*-lUp5TPDH4Yw6PJjNWUfaA.png" /><figcaption>Fused lasso regression — Image source: [1]</figcaption></figure><p>The motivation of this idea is “forcing a learning algorithm to take features as groups and then learn a target model based on feature groups” which can be convenient for sequence and time-series data.</p><p>I found the idea very interesting, however I couldn’t find a software implementation anywhere. I decided to implement it. The lazy option was to figure out if it can be done as an extension to Lime which has code available and a well documented API. I am sharing my experience here.</p><p>As for application, I revisited one of the Lime tutorials where a classifier predicts the category of a given text (e.g. atheism vs. christianity). I chose motorcycles vs. space as an example in the following.</p><p>Original tutorial: <a href="https://marcotcr.github.io/lime/tutorials/Lime%20-%20basic%20usage%2C%20two%20class%20case.html">https://marcotcr.github.io/lime/tutorials/Lime%20-%20basic%20usage%2C%20two%20class%20case.html</a></p><p>In this context, explaining is highlighting some words in the text. It is worth noting that classifiers are prone to overfitting with the used dataset The dataset can be found here: <a href="http://qwone.com/~jason/20Newsgroups/">http://qwone.com/~jason/20Newsgroups/</a></p><p>Anyways, this can be a nice opportunity to experiment with Lemna because text can be considered simply as a sequence of words.</p><p>We will use the LimeTextExplainer.</p><pre>from lime.lime_text import LimeTextExplainer</pre><p>Here how we can turn Lime into Lemna for this scenario:</p><ol><li>We have to create our own regressor. The default regressor for Lime is Ridge regression. However, we want a Gaussian mixture and fused Lasso implemented.</li><li>If we do any kind of feature selection or word bagging, we lose the positioning information. Lemna loses its intention of working on subsequent features. We have to force the regressor to take the features in-order.</li></ol><p>Thus, we have to disable <strong>bow</strong> (bag of words) in the explainer declaration:</p><pre>explainer = LimeTextExplainer(class_names=class_names, bow=False)</pre><p>and make sure we provide the regressor with all the available features (the full sequence of words). This can be done either by marking <strong>feature_selection = ‘none’</strong> or by setting <strong>num_features</strong> to the count of all features (words) in the input text.</p><p>I opted for the second approach because of the first one leads to a different output array’s shape. To obtain the number of words in the input text, I used the split expression (“\W+”) which is considered the default option in Lime’s API:</p><pre>num_features = len ( re.split(r&quot;\W+&quot;,s) ) </pre><p>This is the call for requesting explanation for an input string <strong>s</strong>:</p><pre>exp = explainer.explain_instance(s, c.predict_proba, \<br>num_features=len(re.split(r&quot;\W+&quot;,s)), \<br>model_regressor=LemnaRegressor())</pre><p>where <strong>c.predict_proba() </strong>is the prediction function. <strong>c</strong> is a pipeline composed of a TF-IDF vectorizer and a random forest classifier. But <strong>c</strong> can be anything in general, an RNN, a CNN, a transformer, etc.</p><p>By default, 5000 samples are generated for explaining an instance (<strong>num_samples=5000</strong>).</p><p><strong>LemnaRegressor</strong> is my implementation of Lemna as a Lime regressor class.</p><p>A regressor class should extend two base classes: <strong>LinearModel</strong> and <strong>RegressorMixin</strong>. It must implement a <strong>fit()</strong> function taking three arguments: a mini dataset of samples <strong>X</strong>, their labels <strong>y</strong> and a list of weights <strong>sample_weight</strong>. The weights are obtained via a kernel function and the distances of the neighborhood samples to the original datapoint. All three arguments are independently provided by Lime. We just need to handle them for building the surrogate model.</p><pre>from sklearn.linear_model._base import LinearModel,RegressorMixin<br>class LemnaRegressor(LinearModel, RegressorMixin):<br>  def fit(self, X, y, sample_weight=None):<br>  return self</pre><p>Here is my simplistic implementation of the Lemna approach. For simplicity I worked with a mixture of two components (<strong>K=2</strong>). Another parameter is <strong>eps</strong> (same as <strong>S</strong> in the fused lasso constraint above). eps is usually set to a small value such as 10^-2, 10^-3, etc. or to a high value such as 10⁴ if we want to disable fused lasso altogether.</p><p>I start by assigning samples randomly to one or the other of the two components of the mixture. I then go into many iterations of minimization and expectation. In minimization, we fit a linear model per component by solving a constrained convex optimization problem. For this task, I used the <strong>cvxpy</strong> python library.</p><p>In expectation, we redistribute the samples among components based on the newly learned models. Each sample is assigned to the model that fits it better (here a model is simply the vector <strong>beta</strong>):</p><pre>np.abs(X @ beta0.value - y) &lt; np.abs(X @ beta1.value - y)</pre><p>The iterations continue until there is no change in the distribution of samples among components (no body changes its camp).</p><pre>def fit(self, X, y, sample_weight=None):<br>  # Make sure X&#39;s row represents the ordered set of words (features)<br>  n_samples, n_feat = X.shape<br>  K = 2 # number of components in mixture<br>  eps = 10 ** -4<br>  # Expectation<br>  # start with random assignment to mixture components<br>  Z0 = np.random.randint(2, size=(n_samples, 1))<br>  Z_ = np.concatenate([Z0,1-Z0], axis = 1)<br>  Z = np.concatenate([1-Z0,Z0], axis = 1) # just make it different than Z<br>  itr = 0<br>  while np.any(Z != Z_):<br>    itr += 1<br>    print(itr)<br>    Z = Z_.copy()<br><br>    # Minimization<br>    # split into two clusters<br>    X0 = X[np.where(Z[:,0]==1)]<br>    y0 = y[np.where(Z[:,0]==1)]<br>    w0 = sample_weight[np.where(Z[:,0]==1)]<br>    X1 = X[np.where(Z[:,1]==1)]<br>    y1 = y[np.where(Z[:,1]==1)]<br>    w1 = sample_weight[np.where(Z[:,1]==1)]<br><br>    # compute a LR model with fused LASSO for each cluster<br>    beta0 = cp.Variable(n_feat)<br>    forwardDiff0 = beta0[1:] - beta0[0:-1]<br>    objective0 = 0.5 * cp.Minimize( cp.sum_squares( cp.multiply(w0, (X0 @ beta0 - y0) ) ) )<br>    constraints0 = [cp.norm(forwardDiff0,2) &lt;= eps]<br>    prob0 = cp.Problem(objective0, constraints0)<br>    result0 = prob0.solve()<br>    beta1 = cp.Variable(n_feat)<br>    forwardDiff1 = beta1[1:] - beta1[0:-1]<br>    objective1 = 0.5 * cp.Minimize(cp.sum_squares( cp.multiply(w1, (X1 @ beta1 - y1) ) ) )<br>    constraints1 = [cp.norm(forwardDiff1,2) &lt;= eps]<br>    prob1 = cp.Problem(objective1, constraints1)<br>    result1 = prob1.solve()<br><br>    # Expectation<br>    # reassign Z<br>    Z_ = np.reshape(np.abs(X @ beta0.value - y) &lt; np.abs(X @ beta1.value - y), (n_samples, 1))<br>    Z_ = np.concatenate([Z_,1-Z_], axis = 1)<br>  <br>  print (&quot;after &quot;, itr, &quot;iterations:&quot;)<br>  # find the original sample we&#39;re explaining (all ones)<br>  idx = np.where(X.all(1))[0][0]<br>  if Z[idx][0] == 1: # it belongs to cluster 0<br>  self.coef_ = beta0.value<br>  else: # it belongs to cluster 1<br>  self.coef_ = beta1.value<br>  self.intercept_ = 0<br>  return self</pre><p>At the end, we check which component gained the original datapoint to its side (usually the input datapoint is the first row of the matrix <strong>X</strong>). This is the vector with all ones (which means all the features are included, not masked). We assign the coefficients of the winner component (the betas) to the <strong>coef_</strong> data member of the regressor object. We set the <strong>intercept_</strong> to 0 and return the object it<strong>self</strong>. Lime will take it from there.</p><p>Note that the sample weights are calculated by Lime and provided to the regressor fit function. For our purpose they should be split into two lists <strong>w1</strong> and <strong>w2</strong> based on the component membership.</p><p>Although not mentioned in the original paper, I used the sample weights in the minimization formulation:</p><pre>cp.sum_squares( cp.multiply(w1, (X1 @ beta1 - y1)<br>cp.sum_squares( cp.multiply(w2, (X2 @ beta1 - y2)</pre><p>Speed forward to some results, I was a bit disappointed. It is true that Lemna has the effect of smoothing the difference between coefficients, and can lead to a model with better fidelity to the classifier (the random forest), but this has not led to generating a group of contiguous words as explanations, or change the explanation that can be obtained via ridge regression by much.</p><p>Here is a random example for the classification: motorcycles vs. space. We take one instance of the space class. The explanations of Lime and Lemna (features and their coefficients, pushing for one class or the other) are displayed next.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BOW3CGhLIRsaRAkUcwUsCw.png" /><figcaption>Lime vs. Lemna. Which is which?</figcaption></figure><p>Can you guess which barplot corresponds to Lemna? and which is Lime?</p><p>You guessed it! Lemna forces the difference between subsequent coefficients to be small. It gives this sensation of overall smoothness (figure to the right). Note that the features are sorted by their coefficient’s absolute value in both figures.</p><p>The word indices of top features for Lemna; [28, 43, 56, 12, 29, 17, 30, 52, 71, 69]</p><p>These indices correspond to the words: [‘042810’, ‘on’, ‘growing’, ‘Nntp’, ‘1’, ‘edu’, ‘aurora’, ‘and’, ‘eat’, ‘Id’]. We can notice the overfitting, although aurora and edu look more like space than motorcycle.</p><p>The word indices of top features for Lime: [28, 43, 29, 56, 12, 30, 52, 17, 7, 21]</p><p>These indices correspond to the words: [‘042810’, ‘on’, ‘1’, ‘growing’, ‘Nntp’, ‘aurora’, ‘and’, ‘edu’, ‘Subject’, ‘Colorado’]</p><p>We can see that some top features are common. However, top features are not necessarily grouped for Lemna (no subsequent indices).</p><p>Here is the notebook if you like to experiment with this tutorial, <a href="https://colab.research.google.com/drive/19LEOqJFw8Rm3lGWTQwmmKjR_2oVcqAFg?usp=sharing">https://colab.research.google.com/drive/19LEOqJFw8Rm3lGWTQwmmKjR_2oVcqAFg?usp=sharing</a></p><p>Of course, you can try with other applications, such as sentiment prediction, fake news detection, or any time series. Let me know if Lemna gives you an edge over Lime (in terms of better, or grouped explanations). You can also try with characters rather than words (simply set <strong>char_level=True</strong>) which might have another range of applications, such as classifying a sequence of bytes or a discrete signal.</p><p>Good luck!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=07bbb8f48664" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>