JavaScript
x
24
24
1
import torch
2
torch.cuda.is_available()
3
torch.cuda.current_device()
4
torch.cuda.get_device_name(0)
5
torch.cuda.memory_reserved()
6
torch.cuda.memory_allocated()
7
torch.cuda.memory_allocated()
8
var1=torch.FloatTensor([1.0,2.0,3.0]).cuda()
9
var1
10
var1.device
11
import pandas as pd
12
df=pd.read_csv('diabetes.csv')
13
df.head()
14
df.isnull().sum()
15
16
import seaborn as sns
17
import numpy as np
18
df['Outcome']=np.where(df['Outcome']==1,"Diabetic","No Diabetic")
19
df.head()
20
sns.pairplot(df,hue="Outcome")
21
X=df.drop('Outcome',axis=1).values### independent features
22
y=df['Outcome'].values###dependent features
23
from sklearn.model_selection import train_test_split
24
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
JavaScript
1
5
1
X_train=torch.FloatTensor(X_train).cuda()
2
X_test=torch.FloatTensor(X_test).cuda()
3
y_train=torch.LongTensor(y_train).cuda()
4
y_test=torch.LongTensor(y_test).cuda()
5
when I Run this code I got this error:
JavaScript
1
4
1
Traceback (most recent call last):
2
File "<stdin>", line 24, in <module>
3
TypeError: expected CPU (got CUDA)
4
How to can I solve this error?
Advertisement
Answer
To transfer the variables to GPU, try the following:
JavaScript
1
7
1
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
2
3
X_train=torch.FloatTensor(X_train).to(device)
4
X_test=torch.FloatTensor(X_test).to(device)
5
y_train=torch.LongTensor(y_train).to(device)
6
y_test=torch.LongTensor(y_test).to(device)
7