<?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 Nils Schlüter on Medium]]></title>
        <description><![CDATA[Stories by Nils Schlüter on Medium]]></description>
        <link>https://medium.com/@NilsSchlueter?source=rss-97f919f7f84------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Nils Schlüter on Medium</title>
            <link>https://medium.com/@NilsSchlueter?source=rss-97f919f7f84------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 23:44:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@NilsSchlueter/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[Don’t Overfit! — How to prevent Overfitting in your Deep Learning Models]]></title>
            <link>https://medium.com/data-science/dont-overfit-how-to-prevent-overfitting-in-your-deep-learning-models-63274e552323?source=rss-97f919f7f84------2</link>
            <guid isPermaLink="false">https://medium.com/p/63274e552323</guid>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[tensorflow]]></category>
            <dc:creator><![CDATA[Nils Schlüter]]></dc:creator>
            <pubDate>Wed, 05 Jun 2019 14:02:27 GMT</pubDate>
            <atom:updated>2019-06-05T14:02:27.770Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u0LZB6PWVIDcvugn" /><figcaption>Feature Learning can help you prevent overfitting</figcaption></figure><h3>Don’t Overfit! — How to prevent Overfitting in your Deep Learning Models</h3><h4>Learn more about common strategies like unsupervised feature learning which can help you to prevent overfitting</h4><p>In this article, I am going to talk about how you can prevent overfitting in your deep learning models. To have a reference dataset, I used the <a href="https://www.kaggle.com/c/dont-overfit-ii">Don’t Overfit! II</a> Challenge from Kaggle.</p><blockquote>If you actually wanted to win a challenge like this, <strong>don’t use Neural Networks</strong> as they are very prone to overfitting. But, we’re not here to win a Kaggle challenge, but to learn how to prevent overfitting in our deep learning models.</blockquote><p>So let’s get started!</p><h3>Base Model</h3><p>To see how we can prevent overfitting, we first need to create a base model to compare the improved models to. The base model is a simple keras model with two hidden layers with 128 and 64 neurons. You can check it out here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a8164b9b3fdd38b1dd436f32beda2ea/href">https://medium.com/media/3a8164b9b3fdd38b1dd436f32beda2ea/href</a></iframe><p>With this model we can achieve a training accuracy of over 97%, but a validation accuracy of only about 60%. In the graphic below we can see clear signs of overfitting: The Train Loss <strong>decreases</strong>, but the validation loss <strong>increases</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*6f5YKyzGpz_tVM47QoNWGQ.png" /><figcaption>This is a sign of overfitting: Train loss is going down, but validation loss is rising</figcaption></figure><p>If you see something like this, this is a clear sign that your model is overfitting: It’s learning the training data really well but fails to generalize the knowledge to the test data. With this model, we get a score of about <strong>59% </strong>in the Kaggle challenge — not very good.</p><p>So, let’s see how we can improve the model</p><h3>Improving the Score</h3><p>To improve the score, we can essentially do two things</p><ul><li>Improve our model</li><li>Improve our data</li></ul><p>I’ll start by showing you how to change the base model. Then I’ll go into <strong>feature selection</strong>, which allows you to change the data</p><h3>Improving our model</h3><p>I’m going to be talking about three common ways to adapt your model in order to prevent overfitting.</p><h4>1: Simplifying the model</h4><p>The first step when dealing with overfitting is to decrease the complexity of the model. In the given base model, there are 2 hidden Layers, one with 128 and one with 64 neurons. Additionally, the input layer has 300 neurons. This is a huge number of neurons. To decrease the complexity, we can simply remove layers or reduce the number of neurons in order to make our network smaller. There is no general rule on how much to remove or how big your network should be. But, if your network is overfitting, try making it smaller.</p><h4>2: Adding Dropout Layers</h4><p>Dropout Layers can be an <a href="http://www.jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf">easy and effective way to prevent overfitting</a> in your models. A dropout layer randomly drops some of the connections between layers. This helps to prevent overfitting, because if a connection is dropped, the network is forced to Luckily, with keras it’s really easy to add <a href="https://keras.io/layers/core/">a dropout layer.</a></p><p>The new, simplified model with dropout layers could look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec9c2035913236be7dade5e48d48d40a/href">https://medium.com/media/ec9c2035913236be7dade5e48d48d40a/href</a></iframe><p>As you can see, the new model only has one hidden layer and fewer neurons. Additionally, I added Dropout layers between the layers with a dropout rate of 0.4.</p><h4>3: Early Stopping</h4><p>Another way to prevent overfitting is to stop your training process early: Instead of training for a fixed number of epochs, you stop as soon as the validation loss rises — because, after that, your model will generally only get worse with more training. You can implement early stopping easily with a callback in keras:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/252ff6c0df98f81aa81fc6a476167e4a/href">https://medium.com/media/252ff6c0df98f81aa81fc6a476167e4a/href</a></iframe><p>For this to work, you need to add the validation_split parameter to your fit function. Otherwise, the val_loss is not measured by keras.</p><h3>Feature Selection</h3><p>If you take a look at the raw data, you will see that there are <strong>300 columns</strong> and only <strong>250 rows.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*04QDRF64JR95HzseIuXfrw.png" /><figcaption>Overview over the datset</figcaption></figure><p>That is a lot of features for only very few training samples. So, instead of using all features, it’s better to use only the most important ones. This will, on the one hand, make the training process notably faster, on the other hand, it can help to prevent overfitting because the model doesn’t need to learn as many features.</p><p>Luckily, scikit-learn provides the great <a href="https://scikit-learn.org/stable/modules/feature_selection.html">Feature selection</a> Module, which helps you identify the most relevant features of a dataset. So, let’s explore some of those ways!</p><h3>F-Score Selection</h3><p>One of the simplest ways to select relevant features is to calculate the F-Score for each feature. The F-Score is calculated using the variance between the features and the variance within each feature. A high F-score usually means that the feature is more important than a feature with a low F-score. You can calculate the F-Scores for the Features like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec0bc71f0ffb763de05ecda1ce945272/href">https://medium.com/media/ec0bc71f0ffb763de05ecda1ce945272/href</a></iframe><p>If you plot the data, you will see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*f14UWNfTIBWlvCnlt1bFwg.png" /><figcaption>F-Score of each of the 300 features of the dataset</figcaption></figure><p>As you can see, the F-score between the features varies greatly. You can get the score for each column with selector.scores_ or you can get the index of the top 10 features like this:</p><pre>f_score_indexes = (-selector.scores_).argsort()[:10]</pre><h3>Recursive Feature Elimination</h3><p>Another way is the recursive feature selection. Unlike the other method, with RFE you don’t calculate a score for each feature, but you train a classifier multiple times on smaller and smaller feature set. After each training, the importance of the features is calculated and the least important feature is eliminated from the feature set.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b05d4398fbdf3137190519034cea9c46/href">https://medium.com/media/b05d4398fbdf3137190519034cea9c46/href</a></iframe><p>You can get the index of these features like this:</p><pre>rfe_indexes = np.where(rfe_values)[0]</pre><h3>Results</h3><p>At the beginning of this article, we started with a model which was overfitting and could barely get more than 50% accuracy. Below, you can see the results of the new model, trained on the data after the feature selection:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*9SUIv9-99XJIYurnC66_Jw.png" /><figcaption>Results of the improved model</figcaption></figure><p>It’s still not perfect, but as you can see, the model is overfitting way less. In the Kaggle challenge, the new model scores at about <strong>80%</strong> — which is 20% better than the base model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63274e552323" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/dont-overfit-how-to-prevent-overfitting-in-your-deep-learning-models-63274e552323">Don’t Overfit! — How to prevent Overfitting in your Deep Learning Models</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Can automated machine learning outperform handcrafted models?]]></title>
            <link>https://medium.com/data-science/can-automated-machine-learning-outperform-handcrafted-models-feff83e0dc97?source=rss-97f919f7f84------2</link>
            <guid isPermaLink="false">https://medium.com/p/feff83e0dc97</guid>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <dc:creator><![CDATA[Nils Schlüter]]></dc:creator>
            <pubDate>Sat, 05 Jan 2019 13:19:39 GMT</pubDate>
            <atom:updated>2019-01-05T13:19:39.767Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*__30UA4apj0wR6a-" /><figcaption>Can Auto-Keras really find better models than you? — Photo by <a href="https://unsplash.com/@annietheby?utm_source=medium&amp;utm_medium=referral">Annie Theby</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Testing Auto-Keras models on a real-world dataset</h4><p>Automated machine learning (AutoML) can be used to automatically find and train machine learning models. You no longer need to create the model yourself — The AutoMl Algorithms will analyze your data and pick the best model automatically.</p><p>But how good are those models really? Can they be compared to custom models or are they even better? Do we never need to handpick another model again? Let’s find out!</p><h3>Introduction: Auto-Keras</h3><p>Companies like Google already offer AutoML Products, but with <a href="https://autokeras.com/">Auto-Keras</a> there is also an open-source solution for this. In the <a href="https://autokeras.com/start/">official getting started example</a>, Auto-Keras is used to find the best neural architecture for the MNIST Dataset. When I tried the example, the resulting model reached a score of ~98%. That’s pretty impressive, so I decided to use Auto-Keras to try and beat myself on the <a href="https://www.kaggle.com/francksylla/titanic-machine-learning-from-disaster"><strong>Kaggle Titanic Dataset</strong></a>.</p><p>My best score for this competition is at about<strong> 80%</strong> precision, which currently puts me in the Top 900 of all competitors. That’s a decent enough score, so let’s see if Auto-Keras can outperform me!</p><blockquote>You can find the Notebook with the complete code <a href="https://colab.research.google.com/drive/1UmgQV2O0D8k1CKPWMB1fYvIuC6_50zSx">here</a></blockquote><h4>Setup</h4><p>I used Google Colab for this project. To install Auto-Keras in Google Colab, simply run:</p><pre>!pip install autokeras</pre><p>If you want to run this locally, you can just install Auto-Keras from the command line using pip.</p><h4>Data</h4><p>For this example, I used the dataset you can download from the <a href="https://www.kaggle.com/c/titanic/data">Kaggle Competition</a>. To use the data in the Auto-Keras Model, you need to import it as a numpy array. Because the Titanic data contains text data, we need to do some preprocessing first:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34903fd5d6b9c38d7d13e23a9a278f21/href">https://medium.com/media/34903fd5d6b9c38d7d13e23a9a278f21/href</a></iframe><p>This is the same preprocessing and feature engineering I did for my own titanic solution. The preprocessing is, of course, different for each project, but if you want to use Auto-Keras, you will need to provide numpy arrays.</p><p>After preprocessing, you can load the data as train and test data:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36ecb749ef8434c2de6b5081a1e2de26/href">https://medium.com/media/36ecb749ef8434c2de6b5081a1e2de26/href</a></iframe><h4>Finding the right model</h4><p>After you got <em>x_train</em>, <em>y_train</em>, <em>x_test</em> and <em>y_test</em> for your dataset, you can use the <em>fit </em>method to find the best model and train it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b553d66daa4c58d52a58fd8df4d568b/href">https://medium.com/media/4b553d66daa4c58d52a58fd8df4d568b/href</a></iframe><p>That’s it — Two lines of code is all you need. Everything else happens automatically.</p><h3>Results</h3><p>After training the model, I used it to generate predictions and uploaded the predictions to Kaggle. I got a score of <strong>0.79904. </strong>This is slightly worse than my handmade model, but we need to consider that Auto-Keras is still very new. The TabularClassifier currently only supports LGBMClassifier — which is not ideal for this dataset. If the project is continued in the future, we can probably expect even better results. Also finding the model was incredibly fast and easy, so I think this is a very good and promising result!</p><h4>Pros and cons</h4><p>While it&#39;s very easy to use the resulting model to generate predictions, you lose a lot of knowledge about your model. If you don’t actively search for it, you don’t know the architecture and parameter values of the resulting model — after all, they are chosen automatically, so you never really need them. While this is probably a good thing for beginners and inexperienced machine learning engineers, it can get dangerous if you’re later trying to adapt or expand your model.</p><p>“Black Box” models are already heavily discussed in machine learning projects, but when using AutoML the <em>whole process</em> becomes even more of a black box — you just throw some data in and hope for the best.</p><p>These are some risks you need to be aware of when implementing AutoML into your machine learning projects.</p><h4>Significance for future work</h4><p>So, does AutoML eliminate the need for custom models? No, probably not. <br>Can it help you to create better models? Probably.<br>For example, you can use it to quickly find and train good base models, which you can later improve on your own. You can also try different feature engineering strategies quickly to get a feeling which one works the best. Just give it a try and see how you can implement it in your own projects.</p><p>I think AutoML can have a big impact on many machine learning projects in the future — I am looking forward to further developments and improvements in this area.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=feff83e0dc97" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/can-automated-machine-learning-outperform-handcrafted-models-feff83e0dc97">Can automated machine learning outperform handcrafted models?</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Keras Hyperparameter Tuning in Google Colab using Hyperas]]></title>
            <link>https://medium.com/data-science/keras-hyperparameter-tuning-in-google-colab-using-hyperas-624fa4bbf673?source=rss-97f919f7f84------2</link>
            <guid isPermaLink="false">https://medium.com/p/624fa4bbf673</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Nils Schlüter]]></dc:creator>
            <pubDate>Thu, 06 Dec 2018 14:07:10 GMT</pubDate>
            <atom:updated>2018-12-06T14:07:10.037Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*K-0v6zWiCXt2_FHB" /><figcaption>Tuning Hyperparameters for your neural network can be tricky (Photo by <a href="https://unsplash.com/@aroberts1228?utm_source=medium&amp;utm_medium=referral">Anthony Roberts</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a>)</figcaption></figure><p>Hyperparameter Tuning is one of the most computationally expensive tasks when creating deep learning networks. Luckily, you can use Google Colab to speed up the process significantly. In this post, I will show you how you can tune the hyperparameters of your existing keras models using Hyperas and run everything in a Google Colab Notebook.</p><h4>Creating a new notebook and enable the GPU Runtime</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*enXh4J9hOc8ggaM0j-757A.png" /><figcaption>Dialog to change the runtime to GPU</figcaption></figure><p>First, you need to create a new notebook. Open your <a href="https://colab.research.google.com/">Colab Console </a>and select <strong>New Python 3 Notebook.</strong> In your notebook, choose <strong>Runtime </strong>from the menu and then <strong>Change runtime type. </strong>Select Hardware accelerator: GPU and hit save. This will significantly speed up every calculation you do in this notebook.</p><h4>Installing the packages</h4><p>You can install new packages using <em>pip</em>. In this case, we need <a href="https://github.com/maxpumperla/hyperas">hyperas</a> and <a href="https://github.com/hyperopt/hyperopt">hyperopt</a>. Copy and paste the following into the first cell of your notebook:</p><pre>!pip install hyperas<br>!pip install hyperopt</pre><p>When you run the cell you will see that pip is downloading and installing the dependencies.</p><h3><strong>Getting the data and creating the model</strong></h3><p>For this post I’m going to use the example from the <a href="https://github.com/maxpumperla/hyperas">hyperas github page</a>. You can find the finished Colab Notebook <a href="https://colab.research.google.com/drive/184Tas98M-pPQanCfTbOHRBRUeE9dtdM9#scrollTo=rdo3mScvBHF4">here</a>.</p><h4><strong>Data Function</strong></h4><p>You’ll need a data function to load your data. It needs to return your X_train, Y_train, X_test and Y_test values. Here is an example for a data function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc8fd5491bbc85841e1a0013a304457f/href">https://medium.com/media/fc8fd5491bbc85841e1a0013a304457f/href</a></iframe><blockquote><strong>Note: </strong>Your data function needs to return the values in exactly this order: X_train, Y_train, X_test, Y_test. Be careful if you’re using scikit learns <a href="https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html">train_test_split</a>, as this returns the values in a different order</blockquote><h4><strong>Model Function</strong></h4><p>The Model Function is where you define your model. You can use your all available keras functions and layers. To add Hyperparameters for tuning, you can use the <strong>{{uniform()}}</strong> and <strong>{{choice()}} </strong>keywords.</p><p>Let’s say you want to try out different values for your <em>batch_size</em>. You can simply write <em>batch_size={{choice([32, 64, 128])}}</em> and during each trial, one of the values will be chosen and tried out . A more in-depth explanation on how to define the parameters to tune can be found on the <a href="https://github.com/maxpumperla/hyperas">Hyperas Github Page</a> or you can look at the example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea159ee97a79e46b049c9890544e68ea/href">https://medium.com/media/ea159ee97a79e46b049c9890544e68ea/href</a></iframe><blockquote><strong>Note: </strong>Your model function has to return a python dictionary with a loss key and a status key</blockquote><h3>Problems with Colab</h3><p>If you try to run this example now, the trial will fail because Hyperas won’t be able to find your notebook. You need to copy your notebook and upload it again to your google drive folder. Luckily, you can do this from inside your notebook as described in this <a href="https://stackoverflow.com/questions/49920031/get-the-path-of-the-notebook-on-google-colab">stackoverflow answer</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/193cd37edd65c0491b301cb4c0d8105c/href">https://medium.com/media/193cd37edd65c0491b301cb4c0d8105c/href</a></iframe><blockquote><strong>Note: </strong>In lines 16 and 18, you’ll need to change <em>HyperasMediumExample </em>to the name of your own notebook</blockquote><p>After running this cell, you will be prompted to open a website in your browser and copy &amp; paste a code back into the notebook:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*DwQlmlARWsN_lP8fSaFLAg.png" /><figcaption>The Output after you run the cell above</figcaption></figure><p>Follow the link, log in with your google account and copy &amp; paste the code back into the notebook. If you open the <strong>Files </strong>Tab in the left sidebar, you should now see a File called &lt;YourNotebook&gt;.ipynb</p><h4>Starting the Trial</h4><p>Now you can start the trial. Be careful, you have to set the parameter <em>notebook_name</em> to the name of your Notebook. Otherwise the Trial will fail:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d46079aee2b4ee88ed8e5757505bf58b/href">https://medium.com/media/d46079aee2b4ee88ed8e5757505bf58b/href</a></iframe><p>After running this cell, the Scan starts you can see the result in the output of the cell.</p><h3><strong>Troubleshooting</strong></h3><p>If you have any problems while doing this, I recommend to do the following:</p><ol><li>In the left sidebar, open <strong>Files. </strong>There will be a file called &lt;YourNotebook&gt;.ipynb. Delete that file</li><li>In the menu, select <strong>Runtime </strong>and then <strong>Restart Runtime</strong>.</li><li>Reload the page</li></ol><p>After your runtime is connected again, you can start again by running every cell from top to bottom</p><h3>Conclusion</h3><p>With just a few tweaks you can use Google colab to tune the hyperparameters of your keras network. Again, the full example can be found <a href="https://colab.research.google.com/drive/184Tas98M-pPQanCfTbOHRBRUeE9dtdM9#scrollTo=rdo3mScvBHF4">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=624fa4bbf673" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/keras-hyperparameter-tuning-in-google-colab-using-hyperas-624fa4bbf673">Keras Hyperparameter Tuning in Google Colab using Hyperas</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>