python - 将 CSV 解析为 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'
这是我的代码:
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 解析会产生相同的错误。我还尝试dtype=torch.float64
在对 的调用中指定torch.tensor(data)
,但再次遇到相同的错误。
解决方案
尝试先将其转换为数组:
test_tensor = torch.Tensor(test.values)
推荐阅读
- javascript - 如何使用javascript根据背景颜色更改圆形填充?
- api - 在 Next js 中从表单数据中读取数据时遇到问题
- javascript - HTML canvas shows tiny slice of adjacent image in tileset
- c# - Unity3D activeSelf function always returns true
- rust - 使用 hyper/azure sdk 返回流的终生问题
- docker - abernix/meteord docker build 在尝试构建流星应用程序时失败
- node.js - nodemon watch vs pm2 watch
- c++ - 接收数组作为指针参数的问题
- php - PHP - 转换数组多维键
- c - 如何在 Clang 中使用 WebAssembly 引用类型