-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
92 lines (75 loc) · 2.89 KB
/
datasets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os, sys, random
import numpy as np
import scipy.io as sio
import torch
from torch.utils.data.sampler import SequentialSampler, RandomSampler
class TrainDataset_Com(torch.utils.data.Dataset):
def __init__(self, X_list, Y_list):
self.X_list = X_list
self.Y_list = Y_list
self.view_size = len(X_list)
def __getitem__(self, index):
current_x_list = []
current_y_list = []
for v in range(self.view_size):
current_x = self.X_list[v][index]
current_x_list.append(current_x)
current_y = self.Y_list[v][index]
current_y_list.append(current_y)
# X_list1 = self.X_list
# Y_list1 = self.Y_list
return current_x_list, current_y_list
def __len__(self):
# return the total size of data
return self.X_list[0].shape[0]
class TrainDataset_All(torch.utils.data.Dataset):
def __init__(self, X_list, Y_list, Miss_list):
self.X_list = X_list
self.Y_list = Y_list
self.Miss_list = Miss_list
self.view_size = len(X_list)
def __getitem__(self, index):
current_x_list = []
current_y_list = []
# 缺失数据
current_miss_list = []
for v in range(self.view_size):
current_x = self.X_list[v][index]
current_x_list.append(current_x)
current_y = self.Y_list[v][index]
current_y_list.append(current_y)
#返回缺失数据集的缺失标签
current_miss = self.Miss_list[v][index]
current_miss_list.append(current_miss)
# X_list1 = self.X_list
# Y_list1 = self.Y_list
return current_x_list, current_y_list, current_miss_list
def __len__(self):
# return the total size of data
return self.X_list[0].shape[0]
class Data_Sampler(object):
"""Custom Sampler is required. This sampler prepares batch by passing list of
data indices instead of running over individual index as in pytorch sampler"""
def __init__(self, pairs, shuffle=False, batch_size=1, drop_last=False):
if shuffle:
self.sampler = RandomSampler(pairs)
else:
self.sampler = SequentialSampler(pairs)
self.batch_size = batch_size
self.drop_last = drop_last
def __iter__(self):
batch = []
for idx in self.sampler:
batch.append(idx)
if len(batch) == self.batch_size:
batch = [batch]
yield batch
batch = []
if len(batch) > 0 and not self.drop_last:
batch = [batch]
yield batch
def __len__(self):
if self.drop_last:
return len(self.sampler) // self.batch_size
else:
return (len(self.sampler) + self.batch_size - 1) // self.batch_size