How to find mean of a torch tensor

This recipe helps you find mean of a torch tensor

Recipe Objective

How to find mean of a torch tensor?

We can find mean of a torch tensor by using the torch.mean(tensor) function which will return mean value of all elements in the input tensor. First take a Sample tensor then apply the torch.mean(tensor) function to it. Lets understand this with practical implementation.

Comprehensive List of Computer Vision Project Ideas for Data Enthusiasts

Step 1 - Import library

import torch

Step 2 - Take Sample

data = torch.rand(3, 10)
print("This is the sample data:",data)

This is the sample data: tensor([[0.1430, 0.0198, 0.1964, 0.1264, 0.3057, 0.4229, 0.2136, 0.8550, 0.9403,
         0.7608],
        [0.5985, 0.1856, 0.0264, 0.9874, 0.1756, 0.9313, 0.5572, 0.7367, 0.7252,
         0.5210],
        [0.5838, 0.9319, 0.9853, 0.2943, 0.1678, 0.7704, 0.4864, 0.9439, 0.8497,
         0.5359]])

Step 3 - Find mean

print("The mean of Sample data is:",torch.mean(data))

The mean of Sample data is: tensor(0.5326)

{"mode":"full","isActive":false}

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.