-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
95 lines (75 loc) · 2.69 KB
/
utils.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
93
94
95
import numpy as np
import torch
class AverageMeter(object):
"""Adapted from: https://github.com/pytorch/examples/blob/master/imagenet/main.py
"""
def __init__(self, name, fmt=".4f"):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = f"{self.name} {self.val:{self.fmt}} ({self.avg:{self.fmt}})"
return fmtstr
class BatchDataset(torch.utils.data.Dataset):
def __init__(self, userInput, itemInput, labels):
super(BatchDataset, self).__init__()
self.userInput = torch.Tensor(userInput).long()
self.itemInput = torch.Tensor(itemInput).long()
self.labels = torch.Tensor(labels)
def __getitem__(self, index):
return self.userInput[index], self.itemInput[index], self.labels[index]
def __len__(self):
return self.labels.size(0)
class TestDataset(torch.utils.data.Dataset):
def __init__(self, userInput, itemInput):
super(TestDataset, self).__init__()
self.userInput = torch.Tensor(userInput).long()
self.itemInput = torch.Tensor(itemInput).long()
def __getitem__(self, index):
return self.userInput[index], self.itemInput[index]
def __len__(self):
return self.userInput.size(0)
def get_optimizer(name, lr, scope):
if name.lower() == "adagrad":
return torch.optim.Adagrad(scope, lr=lr)
elif name.lower() == "rmsprop":
return torch.optim.RMSprop(scope, lr=lr)
elif name.lower() == "adam":
return torch.optim.Adam(scope, lr=lr)
elif name.lower() == "sgd":
return torch.optim.SGD(scope, lr=lr)
else:
raise ValueError(f"{name} optimizer is not supported!")
def get_train_matrix(train):
nUsers, nItems = train.shape
trainMatrix = np.zeros([nUsers, nItems], dtype=np.int32)
for (u, i) in train.keys():
trainMatrix[u][i] = 1
return trainMatrix
def get_train_instances(train, nNeg):
userInput, itemInput, labels = [], [], []
nUsers, nItems = train.shape
for (u, i) in train.keys():
# positive instance
userInput.append(u)
itemInput.append(i)
labels.append(1)
# negative instances
for t in range(nNeg):
j = np.random.randint(nItems)
while (u, j) in train.keys():
j = np.random.randint(nItems)
userInput.append(u)
itemInput.append(j)
labels.append(0)
return userInput, itemInput, labels