How to use cpu to compute torch operations

This recipe helps you use cpu to compute torch operations

Recipe Objective

How to use CPU to compute torch operations?

In some of the cases the operations cannot be performed on the Cuda tensors, so at that time CPU or GPU comes into picture on to which the operations can be performed. The tensor. CPU function will moves it back to the memory accessible to the CPU. Also when we are using deep neural networks like Resnet152, transfer learning and many more, then the CPU plays a major role into it.

Access Loan Eligibility Prediction Projects with Source Code

Step 1 - Import library

import torch

Step 2 - Variable for holding device for CPU or GPU.

cpu_device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

cpu_device

device(type='cpu')

Step 3 - Tensor for CPU

cpu_tensor = torch.tensor(2).cpu

cpu_tensor

 

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

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

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.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.