-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (34 loc) · 1.19 KB
/
utils.py
File metadata and controls
46 lines (34 loc) · 1.19 KB
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
import torch
import pickle
import torch.nn as nn
from torch.utils.data import Dataset
# initialize the model weights
def weights_init_(m):
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight, gain=1)
nn.init.constant_(m.bias, 0)
# import dataset for training
# this is used for everything but bc-rnn
class MyData(Dataset):
def __init__(self, data, loadname=None):
if loadname:
self.data = pickle.load(open(loadname, "rb"))
self.data = torch.FloatTensor(self.data)
else:
self.data = torch.FloatTensor(data)
print("imported dataset of length:", len(self.data))
def __len__(self):
return len(self.data)
def __getitem__(self,idx):
return self.data[idx]
# import dataset with history for training
# this is used for bc-rnn
class MyDataHistory(Dataset):
def __init__(self, loadname):
self.data = pickle.load(open(loadname, "rb"))
print("imported dataset of length:", len(self.data))
def __len__(self):
return len(self.data)
def __getitem__(self,idx):
return (torch.FloatTensor(self.data[idx][0]),
torch.FloatTensor(self.data[idx][1]))