python - 将 CSV 解析为 Pytorch 张量

标签 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'

这是我的代码:

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/

相关文章:

python - 从 URL 到 `pandas.DataFrame` 的 Excel 工作簿表

python - pytorch 在张量的某个维度上调用 unique_consecutive() 的最佳方法?

deployment - 保存经过训练的 Detectron2 模型并对单个图像进行预测

python - WxPython[文本Ctrl] : The way of locate the cursor to end of text using TextCtrl function.

python - Django 中 DateTimeField 过滤器的问题

python - Django 在 View 之间传递数据

python - 如何在 pyspark 中创建具有随机值的新列?

python - 在 Pygame 中加载与角色 Sprite 碰撞时爆炸的图像

python - 不支持的操作数类型 - : 'str' and 'int' in python

python - 将剩余连接添加到简单的 CNN