完整报错:
RuntimeError: stack expects each tensor to be equal size, but got [4] at entry 0 and [3] at entry 30
问题所在:
使用pytorch的DataSet类中的getitem方法切分数据时,最后一个数据切分不完整。
解决办法:
重写len方法,把数据集返回长度处理一下即可,避免有不完整的数据留下
from torch.utils.data import Dataset
class MyDataset(Dataset):
def __init__(self,data,days_for_train):
self.data = data
self.days_for_train = days_for_train
self.label_len = 0
self.pred_len = 1
def __len__(self):
# 返回数据集的大小
return len(self.data) - self.days_for_train + 1
def __getitem__(self, index):
s_begin = index
s_end = s_begin + self.days_for_train
r_begin = s_end
r_end = r_begin + self.pred_len
seq_x = self.data[s_begin:s_end]
seq_y = self.data[r_begin:r_end]
return seq_x, seq_y