将CSV解析为Pytorch张量

Hil*_*urn 2 python pandas pytorch

我有一个CSV文件,其中包含标题行以外的所有数字值。尝试构建张量时,出现以下异常:

Traceback (most recent call last):
  File "pytorch.py", line 14, in <module>
    test_tensor = torch.tensor(test)
ValueError: could not determine the shape of object type 'DataFrame'
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

import torch
import dask.dataframe as dd

device = torch.device("cuda:0")

print("Loading CSV...")
test = dd.read_csv("test.csv", encoding = "UTF-8")
train = dd.read_csv("train.csv", encoding = "UTF-8")

print("Converting to Tensor...")
test_tensor = torch.tensor(test)
train_tensor = torch.tensor(train)
Run Code Online (Sandbox Code Playgroud)

使用pandas而不是DaskCSV解析会产生相同的错误。我也尝试dtype=torch.float64在对的调用中指定torch.tensor(data),但再次遇到相同的错误。

Ara*_*ash 8

我想你只是失踪了 .values

import torch
import pandas as pd

train = pd.read_csv('train.csv')
train_tensor = torch.tensor(train.values)
Run Code Online (Sandbox Code Playgroud)


Dis*_*ani 7

强烈建议使用to_numpy较新版本的 pandasvalues

train_tensor = torch.tensor(train.to_numpy())
Run Code Online (Sandbox Code Playgroud)


小智 5

尝试先将其转换为数组:

test_tensor = torch.Tensor(test.values)
Run Code Online (Sandbox Code Playgroud)