Module reclab.recommenders.autorec.autorec_lib.autorec
Expand source code
import torch
class AutoRec(torch.nn.Module):
def __init__(self, num_users, num_items,
seen_users, seen_items,
hidden_neuron,
dropout=0.05, random_seed=0):
super(AutoRec, self).__init__()
self.num_users = num_users
self.num_items = num_items
self.seen_users = seen_users
self.seen_items = seen_items
self.hidden_neuron = hidden_neuron
self.random_seed = random_seed
self.dropout_p = dropout
self.sigmoid = torch.nn.Sigmoid()
def loss(self, pred, test, mask, lambda_value=1):
mse = (((pred * mask) - test) ** 2).sum()
reg_value_enc = torch.mul(lambda_value / 2, list(self.encoder.parameters())[0].norm(p='fro') ** 2)
reg_value_dec = torch.mul(lambda_value / 2, list(self.decoder.parameters())[0].norm(p='fro') ** 2)
return torch.add(mse, torch.add(reg_value_enc, reg_value_dec))
def prepare_model(self):
self.encoder = torch.nn.Linear(self.num_users, self.hidden_neuron, bias=True)
self.dropout = torch.nn.Dropout(p=self.dropout_p)
self.decoder = torch.nn.Linear(self.hidden_neuron, self.num_users, bias=True)
def forward(self, x):
x = self.encoder(x)
x = self.sigmoid(x)
x = self.dropout(x)
x = self.decoder(x)
return x
def predict(self, user_item, test_data):
users = [triple[0] for triple in user_item]
items = [triple[1] for triple in user_item]
user_item = zip(users, items)
user_idx = set(users)
item_idx = set(items)
Estimated_R = self.forward(test_data)
for item in range(test_data.shape[0]):
for user in range(test_data.shape[1]):
if user not in self.seen_users and item not in self.seen_items:
Estimated_R[item, user] = 3
idx = [tuple(users), tuple(items)]
Estimated_R = Estimated_R.clamp(1, 5)
return Estimated_R.T[idx].cpu().detach().numpy()
Classes
class AutoRec (num_users, num_items, seen_users, seen_items, hidden_neuron, dropout=0.05, random_seed=0)
-
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self): super(Model, self).__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:
to
, etc.Initializes internal Module state, shared by both nn.Module and ScriptModule.
Expand source code
class AutoRec(torch.nn.Module): def __init__(self, num_users, num_items, seen_users, seen_items, hidden_neuron, dropout=0.05, random_seed=0): super(AutoRec, self).__init__() self.num_users = num_users self.num_items = num_items self.seen_users = seen_users self.seen_items = seen_items self.hidden_neuron = hidden_neuron self.random_seed = random_seed self.dropout_p = dropout self.sigmoid = torch.nn.Sigmoid() def loss(self, pred, test, mask, lambda_value=1): mse = (((pred * mask) - test) ** 2).sum() reg_value_enc = torch.mul(lambda_value / 2, list(self.encoder.parameters())[0].norm(p='fro') ** 2) reg_value_dec = torch.mul(lambda_value / 2, list(self.decoder.parameters())[0].norm(p='fro') ** 2) return torch.add(mse, torch.add(reg_value_enc, reg_value_dec)) def prepare_model(self): self.encoder = torch.nn.Linear(self.num_users, self.hidden_neuron, bias=True) self.dropout = torch.nn.Dropout(p=self.dropout_p) self.decoder = torch.nn.Linear(self.hidden_neuron, self.num_users, bias=True) def forward(self, x): x = self.encoder(x) x = self.sigmoid(x) x = self.dropout(x) x = self.decoder(x) return x def predict(self, user_item, test_data): users = [triple[0] for triple in user_item] items = [triple[1] for triple in user_item] user_item = zip(users, items) user_idx = set(users) item_idx = set(items) Estimated_R = self.forward(test_data) for item in range(test_data.shape[0]): for user in range(test_data.shape[1]): if user not in self.seen_users and item not in self.seen_items: Estimated_R[item, user] = 3 idx = [tuple(users), tuple(items)] Estimated_R = Estimated_R.clamp(1, 5) return Estimated_R.T[idx].cpu().detach().numpy()
Ancestors
- torch.nn.modules.module.Module
Methods
def forward(self, x)
-
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the :class:
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.Expand source code
def forward(self, x): x = self.encoder(x) x = self.sigmoid(x) x = self.dropout(x) x = self.decoder(x) return x
def loss(self, pred, test, mask, lambda_value=1)
-
Expand source code
def loss(self, pred, test, mask, lambda_value=1): mse = (((pred * mask) - test) ** 2).sum() reg_value_enc = torch.mul(lambda_value / 2, list(self.encoder.parameters())[0].norm(p='fro') ** 2) reg_value_dec = torch.mul(lambda_value / 2, list(self.decoder.parameters())[0].norm(p='fro') ** 2) return torch.add(mse, torch.add(reg_value_enc, reg_value_dec))
def predict(self, user_item, test_data)
-
Expand source code
def predict(self, user_item, test_data): users = [triple[0] for triple in user_item] items = [triple[1] for triple in user_item] user_item = zip(users, items) user_idx = set(users) item_idx = set(items) Estimated_R = self.forward(test_data) for item in range(test_data.shape[0]): for user in range(test_data.shape[1]): if user not in self.seen_users and item not in self.seen_items: Estimated_R[item, user] = 3 idx = [tuple(users), tuple(items)] Estimated_R = Estimated_R.clamp(1, 5) return Estimated_R.T[idx].cpu().detach().numpy()
def prepare_model(self)
-
Expand source code
def prepare_model(self): self.encoder = torch.nn.Linear(self.num_users, self.hidden_neuron, bias=True) self.dropout = torch.nn.Dropout(p=self.dropout_p) self.decoder = torch.nn.Linear(self.hidden_neuron, self.num_users, bias=True)