Question
import pandas as pd import numpy as np import torch import torch.nn as nn import torch.nn.functional as F def proc_col(col, train_col=None): Encodes a pandas column
import pandas as pd import numpy as np
import torch import torch.nn as nn import torch.nn.functional as F
def proc_col(col, train_col=None): """Encodes a pandas column with continous ids. """ if train_col is not None: uniq = train_col.unique() else: uniq = col.unique() name2idx = {o:i for i,o in enumerate(uniq)} return name2idx, np.array([name2idx.get(x, -1) for x in col]), len(uniq)
def encode_data(df, train=None): """ Encodes rating data with continous user and movie ids. If train is provided, encodes df with the same encoding as train. """ df = df.copy() for col_name in ["user", "item"]: train_col = None if train is not None: train_col = train[col_name] _,col,_ = proc_col(df[col_name], train_col) df[col_name] = col df = df[df[col_name] >= 0] return df
class MF(nn.Module): def __init__(self, num_users, num_items, emb_size=100, seed=23): super(MF, self).__init__() torch.manual_seed(seed) self.user_emb = nn.Embedding(num_users, emb_size) self.user_bias = nn.Embedding(num_users, 1) self.item_emb = nn.Embedding(num_items, emb_size) self.item_bias = nn.Embedding(num_items, 1) # init self.user_emb.weight.data.uniform_(0,0.05) self.item_emb.weight.data.uniform_(0,0.05) self.user_bias.weight.data.uniform_(-0.01,0.01) self.item_bias.weight.data.uniform_(-0.01,0.01)
def forward(self, u, v): # Compute user embedding and bias u_emb = self.user_emb(u) u_bias = self.user_bias(u) # Compute item embedding and bias v_emb = self.item_emb(v) v_bias = self.item_bias(v) # Calculate predicted rating pred = torch.sum(u_emb*v_emb, dim=1) + u_bias.squeeze() + v_bias.squeeze() pred = torch.sigmoid(pred) return pred
def train_one_epoch(model, train_df, optimizer): """ Trains the model for one epoch""" model.train() ### BEGIN SOLUTION
###END SOLUTION return loss.item()
def valid_metrics(model, valid_df): """Computes validation loss and accuracy""" ### BEGIN SOLUTION
### END SOLUTION
def training(model, train_df, valid_df, epochs=10, lr=0.01, wd=0.0): optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=wd) for i in range(epochs): train_loss = train_one_epoch(model, train_df, optimizer) valid_loss, valid_acc = valid_metrics(model, valid_df) print("train loss %.3f valid loss %.3f valid acc %.3f" % (train_loss, valid_loss, valid_acc))
I just need help writing two functions: valid_metrics(model, valid_df): Computes validation loss and accuracy and train_one_epoch(model, train_df, optimizer): Trains the model for one epoch
here's the test cases:
def init_model_and_data(): train = pd.read_csv("tiny_data/train_books_ratings_tiny.csv") valid = pd.read_csv("tiny_data/valid_books_ratings_tiny.csv") train_df = encode_data(train, train=None) valid_df = encode_data(valid, train=train) num_users = len(train.user.unique()) num_items = len(train.item.unique()) model = MF(num_users, num_items) return model, train_df, valid_df
def test_train_one_epoch(): model, df, _ = init_model_and_data() optimizer = torch.optim.Adam(model.parameters(), lr=0.1, weight_decay=0) for i in range(3): train_loss = train_one_epoch(model, df, optimizer) assert(np.around(train_loss, decimals=2) == 0.53)
def test_val_metrics(): model, df, _ = init_model_and_data() loss, acc = valid_metrics(model, df) assert(np.around(loss, decimals=2) == 0.73)
Given a training and validation dataset with amazon ratings of books (train_books_ratings.csv and valid_books_ratings.csv), use PyTorch to implement the following model: y^ij=(uivj+bi+cj), where uivj represents a traditional matrix factorization model and bi,cj are bias terms for the users and items. Note that this is a binary dataset, and the loss function for this model is binary cross entropy. Write your code based on the notebook: MF_with_pytorch.ipynb
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started