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?
Advertisement
Answer
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)