Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:预期的CPU(获得CUDA)_Python_Pytorch_Gpu - Fatal编程技术网

Python TypeError:预期的CPU(获得CUDA)

Python TypeError:预期的CPU(获得CUDA),python,pytorch,gpu,Python,Pytorch,Gpu,X_序列,X_测试,y_序列,y_测试=序列测试分割(X,y,测试大小=0.2,随机状态=0) 你的火车 进口火炬 导入torch.nn作为nn 导入torch.nn.功能为F 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

X_序列,X_测试,y_序列,y_测试=序列测试分割(X,y,测试大小=0.2,随机状态=0) 你的火车 进口火炬 导入torch.nn作为nn 导入torch.nn.功能为F

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=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()
回溯(最近一次呼叫最后一次):
文件“”,第24行,在
TypeError:预期的CPU(获得CUDA)

如何解决此错误?

要将变量传输到GPU,请尝试以下操作:

device=torch.device('cuda:0'如果torch.cuda.is_可用(),则为'cpu')
X_列=火炬.浮子张量(X_列).至(装置)
X_测试=火炬.浮子张量(X_测试).至(装置)
y_列=火炬长传感器(y_列)至(装置)
y_测试=火炬长传感器(y_测试)至(装置)

创建张量X#train=torch.FloatTensor(X#train).cuda()X#u test=torch.FloatTensor(X#test).cuda()y#u train=torch.lontensor(y#train).cuda()y#u test=torch.lontensor.lontensor(y#test.cuda().cuda()请阅读如何和使用按钮改进帖子。错误是:>>X#train=torch:File“”,第1行,类型错误:预期CPU(获得CUDA)@VinothKumarS,您的m/c是否有GPU?您是否使用混合的CPU/GPU类型执行任何操作?pytorch是GPU版本吗?显示完整的错误消息@anurag是的,我的机器的GPU是RTX 3070,它是PyTorch GPU版本。
cuda:0
-这里将
0
更改为所需的GPU索引(如果您有多个GPU)非常感谢先生!它现在可以工作了,你能解释一下为什么我们需要使用它吗?我不知道为什么你的代码不能工作,但是,根据讨论,应该没有任何区别!此外,如果这解决了您的问题,请向上投票/接受!
Traceback (most recent call last):
  File "<stdin>", line 24, in <module>
TypeError: expected CPU (got CUDA)