<?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 Bénédicte Rallet on Medium]]></title>
        <description><![CDATA[Stories by Bénédicte Rallet on Medium]]></description>
        <link>https://medium.com/@benedicterallet?source=rss-a7ba74bf1e7d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*epV1eXrjfu5Niufk9ojuOQ.png</url>
            <title>Stories by Bénédicte Rallet on Medium</title>
            <link>https://medium.com/@benedicterallet?source=rss-a7ba74bf1e7d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 12:49:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@benedicterallet/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[Sport Analytics — NBA 2K ratings prediction]]></title>
            <link>https://medium.com/data-science/sport-analytics-nba-2k-ratings-prediction-b7b72e2e72eb?source=rss-a7ba74bf1e7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7b72e2e72eb</guid>
            <category><![CDATA[sports]]></category>
            <category><![CDATA[basketball]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[k2]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Bénédicte Rallet]]></dc:creator>
            <pubDate>Mon, 04 Jan 2021 17:27:24 GMT</pubDate>
            <atom:updated>2021-01-10T14:52:59.822Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sport Analytics — NBA 2K ratings prediction</h3><h4>What will be next 2K22 ratings? Are some teams over-rated by the editor? Which statistics impact the most ratings? Let’s find out these answers.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1VKXlsfk3eBCgsXr" /><figcaption>Photo by <a href="https://unsplash.com/@neonbrand?utm_source=medium&amp;utm_medium=referral">NeONBRAND</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As a Data Scientist passionate about sport, especially basketball, I am writing a series of articles related to <strong>Sport Analytics</strong>. It will go from classic regression or classification problems to more advanced computer vision applications.</p><p><strong>NBA 2K</strong> is a series of basketball sport simulation video games developed since 1999 with annual release. The latest edition is 2K21, issued on September 4, 2020. The video games are now published by 2K Sports.</p><p>In each release, all active players in the NBA and some legends are individually <strong>rated on a 99-point scale</strong>. Those ratings always lead to discussion, debate, reactions… even from the players themselves.</p><h3></h3><p>Zion got the wrong 2K rating so he had to let @Ronnie2K know about it 😂(via @PelicansNBA) pic.twitter.com/V3I9RPMMJh</p><p>The objective of this post is to build a model to predict this 99-point scale rating for each player, using features related to the player itself and its game statistics from the previous year. As we try to predict a number, it is a <em>regression</em> problem.</p><h4>Data Sources</h4><p>When it comes to NBA data, a large variety of open data exists, the statistics are widely used by the league itself. <strong>Data is part of the NBA culture</strong>.</p><p>NBA 2K ratings are available at <a href="https://hoopshype.com/nba2k/">https://hoopshype.com/nba2k/</a> from 2K14 to 2K21. These 8 years would be our project scope.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hm4igCGcaLsIy08JjfPuRA.png" /></figure><p>We observe that the distribution of the ratings are pretty similar since 2015 with a median in the range [74.5 ; 76.9] and rather constant quartiles.</p><p>In 2K14, the scores were in a broader space with a lower median (71.4). This evolution may be due to a change in the rating strategy by 2K Sports. It is an important information to note as it could introduce bias and worsen model performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OCQU176IsK0iRM_2cR7iFg.png" /></figure><p>Regarding NBA player statistics, the data can be retrieved on <a href="https://www.basketball-reference.com/leagues">https://www.basketball-reference.com/leagues</a>. We will only scrap the seasons 2013–2014 to 2020–2021 in order to be aligned with 2K data period.</p><p>Many views are available on the website, here we focus only on <em>Totals</em> and <em>Advanced </em>tabs.</p><p>Type of features in the dataset:</p><ul><li>Age, position and team</li><li>Number of games played and started, % of win, minutes played</li><li>Field goals (3-points, 2-points, free throws…)</li><li>Rebounds, assists, fouls, blocks, steals…</li></ul><p>To enrich our dataset, we will perform <strong>feature engineering </strong>by combining variables especially with normalization by minutes and games played as well as position and team one-hot encoding.</p><h4>Modeling</h4><p>The dataset is split in training/test/validation with time dependance to avoid <strong>leakage</strong> in the model. Note that we removed 2014 as the distribution of the rating was too different from following years.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vULAbE8Vkqpvfi33PHz32g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*9p0uzpstjYC5wXp5pPJGfQ.png" /></figure><p>Many algorithms can be used to perform regression: from basic Linear Regression to advanced tree methods.</p><p>Comparing different algorithm performances, we will choose XGBoost and do hyper-parameters tuning with Grid Search.</p><p>On the <strong>validation set </strong>(that corresponds to 2K21 ratings), we achieve a MSE of 1.75 and a MAE of 1.33 with the following residuals:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dqbi01RVI6XSv5oPqoomRg.png" /></figure><p>The color of the dot represents the number of game played. We observe that the main outliers are players that did not play enough game during the previous season (e.g Stephen Curry in season 2019/20). In that case, the statistics may be hardly representative of the real player value, this bias is corrected by 2K.</p><h4>Model interpretation</h4><p>Now that we have a model with a reasonable performance, we can open the <em>black box</em> and move to interpretable Machine Learning.</p><p>The objective is to analyze what is behind the model and which are the features that have the <strong>higher impact on the rating prediction</strong>. In other words, we can highlight which player attributes will affect the most (either in a positive or negative way) their 2K rating.</p><blockquote>The goal of SHAP (SHapley Additive exPlanations) is to explain the prediction of an instance x by computing the contribution of each feature to the prediction.</blockquote><blockquote><a href="https://christophm.github.io/interpretable-ml-book/">Interpretable Machine Learning</a></blockquote><p>Using SHAP library, we can plot the <strong>top 10 most important features</strong> (left) and the <strong>combination of feature importance with feature effects</strong> (right).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BMA-L-D_oXSBhf83w9QmiA.png" /></figure><p>The 2 top features reflects a <strong>player importance</strong> within his teams with his average play time and the percentage of games in the starting five. We also notice that the general team performance (% of win share) have a positive influence on the individual player ratings.</p><p>Any position (guard, center…) gives an advantage for higher ratings and it doesn’t seem either that the model favors a specific teams (the first franchise that appears in the feature importance plot is Golden State Warriors in 27th position).</p><p>Finally, we can explore <strong>SHAP explanation force plots</strong> for two specifics player to visualize which features had a positive impact (= increase the rating in red) and a negative impact (= decrease the rating in blue).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WrsfKaal2VQV53PJa_7EAQ.png" /><figcaption>Rudy Gobert, Center in Utah Jazz and <strong>two-times Defensive Player</strong> of the Year, has a high impact on his team (PER, %GS, %MPPG, DWS) but his low scoring decreases his rating by 0.44</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lOnXSGH7UzmQm3MI6ftEww.png" /><figcaption>Montrezl Harrell Wins <strong>2020 NBA’s Sixth Man</strong> of the Year. We observe that %GS decreases his score that is offset by the time he spent on the court and his high offensive impact (FGPM, PTSPM, PER)</figcaption></figure><p><strong>Conclusion</strong></p><p>Using player statistics, I built a XGBoost model that predicts 2K ratings with a great performance. This shows that 2K notation is globally <strong>impartial</strong> and <strong>honestly represent the reality on the court</strong>. Some exceptions can be found, when a player has been injured during the season for example.</p><p>Manipulating basketball data was a lot of fun and the model interpretation is really interesting to have a <strong>proper understanding</strong> of each prediction and to deep dive in player attributes.</p><p>2K rating prediction is a very specific use case but Sport Analytics is an infinite playground with a lot of <strong>under-exploited data </strong>that I will continue to explore in my Data Science journey.</p><p>All the code is available in <a href="https://github.com/benrallet/NBA-2K-rating">Github</a>.</p><h4>References</h4><p>[1] NBA 2K, <a href="https://fr.wikipedia.org/wiki/NBA_2K">Wikipedia</a> (2020)</p><p>[2] Christoph Molnar, <a href="https://christophm.github.io/interpretable-ml-book/">Interpretable Machine Learning</a> (2021)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7b72e2e72eb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/sport-analytics-nba-2k-ratings-prediction-b7b72e2e72eb">Sport Analytics — NBA 2K ratings prediction</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[Sport image classification with Neural Networks]]></title>
            <link>https://benedicterallet.medium.com/sport-image-classification-with-neural-networks-16929b9f7932?source=rss-a7ba74bf1e7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/16929b9f7932</guid>
            <category><![CDATA[transfer-learning]]></category>
            <category><![CDATA[sports]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[pytorch]]></category>
            <dc:creator><![CDATA[Bénédicte Rallet]]></dc:creator>
            <pubDate>Sat, 27 Jun 2020 18:31:54 GMT</pubDate>
            <atom:updated>2020-07-22T14:48:50.179Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*THYpC_rtaKYZPN7dROkI1w.jpeg" /><figcaption>Classifying sports — <a href="https://www.stack.com/a/should-my-kid-play-multiple-sports">Image source</a></figcaption></figure><p>To conclude the <a href="https://jovian.ml/forum/c/pytorch-zero-to-gans/">course</a> offered by the Jovian Community: <strong>Deep Learning with PyTorch: Zero to GANs, </strong>I built a PyTorch classifier to identify<strong> sports </strong>based on image inputs.</p><h3>Data Exploration</h3><p>To build my model, I used a dataset found on Kaggle named “<a href="https://www.kaggle.com/sovitrath/sports-image-dataset">Sports Images</a>”. It contains 13 800 images corresponding to 22 different sports with balanced proportion.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F4%26cellId%3D9&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F4%26cellId%3D9&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fbenrallet%2Fprofile_images%2Fb41ef192454f4733adba1b6db67bcfe6%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/4646154c411189ef7e6f3fae39528f0e/href">https://medium.com/media/4646154c411189ef7e6f3fae39528f0e/href</a></iframe><p>It goes from classical sports, such as swimming or basketball, to sports I didn’t even know before. Have you ever heard about <a href="https://en.wikipedia.org/wiki/Kabaddi">kabaddi</a>? Let’s see if my model does.</p><h3>Feedforward Neural Networks</h3><p>To get the first baseline result, I started by implementing a simple <em>feedforward neural network </em>with limited data preprocessing.</p><p>After fighting for hours with 6% accuracy, I figured out that a too-large image prevent the model from converging and gave very bad results. After various experiments, I finally decided to keep a <strong>64x64 input dimension (</strong>versus initial dimensions around 256x256).</p><p>Before creating the neural network, I went through :</p><ul><li>Split my dataset into training (80%), validation (10%) and test (10%)</li><li>Provide an iterable over the datasets using DataLoader() and batch_size</li><li>Create an ImageClassificationBase class, built on top of nn.module, with a training and a validation method</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F5%26cellId%3D24%26hideInput%3Dtrue&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F5%26cellId%3D24%26hideInput%3Dtrue&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fbenrallet%2Fprofile_images%2Fb41ef192454f4733adba1b6db67bcfe6%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7968b94951f91d3999ca9f5b898636d/href">https://medium.com/media/d7968b94951f91d3999ca9f5b898636d/href</a></iframe><p>Finally, I will extend the ImageClassificationBase with a constructor and a forward() method by designing a 5-layer architecture.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F4%26cellId%3D28&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F4%26cellId%3D28&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fbenrallet%2Fprofile_images%2Fb41ef192454f4733adba1b6db67bcfe6%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d9c82de9ab975e1e82835a106ee8d35/href">https://medium.com/media/1d9c82de9ab975e1e82835a106ee8d35/href</a></iframe><p>To train the model, I defined a fit function that will train the model, compute losses for batches, compute gradients using an optimizer (by default we use <em>Stochastic Gradient Descent -SGD</em>), update parameters and reset gradients.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F5%26cellId%3D33%26hideOutput%3Dtrue&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F5%26cellId%3D33%26hideOutput%3Dtrue&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fbenrallet%2Fprofile_images%2Fb41ef192454f4733adba1b6db67bcfe6%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/e40bceeb289ccf98cda38a6e605471e9/href">https://medium.com/media/e40bceeb289ccf98cda38a6e605471e9/href</a></iframe><p>To train my initial model, I simply tried different learning rates over some epochs and got the following results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*sTCOfh5Yd0IvpbKtPEWypQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*PLH-A7W5MOFjlN-qRJdaKA.png" /><figcaption>Evolution of test accuracy and loss regarding epochs. The train loss enable to detect potential overfitting.</figcaption></figure><p>The best results I obtained for test data were <strong>31%</strong> test accuracy and<strong> 2.28</strong> test loss. Not very convincing…</p><h3>Convolutional Neural Networks</h3><p>To improve my model, I experimented with several techniques, from data preparation to training hyperparameters.</p><h4>Data pre-processing</h4><p>PyTorch provides a bunch of <a href="https://pytorch.org/docs/stable/torchvision/transforms.html">image transformation functions</a> that can be chained together with transforms.Compose().</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F10%26cellId%3D14&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F10%26cellId%3D14&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fbenrallet%2Fprofile_images%2Fb41ef192454f4733adba1b6db67bcfe6%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bdf9899a91db0b5118f4cdb4556f7d1/href">https://medium.com/media/4bdf9899a91db0b5118f4cdb4556f7d1/href</a></iframe><ol><li><strong>Randomized data augmentations: </strong>Image augmentation consists of applying random transformations to the training set (cropping, flipping, changing color scale, erasing pixels …). It will increase the size of the training set without adding any data. Moreover, since the transformation will be applied randomly each time a particular image is loaded, the model will generalize better.</li><li><strong>Channel-wise data normalization</strong>: We will normalize the image tensors by subtracting the mean and dividing by the standard deviation across each channel. Normalizing the data will prevent one channel from disproportionately affecting gradients</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*TA7p6rm-M-liaZEe-MNfnQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/251/1*SrlOicnmOch0XAsraoztNw.png" /><figcaption>Before and after data pre-processing</figcaption></figure><h4>Convolution Neural Networks</h4><p>Convolution Neural Networks (CNN) is a very famous algorithm when coming to image classification.</p><p>It is able to successfully capture the<strong> spatial and temporal dependencies</strong> in an image through the application of relevant filters. The architecture performs a better fitting to the image dataset due to the <strong>reduction in the number of parameters</strong> involved and efficiency due to the <strong>connections</strong> <strong>sparsity</strong>.</p><h4><strong>Transfer Learning</strong></h4><p>Transfer learning is a machine learning method to take a model trained on a large dataset and <em>transfer </em>its knowledge to our own dataset by changing last layer(s).</p><p>PyTorch has a lot of very popular pre-trained models such as VGG, ResNet that can be reused in very few lines of code:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F10%26cellId%3D28&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F05-sport-classification%2Fv%2F10%26cellId%3D28&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fbenrallet%2Fprofile_images%2Fb41ef192454f4733adba1b6db67bcfe6%2Fmedium.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="200" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb5238de4f2bd0ff06286e5fdee9ca9a/href">https://medium.com/media/eb5238de4f2bd0ff06286e5fdee9ca9a/href</a></iframe><h4>Optimized training</h4><p>Finally, one major optimization we have to perform is on training hyperparameters. Even using the best model, if the training is not well set-up, the algorithm may perform poorly.</p><p>There are many techniques and I mainly used 3 of them: learning rate scheduling, weight decay, and gradient clipping.</p><h3>Performance and opportunities</h3><p>I played with many different data transformations, architecture, hyperparameters through the journey of this project. To keep a track of my results and be able to replicate experiments, I used <a href="https://jovian.ml/benrallet/05-sport-classification/compare">Jovian.ml</a>.</p><p>The major improvement in the performance I got was by optimizing the maximum learning rate with <strong>One Cycle Learning Rate Policy.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/428193368ef7ec29b7de68da170d09a9/href">https://medium.com/media/428193368ef7ec29b7de68da170d09a9/href</a></iframe><p>Data augmentation also resulted in better performance (~+4% accuracy for similar models and training parameters).</p><p>I finally achieved to get <strong>76%</strong> test accuracy and <strong>2.35</strong> test loss with the following parameters:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa0f44b4dc52383dd90fa6a7560dfbb6/href">https://medium.com/media/fa0f44b4dc52383dd90fa6a7560dfbb6/href</a></iframe><h4>What I learned</h4><ul><li>I faced doubts, misunderstanding but I kept asking me <strong>WHY</strong>, why is my model poorly classify the images, why are the classes predicted always the same, why is the model running so long. Always try to understand what happens behind your line of code to find room for improvements.</li><li>To <strong>keep a track </strong>of all your experiments (the <em>good</em> but also the <em>bad</em> ones), log them. Many tools exist such as Jovian.ml, MlFlow… It will allow you to get insights and reproduce your best models.</li><li>Don’t be afraid to play with data transformation and your hyperparameters. Even the ones you don’t suspect can be very impactful on your final results.<strong> TRY TRY TRY</strong></li><li>Make sure your model is not <strong>overfitting</strong> with a very simple plot: training loss evolution versus validation loss.</li><li><strong>Pre-trained models</strong> are widely used and perform well on many datasets.</li></ul><p>The entire notebook is available <a href="https://jovian.ml/benrallet/05-sport-classification">here</a>.</p><p><strong>References</strong></p><p>[1] Medium: <em>Understanding of convolutional neural network </em><a href="https://medium.com/@RaghavPrabhu/understanding-of-convolutional-neural-network-cnn-deep-learning-99760835f148">https://medium.com/@RaghavPrabhu/understanding-of-convolutional-neural-network-cnn-deep-learning-99760835f148</a></p><p>[2] Jovian.ml: <em>Classifying CIFAR10 images using ResNet and Regularization techniques in PyTorch</em> <a href="https://jovian.ml/aakashns/05b-cifar10-resnet">https://jovian.ml/aakashns/05b-cifar10-resnet</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16929b9f7932" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[First step using tensors in PyTorch]]></title>
            <link>https://benedicterallet.medium.com/first-step-using-tensors-in-pytorch-3728852a3b0e?source=rss-a7ba74bf1e7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3728852a3b0e</guid>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[tensor]]></category>
            <category><![CDATA[jovian]]></category>
            <dc:creator><![CDATA[Bénédicte Rallet]]></dc:creator>
            <pubDate>Sun, 31 May 2020 09:24:48 GMT</pubDate>
            <atom:updated>2020-05-31T14:30:02.927Z</atom:updated>
            <content:encoded><![CDATA[<p>In my journey to Deep Learning, I am following a <a href="https://jovian.ml/forum/c/pytorch-zero-to-gans/">course</a> offered by the Jovian Community: <strong>Deep Learning with PyTorch: Zero to GANs.</strong></p><p>Our first assignment consisted in picking 5 functions (or family of functions) from <a href="https://pytorch.org/docs/stable/tensors.html">PyTorch tensors documentation</a> and illustrate their usage, limits and tricks. It sounds like a basic exercice but it is actually a really good way to get immediately <strong>hands-on</strong> and acquire a <strong>quick overview of tensor functionalities</strong>.</p><h3>PyTorch library</h3><p>PyTorch is an open-source library initially developed by Facebook’s AI Research lab. It is used for Machine Learning applications as Computer Vision or Natural Langage Processing (NLP).</p><p>The library is based on two main features:</p><ul><li><strong>Tensor computing</strong> (that is the subject of this post) using GPU</li><li><strong>Deep neural networks</strong> based on a automatic differentiation system</li></ul><h3>Tensors</h3><p>A PyTorch tensor (torch.Tensor)looks like a <a href="https://numpy.org/">NumPy</a> Array: it is a multi-dimensional matrix containing elements of a single data type.</p><p>The difference between the two objects is thatTensors can be operated either on a CPU or on GPU (using <em>cuda</em> datatype). In the area of Deep Learning, using GPU will speed up computation and improve performance.</p><p>Let’s get started with some functions to know more about PyTorch tensors.</p><h4>Tensors size</h4><p>Knowing tensors’ sizes is essential. It can impact computation or lead to errors. When developing Deep Learning algorithms, a tensor dimension issue can happen so fast and be so long to debug.</p><p>I still remember, back in high school, writing down memos about my matrices’ dimensions to make sure my operations are feasible. It is the exact same necessity.</p><p>To get a torch.Tensor dimension, we can use either the function <strong>size()</strong> or simply <strong>.shape</strong> (similar in NumPy).</p><p>In the example below, we created two tensors a et b.</p><p>We first checked their size and validate that <strong>size</strong> and <strong>shape</strong> lead to the same result using <a href="https://www.w3schools.com/python/ref_keyword_assert.asp">assert</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D4&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D4&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b212a6ecf37ea5a0b7564e59b955a47/href">https://medium.com/media/1b212a6ecf37ea5a0b7564e59b955a47/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D6&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D6&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/17107ee2ac5f7e21fdeaf14b3790be45/href">https://medium.com/media/17107ee2ac5f7e21fdeaf14b3790be45/href</a></iframe><p>Then, we tried to apply matrice multiplication for a and b using <strong>@. </strong>We observe for the first cell that we have a dimension issue that can be solved using the transpose of b (<strong>b.T</strong>). This is typical example of tensor size error.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D8&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D8&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4040820e4e95113f2db9b0978093f5b/href">https://medium.com/media/c4040820e4e95113f2db9b0978093f5b/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D10&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D10&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/047c7fa49e8d5ec9028de5ed9c4cb41a/href">https://medium.com/media/047c7fa49e8d5ec9028de5ed9c4cb41a/href</a></iframe><p>To go further and change a tensor shape, the functions <strong>view</strong> or <strong>reshape</strong> should be used :</p><ul><li><strong>Tensor.view()</strong> works only on contiguous tensors and will never copy memory</li><li><strong>Tensor.reshape()</strong> will work on any tensor and can make a clone</li></ul><h4>Casting functions</h4><p>A torch.Tensoris a matrix containing elements of a <strong>single data type</strong>. It is very important to be aware of the type of data we have in our tensors: it is a <strong>common source of error</strong>.</p><p>The list of supported format for PyTorch tensors can be found <a href="https://pytorch.org/docs/stable/tensors.html">here</a>.</p><p>The tensor type is accessible using the function <strong>type()</strong> without any parameter.</p><p>If the current data type doesn’t fit our expectations (for instance after converting a NumPy Array), there are different functions available to cast a tensor:</p><ul><li><strong>to(dtype)</strong></li><li><strong>type(dtype)</strong></li><li><strong>type_as(tensor)</strong></li><li>specific functions for each data type: <strong>float()</strong>, <strong>double()</strong>, <strong>int()</strong>…</li></ul><p>Let’s no take an example!</p><p>We initialize a tensor <strong>t_int</strong> as a torch.LongTensor.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D14&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D14&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/304c136b88dadfb18f1b669763ac7e0d/href">https://medium.com/media/304c136b88dadfb18f1b669763ac7e0d/href</a></iframe><p>After few lines of code, we realize this type is not convenient for our needs and we have to convert it into float data type.</p><p>4 different functions can help us to achieve this same operation:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D17&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D17&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/1eecb98634edb1e6fffb268820ee4e9d/href">https://medium.com/media/1eecb98634edb1e6fffb268820ee4e9d/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D19&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D19&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/4237f3d9de23b8b80d19f0ed7655a9ae/href">https://medium.com/media/4237f3d9de23b8b80d19f0ed7655a9ae/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D21&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D21&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc9faa34da5bd64080e3519edda0a4af/href">https://medium.com/media/cc9faa34da5bd64080e3519edda0a4af/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D23&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D23&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/5cf150955f59dd300a58146c87ff2700/href">https://medium.com/media/5cf150955f59dd300a58146c87ff2700/href</a></iframe><p>Now you just need to pick the one you prefer and <strong>take the power over your data types</strong>.</p><h4>Arithmetic functions</h4><p>Arithmetic operations are everywhere in a Deep Learning algorithms: to compute loss, to transform input tensors and many more.</p><p>PyTorch library contains a bunch of <strong>built-in arithmetic functions</strong>. When calling these functions on a tensor, it will apply the transformation element-wise.</p><p>I have chosen to show the usage of one: <strong>sqrt()</strong> to compute the element-wise square root of a tensor.</p><p>First, let create two tensors.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D28&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D28&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/440da7fb7ce2843a89704ebc712b57c8/href">https://medium.com/media/440da7fb7ce2843a89704ebc712b57c8/href</a></iframe><p>We observe below that <strong>nan</strong> is returned for negative values.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D30&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D30&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/71ce3e80b1a8d73ea6fb5286a9dd4716/href">https://medium.com/media/71ce3e80b1a8d73ea6fb5286a9dd4716/href</a></iframe><p>Now, let’s try on tensor b, that is a torch.LongTensor.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D32&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D32&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/37af429bd445317c6d5cb979519d276f/href">https://medium.com/media/37af429bd445317c6d5cb979519d276f/href</a></iframe><p>Long type is not supported by the function… Seems that we’ll need to use our casting skills.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D34&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D34&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb06442121f4fa9df065584b6773e990/href">https://medium.com/media/fb06442121f4fa9df065584b6773e990/href</a></iframe><p>Magic! I told you it would be useful to know how to convert a tensor.</p><h4>Matrice functions</h4><p>To build any Deep Learning algorithm, we will also need matrice functions.</p><p>Here I take the example of one: the <strong>inverse()</strong> function. As you may remember from algebra class, it computes the <a href="https://www.mathsisfun.com/algebra/matrix-inverse.html">inverse of the square matrix</a>.</p><p>Our first tensor is a [2,2] torch.FloatTensor. Everything works well so far.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D50&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D50&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0ad943ad6fb9df4a4d25c7eec6e0e95/href">https://medium.com/media/f0ad943ad6fb9df4a4d25c7eec6e0e95/href</a></iframe><p>Ouch, the Long type is not supported by the function. A <strong>type error </strong>again…</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D52&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D52&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5210bb4bb54f028dfde94fd32c91488/href">https://medium.com/media/a5210bb4bb54f028dfde94fd32c91488/href</a></iframe><p>Below, the issue comes from the <strong>tensor size</strong> [3,2]. The inverse of a matrix can only be computed for a square matrix (meaning dim1=dim2).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D54&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fbenrallet%2F01-tensor-operations%2Fv%2F5%26cellId%3D54&amp;image=https%3A%2F%2Fjovianml.s3.amazonaws.com%2Fpreview-large.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=jovian" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f2912ca0a75f185c3e65587394c0f30/href">https://medium.com/media/2f2912ca0a75f185c3e65587394c0f30/href</a></iframe><p><strong>Conclusion</strong></p><p>Going though PyTorch documentation and writing this post was a fun exercice.</p><p>I had to question myself about the <strong>interest</strong> of many functions, to find out how to <strong>break a function</strong> and to explain with my words <strong>how to use it</strong>.</p><p>You can access the <a href="https://jovian.ml/benrallet/01-tensor-operations">full Python notebook</a>, hosted on Jovian.ml.</p><p><strong>References</strong></p><p>[1] Wikipedia: <em>PyTorch</em> <a href="https://en.wikipedia.org/wiki/PyTorch">https://en.wikipedia.org/wiki/PyTorch</a></p><p>[2] PyTorch official documentation: <em>Torch.Tensor </em><a href="https://jovian.ml/outlink?url=https%3A%2F%2Fpytorch.org%2Fdocs%2Fstable%2Ftensors.html">https://pytorch.org/docs/stable/tensors.html</a></p><p>[3] PyTorch Forum: <em>Is it recommended to use reshape than wiew chen it is possible?</em> <a href="https://jovian.ml/outlink?url=https%3A%2F%2Fdiscuss.pytorch.org%2Ft%2Fin-pytorch-0-4-is-it-recommended-to-use-reshape-than-view-when-it-is-possible%2F17034">https://discuss.pytorch.org/t/in-pytorch-0-4-is-it-recommended-to-use-reshape-than-view-when-it-is-possible/17034</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3728852a3b0e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>