pandas 将 CSV 解析为 Pytorch 张量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51858067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:56:36  来源:igfitidea点击:

Parsing CSV into Pytorch tensors

pythonpandaspytorch

提问by hildebro

I have a CSV files with all numeric values except the header row. When trying to build tensors, I get the following exception:

我有一个 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'

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 pandasinstead of Daskfor CSV parsing produced the same error. I also tried to specify dtype=torch.float64inside the call to torch.tensor(data), but got the same error again.

使用pandas代替DaskCSV 解析产生了相同的错误。我还尝试dtype=torch.float64在对 的调用中指定torch.tensor(data),但再次出现相同的错误。

采纳答案by karla

Try converting it to an array first:

首先尝试将其转换为数组:

test_tensor = torch.Tensor(test.values)

回答by Arash

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)