[英]Parsing CSV into Pytorch tensors
I have a CSV files with all numeric values except the header row.我有一个 CSV 文件,其中包含除 header 行以外的所有数值。 When trying to build tensors, I get the following exception:
尝试构建张量时,出现以下异常:
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'
This is my code:这是我的代码:
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)
Using pandas
instead of Dask
for CSV parsing produced the same error.使用
pandas
而不是Dask
进行 CSV 解析产生了同样的错误。 I also tried to specify dtype=torch.float64
inside the call to torch.tensor(data)
, but got the same error again.我还尝试在对
torch.tensor(data)
的调用中指定dtype=torch.float64
,但再次遇到相同的错误。
首先尝试将其转换为数组:
test_tensor = torch.Tensor(test.values)
I think you're just missing .values
我认为你只是缺少
.values
import torch
import pandas as pd
train = pd.read_csv('train.csv')
train_tensor = torch.tensor(train.values)
Only using NumPy仅使用 NumPy
import numpy as np
import torch
tensor = torch.from_numpy(
np.genfromtxt("train.csv", delimiter=",")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.