gpt4 book ai didi

python - 将 CSV 解析为 Pytorch 张量

转载 作者:行者123 更新时间:2023-11-28 20:13:57 24 4
gpt4 key购买 nike

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

这是我的代码:

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)

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

最佳答案

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

test_tensor = torch.Tensor(test.values)

关于python - 将 CSV 解析为 Pytorch 张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51858067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com