3
votes
import torch
torch.cuda.is_available()
torch.cuda.current_device()
torch.cuda.get_device_name(0)
torch.cuda.memory_reserved()
torch.cuda.memory_allocated()
torch.cuda.memory_allocated()
var1=torch.FloatTensor([1.0,2.0,3.0]).cuda()
var1
var1.device
import pandas as pd
df=pd.read_csv('diabetes.csv')
df.head()
df.isnull().sum()

import seaborn as sns
import numpy as np
df['Outcome']=np.where(df['Outcome']==1,"Diabetic","No Diabetic")
df.head()
sns.pairplot(df,hue="Outcome")
X=df.drop('Outcome',axis=1).values### independent features
y=df['Outcome'].values###dependent features
from sklearn.model_selection import train_test_split

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0) y_train import torch import torch.nn as nn import torch.nn.functional as F

X_train=torch.FloatTensor(X_train).cuda()
X_test=torch.FloatTensor(X_test).cuda()
y_train=torch.LongTensor(y_train).cuda()
y_test=torch.LongTensor(y_test).cuda()

when I Run this code I got this error:

Traceback (most recent call last):
  File "<stdin>", line 24, in <module>
TypeError: expected CPU (got CUDA)

How to can I solve this error?

1
##### Creating Tensors X_train=torch.FloatTensor(X_train).cuda() X_test=torch.FloatTensor(X_test).cuda() y_train=torch.LongTensor(y_train).cuda() y_test=torch.LongTensor(y_test).cuda()Vinoth Kumar S
Please read how to create a minimal reproducible example and how to ask and use edit button to improve your post.Ruli
and the error is: >>> X_train=torch.FloatTensor(X_train).cuda() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: expected CPU (got CUDA)Vinoth Kumar S
@VinothKumarS, does your m/c have a GPU? Are you performing any operation with mixed CPU/GPU types? Is the pytorch GPU version? Show the complete error msg!anurag
@anurag yes my machine has GPU that is RTX 3070 and it is the PyTorch GPU version.Vinoth Kumar S

1 Answers

1
votes

To transfer the variables to GPU, try the following:

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

X_train=torch.FloatTensor(X_train).to(device)
X_test=torch.FloatTensor(X_test).to(device)
y_train=torch.LongTensor(y_train).to(device)
y_test=torch.LongTensor(y_test).to(device)