<?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 Lasse Schmidt on Medium]]></title>
        <description><![CDATA[Stories by Lasse Schmidt on Medium]]></description>
        <link>https://medium.com/@lasse-schmidt90?source=rss-b9e15f1839f6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*rSKgyom324Gmsixb.jpg</url>
            <title>Stories by Lasse Schmidt on Medium</title>
            <link>https://medium.com/@lasse-schmidt90?source=rss-b9e15f1839f6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 02:17:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lasse-schmidt90/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[How to use GCP Pub/Sub Feature to trigger Vertex AI Pipelines from Airflow]]></title>
            <link>https://lasse-schmidt90.medium.com/how-to-use-gcp-pub-sub-feature-to-trigger-vertex-ai-pipelines-from-airflow-a6385597d873?source=rss-b9e15f1839f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6385597d873</guid>
            <category><![CDATA[google-cloud]]></category>
            <category><![CDATA[google-cloud-functions]]></category>
            <category><![CDATA[vertex-pipeline]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[vertex-ai]]></category>
            <dc:creator><![CDATA[Lasse Schmidt]]></dc:creator>
            <pubDate>Wed, 26 Oct 2022 11:40:38 GMT</pubDate>
            <atom:updated>2022-10-26T11:42:10.829Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aQd21wtHk7TUqL4Y" /><figcaption>Photo by <a href="https://unsplash.com/@rajeshwerbatchu7?utm_source=medium&amp;utm_medium=referral">Rajeshwar Bachu</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Vertex AI is a great tool for all Data Scientists out there. It facilitates prototyping of models, testing models and creating pipelines for your Data Science projects. However, as this is still a very new product from Google Cloud, some functionalities aren’t yet neatly integrated. As of now the scheduling part and integration part into Airflow is not well implemented. There are a few ways of scheduling and triggering your pipeline from Airflow, one of them is using the Pub/Sub feature. How this works I will show you here.</p><p>As a pre-requisit I expect you to have a working Vertex AI pipeline with the compiled pipeline saved as a json-file in your Google Cloud Storage. For many pipeline cases this would already be enough, but I want to show you how to run a customized container in that pipeline, so what I also take as a pre-requisit is for you to have a dockerized image of your code you want to run on Artifact Repository.</p><p>To sum up the pre-requisites:</p><p>— have a compiled Vertex AI pipeline json file saved in Google Cloud Storage</p><p>— have your custom code as a Docker image saved in Artifact Repository</p><p>The task we now want to solve is that we would like to trigger this pipeline from Airflow, so we can have it run on a scheduled basis.</p><p>The first step is to create a Cloud Function. For Pub/Sub to work, first you need to set a topic. The basic instructions for that can also be found in the Google Cloud Documentation <a href="https://cloud.google.com/vertex-ai/docs/pipelines/trigger-pubsub">here</a>, but this does not directly show you how it will work then. So let me guide you through the process. Your main.py needs to look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e0b4b1c8ef700c1ad6c87864b58693a/href">https://medium.com/media/4e0b4b1c8ef700c1ad6c87864b58693a/href</a></iframe><p>What exactly is this doing? First, it expects a message to the subscribe method, here called event. This message we will be sending via our Airflow. From the given message it receives, it will extract two things: first, the Google Cloud Storage path to your compiled json-file, second the URI to your dockerized image on Artifact Repository. After extracting this information, it will simply use the python SDK for Vertex AI (which is weirdly enough called aiplatform) to trigger your pipeline.</p><p>As you can already see, this is highly customizable. This is a simple python function, and you can put in here anything you actually want. As mentioned before, I wanted to show you an example of how to run a customized Vertex AI pipeline with a Docker Image, but this can be anything you want the pipeline to do (for example using an AutoML task).</p><p>Next and lastly, we want to use Airflow to send the message and trigger our pipeline. So let’s have a look at the Airflow DAG:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a0431d8a8885ce2f4257a72ab27d9c0/href">https://medium.com/media/6a0431d8a8885ce2f4257a72ab27d9c0/href</a></iframe><p>What exactly is going on here? First, we use the PubSubPublishMessageOperator from Airflow. The MESSAGE as you can see consists of two parts, first the data part (which is expected to be a bytestring) and second the attributes part, which can be anything. Here I use a dict. This exact MESSAGE is then in turn send to the Cloud Function created earlier. If you have a look at the Cloud Function from above, you can see that we take the <em>pipeline_spec_uri = base64.b64decode(event[‘data’]).decode(‘utf-8’) </em>part to decode the part of the MESSAGE called data, and the <em>attributes_dict = event[‘attributes’] </em>part to get the dict we send in the attributes part.</p><p>How does it know that we want to send the MESSAGE to this specific Cloud Function? Because we gave the Cloud Function a topic, which is part of the Airflow Operator arguments. Whenever you now trigger this DAG (in this case every day at 11) all Cloud Functions with the corresponding topic will receive this MESSAGE.</p><p>After that MESSAGE has been send, you can see in your VERTEX AI pipelines tab that a job has been started. If that is the case than congrats, you did it ;)</p><p>A few concluding notes on this approach. The Airflow Operator will mark the task as completed once the MESSAGE is send, meaning it does not know when the actual Vertex AI pipeline actually finishes or whether it crashes. So whenever you have tasks after this pub/sub task which are dependend on the outcome of the pub/sub task, then this approach might not be the best for you. In this case, I recommend writing your own Airflow Operator with the python SDK that Google Cloud provides (called aiplatform).</p><p>I hope this article shed some light on how the Pub/Sub functionality in Google Cloud works, how the messages need to look like and how to trigger it via Airflow.</p><p>Cheers, Lasse</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6385597d873" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Tabular Augmentation for Regression Tasks]]></title>
            <link>https://medium.com/analytics-vidhya/deep-tabular-augmentation-for-regression-tasks-81c1dfdb41d4?source=rss-b9e15f1839f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/81c1dfdb41d4</guid>
            <category><![CDATA[regression]]></category>
            <category><![CDATA[variational-autoencoder]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Lasse Schmidt]]></dc:creator>
            <pubDate>Sat, 23 Apr 2022 14:57:53 GMT</pubDate>
            <atom:updated>2022-04-23T14:57:53.488Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_sL3j6q3SLdyXuhD" /><figcaption>Photo by <a href="https://unsplash.com/@timmossholder?utm_source=medium&amp;utm_medium=referral">Tim Mossholder</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So far I did show how to use Deep Learning for Data Augmentation with the idea in mind to create data for an underrepresented class in your dataset (<a href="https://medium.com/analytics-vidhya/smote-vs-deep-augmenter-testing-the-predictive-power-on-imbalanced-data-c85b34bd706b">here</a>, <a href="https://medium.com/analytics-vidhya/data-augmentation-for-tabular-data-f75c94398c3e">here</a> and <a href="https://medium.com/analytics-vidhya/comparison-randomforest-with-oversampling-vs-augmented-data-bd7fcb96ef0f">here</a>). However, the Deep Tabular Augmenter can be used for more than just that. In this blogpost I want to show you how you can use the technique to create data for regression tasks.</p><p>First, we need a dataset. Here, I use the infamous <a href="https://www.kaggle.com/code/sreenivasuluk/prediction-on-boston-house-prices">Boston Housing Dataset</a>. After loading the data, it looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h2rwew9ftPrYZddBq9gYAQ.png" /></figure><p>When looking at the data, we see that we have only 506 entries. This makes it a good example for showing how the Deep Tabular Augmenter works in cases like this. Unlike my other blogposts about the Deep Tabular Augmenter, we now want to use it to create data for the complete dataset. Usually, people use this dataset for predicting the MEDV, so the Median value of owner-occupied homes in $1000’s. Let’s first create train and test datasets:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/419308b8d782fa0070572f994d60977f/href">https://medium.com/media/419308b8d782fa0070572f994d60977f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H8nLRWY7XQJiMK2sH8KrLA.png" /></figure><p>Then, we’ll put the data into dataloaders, so our Deep Tabular Augmenter can do calculations on it. This time, we use a slightly different dataloader-function: dta.create_datasets_no_target_var. This is a convenience function for when we do not have a target-variable. Usually, for example in the fraud-detection case, we have a specific target vaiable at hand, for which we do not want to create fake data. This here is different, as we also want to create fake data for our (in later tasks used) MEDV variable.</p><p>Also, make sure to have at least version 0.5.0 of deep_tabular_augmentation, otherwise you won’t have this function. If not, just go:</p><blockquote>pip install deep_tabular_augmentation — upgrade</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e974d264f00da2eca292447d9dd21812/href">https://medium.com/media/e974d264f00da2eca292447d9dd21812/href</a></iframe><p>From now on, everything should be very familiar. Let’s start with the basic architecture of our model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a53df24579ebc3ccf78933e92f497723/href">https://medium.com/media/a53df24579ebc3ccf78933e92f497723/href</a></iframe><p>Put everything data related into the learner class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c63833807dee88ec055d68f6cb0f766/href">https://medium.com/media/8c63833807dee88ec055d68f6cb0f766/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-GhRmD1x-OuwKmcnU1J_aw.png" /></figure><p>Set up a Learning Rate schema of your choice and train the model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a06e864f75ad7f511bb02041b8eea0c5/href">https://medium.com/media/a06e864f75ad7f511bb02041b8eea0c5/href</a></iframe><p>Let’s have a look at the created data:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8ed490a8dd2c2fc7ade1786acb91829/href">https://medium.com/media/d8ed490a8dd2c2fc7ade1786acb91829/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NRJ8eGwUSudWZKPrG3elrA.png" /></figure><p>As RAD and CHAS are not continuous, let’s just round it to the nearest number.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59c952ba156ae6b111f2107e6fd777bd/href">https://medium.com/media/59c952ba156ae6b111f2107e6fd777bd/href</a></iframe><p>Now that we have that, let’s come to the evaluation part. Training is all fun and good, but we want to see some results. So let’s plot it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1UtDuHHdy4uYbv33cV3AJA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U1SA3xHsd5X8RVPK8_0kFw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hDt-d306NG0z46xlo_tgqQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zUkeMNKolPx6WNrqY6YrqQ.png" /></figure><p>I think that looks pretty awesome! So remember, whenever you have just not enough data for your model to train properly, you can try the Deep Tabular Augmenter to create enough fake data, so your model is actually able to learn something. I hope this helps and stay tuned for more!</p><p>If you have any questions or want anything added to the package, just ask me.</p><p>Lasse</p><p><em>Originally published at </em><a href="https://lschmiddey.github.io/fastpages_/2022/04/23/DataAugmentation_for_Regression_Tasks.ipynb.html"><em>https://lschmiddey.github.io</em></a><em> on April 23, 2022.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81c1dfdb41d4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/deep-tabular-augmentation-for-regression-tasks-81c1dfdb41d4">Deep Tabular Augmentation for Regression Tasks</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use GitHub Actions for creating docker images and pushing it to Docker Hub]]></title>
            <link>https://medium.com/analytics-vidhya/how-to-use-github-actions-for-creating-docker-images-and-pushing-it-to-docker-hub-a3dcda22a824?source=rss-b9e15f1839f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3dcda22a824</guid>
            <category><![CDATA[github-actions]]></category>
            <category><![CDATA[mlops]]></category>
            <dc:creator><![CDATA[Lasse Schmidt]]></dc:creator>
            <pubDate>Sun, 20 Mar 2022 18:15:09 GMT</pubDate>
            <atom:updated>2022-03-20T18:15:09.232Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QMt4t6QDHUr2GlUt" /><figcaption>Photo by <a href="https://unsplash.com/@synkevych?utm_source=medium&amp;utm_medium=referral">Roman Synkevych 🇺🇦</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>While the field of Machine Learning is more and more maturing, the operationalization side of this field is still in its infancy. This short blogpost should give you a small guide on how to use GitHub Actions to first CI your code and then further automize the creating of docker images and pushing these images to a container registry. In my case, I use Docker Hub for this.</p><p>First of, why should you care about this? In my experience, creating a prototype of a model which runs successfully takes way less time then putting this model then into production. This is why Machine Learning should embrace Docker and use it to package Scripts, which then could run anywhere. Also with the emergence of cloud computing this will evermore become the blueprint on how to deploy models, may it be for batch prediction or on edge devices.</p><p>Second, many Machine Learning projects are still lacking a sound Dev-architecture. By this I mean more often than not there are no automated linting checks and testing checks whenever you push code or merge pull requests. This blogpost should provide you with a really simple example project, which contains all of the points I just mentioned.</p><p>Let’s start by login into DockerHub and create a repository. In my case, I did create the “hello_world_test” repository:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pRd5iSNhloZUOQOhRZGKQw.png" /></figure><p>Next, create a GitHub repository. In this repository, go to “Settings” and then to “Secrets” and create two secrets: one names “DOCKER_USER” and the other one named “DOCKER_PASSWORD”. Give you respective DockerID and password as values to these secrets.</p><p>As this should be a kind of blueprint for Python-packages, let’s create a hello.py file and put it into the “src” folder:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5476eb813ff34f79e7d4eec0bf43e2a/href">https://medium.com/media/c5476eb813ff34f79e7d4eec0bf43e2a/href</a></iframe><p>This is just a simple example. You can think of this file to be the main.py file, the file you do want to execute. Let’s also write a simple test_hello.py file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1edb4e39ce9c96384dcf793e7499b98f/href">https://medium.com/media/1edb4e39ce9c96384dcf793e7499b98f/href</a></iframe><p>As I already mentioned in the beginning, I think many ML-projects are missing a proper testing. With GitHub actions we want all of this to automatically run whenever I do push something in my repository. A great way of defining what should be run is a Makefile, which works like a recipe. This is how the Makefile looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ebbf6c69053bf4b5df92d339cba4f9a/href">https://medium.com/media/3ebbf6c69053bf4b5df92d339cba4f9a/href</a></iframe><p>It’s really simple, we define three steps: installing, linting and testing. You run these commands using “make install” or “make lint”.</p><p>When it comes to our example project, we only need one more file, the Dockerfile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcbcc8ac56bd9b8cf85732097049250d/href">https://medium.com/media/bcbcc8ac56bd9b8cf85732097049250d/href</a></iframe><p>Again, this is sufficient enough for most Python-Scripts to just work. What this Dockerfile does is the following:</p><ul><li>it downloads a predefined python image from dockerhub, in this case with python version 3.8</li><li>it adds the VERSION as an argument, which we then use to automatically LABEL our image once we build it</li><li>we copy the requirements.txt file into that image, in a folder we call /src/</li><li>we set the working directory within that image to that src folder</li><li>we than pip install the requirements into our image</li><li>we then copy everything from our src-folder (from our repository) to the src-folder within our image</li><li>When we run our image, we tell the image that it should execute the hello.py file</li></ul><p>And that’s it. When it comes to packaging, we’re already done. The last step is to use GitHub Actions to automate the complete process:</p><ul><li>whenever we push, run linting, run testing</li><li>when these steps are successful, build the image from our Dockerfile</li><li>when this is successful, automatically push the image to our (Docker Hub) registry</li></ul><p>How is this done? Within GitHub, go to actions and create a new workflow. This will automatically create a main.yml file for you in your repository. The workflow should look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c887bd6ad0aed286c486b66cdffac8a4/href">https://medium.com/media/c887bd6ad0aed286c486b66cdffac8a4/href</a></iframe><p>It is quite simple, but yet incredibly powerful. Let me explain what happens here:</p><ul><li>“on: [push]” will run this whenever we push something to our repository</li><li>the build command will tell GitHub that the following steps should be executed. Where the build process should take place is defined via “runs-on”. I use the “ubuntu-latest” which already comes with python and docker, so I advise you to use this too. GitHub will create an instance for us, on which the steps we will define will then run</li><li>the first step we will do is to login into our DockerHub registry. This is where the above mentioned secrets come in. We use them as environment variables so the ubuntu-latest machine knows where to find them. Then we use the CLI command for docker to login with our user-name and password</li><li>we then define on which Python version we want to run our script. As I already mentioned, we want to run it with Python 3.8</li><li>next, our Makefile comes in handy. As we already defined what should be done when we call “make install” or “make lint”, we can easily pass these arguments here and they then automatically install the required packages (on the instance GitHub offers us), run the linting and run the tests. Only when this is successful we get to the next step</li><li>after these tests have passed, we will build our image</li><li>finally, we push our image to our registry</li></ul><p>And that’s it. This is how the GitHub Actions look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*pyfnXpmdVQC8dFOALh3YJg.png" /></figure><p>And that’s my registry on Docker Hub after the build job was completed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*agYk-kWOKxhKWXej3DWWXg.png" /></figure><p>Every machine in the world which has docker installed can now pull this image from my Docker Hub registry and simply do:</p><p><em>docker run — rm -d lasse1990/hello_world_test:0.0.1</em></p><p>And the outcome will be 2.</p><p>You can find the repository <a href="https://github.com/lschmiddey/MLOps_prototype">here</a>. As I said, almost all Python projects can make use of this simple technique. This makes your code first of all more stable, more professional and second with the created image your script now runs on every machine which does have Docker installed. If you push into a private registry, only users with access to that registry will be able to run your image. You can also take these images and deploy them on AWS/GCP or Azure.</p><p>I hope this small example did clarify GitHub Actions a bit and also made it more clear, why Docker and Docker images should be the way to go for your Machine Learning Projects.</p><p>Lasse</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3dcda22a824" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/how-to-use-github-actions-for-creating-docker-images-and-pushing-it-to-docker-hub-a3dcda22a824">How to use GitHub Actions for creating docker images and pushing it to Docker Hub</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SMOTE vs Deep Augmenter — testing the predictive power on imbalanced data]]></title>
            <link>https://medium.com/analytics-vidhya/smote-vs-deep-augmenter-testing-the-predictive-power-on-imbalanced-data-c85b34bd706b?source=rss-b9e15f1839f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c85b34bd706b</guid>
            <category><![CDATA[variational-autoencoder]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[smote]]></category>
            <category><![CDATA[random-forest]]></category>
            <dc:creator><![CDATA[Lasse Schmidt]]></dc:creator>
            <pubDate>Sun, 20 Feb 2022 13:45:19 GMT</pubDate>
            <atom:updated>2022-04-23T14:57:22.082Z</atom:updated>
            <content:encoded><![CDATA[<h3>SMOTE vs Deep Augmenter — testing the predictive power on imbalanced data</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Q4jJ0efSrq6W2WIv" /><figcaption>Photo by <a href="https://unsplash.com/@kazuend?utm_source=medium&amp;utm_medium=referral">kazuend</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this blog I want to test the Deep Learning Augmentation approach vs the popular <a href="https://arxiv.org/pdf/1106.1813.pdf">SMOTE</a> approach. For this I again use the <a href="https://www.kaggle.com/mlg-ulb/creditcardfraud">credit card fraud dataset</a>. This dataset is highly imbalanced meaning we have way less fraud cases than non-fraud cases. The goal is to train a RandomForest model to predict non-fraud vs fraud cases. For this I will use three approaches:</p><ul><li>assigning a higher weight to the fraud cases (in-built in the sklearn model for RandomForests)</li><li>using the SMOTE approach as implemented <a href="https://imbalanced-learn.org/stable/references/generated/imblearn.over_sampling.SMOTE.html">here</a></li><li>using the <a href="https://github.com/lschmiddey/deep_tabular_augmentation">Deep Augmenter</a></li></ul><p>How to make use of the Deep Augmenter package I already wrote <a href="https://lasse-schmidt90.medium.com/comparison-randomforest-with-oversampling-vs-augmented-data-bd7fcb96ef0f">here</a> and <a href="https://medium.com/analytics-vidhya/data-augmentation-for-tabular-data-f75c94398c3e">here</a>. In the credit card fraud dataset we have 199.032 non-fraud cases vs 332 fraud cases. So let’s prepare the Deep Augmenter first, loading the data, selecting only the data we want to build fake data for (in this case the fraud cases), splitting it into train vs test sets and putting them into dataloaders. We then built our model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/983e4a231f34216cda403a8cc0e2482b/href">https://medium.com/media/983e4a231f34216cda403a8cc0e2482b/href</a></iframe><p>After our model is trained we can now use it to create data of fraud cases:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff81aa4ca1a36eef100e21329cb86980/href">https://medium.com/media/ff81aa4ca1a36eef100e21329cb86980/href</a></iframe><p>One nice thing here is that we can add the sigma parameter per column. Sigma defines the standard deviation, we pass it as a list, and if we want some of the columns to have a specific standard deviation you can just put it in the list. I did use the standard deviation from the fraud cases but only 25% of it, as this seem to give the data enough “wiggle” room while still keeping the trained relations between the variables (check out my blogpost on the latent factors <a href="https://lasse-schmidt90.medium.com/understand-latent-factors-in-deep-tabular-augmentation-fd6d9e2c1307">here</a>).</p><p>Now we use the SMOTE package to create data. SMOTE just needs the (complete) trainset and target values for this trainset:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dea1ff954bcf693c55a517386fb326f2/href">https://medium.com/media/dea1ff954bcf693c55a517386fb326f2/href</a></iframe><p>So basically SMOTE creates as many entries of the minority class until we have the same amount of cases in both classes. We can do this with the DeepLearning Augmenter as well:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f1fff9a1a84ecf9e913cb560d1f6831/href">https://medium.com/media/8f1fff9a1a84ecf9e913cb560d1f6831/href</a></iframe><p>In both cases we now have 199.032 fraud cases simulated. While SMOTE creates these synthetic data in almost no-time, the DeepLearning Augmentation takes about a minute.</p><h3>Train the Random Forests</h3><p>We want to compare how the built-in class_weight functionality performs vs the new approach vs SMOTE. So we will build three trainsets: the original one, the one with additional data from SMOTE, and the one with additional data from DeepLearning Augmentation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c893efa0a62cc8dca1e49f20e0c3931b/href">https://medium.com/media/c893efa0a62cc8dca1e49f20e0c3931b/href</a></iframe><p>First, let’s train model on the original data while using the differences in class occurences as weights.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23a59cde5484ff38cf47dfa08b8907d7/href">https://medium.com/media/23a59cde5484ff38cf47dfa08b8907d7/href</a></iframe><p>We can also define our Random Forests for the SMOTE and Deep Augmenter approach:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40c00632c45bc83ba8b0ed72fe65347b/href">https://medium.com/media/40c00632c45bc83ba8b0ed72fe65347b/href</a></iframe><p>Let’s run all of these models:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfd23aa4d1e8aa011ca88a6aee0e8fc2/href">https://medium.com/media/bfd23aa4d1e8aa011ca88a6aee0e8fc2/href</a></iframe><p>Then finally let’s compare the results. First, the class_weight approach:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*dDqB7qvIjfUMipWFEv26ag.png" /></figure><p>Second, the SMOTE result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*oebuxQqZj0IhhB0_2HVI6w.png" /></figure><p>Lastly, the Deep Augmenter result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*GDbWrzilXCAEJ8RiGJjb4w.png" /></figure><p>We see quite huge differences between the three approaches. Simply attaching a higher weight to the fraud-class didn’t help at all, we were only able to identify 4 fraud cases correctly. The SMOTE approach lead to way more identified fraud cases, it found 125 cases out of 136. This leads to an recall of 0.92. However, this comes at a cost: we have astonishing 1283 missclassified fraud cases, leading to a precision in the fraud case of 0.09. The Deep Learning Augmentation correctly predicted 107 out of the 136 cases while only misclassifying 58 cases — this leads to a precision of 0.65 in the fraud case.</p><p>To conclude, I think this blog was able to show the merits of Deep Learning Augmentation. While increasing the correctly identified fraud cases we were also able to keep a high precision in this case, meaning we only have a few misclassified cases. While SMOTE was able to correctly identify a few more fraud cases it did also create a huge amount of misclassified fraud cases which might be costly when it comes to resource allocation.</p><p>If you have any questions or want anything added to the package, just ask me.</p><p>Lasse</p><p><em>Originally published at </em><a href="https://lschmiddey.github.io/fastpages_/2022/02/19/DataAugmentation_vs_SMOTE.html"><em>https://lschmiddey.github.io</em></a><em> on February 19, 2022.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c85b34bd706b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/smote-vs-deep-augmenter-testing-the-predictive-power-on-imbalanced-data-c85b34bd706b">SMOTE vs Deep Augmenter — testing the predictive power on imbalanced data</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understand Latent Factors in Deep Tabular Augmentation]]></title>
            <link>https://medium.com/analytics-vidhya/understand-latent-factors-in-deep-tabular-augmentation-fd6d9e2c1307?source=rss-b9e15f1839f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd6d9e2c1307</guid>
            <dc:creator><![CDATA[Lasse Schmidt]]></dc:creator>
            <pubDate>Sun, 06 Feb 2022 14:30:00 GMT</pubDate>
            <atom:updated>2022-02-25T16:07:07.359Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2aT-kI5EnQmwI-JZ" /><figcaption>Photo by <a href="https://unsplash.com/@lukechesser?utm_source=medium&amp;utm_medium=referral">Luke Chesser</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This blogpost should give an overview of how different amount of latent factors affects the data created by the <a href="https://github.com/lschmiddey/deep_tabular_augmentation">Deep Tabular Augmenter</a>. If you want to learn more about the general idea of deep tabular augmentation, see these posts <a href="https://medium.com/analytics-vidhya/tabular-data-augmentation-with-deep-learning-7270584e7ff">here </a>and <a href="https://medium.com/analytics-vidhya/data-augmentation-for-tabular-data-f75c94398c3e">here</a>.</p><p>This blogpost is based on version 0.4.0 of deep_tabular_augmentation. Again, we use the <a href="https://www.kaggle.com/mlg-ulb/creditcardfraud">credit-card fraud dataset</a> from kaggle.</p><p>The deep_tabular_augmentation works on the simple idea, that we want to keep the data in a dedicated class (which we call the Learner) together with the model. The data has to come as a dataloader object, which I store in the DataBunch class. In it are the dataloaders for the training and test data. The runner class then defines the flow.</p><p>We first scale the data and only keep the data of the class we want to augment. As mentioned, I then put the train and testloader into a class called DataBunch, which is just a container for the data. You can easily create your own dataloaders and put them in a DataBunch. All of the aforementioned actions steps you can already find in the blogposts mentioned above.</p><p>To make use of the deep_data_augmentation, we need to specify the input shape (so basically how many variables are in the dataset), the column name of the target-class we want to augment and the corresponding number, and lastly the column names of the input variables.</p><p>Then, wen can define whatever model architecture we would like to have. We just pass it as a list into the model. We can also define how many latent dimension we would like to add to our Autoencoder. As I want to compare the results of different latent factors, I will build four models only different in how many latent factors we set up.</p><p>Also, the model allows to control the variance you want to add per each column. If desired, you can add as many variance so that the variance of the real data matches the variance of the designed data. However, this is most of the time not desired, since this adds so much noise that the model will not support you as good as it can when it comes to predictions. I recommend using around 10%-25% of the real standard deviance of the data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/841ef22b5f352dfa14ad8915a8ba88ba/href">https://medium.com/media/841ef22b5f352dfa14ad8915a8ba88ba/href</a></iframe><p>Let’s train all four models and predict fake data from each of them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1dbd2a60050006fd8a9cd9f0ca83469d/href">https://medium.com/media/1dbd2a60050006fd8a9cd9f0ca83469d/href</a></iframe><p>We repeat this with each of the four different models we just created. Next, we can compare how the models differ on a graphical level. Therefore, we first look at the real data divided by Class=0 and Class=1 (which we want to augment), then compare the results with the augmented data.</p><figure><img alt="V1 vs V2" src="https://cdn-images-1.medium.com/max/1024/1*MjtIzvBZ6JiIvatoYu57Iw.png" /></figure><figure><img alt="V2 vs V3" src="https://cdn-images-1.medium.com/max/1024/1*rZZVVDvX9h1r_aAJ0N99Mw.png" /></figure><figure><img alt="V10 vs V6" src="https://cdn-images-1.medium.com/max/1024/1*V8ho896yiGh4czIsHsrG-A.png" /></figure><p>These results look really promising. Also, it seems like when comparing 2 variables it does not really matter how many latent factors you choose although 30 latent factors seem to provide the most compelling results. What if we have a look at 3 different variables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6x5kogYKw_qbAn6i1FqNog.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FHtsqyRsMdj5jP58DI52Yw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eYZwqSJf6MahTRq3uvYOEw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mwJiTdAQRwXKUNo2ICrlNw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gLbM4eIHSMLTonmLEuJ32g.png" /></figure><p>When comparing the 3D plots it becomes more clearly that fewer latent factors seem to further split the data apart. The more latent factors the better it seems the relations between the variables can be better captured.</p><p>If you have any questions or want anything added to the package, just ask me.</p><p>Lasse</p><p><em>Originally published at </em><a href="https://lschmiddey.github.io/fastpages_/2022/02/06/Understand-Latent-Factors.html"><em>https://lschmiddey.github.io</em></a><em> on February 6, 2022.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd6d9e2c1307" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/understand-latent-factors-in-deep-tabular-augmentation-fd6d9e2c1307">Understand Latent Factors in Deep Tabular Augmentation</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Comparison — RandomForest with Oversampling vs Augmented Data]]></title>
            <link>https://medium.com/analytics-vidhya/comparison-randomforest-with-oversampling-vs-augmented-data-bd7fcb96ef0f?source=rss-b9e15f1839f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd7fcb96ef0f</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[random-forest]]></category>
            <category><![CDATA[autoencoder]]></category>
            <category><![CDATA[variational-autoencoder]]></category>
            <dc:creator><![CDATA[Lasse Schmidt]]></dc:creator>
            <pubDate>Wed, 26 Jan 2022 20:49:21 GMT</pubDate>
            <atom:updated>2022-02-25T16:21:57.032Z</atom:updated>
            <content:encoded><![CDATA[<h3>Comparison — RandomForest with Oversampling vs Augmented Data</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PPaDJSZHcNkKMFmZ" /><figcaption>Photo by <a href="https://unsplash.com/@lucabravo?utm_source=medium&amp;utm_medium=referral">Luca Bravo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this blog I’d like to show the difference deep tabular augmentation can have when training a Random Forest on a highly biased data base. In this case, we have a look at creditcard fraud, where fraud itself is is way less represented than non-fraud. The dataset is available <a href="https://www.kaggle.com/mlg-ulb/creditcardfraud">here</a>.</p><p>Let’s have a look of how many more non-frauf cases we have compared to fraud cases:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e54485f55f63a971961e1b0c8fb55cfa/href">https://medium.com/media/e54485f55f63a971961e1b0c8fb55cfa/href</a></iframe><p>Overall, we have 284.807 rows, of which are 283.823 non-fraud cases. In order to make use of the deep tabular augmentation we need to scale the data and then use only those cases, in which class we are interested in, in this case “Class” is equal to 1.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efff2a05c2197934ddecee831af11707/href">https://medium.com/media/efff2a05c2197934ddecee831af11707/href</a></iframe><p>For our model to work we need to put our data in a DataLoader (here I use the DataBunch Class from deep data augmentation).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/abbc9f62931c2658bf86896c3cea0676/href">https://medium.com/media/abbc9f62931c2658bf86896c3cea0676/href</a></iframe><p>Now we’re already good to go. We can define our Variational Encoder Architecture (here: 50 –12–12–5–12–12–50) and then use the LearningRate Finder to tell us the best Learning rate:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c66e6d0815d08875e6cebecd2c4a10d/href">https://medium.com/media/3c66e6d0815d08875e6cebecd2c4a10d/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*fz0UJ5-LUwuQsSw5gi7T8Q.png" /></figure><p>We set up a desirable learning rate and scheduler for our learning rate:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd3eaa751b4f19c9ccffb3378b2b6a3b/href">https://medium.com/media/fd3eaa751b4f19c9ccffb3378b2b6a3b/href</a></iframe><p>Now, let’s train the model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d13d529dd311f67f911b1f5543e55080/href">https://medium.com/media/d13d529dd311f67f911b1f5543e55080/href</a></iframe><p>Let’s see how the created data looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50f9a0ef688cbcff58ba041955b4d499/href">https://medium.com/media/50f9a0ef688cbcff58ba041955b4d499/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*JiN2zg6vxElT3elYWncA9g.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4dd96ac385d129a9a23a2100f05d8ca4/href">https://medium.com/media/4dd96ac385d129a9a23a2100f05d8ca4/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/869/1*NBEgWcJCvo5byFGYpFnqPg.png" /></figure><h3>Train Random Forest</h3><p>We want to compare how the built-in class_weight functionality performs vs the new approach (spoiler: if you do not use any weights the RandomForest will always predict 0). Hence, we create three dataframes: the original, the original appended with fake_data, the original appended with fake data with noise.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/102a6b4a2b666035c9fd96faa0d2376e/href">https://medium.com/media/102a6b4a2b666035c9fd96faa0d2376e/href</a></iframe><p>To make things easier to understand, let’s define the datasets on which to train and on which to assess the results:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/676db435b529d33caf11fbff8900b1d1/href">https://medium.com/media/676db435b529d33caf11fbff8900b1d1/href</a></iframe><p>First, let’s train model on the original data while using the differences in class occurences as weights.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0674a7adf9a1c61ef6465d9b90b9b773/href">https://medium.com/media/0674a7adf9a1c61ef6465d9b90b9b773/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c23b7e0b6996d502e213839108d46758/href">https://medium.com/media/c23b7e0b6996d502e213839108d46758/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/172/1*mC5c8C63Sw07jVzpviIakg.png" /></figure><p>Then, we use the augmented dataframe:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b61f6cddd776e56dd65db5a665f9f895/href">https://medium.com/media/b61f6cddd776e56dd65db5a665f9f895/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/163/1*jP9JNSEQ4WOO3Fpx91tX7Q.png" /></figure><p>Wow, I think that is quite astonishing. We managed to highly increase the number of fraud cases we are able to detect. Moreover, we achieved these results without any finetuning of the model architecture and simply using the default structure of the VAE.</p><p>I hope this blog shed some light on why using this approach on highly biased data is worth a shot trying.</p><p>Also, if you want to try it for yourself, I made this a package you can pip install and just use. Here is the <a href="https://github.com/lschmiddey/deep_tabular_augmentation">repo</a>.</p><p>Lasse</p><p><em>Originally published at </em><a href="https://lschmiddey.github.io/fastpages_/2021/04/10/DeepLearning_TabularDataAugmentation.html"><em>https://lschmiddey.github.io</em></a><em> on January 24, 2021.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd7fcb96ef0f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/comparison-randomforest-with-oversampling-vs-augmented-data-bd7fcb96ef0f">Comparison — RandomForest with Oversampling vs Augmented Data</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Augmentation for Tabular Data]]></title>
            <link>https://medium.com/analytics-vidhya/data-augmentation-for-tabular-data-f75c94398c3e?source=rss-b9e15f1839f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/f75c94398c3e</guid>
            <category><![CDATA[autoencoder]]></category>
            <category><![CDATA[data-augmentation]]></category>
            <category><![CDATA[variational-autoencoder]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Lasse Schmidt]]></dc:creator>
            <pubDate>Sun, 07 Nov 2021 14:36:10 GMT</pubDate>
            <atom:updated>2021-11-10T04:34:29.109Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dcbdcCTbExjKNg_g" /><figcaption>Photo by <a href="https://unsplash.com/@joshstyle?utm_source=medium&amp;utm_medium=referral">JOSHUA COLEMAN</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The main idea of why and how to use Deep Learning to create data augmentation on tabular data is decribed in <a href="https://medium.com/analytics-vidhya/tabular-data-augmentation-with-deep-learning-7270584e7ff">my previous blogpost</a> on this topic. Since then quite some time has passed and I decided to rewrite the Code so it is easier to use. In this blogpost I want to show you how to use the new approach, how flexible it is regarding the model you want to use and how you can use your own custom Callbacks to better see what the model does in the background. Some of these features I already built-in and here’s how to use them.</p><p>Again, we use the credit-card fraud dataset from kaggle.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8c95f7f8851e99e77f4a6913687f314/href">https://medium.com/media/d8c95f7f8851e99e77f4a6913687f314/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*UYHyuRmcCG_rd4ts_1-YJA.png" /></figure><p>The deep_tabular_augmentation works on the simple idea, that we want to keep the data in a dedicated class (which we call the Learner) together with the model. The data has to come as a dataloader object, which I store in the DataBunch class. In it are the dataloaders for the training and test data. The runner class then defines the flow.</p><p>We first scale the data and only keep the data of the class we want to augment:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*qVtokLjVSODVa5JO0J0QNw.png" /></figure><p>As mentioned, I then put the train and testloader into a class called DataBunch, which is just a container for the data. You can easily create your own dataloaders and put them in a DataBunch.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/165c88eb302593ff306df99d61f61905/href">https://medium.com/media/165c88eb302593ff306df99d61f61905/href</a></iframe><p>To make use of the deep_data_augmentation, we need to specify the input shape (so basically how many variables are in the dataset), the column name of the target-class we want to augment and the corresponding number, and lastly the column names of the input variables.</p><p>Then, wen can define whatever model architecture we would like to have. We just pass it as a list into the model. We can also define how many latent dimension we would like to add to our Autoencoder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b41b31989c54fb12ec0f7042e18f8f1c/href">https://medium.com/media/b41b31989c54fb12ec0f7042e18f8f1c/href</a></iframe><p>The runner class then provides us with the flow of the data and also let’s you add Callbacks. One of the built-in Classbacks is a Learning Rate Finder, and this is how you can use it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/542880edfeb730ccdff891525995639a/href">https://medium.com/media/542880edfeb730ccdff891525995639a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*cGAyJ_ZvJKWIxJ6I2Mwvfw.png" /></figure><p>We can also use the callbacks to create a learning rate scheduler. Here is an example: use 30% of the budget to go from 0.01 to 0.1 following a cosine, then the last 70% of the budget to go from 0.1 to 0.01, still following a cosine.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f3a1e93c9181acc9c72bec6a705b6e6/href">https://medium.com/media/9f3a1e93c9181acc9c72bec6a705b6e6/href</a></iframe><p>Now we can train our Autoencoder. We want to keep track of the loss, we want to be able to do some loss plotting and we want to add our learning-rate scheduling:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94bafb63c5630d818a846166f82a3423/href">https://medium.com/media/94bafb63c5630d818a846166f82a3423/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*WXZD-ElveXI-5TKt1mJ41A.png" /></figure><p>We can have a look at the training loss:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*Y1yDU-kf16QkRGGrMTg_rQ.png" /></figure><p>And the learnig rate over the epochs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*medi2MgEFWrVD_5aH5b0bA.png" /></figure><p>ou can create any kind of Callbacks you want and pass them to the training. Within the runner I created some hooks after which you can directly link your Callback, for example <em>begin_batch</em>, <em>begin_epoch</em>, <em>after_pred</em>, <em>after_fit</em> and quite a few more. When you create your Callback you can directly refer to these to pinpoint where your Callback should do specific tasks. This is how the LossTracker looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c18337301282a60cf1d91affbb06b02/href">https://medium.com/media/5c18337301282a60cf1d91affbb06b02/href</a></iframe><p>When beginning the training, I start with an empty list, and after each batch I add the loss. After each epoch then I print the loss.</p><p>Moreover, the runner also provides you with the possibility to create augmented data by the trained model. You can specify how many samples of the specified class you want, and you can also add some noise to it, which I found to create better input for later use.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bcaab1cdf50dfeb0e29089dfb28b5a7/href">https://medium.com/media/2bcaab1cdf50dfeb0e29089dfb28b5a7/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*1QWYc08uINJsOsgRk2umsA.png" /></figure><p>Let’s see how our model does when it comes to replicating the fraud cases. First we plot V1 against V2 on the non-fraud cases, then we do the same for the fraud cases:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/881/1*m1WkHQDhlhvk5lK67WLfQQ.png" /></figure><p>They look quite different. Let’s see how our fake data does:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*0wqMRURM-Z8hsimcRylt6Q.png" /></figure><p>This looks actually quite amazing. If you have any questions or want anything added to the package, just ask me.</p><p>Lasse</p><p><em>Originally published at </em><a href="https://lschmiddey.github.io/fastpages_/2021/04/10/DeepLearning_TabularDataAugmentation.html"><em>https://lschmiddey.github.io</em></a><em> on November 07, 2021.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f75c94398c3e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/data-augmentation-for-tabular-data-f75c94398c3e">Data Augmentation for Tabular Data</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Software Engineering to improve Data Science Code]]></title>
            <link>https://medium.com/analytics-vidhya/how-to-use-software-engineering-to-improve-data-science-code-b2b131cbc9d0?source=rss-b9e15f1839f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2b131cbc9d0</guid>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[software-principles]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Lasse Schmidt]]></dc:creator>
            <pubDate>Wed, 27 Oct 2021 20:10:02 GMT</pubDate>
            <atom:updated>2021-11-10T04:51:32.998Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kFm_6Af47RNKeXpx" /><figcaption>Photo by <a href="https://unsplash.com/@karlp?utm_source=medium&amp;utm_medium=referral">Karl Pawlowicz</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Why Data Science has to stop producing Spaghetti Code</h3><p>As a common Data Scientist, I know the pain. Usually, what we do is pretty straight forward processwise, but somehow this almost always end up with a code that has become so complex, it’s hard to keep track. Moreover, most Data Scientist do not really care about Software Engineering and use functions and classes all over the place, without any proper thinking about it first. Admittedly, I also did code like that.</p><p>For some time now I wanted to change this. After reading some books and articles about Software Engineering and watching a ton of YouTube tutorials on it (thanks so much internet!) I came up with a few ideas how every Data Scientist can and should improve her code. Let’s start with the basic design pattern I found helpful for each of my Data Science projects.</p><h3>The Information Expert Principle</h3><p>This principle is pretty straightforward, but when applying it from the start of your project it will help you a lot writing cleaner and more beautiful code. It means that the design of your software (which, in the end, is what we are writing) should follow the data pipeline. What does this mean in action? Let me give you a brief example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d157085340edbdd6bdecd72cc3a1c67/href">https://medium.com/media/9d157085340edbdd6bdecd72cc3a1c67/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*_n7EPAGAT2N61L1TI7J3zQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*RY_0NUjMc47q7Hk1rWnWsw.png" /></figure><p>What happens here is that everything connected to the data is now stored in the BaseData class. Each and every function within this class needs the data where it is and gets the data directly, so there is no jumping to other objects or methods to make this happen. So my first take away: keep your data together!</p><p>Next I will use this data to pass it on to the next point within our pipeline. Just to keep you informed, what I do here is actually a refactoring of an <a href="https://medium.com/analytics-vidhya/time-series-classification-with-convolutions-ed5cb33b1e3b">earlier project of mine</a>, where I will use a convolutional neural network in PyTorch to classify a timeseries. So in order to use PyTorches power, the data needs to be in so called Dataloaders.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ffc161132b5badd59538b1dabed31a5/href">https://medium.com/media/3ffc161132b5badd59538b1dabed31a5/href</a></iframe><p>If you want to have a closer look at what this exactly does, just go inside the dataloaders.py file. Basically it splits the train_data into train and validation and then creates a PyTorch dataset from it. This then can be put into a dataloader. Now I could have used three different dataloaders, but again, I want to keep my data together, which is why I created a class called DataBunch (naming and idea stolen from fastai), where I have all of my data in one place.</p><p>Furthermore, realize that I use one, and only one config file. I often see Code with different kind of constants aka configurations all over different files, which makes it extremely hard to later on debug the code or change settings. So please keep your constants in one place.</p><p>Now let’s have a closer how we can use a clever way of building models in a more flexible way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54f720e0e73abe8823030b3ee8402729/href">https://medium.com/media/54f720e0e73abe8823030b3ee8402729/href</a></iframe><p>First thing to notice here is that I combined the basic structure of a Conv1d layer in one function. Every Conv1d layer should have a BatchNorm followed by a ReLU. You can adjust this just as you like. Then, in the next step I created a function which basically makes the Conv1d layer repetitive, given a list of arguments you provide. So based on that list of arguments, you can have 1, 2, 3, 4 or as many layer as you would like. This then in turn is then used in the Classifier class, which is our main model. The convolutional part, here called <em>self.raw</em> is then build given on what we specify how the architecture should look like. The rest of the model I kept rather inflexible, which you could if you want also change. But let’s see this in action:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88a107974641b4d3b24074c9392c0e2a/href">https://medium.com/media/88a107974641b4d3b24074c9392c0e2a/href</a></iframe><p>Everything written in all caps is actually defined within the config file, but for seeing what’s going on here I just put them into this code snippet. So if I wanted to add another layer, I could just go into my config file and change that, so that I have three layers with different kernels and strides. When doing experiments, you can just provide different settings in your config and then use them for your second model. Do not underestimate the idea of having parameters in one place.</p><p>The next big part is directly connected to the Information Expert principle. Let’s start with the Learner, a simple idea based on that principle. When training the model, we need another class which keeps all the information needed, the Learner. And what information does the Learner need? The data, the model, the loss function and the optimizer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/292df3c27b9468128322d6a12e41efa1/href">https://medium.com/media/292df3c27b9468128322d6a12e41efa1/href</a></iframe><p>Next, we need to build the pipeline for our process, let’s call it runner. We pass in the Learner, which is responsible for the data and modelling part. The runner itself only guides the data flow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67168d05e66cd335f45e875691a31f02/href">https://medium.com/media/67168d05e66cd335f45e875691a31f02/href</a></iframe><p>The last idea I want every Data Scientist to know about are Callbacks. Basically that means we’re calling a different function and make use of their behaviour. To be able to clearly pinpoint where the Callback should be applied, I added things like <em>self(‘after_epoch’)</em>. This then let’s us easily create our own Callbacks which we can use exactly where we want to:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a42eb90433f18ef2bf6f2992a257b71/href">https://medium.com/media/6a42eb90433f18ef2bf6f2992a257b71/href</a></iframe><p>After creating the base Callback class, each and every Callback we want to use inherits from it. Take the LR_Find Callback: This will be applied after a batch begins and after each step. These we have defined within our Runner class. Now we can use these Callbacks like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b67c892478311968b6baf4aa5daeee5d/href">https://medium.com/media/b67c892478311968b6baf4aa5daeee5d/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*2KL3Cr4PsKSagGYoGfGbGg.png" /></figure><p>We can add all sorts of Callbacks, we can also trigger a Tensorboard run as a Callback:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41c994596bf7410699a811ab34ff3bff/href">https://medium.com/media/41c994596bf7410699a811ab34ff3bff/href</a></iframe><p>This now has triggered a <em>run</em> for Tensorboard and saved it in this folder. We can now have a look at this by typing <em>tensorboard — logdir=runs</em> in the Terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3piiC8GxzXkLBSLQinFR7w.png" /></figure><p>And that’s it for now. I hope the idea of Information Expert Principle is now a bit more familiar, and I could give you some ideas of how to refactor your existing Data Science projects. If you want to have a closer look at the repo, here it is: <a href="https://github.com/lschmiddey/data-science-software-principles">Repo</a>.</p><p>Lasse</p><p><em>Originally published at </em><a href="https://lschmiddey.github.io/fastpages_/2021/04/10/DeepLearning_TabularDataAugmentation.html"><em>https://lschmiddey.github.io</em></a><em> on October 27th, 2021.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2b131cbc9d0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/how-to-use-software-engineering-to-improve-data-science-code-b2b131cbc9d0">How to use Software Engineering to improve Data Science Code</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why it makes a difference how to standardize training and test set]]></title>
            <link>https://medium.com/analytics-vidhya/why-it-makes-a-difference-how-to-standardize-training-and-test-set-e95bf350bed3?source=rss-b9e15f1839f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/e95bf350bed3</guid>
            <category><![CDATA[standardizing]]></category>
            <category><![CDATA[sklearn]]></category>
            <category><![CDATA[train-test-split]]></category>
            <dc:creator><![CDATA[Lasse Schmidt]]></dc:creator>
            <pubDate>Mon, 25 Oct 2021 19:20:34 GMT</pubDate>
            <atom:updated>2021-11-10T04:52:49.357Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*brBn5-PcThNA9PxI" /><figcaption>Photo by <a href="https://unsplash.com/@callmefred?utm_source=medium&amp;utm_medium=referral">Call Me Fred</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this blogpost I want to briefly show why it is important to correctly scale you train and test data. Although I think most machine learning practicioners automatically avoid the fallacy of not standardizing the test data with the learned scaler from the trainset, I think many practitioners do not know exactly why. Here, I will give a concrete example of why you need to use the scaler from the trainset for the testset as well.</p><p>Let’s first create some dummy data. For example, we can assume the following are 3 different users, decribes by three variables. We also create the targets, which for example we can think of different clusters each user belongs to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*sy_nVBepCwZexUrJEZPIGw.png" /></figure><p>You see that in the <em>test_data</em> user 1 and user 3 are exactly alike. This is on purpose. Let’s create two standard scalers, meaning we will substract the mean and divide by the variance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*yBbKxJzQnvSAOjop27VYyA.png" /></figure><p>You can already see the difference between the two approaches. The first two <em>scaled_data</em> are built with the fit_transform method from the StandardScaler, while the last approach uses the “trained” scaler from the trainset to scale the test_dataset. Let’s have a look at the different means and variance:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*4d1mHggvMbU8SjN0r6X4lg.png" /></figure><p>And this is how the data looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*0QD57qeMi0tBf_2-TH8SHQ.png" /></figure><p>From first glance, the correctly scaled test data looks wrong, simply because the numbers seem so far off. However, let’s have a look what happens when we fit a simple Linear Regression on our trainset and make predictions on the testsets:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*cy4C2dnjFegNwBmZvXQwLw.png" /></figure><p>Remember, in our testset, we expect user 1 and user 3 to be classified as 2 and 0 respectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/881/1*0RBzEb88BV9CjPgeeUhsJw.png" /></figure><p>But this did not happen at all here. We see that neither the first user nor the third user are classified correctly, even though they are exactly the same. Let’s see what the results are, when using the scaler from our trainset:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*w1bVXzWIei-HZjJb2xQ3yQ.png" /></figure><p>This time, the Linear Model correctly classified user 1 and user 3. So you see what a difference wrongly used scaling makes. So this is why we use the mean and variance from the trainset to standardize the testset.</p><p>Lasse</p><p><em>Originally published at </em><a href="https://lschmiddey.github.io/fastpages_/2021/04/10/DeepLearning_TabularDataAugmentation.html"><em>https://lschmiddey.github.io</em></a><em> on October 25th, 2021.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e95bf350bed3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/why-it-makes-a-difference-how-to-standardize-training-and-test-set-e95bf350bed3">Why it makes a difference how to standardize training and test set</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transform a PyTorch model to onnx]]></title>
            <link>https://medium.com/analytics-vidhya/transform-a-pytorch-model-to-onnx-90e581258997?source=rss-b9e15f1839f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/90e581258997</guid>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[convolution-neural-net]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[onnx]]></category>
            <category><![CDATA[convolutional-network]]></category>
            <dc:creator><![CDATA[Lasse Schmidt]]></dc:creator>
            <pubDate>Sat, 28 Aug 2021 09:58:57 GMT</pubDate>
            <atom:updated>2021-08-31T04:24:58.527Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RmbN2a7cJwv9D0H6" /><figcaption>Photo by <a href="https://unsplash.com/@andy_brunner?utm_source=medium&amp;utm_medium=referral">Andy Brunner</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this tutorial, I want to show how easily you can transform a PyTorch model to the onnx format. But first of all, why would you want to do that? There are several reasons in my experience when this might come in handy for you:</p><ul><li>your development area is different from your production area. In my experience, this is often the case. When we have huge data on which to do inference in batches, it is often not the best idea to first shift your data from one source towards your python-server, do the inference there and then put the data back again. onnx can make you use your Deep Learning models directly where your data is stored which is a huge benefit.</li><li>onnx runs on Java via their API, meaning you can train your model with Python but deploy it on Java. This makes a huge difference because you can then deploy your model for example directly in your cluster.</li><li>onnx supports all of the popular Deep Learning frameworks like PyTorch, TensorFlow or Caffe (and a lot more). That means in teams where some feel more confortable using TensorFlow and other feel more comfortable using PyTorch, the deployment process can still be the same.</li></ul><p>In a <a href="https://medium.com/analytics-vidhya/time-series-classification-with-convolutions-ed5cb33b1e3b">previous tutorial</a> I already showed you how powerful convolutional neural networks are when it comes to timeseries. I used the Italy Power demand dataset to do a classification task, which was to distinguish which day stems from. To show you how to deploy your model I will use this model trained on CPU with PyTorch and show you how then to save it as an onnx model. The way onnx works is that it first needs a “sample” of one example data point (tensor) with which we once do the inference part. So let’s first load the data and just take the first example for which we want to make a prediction.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ee39a0b79e84d8bab5866fc49a872a1/href">https://medium.com/media/7ee39a0b79e84d8bab5866fc49a872a1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*RRJvateszD3GLN0wkK9P0w.png" /></figure><p>Just as a reminder, this numpy array represents the power usage within 24 hours, and we tried to predict whether this day is from Oct to March (inclusive) or from April to September.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*ROMDSLaskMAM87DXTsGFDw.png" /></figure><p>We now transform this numpy array to a PyTorch tensor. The only thing we need to keep in mind from building our model is that the tensor has to follow a certain shape. Right now this shape looks like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*qAZMDU6LhKjM4M27xUINKw.png" /></figure><p>However, the model expects the shape like so: [batch_size, no_of_variables, no_of_observations]. In our case, we want the batch_size to be 1, we only have 1 variable over which we want to predict (power usage) and we have 24 observations, one for each hour. Also, we need to make the tensor of type float in this case, because when training the model we used the float type as input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*b7YwvDSkMnTmxoUCs_zFJA.png" /></figure><p>Now we need to reload our model. Best practice for saving models is to only save the state_dict(). This however means when realoding the model, we first need to define the “skeleton” of the model if you will, which will then be updated by the corresponding weights and biases from the saved state_dict(). Again, if you haven’t read the tutorial on how to build the model, I highly recommend to read that again.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cdb363281fdd00b27b755187c1303d0/href">https://medium.com/media/2cdb363281fdd00b27b755187c1303d0/href</a></iframe><p>Remember, we only have one variable with which we predict one of two classes (either 0 or 1).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*0oR1-uwFhoE6fvb5KaScXg.png" /></figure><p>We’re already almost done. Last thing missing is the inference part. To do so, we first have to put the mode in evaluation mode (otherwise it expects the target variable as an input to the model as well) and we also for faster inference later on tell the model not to calculate the gradients each time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*VvpcwXelfN-4WgH05kt_gQ.png" /></figure><p>Now we’re ready to save our model as a onnx model. Keep in mind that any changes done to the output (like applying a sigmoid function for example) will not be saved in the onnx model. Later operations then need to be done where you apply the model, for example when using it in a Java set up you need to use the Java functionality for sigmoid.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c53d52ac2a957856e21a4b9b52fbacb/href">https://medium.com/media/4c53d52ac2a957856e21a4b9b52fbacb/href</a></iframe><p>And that’s it! We now have saved our model with onnx, ready to be deployed on other frameworks directly. In the next tutorial, I want to show you how to use this onnx model and make it run on Java.</p><p>Lasse</p><p><em>Originally published at </em><a href="https://lschmiddey.github.io/fastpages_/2021/04/10/DeepLearning_TabularDataAugmentation.html"><em>https://lschmiddey.github.io</em></a><em> on August 28, 2021.<br>Git Repo: </em><a href="https://github.com/lschmiddey/pytorch_to_onnx">https://github.com/lschmiddey/pytorch_to_onnx</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90e581258997" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/transform-a-pytorch-model-to-onnx-90e581258997">Transform a PyTorch model to onnx</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>