如何解决类型错误:“torch.dtype”对象不可调用如何调用这个函数?
如何调用这个torch.dtype?因为这里的错误表明它不可调用。在我使用 floatTensor 之前它显示了这样的错误 can't convert np.ndarray of type numpy.object_
现在我使用 float64 它显示了错误 'torch.dtype' object is not callable
。请帮助解决这个问题。
import torch
a = torch.cuda.is_available()
b = torch.cuda.current_device()
c = torch.cuda.get_device_name()
d = torch.cuda.memory_reserved()
e = torch.cuda.memory_allocated()
var1 = torch.FloatTensor([1.0,2.0,3.0]).cuda()
var1
a1 = var1.device
import pandas as pd
df = pd.read_csv('diabetes.csv')
df.head()
b1 = df.isnull().sum()
import seaborn as sns
import numpy as np
df['Outcome']=np.where(df['Outcome']==1,"Diabetic","No Diabetic")
b2 = df.head()
b3 = sns.pairplot(df,hue="Outcome")
X=df.drop('Outcome',axis=1).values
y=df['Outcome'].values
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.float64(y_train).cuda()
这是错误:
C:\Users\vinot\.conda\envs\python21\python.exe D:/python/python_work/pythonProject/diabetes.py
Traceback (most recent call last):
File "D:/python/python_work/pythonProject/diabetes.py",line 35,in <module>
y_train=torch.float64(y_train).cuda()
TypeError: 'torch.dtype' object is not callable
Process finished with exit code 1
解决方法
torch.float64 是 dtype 对象而不是函数,因此无法调用。
要使其成为双浮点数(或至少确保它是),我会改为调用:
y_train = torch.from_numpy(y_train).double().cuda()
,
在 PyTorch 中,您可以通过 Tensor.type(dtype)
更改 Tensor 的类型,您可以通过 this link 查看您需要的类型。此外,我建议您首先检查您的 GPU 是否可用,然后为 float32
选择 float64
(在大多数情况下,32 位已足够复杂)。所以那行应该变成:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
y_train = y_train.type(torch.float).to(device) # change torch.float to torch.float64 if you still want to use float 64
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。