<?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 Saurabh Palhade on Medium]]></title>
        <description><![CDATA[Stories by Saurabh Palhade on Medium]]></description>
        <link>https://medium.com/@saurabh_palhade45?source=rss-a59b1d58b1ca------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-MJQqkR1RcA8BaXu1SlJjg.jpeg</url>
            <title>Stories by Saurabh Palhade on Medium</title>
            <link>https://medium.com/@saurabh_palhade45?source=rss-a59b1d58b1ca------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 16:10:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saurabh_palhade45/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[Face Mask Detection Using Deep Neural Networks (PyTorch)]]></title>
            <link>https://medium.com/dsc-dypcoe/face-mask-detection-using-deep-neural-networks-pytorch-af448f78a8b6?source=rss-a59b1d58b1ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/af448f78a8b6</guid>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Saurabh Palhade]]></dc:creator>
            <pubDate>Tue, 30 Jun 2020 09:08:54 GMT</pubDate>
            <atom:updated>2020-06-30T12:35:54.548Z</atom:updated>
            <content:encoded><![CDATA[<h3>Face Mask 😷 Detection Using Deep Neural Networks (PyTorch)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*i5mNzWQ_7T-SIp4k" /><figcaption>Photo by <a href="https://unsplash.com/@anastasiiachepinska?utm_source=medium&amp;utm_medium=referral">Anastasiia Chepinska</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote><strong>ABOUT THE PROJECT</strong></blockquote><p>Face Masks play a vital role in protecting the health of individuals against respiratory diseases, and is one of the few precautions available for COVID-19 in the absence of a vaccine. And now everywhere it’s compulsory to wear masks. So hear i created a model which detects whether a person in the image have a face mask or not.In this article i will explain you about my approach towards making this project and how i created it from scratch.</p><blockquote><strong>ABOUT THE DATASET</strong></blockquote><p>The dataset contains around 12000 images of different sizes belonging to the 2 classes(with mask and without masks) .The dataset is available on kaggle,you can find it <a href="https://www.kaggle.com/ashishjangra27/face-mask-12k-images-dataset">here</a>.</p><p>Training set size : 10,000 images</p><p>Validation set size : 800 images</p><p>Test set size : 992 images</p><p><strong><em>Lets get started….</em></strong></p><blockquote><strong>PREPARING THE DATA</strong></blockquote><p>We begin by importing all the required libraries and functions.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D0&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D0&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/c326f3f46c3140a93f9ff502a576bf9c/href">https://medium.com/media/c326f3f46c3140a93f9ff502a576bf9c/href</a></iframe><p>We have imported <em>os</em> and <em>tarfile</em> for extracting data from dataset directories, <em>ImageFolder</em> to load the data from different folders as pytorch tensors, <em>random_split</em> and <em>DataLoader</em> to load data into batches for training and validation, <em>matplotlib </em>and<em> make_grid </em>for data visualization purpose, <em>torch</em>.nn package from PyTorch, which contains utility classes for building neural networks.</p><p>Lets take a look at data directories and classes and load the data as pytorch tensors</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D1&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D1&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/d3788b90b8ec28c4d0c4f173f85f571c/href">https://medium.com/media/d3788b90b8ec28c4d0c4f173f85f571c/href</a></iframe><p><strong>Training Validation and Test Dataset</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D2&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D2&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/92b87228deaa3fac627e6f824ebc2b58/href">https://medium.com/media/92b87228deaa3fac627e6f824ebc2b58/href</a></iframe><p>As our dataset contains images of different sizes so we will apply some transformations on the training,validation and test set to make all the images of same size(most of the computer vision applications need images of same size).For that we will use PyTorch inbuilt functions and we will resize all the images to 224x224 pixels and then we will convert them into pytorch tensors.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D12&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D12&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/d8162e63e5334a91cc359a01a3b85145/href">https://medium.com/media/d8162e63e5334a91cc359a01a3b85145/href</a></iframe><p><strong>Dataloaders</strong></p><p>Now we will create dataloaders for splitting data into batches for training and validation purpose.Dataloaders returns the dataset batch by batch of predefined batch size,instead of loading whole data at once we use dataloaders to load our dataset batch by batch so that we don’t run out of memory and to prevent our training process from slow down.Here we will use a batch size of 128.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D14&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D14&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/9eec63be99f49a790b0bea4949bb59f7/href">https://medium.com/media/9eec63be99f49a790b0bea4949bb59f7/href</a></iframe><p>Let’s take a look at a batch of data from the training dataset,for that we have to create a helper function <em>show_batch .</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D16&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D16&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/c2589853842a5d00ad704df4c6909ea6/href">https://medium.com/media/c2589853842a5d00ad704df4c6909ea6/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%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D17&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D17&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/4f9ca1dc7d26fdc2146beaeb4c580a74/href">https://medium.com/media/4f9ca1dc7d26fdc2146beaeb4c580a74/href</a></iframe><p>As our dataset is huge so we need GPU to train our model within a reasonable amount of time,so for that purpose we define a helper function for selecting the available device(cpu or gpu).This function checks if a GPU is available and the required NVIDIA CUDA drivers are installed and returns a available device.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D21&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D21&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/5e049de993a9f65829070b07a807c5e6/href">https://medium.com/media/5e049de993a9f65829070b07a807c5e6/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%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D22&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D22&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/49dc54277cd0191521ddbb8c7dcb2785/href">https://medium.com/media/49dc54277cd0191521ddbb8c7dcb2785/href</a></iframe><p>Now lets define few helper functions and class to move our training ,validation and test data on the available device(cpu or gpu).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D24&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D24&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/909f3cda4e3778dab10bda59ac2d1d75/href">https://medium.com/media/909f3cda4e3778dab10bda59ac2d1d75/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%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D25&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D25&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/9a402e675a93a6a85d89ddba36efd99b/href">https://medium.com/media/9a402e675a93a6a85d89ddba36efd99b/href</a></iframe><blockquote><strong>DEFINING THE MODEL</strong></blockquote><p>To include some additional functionalities within our model, we need to define a custom model by extending the nn.module class from PyTorch. nn.module class is basically a base class for all neural network modules.Before defining our model let’s create some helper functions.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D28&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D28&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/80f78f74a11a7f9d8a643a526dc30112/href">https://medium.com/media/80f78f74a11a7f9d8a643a526dc30112/href</a></iframe><p>Here we have class <em>Dnn</em> in which we have <em>training_step</em> function which calculates loss of training data,<em>validation_step</em> which calculates loss and accuracy of validation data,<em>validation_epoch_end</em> and <em>epoch_end</em> function which calculates and prints validation loss and accuracy after every epoch(iteration). Note here we have used loss function as cross entropy which performs better on these kind of problems.you can check more about it <a href="https://medium.com/data-science-bootcamp/understand-cross-entropy-loss-in-minutes-9fb263caee9a">here</a> .</p><p>Now we will extend the Dnn class to complete the model definition.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D30&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D30&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fjvn%2Fassets%2Fsaurabhpalhade%2Fprofile_images%2Fe2a77a3ca33b408abbd351f881c256c7%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/3fd6476e1234d1a07d36d08d9d634a7e/href">https://medium.com/media/3fd6476e1234d1a07d36d08d9d634a7e/href</a></iframe><p>Here we have created a neural network with 4 layers.one input layer,two hidden layers and a output layer. For creating each layer of our neural network we have used a inbuilt nn.linear function from pytorch which takes two arguments input size(features) and output size(no of classes or no of output labels).size of the hidden layers are 128 and 256 respectively,and the activation function which we have used here is ReLU(rectified linear unit).it has a simple formula: relu(x) = max(0,x) i.e. if an element is negative, we replace it by 0, otherwise we leave it unchanged.Introducing a hidden layer and an activation function allows the model to learn more complex, multi-layered and non-linear relationships between the inputs and the targets(outputs or labels).</p><blockquote><strong>TRAINING MODEL</strong></blockquote><p>Before we actually begin with the model training process,we need to define some helper functions,for model training and evaluation purpose.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D32&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D32&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/2c432d34be6e6f962929b88397c25c3c/href">https://medium.com/media/2c432d34be6e6f962929b88397c25c3c/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%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D33&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D33&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/c0658629f91dca54efdc7ec850c5623b/href">https://medium.com/media/c0658629f91dca54efdc7ec850c5623b/href</a></iframe><p>We have define an accuracy function which calculates the overall accuracy of the model on an entire batch of outputs, so that we can use it as a metric in fit function. evaluate function for calculating loss and accuracy of validation data after every epoch. and the most important utility function fit which trains the model for a given number of epochs. It basically preforms the following operations: 1- Generates predictions, 2- Calculate the loss , 3-Compute gradients w.r.t the weights and biases, 4-Adjust the weights by subtracting a small quantity proportional to the gradient, 5-Reset the gradients to zero and at the end of every epoch it evaluates the model on validation data and prints it’s loss and accuracy.Note that the optimization algorithm we have used here is stochastic gradient descent (optim.SGD).you can learn more about it <a href="https://medium.com/@Biboswan98/optim-adam-vs-optim-sgd-lets-dive-in-8dbf1890fbdc">here</a> .</p><p>Before we train the model, we need to ensure that the data and the model’s parameters (weights and biases) are on the same device (CPU or GPU). We can reuse the <em>to_device</em> function to move the model&#39;s parameters to the right device(cpu or gpu).</p><p>Let’s initialize our model and move it on the available device.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D35&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D35&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/cf34bc48a987d4d8d7edcb57fca1123d/href">https://medium.com/media/cf34bc48a987d4d8d7edcb57fca1123d/href</a></iframe><p>Now we are ready to train our model.let’s train it for 10 epochs with the initial learning rate of 0.01.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D38&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D38&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/e74e045d39da7962d7252098917b96eb/href">https://medium.com/media/e74e045d39da7962d7252098917b96eb/href</a></iframe><p>After this I trained the model for few more epochs with learning rates 0.001 and 0.0001,and after evaluating it on the test data i was able to achieve the accuracy of 95.37%.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D41&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D41&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/a4ba9af01bac61ccf906e305132a4043/href">https://medium.com/media/a4ba9af01bac61ccf906e305132a4043/href</a></iframe><p>Now let’s test our model on the predefined test data and see how it performs.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D44&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D44&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/f57f9e74776f375dca24b2da13f7b8ad/href">https://medium.com/media/f57f9e74776f375dca24b2da13f7b8ad/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%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D45&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2Fface-mask-detection-neuralnet1%2Fv%2F4%26cellId%3D45&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/6486ab45aca2983ae3331966d1bfc5f2/href">https://medium.com/media/6486ab45aca2983ae3331966d1bfc5f2/href</a></iframe><p>Well you can see we got a test accuracy 95.37% which is pretty good and our model is performing quite well. check out the entire notebook of this project <a href="https://jovian.ml/saurabhpalhade/face-mask-detection-neuralnet1">here</a>.</p><p>If you are reading this i hope this article helped you to gain some knowledge about creating end to end model using neural networks and encouraged you to learn more about it.</p><p><strong><em>Thank you so much for taking the time to read this! I hope you enjoyed reading it .you can connect with me on </em></strong><a href="http://www.linkedin.com/in/saurabh-palhade-797a86175"><strong><em>LinkedIn</em></strong></a><strong><em> and </em></strong><a href="https://twitter.com/saurabhpalhade"><strong><em>Twitter</em></strong></a></p><blockquote><strong>REFERENCE LINKS</strong></blockquote><p><em>Check out the playlist of the course “Deep Learning With PyTorch” </em><a href="https://www.youtube.com/playlist?list=PLWKjhJtqVAbm3T2Eq1_KgloC7ogdXxdRa"><em>here</em></a></p><p><em>Check out </em><a href="https://www.jovian.ml/"><em>jovian.ml</em></a><em>, It’s a sharing and collaboration platform for data science projects and jupyter notebooks.</em></p><p><em>Check out my other projects </em><a href="https://jovian.ml/saurabhpalhade"><em>here</em></a><em> .</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af448f78a8b6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dsc-dypcoe/face-mask-detection-using-deep-neural-networks-pytorch-af448f78a8b6">Face Mask Detection Using Deep Neural Networks (PyTorch)</a> was originally published in <a href="https://medium.com/dsc-dypcoe">GDSC DYPCOE</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Beginners Guide For Creating PyTorch Tensors]]></title>
            <link>https://medium.com/dsc-dypcoe/a-beginners-guide-for-creating-pytorch-tensors-7cfc9269c573?source=rss-a59b1d58b1ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/7cfc9269c573</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Saurabh Palhade]]></dc:creator>
            <pubDate>Wed, 27 May 2020 17:56:04 GMT</pubDate>
            <atom:updated>2020-05-28T08:17:40.338Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3G90f7g9YfnJl0Haif-s5w.png" /></figure><p>In this article, we will explore some basic PyTorch tensor functions, To begin with let us first understand what is PyTorch and what are Tensors.</p><p>PyTorch is a python based library built to provide flexibility as a deep learning development platform, also used for applications such as computer vision and natural language processing developed by facebook’s AI Research lab (FAIR), and A PyTorch Tensor is conceptually identical to a NumPy array, A tensor is a number, vector, matrix or an n-dimensional array, and PyTorch provides many functions for operating on these Tensors.</p><p>Now that you know what a tensor is, Here’s the list of five interesting functions we will go through :</p><ul><li>torch.tensor()</li><li>torch.randn()</li><li>torch.linspace()</li><li>torch.reshape()</li><li>torch.sum()</li></ul><p>let us see how to create a PyTorch tensor. To create a tensor the most basic function is:<strong><em> torch.tensor()</em></strong></p><h4><strong><em>torch.tensor()</em></strong></h4><p>We start by importing the PyTorch library</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-pytorch-basics-b185d%2Fv%2F1%26cellId%3D4&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-pytorch-basics-b185d%2Fv%2F1%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/b507486b091b97c38bc9ff63c4c21cb5/href">https://medium.com/media/b507486b091b97c38bc9ff63c4c21cb5/href</a></iframe><p>Now we are all set to play with tensors, Let’s create a tensor with a single number:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-pytorch-basics-b185d%2Fv%2F1%26cellId%3D6&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-pytorch-basics-b185d%2Fv%2F1%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/a1d52ebce3614371e5e94f61b5127a88/href">https://medium.com/media/a1d52ebce3614371e5e94f61b5127a88/href</a></iframe><p>4. is a shorthand for 4.0. It is used to indicate to Python (and PyTorch) that you want to create a floating-point number.</p><p>Let’s try creating slightly more complex tensors:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-pytorch-basics-b185d%2Fv%2F1%26cellId%3D10&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-pytorch-basics-b185d%2Fv%2F1%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/0707f86e123d4a342261ff4a21da7c4e/href">https://medium.com/media/0707f86e123d4a342261ff4a21da7c4e/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%2Fsaurabhpalhade%2F01-pytorch-basics-b185d%2Fv%2F1%26cellId%3D11&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-pytorch-basics-b185d%2Fv%2F1%26cellId%3D11&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/ccf8f7e65644d4d826bae9f0baec3bc6/href">https://medium.com/media/ccf8f7e65644d4d826bae9f0baec3bc6/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%2Fsaurabhpalhade%2F01-pytorch-basics-b185d%2Fv%2F1%26cellId%3D12&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-pytorch-basics-b185d%2Fv%2F1%26cellId%3D12&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/49e5ff1cef57d30dfd0a3223429ab0f7/href">https://medium.com/media/49e5ff1cef57d30dfd0a3223429ab0f7/href</a></iframe><p>The datatype of tensor if not specified will be of data,but we can specify the datatype using <strong>dtype parameter i.e </strong>dtype=torch.float64 .Tensors can have any number of dimensions, and different lengths along each dimension. It can be constructed from a Python list,Initial values for the tensor can be a list, tuple, NumPy NDarray, scalar, and other types.here we have created tensors <em>t2</em> ,<em>t3</em> and <em>t4</em> of one,two and three dimensions respectively.</p><p>As you have seen that using torch.tensor() we have created a tensor using specific values,but to create a tensor filled with random numbers we can use a function <strong><em>torch.randn().</em></strong></p><h4><strong><em>torch.randn()</em></strong></h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttp%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D3&amp;dntp=1&amp;display_name=Jovian&amp;url=http%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D3&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/da5c960d251fb1e03ca095b9f9268884/href">https://medium.com/media/da5c960d251fb1e03ca095b9f9268884/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%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F5%26cellId%3D5&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F5%26cellId%3D5&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/ba465334f223866ac75b48e6bf228fc9/href">https://medium.com/media/ba465334f223866ac75b48e6bf228fc9/href</a></iframe><p>so, torch.randn() Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1. here in example 1, we have created a <strong><em>x</em></strong> tensor of size 4 (1D) with the values between 0 and 1.We can create a tensor of any size by passing the size as an argument to the function, as here in example 2 you can see we created a <strong><em>y</em></strong> tensor of size 3 by 3 (2D). This function is very useful when we want to initialize a tensor of big size with random values.</p><p>Now let’s see another important tensor function:</p><h4><strong><em>torch.linspace()</em></strong></h4><pre>torch.linspace(<em>start</em>, <em>end</em>, <em>steps=100</em>, <em>out=None</em>, <em>dtype=None</em>, <em>layout=torch.strided</em>, <em>device=None</em>, <em>requires_grad=False</em>)</pre><p>This returns a one-dimensional tensor of steps equally spaced points between start and end.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D11&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D11&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/c2301ff403717c1701e13451b5f1b7d2/href">https://medium.com/media/c2301ff403717c1701e13451b5f1b7d2/href</a></iframe><p>In this function we have to specify the starting,ending and step parameters as we will be getting a tensor of equally spaced values between starting and ending parameters with its size equal to the step parameter. Here in example 1 we have created a tensor of size 10(steps),with equally spaced values from 1 to 10.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D13&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D13&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/21700891d8c9a061f173d5f531c17746/href">https://medium.com/media/21700891d8c9a061f173d5f531c17746/href</a></iframe><p>Here in example 2 notice that how the values of tensor got rounded to int as we defined the data type,the default tensor type will be float.</p><p>Our next tensor function is:</p><h4><strong><em>torch.reshape()</em></strong></h4><p>This function basically reshapes the tensor into another shape.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D19&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%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/453869029368c2b981b785f6ebf8a07c/href">https://medium.com/media/453869029368c2b981b785f6ebf8a07c/href</a></iframe><p>In this function we pass the tensor which we want to reshape as the first parameter and second parameter is the size in which we want to reshape the tensor. In example 1 we converted the 1D tensor of size 10 into a 2D tensor of size 2 by 5 using torch.reshape() function.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D21&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%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/4b3b475dcb3ba9169a560807c440c7f5/href">https://medium.com/media/4b3b475dcb3ba9169a560807c440c7f5/href</a></iframe><p>Here In Example 2 we reshaped the 2D tensor of size 4 by 4 into a another 2D tensor of size 8 by 2.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D23&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%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/ebdbca3f214aae487375306e9f7b571c/href">https://medium.com/media/ebdbca3f214aae487375306e9f7b571c/href</a></iframe><p>Take a look at example 3, This is one of the most common mistakes, always remember while giving the reshape size you must check that the original tensor and the reshaped tensor should have same no of elements, as in this example the input x tensor is of size 4 by 4 and will have 16 elements, and our reshape size is 2 by 2 which will be having only four elements, so we cannot convert a 4 by 4 tensor into 2 by 2, hence we got a traceback here.</p><p>Now let’s see another tensor function:</p><h4><strong><em>torch.sum()</em></strong></h4><p>This function returns the sum of all elements in the input tensor.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F5%26cellId%3D35&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F5%26cellId%3D35&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/2eba08edb7066c1c5a5d76d15f12d256/href">https://medium.com/media/2eba08edb7066c1c5a5d76d15f12d256/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%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D37&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F4%26cellId%3D37&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/947d0a059ed69f7c31f4204dc3abac4e/href">https://medium.com/media/947d0a059ed69f7c31f4204dc3abac4e/href</a></iframe><p>We pass the tensor to the torch.sum() function as a parameter,In example 1 we got a sum of all the elements in the tensor and in In example 2 we got the sum of the elements of the tensor according to the dimension specified,row-wise and column-wise.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fjovian.ml%2Fembed%3Furl%3Dhttps%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F5%26cellId%3D39&amp;dntp=1&amp;display_name=Jovian&amp;url=https%3A%2F%2Fjovian.ml%2Fsaurabhpalhade%2F01-tensor-operations%2Fv%2F5%26cellId%3D39&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/c619e150e0dff1ceb4db52dd0c1c3f5a/href">https://medium.com/media/c619e150e0dff1ceb4db52dd0c1c3f5a/href</a></iframe><p>Don’t try to add two tensors using this sum function otherwise you will end up getting a traceback as shown in example 3,we can directly add two tensors in PyTorch with arithmetic operator i.e x+y. This function can be used when we need to get the sum of all the elements of tensor,and also when we need the column-wise and row-wise sum of elements.</p><p>So this brings us to the end of this article,we have seen some basic PyTorch tensor functions which we can use while performing tensor operations,there are many other useful functions as well,if you are reading this i hope this article helped you to gain some knowledge about PyTorch tensor operations and encouraged you to learn more about tensors,to check out more functions please visit official documentation of PyTorch.</p><p><strong><em>Thank you so much for taking the time to read this! I hope you enjoyed reading it .you can connect with me on </em></strong><a href="http://www.linkedin.com/in/saurabh-palhade-797a86175"><strong><em>LinkedIn</em></strong></a><strong><em> and </em></strong><a href="https://twitter.com/saurabhpalhade"><strong><em>Twitter</em></strong></a></p><h4>Reference links:</h4><p><a href="https://pytorch.org/">Documentation for PyTorch</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7cfc9269c573" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dsc-dypcoe/a-beginners-guide-for-creating-pytorch-tensors-7cfc9269c573">A Beginners Guide For Creating PyTorch Tensors</a> was originally published in <a href="https://medium.com/dsc-dypcoe">GDSC DYPCOE</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>