})

print("Model intialized")
print("Beginning Training...")

trainer = NeuralUtilityTrainer(users=X_train[:, 0].reshape(-1, 1),
                               items=X_train[:, 1:].reshape(-1, 1),
                               y_train=y_train,
                               model=mlp,
                               loss=loss_logit,
                               n_epochs=params['n_epochs'],
                               batch_size=params['batch_size'],
                               lr=params["lr"],
                               loss_step_print=params["loss_step"],
                               eps=params["eps"],
                               item_rating_map=item_rating_map,
                               user_item_rating_map=user_item_rating_map,
                               c_size=params["c_size"],
                               s_size=params["s_size"],
                               n_items=stats["n_items"],
                               use_cuda=args.cuda,
                               model_name=MODEL_NAME,
                               model_path=MODEL_DIR,
                               checkpoint=args.checkpoint,
                               lmbda=params["lambda"],
                               max_iter=params["max_iter"],
                               num_workers=params["num_workers"])

if params['loss'] == 'utility':
    print("utility loss")
    trainer.fit_utility_loss()
elif params['loss'] == 'logit':

    grad_linear = linear.coef_.flatten()
    mrs_linear = compute_pariwise_mrs(grad_linear)
    l2_linear = mrs_error(MRS, mrs_linear)
    output['linear'].append(l2_linear)


    # Train Vanilla Wide&Deep
    encoder = UtilityEncoder(stats['n_items'], h_dim_size=params["h_dim_size"], use_cuda=False)

    trainer = NeuralUtilityTrainer(users=X[:, 0].reshape(-1, 1), items=X[:, 1].reshape(-1, 1),
                                   y_train=y, model=encoder, loss=loss_mse,
                                   n_epochs=params['n_epochs'], batch_size=params['batch_size'],
                                   lr=params["lr"], loss_step_print=params["loss_step"],
                                   eps=params["eps"], item_rating_map=item_rating_map,
                                   user_item_rating_map=user_item_rating_map,
                                   c_size=params["c_size"], s_size=params["s_size"],
                                   n_items=stats["n_items"], use_cuda=False,
                                   model_name=None, model_path=None,
                                   checkpoint=False, lmbda=params["lambda"])

    _ = trainer.fit()

    grad_vanilla = NeuralUtilityTrainer.get_gradient(encoder, loss_mse, users, items_for_grad, y_true)
    mrs_vanilla = compute_pariwise_mrs(grad_vanilla)
    l2_vanilla = mrs_error(MRS, mrs_vanilla)
    output['vanilla'].append(l2_vanilla)



    # Train Neural Utility Function
test = np.concatenate([X_test, y_test], axis=1)

X = np.concatenate((train, test), axis=0)

df = pd.DataFrame(X, columns=["user_id", "item_id", "rating"])
item_means = df[['item_id', 'rating']].groupby("item_id").mean()

users = None
items_for_grad = torch.from_numpy(items_for_grad)
y_true = torch.from_numpy(item_means.values.reshape(-1, 1))

# Load Model
model = torch.load(MODEL_PATH, map_location=torch.device('cpu'))

# compute gradietns
gradients = NeuralUtilityTrainer.get_gradient(model, loss_mse, users,
                                              items_for_grad, y_true)
print(gradients)

mrs = get_mrs_arr(gradients)
item_idx = item_to_idx[1]

item = mrs[item_idx, :]
print(item)
supp = get_supp_k(item, k=5)
comp = get_comp_k(item, k=5)

print("Item: {}".format(item_map[item_idx]))
print("Supplements: ")
for i in supp:
    print(item_map[i], i, gradients[i])
print("Complements: ")
Beispiel #4
0
        return y_hat


data_dir = cfg.vals['movielens_dir'] + "/preprocessed/"

df = pd.read_csv(data_dir + "ratings.csv")

X = df[['user_id', 'item_id']].values.astype(np.int64)
y = df['rating'].values.reshape(-1, 1)

user_item_rating_map = load_dict_output(data_dir, "user_item_rating.json",
                                        True)
item_rating_map = load_dict_output(data_dir, "item_rating.json", True)
stats = load_dict_output(data_dir, "stats.json")

X_train, X_test, y_train, y_test = split_train_test_user(X, y)

mlp = MLP(64, 32, 16)

model = NeuralUtility(backbone=mlp, n_items=stats['n_items'], h_dim_size=64)


trainer = NeuralUtilityTrainer(X_train=X_train, y_train=y_train, model=model, loss=loss_mse, \
                               n_epochs=5, batch_size=32, lr=1e-3, loss_step_print=25, eps=.001,
                               item_rating_map=item_rating_map, user_item_rating_map=user_item_rating_map,
                               c_size=5, s_size=5, n_items=stats["n_items"], checkpoint=True,
                               model_path=None, model_name='test_nuf')

#trainer.fit()
trainer.fit_utility_loss()