<?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 Dhanush on Medium]]></title>
        <description><![CDATA[Stories by Dhanush on Medium]]></description>
        <link>https://medium.com/@dhanushbinu123?source=rss-cddfb6674b76------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*m_DpHdcumu6UI3aeD-T6yg.jpeg</url>
            <title>Stories by Dhanush on Medium</title>
            <link>https://medium.com/@dhanushbinu123?source=rss-cddfb6674b76------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 07:26:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhanushbinu123/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[Vanishing Gradients: Causes and Prevention Measures]]></title>
            <link>https://dhanushbinu123.medium.com/vanishing-gradients-causes-and-prevention-measures-210b2f223a35?source=rss-cddfb6674b76------2</link>
            <guid isPermaLink="false">https://medium.com/p/210b2f223a35</guid>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Dhanush]]></dc:creator>
            <pubDate>Thu, 22 Sep 2022 05:49:07 GMT</pubDate>
            <atom:updated>2022-09-22T05:49:07.247Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Underfitting and Overfitting</strong> are the easily the most common types of problems when it comes to training a Neural Network. Since they can easily overfit/underfit based on the number of neurons. but the most overlooked problem is of <strong>Exploding and Vanishing gradients</strong>.</p><p>Sometimes they can completely stop the progress of your network and break them. hence it is of utmost importance to take adequate steps in order to prevent them from happening.</p><h3>Causes</h3><h4>Using Sigmoid and tanh Activation Function</h4><p>Vanishing Gradient usually occurs when using sigmoid activation or tanh activation function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/0*UHVujBNzPDBkw5c6" /><figcaption>Sigmoid function and its derivative.</figcaption></figure><p>As you can see the derivative of sigmoid function becomes zero when the input reaches large values. Hence during Backpropagation of neural networks the gradient becomes very small resulting in a very minimal change in the weights, which is not very desirable.</p><p>The derivative of tanh activation function is very similar in this regard, for large values the derivative function becomes zero.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/0*dyM7DPUUjIObiLka.png" /><figcaption>tanh function and its derivative</figcaption></figure><p>This effect is not prominent when the no of layers in the neural network is relatively small, But when we have significant amount of layers in our neural network then due to chain rule the derivates of each layer gets multiplied as we go down the neural network. Hence the change in weight of the initial layer will be very small. This is usually undesirable since the initial weights are used to distinguish the most basic features.</p><h4>Weight Initialization</h4><p>Sometimes when randomly initializing the weights, some weights may tend to be very small this can also lead to vanishing gradient problem</p><p>Lets try to demonstrate this effect using a simple neural network.</p><pre>X, y = make_circles(n_samples=1000, noise=0.1, random_state=1)<br>scaler = MinMaxScaler(feature_range=(-1, 1))<br>X = scaler.fit_transform(X)</pre><pre>#dataset spliting</pre><pre>n_train = 500<br>trainX, testX = X[:n_train, :], X[n_train:, :]<br>trainy, testy = y[:n_train], y[n_train:]</pre><pre>#model definition</pre><pre>init = RandomUniform(minval=0, maxval=1)<br>model = Sequential()<br>model.add(Dense(5, input_dim=2, activation=&#39;tanh&#39;, kernel_initializer=init))</pre><pre>model.add(Dense(5, activation=&#39;tanh&#39;, kernel_initializer=init))</pre><pre>model.add(Dense(5, activation=&#39;tanh&#39;, kernel_initializer=init))</pre><pre>model.add(Dense(5, activation=&#39;tanh&#39;, kernel_initializer=init))</pre><pre>model.add(Dense(5, activation=&#39;tanh&#39;, kernel_initializer=init))</pre><pre>model.add(Dense(1, activation=&#39;sigmoid&#39;, kernel_initializer=init))</pre><pre>model.add(Dense(1, activation=&#39;sigmoid&#39;, kernel_initializer=init))</pre><pre># compile model</pre><pre>opt = SGD(lr=0.01, momentum=0.9)<br>model.compile(loss=&#39;binary_crossentropy&#39;, optimizer=opt, metrics=[&#39;accuracy&#39;])</pre><pre># fit model</pre><pre>history = model.fit(trainX, trainy, validation_data=(testX, testy), epochs=500)</pre><pre># evaluate the model</pre><pre>_, train_acc = model.evaluate(trainX, trainy, verbose=0)<br>_, test_acc = model.evaluate(testX, testy, verbose=0)</pre><pre>print(&#39;Train: %.3f, Test: %.3f&#39; % (train_acc, test_acc))</pre><pre># plot</pre><pre>plt.plot(history.history[&#39;accuracy&#39;], label=&#39;train&#39;)<br>plt.plot(history.history[&#39;val_accuracy&#39;], label=&#39;test&#39;)<br>plt.legend()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*jAT-b3JWJ6hrNYJBlOPfEQ.png" /><figcaption>Training and Testing Accuracy with tanh activation function</figcaption></figure><p>By observing the Graph of the training and testing accuracy, we can see that the:</p><ul><li>Model is not very accurate(Test accuracy:56%)</li><li>The Model is very slow to learn(for the first 200 epochs the accuracy remains the same and then increases gradually after 200 epochs)</li></ul><p>We can safely conclude that Vanishing gradients must be the culprit here. This can be further illustrated by considering the weights of the first layer, this can be done using <strong>Keras callbacks</strong>.</p><pre>...<br>model.add(Dense(5, activation=&#39;tanh&#39;, kernel_initializer=init))<br>model.add(Dense(5, activation=&#39;tanh&#39;, kernel_initializer=init))<br>model.add(Dense(1, activation=&#39;sigmoid&#39;, kernel_initializer=init))</pre><pre><strong>print_weights = LambdaCallback(on_epoch_end=lambda batch, logs: print(model.layers[0].get_weights()))</strong></pre><pre>...</pre><pre>history = model.fit(trainX, trainy, validation_data=(testX, testy),<strong>callbacks = [[print_weights]]</strong>, epochs=500)</pre><pre>...</pre><p>From the output lets compare the weights of the layer.</p><p>weights of the first layer during the 50th epoch = [0.5584462 , 0.5673716 , 0.10358545, 0.16718468, 0.5542617 ],[0.13693556, 0.81074333, 0.65430593, 0.4081624 , 0.5088767 ]</p><p>weights of the first layer during the 100th epoch = [0.5276652 , 0.56785566, 0.08765446, 0.16682781, 0.5214859 ],[0.13223608, 0.7922534 , 0.6300024 , 0.39186594, 0.502041 ]</p><p>We can clearly see that the weights aren&#39;t drastically changing even during the initial 50 epochs.</p><h3>Prevention Techniques</h3><h4>ReLU activation function</h4><p>By using the ReLU activation instead of sigmoid and tanh activation function we can prevent vanishing gradient.</p><pre>...<br>model = Sequential()</pre><pre>model.add(Dense(5, input_dim=2, activation=&#39;relu&#39;, kernel_initializer=&#39;he_uniform&#39;))</pre><pre>model.add(Dense(5, activation=&#39;relu&#39;, kernel_initializer=&#39;he_uniform&#39;))</pre><pre>model.add(Dense(5, activation=&#39;relu&#39;, kernel_initializer=&#39;he_uniform&#39;))</pre><pre>model.add(Dense(5, activation=&#39;relu&#39;, kernel_initializer=&#39;he_uniform&#39;))</pre><pre>model.add(Dense(5, activation=&#39;relu&#39;, kernel_initializer=&#39;he_uniform&#39;))</pre><pre>model.add(Dense(1, activation=&#39;sigmoid&#39;, kernel_initializer=init))</pre><pre>...</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*kEDuZqe5IljqjSz8yafJrw.png" /><figcaption>Training and Testing Accuracy with ReLUelu activation function</figcaption></figure><p>In this case the model converges much faster and provides higher accuracy.</p><p>weights of the first layer during the 50th epoch = [-1.2463173 , 1.0977441 , 0.606711 , -0.21142364, -0.22407277],[-1.3764595 , -1.158447 , 1.4483132 , -1.476005 , -0.7930375 ]</p><p>weights of the first layer during the 100th epoch = [-2.1367993 , 1.6104459 , 0.3941082 , -0.66891336, -0.24889009],[-0.33443457, -1.1000867 , 1.9572364 , -1.7306052 , -0.7959063 ]</p><p>As observed the weights in this case are changing at a much faster pace than when using tanh activation function.</p><h4>Employing different Weight Initialization techniques</h4><p>By using different weight initialization techniques we can control the amount of randomness in the weights so that the assigned weights are not very small.</p><p>The code used in this article makes use of RandomUniform initializer to set the minimum and maximum value for the weights.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=210b2f223a35" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Cleaning On Complex Dataset]]></title>
            <link>https://dhanushbinu123.medium.com/data-cleaning-on-complex-dataset-8d3870f49b13?source=rss-cddfb6674b76------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d3870f49b13</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[data-cleaning]]></category>
            <dc:creator><![CDATA[Dhanush]]></dc:creator>
            <pubDate>Mon, 29 Aug 2022 14:49:26 GMT</pubDate>
            <atom:updated>2022-08-29T14:49:26.184Z</atom:updated>
            <content:encoded><![CDATA[<p>Data Cleaning is the most crucial step when it comes to getting high accuracy in machine learning problems. Today we will try to apply statistical methods in order to clean and derive useful information form the dataset.</p><p>The Dataset used in this problem was body performance dataset (<a href="https://www.kaggle.com/datasets/kukuroo3/body-performance-data">Body performance Data | Kaggle</a>)</p><pre>import pandas as pd</pre><pre>df = pd.readcsv(&#39;bodyPerformance.csv&#39;)<br>df.head(10)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cBHJgSviwTfgSl-NJW-FCQ.png" /></figure><p>First let us try to find out more info about the dataset.</p><pre>df.describe()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jkAzkfEzW42PV90gADuLpQ.png" /></figure><p>There is surprisingly no missing values in any of the columns.</p><p>let us try to extract the mean, median, mode and the skew of the dataset. Before this let us try to separate the numerical columns from the categorical columns.</p><pre>num_df = df.select_dtypes(exclude = &#39;object&#39;)<br>cat_df = df.select_dtypes(include = &#39;object&#39;)</pre><pre>print(num_df.mean())<br>for i in num_df.columns:<br>   print(i , &quot;=&quot; ,  statistics.median(num_df[i]))<br>   print(num_df.mode())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*OEgeEs1d5OsWKmFavHRMZA.png" /></figure><p>Its not pretty but it gets the job done, from the information above we can try to form a vague picture of the distribution of our data.</p><p>As we know if the mean &gt; median &gt; mode then the data is positively skewed( tail is elongated towards the right side)</p><p>and if the mean &lt; median &lt; mode then the data is negatively skewed(tail is elongated towards the left side).</p><p>The first column(Age) seems to be positively skewed by quite a margin. ‘sit and bend forward_cm’ column also looks negatively skewed.</p><p>This can be confirmed by looking at the skew function given by pandas.</p><pre>num_df.skew()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*3800MC1PlmMp0osW2xYuog.png" /></figure><p>As we can see the ‘age’ and ‘sit and bend forward_cm’ seems to be skewed(if the value of the skew function for a particular parameter is above 0.5 or below -0.5 we can say that the parameter is highly skewed).</p><p>To Correct this we will perform Log Transform to lower the spread of the variable</p><pre>#we apply log transform to convert the skewness to normal<br>distribution<br>num_df[&#39;age&#39;] = np.log(num_df[&#39;age&#39;])</pre><pre>#num_df[&#39;sit and bend forward_cm&#39;] = np.log(num_df[&#39;sit and bend forward_cm&#39;])<br>skew(num_df)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*IwstYxPVDN2pDEx2sm7ptA.png" /></figure><p>To give more information about the data distribution we can also look at the Kurt function.</p><pre>num_df.kurt()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*rhm-AsEFpqEdibYrgZmElA.png" /></figure><p>As far as Kurt function is concerned, if the Kurt value is above 0 then the distribution is more pointy than normal function, values near 0 are ‘normal-like’ and Kurt value below zero means the distribution is not pointy but more flat.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/0*IByTfxgKymM7vE-S.jpg" /></figure><p>Form our Example we can see that ‘age’ , ‘height_cm’ , ‘gripForce’ , ‘sit and bend forward_cm’ have high Kurt values.</p><p>Lets visualize them in order to see what their distribution looks like.</p><pre>cols = [&#39;age&#39;, &#39;height_cm&#39;,&#39;gripForce&#39;,&#39;sit and bend forward_cm&#39;]</pre><pre># create the figure and axes<br>fig, axes = plt.subplots(2, 2,figsize = (10,10))<br>axes = axes.ravel() </pre><pre>for col, ax in zip(cols, axes):<br>sns.histplot(data=num_df[col], kde=True, stat=&#39;density&#39;, ax=ax)<br>fig.tight_layout()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*5wHCoxKORchAKKp9cn415g.png" /></figure><p>We know that high Kurt values are more prone to outliers, hence we will apply IQR test to find the outliers in ‘sit and bend forward_cm’.</p><pre>upper = num_df[&#39;sit and bend forward_cm&#39;] &gt;= (Q3+1.5*IQR)<br>print(&quot;Upper bound:&quot;,upper[upper])</pre><pre>lower = num_df[&#39;sit and bend forward_cm&#39;] &lt;= (Q1-1.5*IQR)<br>print(&quot;Lower bound:&quot;, lower[lower])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*yI7K07kbSLbShBARBV12rg.png" /></figure><p>Hence only 6 datapoints lie above the upper bound but 403 data points lie below the specified lower bound. We will delete all the points that lie below the lower bound, since these will negatively impact our model.</p><p>Here is our final cleaned DataFrame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tyzOaECLfMzhsQFMenRROQ.png" /></figure><p>Next Article will dive deep into training the model using advanced techniques to train the model in order to achieve the highest accuracy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d3870f49b13" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>