-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
60 lines (46 loc) · 1.71 KB
/
model.py
File metadata and controls
60 lines (46 loc) · 1.71 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import torch
import torch.nn as nn
import numpy as np
class Autorec(nn.Module):
def __init__(self, args, num_users, num_items):
super(Autorec, self).__init__()
self.args = args
self.num_users = num_users
self.num_items = num_items
self.hidden_units = args.hidden_units
self.lambda_value = args.lambda_value
self.encoder = nn.Sequential(
nn.Linear(self.num_items, self.hidden_units),
nn.Sigmoid()
)
self.decoder = nn.Sequential(
nn.Linear(self.hidden_units, self.num_items),
)
def forward(self, torch_input):
encoder = self.encoder(torch_input)
decoder = self.decoder(encoder)
return decoder
def loss(self, decoder, input, optimizer, mask_input):
cost = 0
temp2 = 0
cost += ((decoder - input) * mask_input).pow(2).sum()
rmse = cost
for i in optimizer.param_groups:
for j in i['params']:
# print(type(j.data), j.shape,j.data.dim())
if j.data.dim() == 2:
temp2 += torch.t(j.data).pow(2).sum()
cost += temp2 * self.lambda_value * 0.5
return cost, rmse
def saveModel(self, location):
torch.save(self.state_dict(), location)
def loadModel(self, path, map_location):
state_dict = torch.load(path, map_location=map_location)
self.load_state_dict(state_dict, strict=False)
def recommend_user(self, r_u):
p_rate_dict = dict()
predict = self.forward(torch.from_numpy(r_u).float())
predict = predict.detach().numpy()
for i in range(len(predict)):
p_rate_dict[i] = predict[i]
return p_rate_dict