简体   繁体   English

将宽数据框转换为 Pytorch 张量对象的正确尺寸?

[英]Correct dimensions to convert a wide data frame to Pytorch tensor object?

I have this time-series data frame that has 56 columns and 36508 samples;我有这个时间序列数据框,它有 56 列和 36508 个样本; 55 are predictors and the last one is the output. 55 个是预测变量,最后一个是输出。 I'm trying to fit a LSTM neural network and while I'll be able to fit the model, I'm having some hard time converting the features to Pytorch tensor objects.我正在尝试拟合 LSTM 神经网络,虽然我能够拟合模型,但我很难将特征转换为 Pytorch 张量对象。 Currently I have already normalised the data between 0 and 1 and also split the data into train and test sets.目前我已经对 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,)

I've had no problems converting the target to a tensor object, since the series is only 1D like so:我将目标转换为张量对象没有问题,因为该系列只是一维,如下所示:

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

But when it comes to converting the features, then I'm unable to figure out the dimensions that need to be specified.但是在转换功能时,我无法弄清楚需要指定的尺寸。 I've tried this:我试过这个:

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

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

How do I properly convert the features dataset to a tensor object?如何将要素数据集正确转换为张量对象? Sadly the documentation seems vague.可悲的是,文档似乎含糊不清。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM