TypeError:“torch.dtype”对象不可调用。如何调用这个函数?

问题描述 投票:0回答:2

如何调用这个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
python pytorch torch dtype
2个回答
3
投票

在PyTorch中,您可以通过

Tensor.type(dtype)
更改Tensor的类型,您可以通过此链接查看您需要什么类型。此外,我建议您首先检查您的 GPU 是否可用,然后选择
float32
代替
float64
(在大多数情况下,32 位足够复杂)。所以该行应该变成:

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
y_train = y_train.type(torch.float32).to(device) # change torch.float32 to torch.float64 if you still want to use float 64

2
投票

torch.float64 是一个 dtype 对象,而不是一个函数,因此无法调用它。

为了使其成为双浮点(或至少确保它是),我会调用:

y_train = torch.from_numpy(y_train).double().cuda()
© www.soinside.com 2019 - 2024. All rights reserved.