【问题标题】:Correct dimensions to convert a wide data frame to Pytorch tensor object?将宽数据框转换为 Pytorch 张量对象的正确尺寸?
【发布时间】:2020-01-05 09:37:53
【问题描述】:

我有这个时间序列数据框,它有 56 列和 36508 个样本; 55 个是预测变量,最后一个是输出。我正在尝试拟合 LSTM 神经网络,虽然我能够拟合模型,但我很难将特征转换为 Pytorch 张量对象。目前我已经对 0 和 1 之间的数据进行了归一化,并将数据拆分为训练集和测试集。

import torch
import torch.nn as nn

print(x_train.shape)
(27380, 55)

print(y_train.shape)
(27380,)

print(x_test.shape)
(9128, 55)

print(y_test.shape)
(9128,)

我将目标转换为张量对象没有问题,因为该系列只有 1D,如下所示:

y_train = torch.FloatTensor(y_train).view(-1)
print(y_train[:5])
tensor([0.7637, 0.6220, 0.6566, 0.6922, 0.6774])

但是在转换特征时,我无法确定需要指定的尺寸。我试过这个:

x_train = torch.FloatTensor(x_train).view(-1, 55)

ValueError: could not determine the shape of object type 'DataFrame'

如何正确地将特征数据集转换为张量对象?遗憾的是,文档似乎含糊不清。

【问题讨论】:

    标签: python tensorflow pytorch


    【解决方案1】:

    尝试先转换为 numpy,然后再转换为张量:

    x_train = torch.from_numpy(np.array(x_train).astype(np.float32))
    

    【讨论】:

    • 发现torch.FloatTensor(x_train.values.reshape(27380, 55))也会产生张量数组。
    猜你喜欢
    • 2022-10-17
    • 1970-01-01
    • 2018-10-22
    • 2018-11-12
    • 2020-06-25
    • 2020-08-05
    • 2019-07-29
    相关资源
    最近更新 更多