--- title: TAGNN++ keywords: fastai sidebar: home_sidebar summary: "Mitheran et. al. Improved Representation Learning for Session-based Recommendation. arXiv, 2021." description: "Mitheran et. al. Improved Representation Learning for Session-based Recommendation. arXiv, 2021." nb_path: "nbs/models/tagnn_pp.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}

## `class` `Attention_GNN`[source]

> `Attention_GNN`(`hidden_size`, `step`=`1`) :: `Module`

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.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

{% endraw %} {% raw %}
{% endraw %} {% raw %}

## `class` `TAGNN_PP`[source]

> `TAGNN_PP`(`opt`) :: `Module`

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.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

{% endraw %} {% raw %}
{% endraw %}

## Training a session-based recommender using TAGNN++

{% raw %}

    class Args():
        dataset = 'sample'
        batchSize = 100 # input batch size
        hiddenSize = 100 # hidden state size
        epoch = 5 # the number of epochs to train for
        lr = 0.001 # learning rate')  # [0.001, 0.0005, 0.000
        lr_dc = 0.1 # learning rate decay rate
        lr_dc_step = 3 # the number of steps after which the learning rate decay
        l2 = 1e-5 # l2 penalty')  # [0.001, 0.0005, 0.0001, 0.00005, 0.0000
        step = 1 # gnn propogation steps
        patience = 10 # the number of epoch to wait before early stop
        nonhybrid = True # only use the global preference to predict
        validation = True # validation
        valid_portion = 0.1 # split the portion of training set as validation set
        n_node = 310

    args = Args()

{% endraw %} {% raw %}

    def to_cuda(input_variable):
        if torch.cuda.is_available():
            return input_variable.cuda()
        else:
            return input_variable

    def to_cpu(input_variable):
        if torch.cuda.is_available():
            return input_variable.cpu()
        else:
            return input_variable

    def forward(model, i, data):
        alias_inputs, A, items, mask, targets = data.get_slice(i)
        alias_inputs = to_cuda(torch.Tensor(alias_inputs).long())
        items = to_cuda(torch.Tensor(items).long())
        A = to_cuda(torch.Tensor(A).float())
        mask = to_cuda(torch.Tensor(mask).long())
        hidden = model(items, A)

        def get(i): return hidden[i][alias_inputs[i]]
        seq_hidden = torch.stack([get(i)
                                 for i in torch.arange(len(alias_inputs)).long()])

        return targets, model.compute_scores(seq_hidden, mask)

    def train_test(model, train_data, test_data):
        model.scheduler.step()
        print('Start training: ', datetime.datetime.now())
        model.train()
        total_loss = 0.0
        slices = train_data.generate_batch(model.batch_size)

        from tqdm.notebook import tqdm
        for i, j in tqdm(zip(slices, np.arange(len(slices))), total=len(slices)):
            model.optimizer.zero_grad()
            targets, scores = forward(model, i, train_data)
            targets = to_cuda(torch.Tensor(targets).long())
            loss = model.loss_function(scores, targets - 1)
            loss.backward()
            model.optimizer.step()
            total_loss += loss.item()

            if j % int(len(slices) / 5 + 1) == 0:
                print('[%d/%d] Loss: %.4f' % (j, len(slices), loss.item()))

        print('\tLoss Value:\t%.3f' % total_loss)
        print('Start Prediction: ', datetime.datetime.now())

        model.eval()
        hit, mrr = [], []
        slices = test_data.generate_batch(model.batch_size)

        for i in slices:
            targets, scores = forward(model, i, test_data)
            sub_scores = scores.topk(20)[1]
            sub_scores = to_cpu(sub_scores).detach().numpy()

            for score, target, mask in zip(sub_scores, targets, test_data.mask):
                hit.append(np.isin(target - 1, score))
                if len(np.where(score == target - 1)[0]) == 0:
                    mrr.append(0)
                else:
                    mrr.append(1 / (np.where(score == target - 1)[0][0] + 1))

        hit = np.mean(hit) * 100
        mrr = np.mean(mrr) * 100
        return hit, mrr

    def get_pos(seq_len):
        return torch.arange(seq_len).unsqueeze(0)

    def str2bool(v):
        return v.lower() in ('true')

    def split_validation(train_set, valid_portion):
        train_set_x, train_set_y = train_set
        n_samples = len(train_set_x)
        sidx = np.arange(n_samples, dtype='int32')
        np.random.shuffle(sidx)
        n_train = int(np.round(n_samples * (1. - valid_portion)))
        valid_set_x = [train_set_x[s] for s in sidx[n_train:]]
        valid_set_y = [train_set_y[s] for s in sidx[n_train:]]
        train_set_x = [train_set_x[s] for s in sidx[:n_train]]
        train_set_y = [train_set_y[s] for s in sidx[:n_train]]

        return (train_set_x, train_set_y), (valid_set_x, valid_set_y)

{% endraw %} {% raw %}

    import os
    import pickle
    import time
    from torch.utils.tensorboard import SummaryWriter

    from recohut.datasets.session import SampleSessionDataset, GraphData

    import warnings
    warnings.filterwarnings('ignore')

    model_save_dir = 'saved/'
    log_dir='saved/logs'
    writer = SummaryWriter(log_dir=log_dir)
    os.makedirs(log_dir, exist_ok=True)

    _ = SampleSessionDataset('./session_ds')
    train_data = pickle.load(open('./session_ds/processed/train.txt', 'rb'))

    if args.validation:
        train_data, valid_data = split_validation(train_data, args.valid_portion)
        test_data = valid_data
    else:
        test_data = pickle.load(open('./session_ds/processed/test.txt', 'rb'))

    train_data = GraphData(train_data, shuffle=True)
    test_data = GraphData(test_data, shuffle=False)

    model = to_cuda(TAGNN_PP(args))

    start = time.time()
    best_result = [0, 0]
    best_epoch = [0, 0]
    bad_counter = 0

    for epoch in range(args.epoch):
        print('-' * 50)
        print('Epoch: ', epoch)
        hit, mrr = train_test(model, train_data, test_data)
        flag = 0

        # Logging
        writer.add_scalar('epoch/recall', hit, epoch)
        writer.add_scalar('epoch/mrr', mrr, epoch)

        flag = 0

        if hit >= best_result[0]:
            best_result[0] = hit
            best_epoch[0] = epoch
            flag = 1
            torch.save(model, model_save_dir + 'epoch_' +
                        str(epoch) + '_recall_' + str(hit) + '_.pt')
        if mrr >= best_result[1]:
            best_result[1] = mrr
            best_epoch[1] = epoch
            flag = 1
            torch.save(model, model_save_dir + 'epoch_' +
                        str(epoch) + '_mrr_' + str(mrr) + '_.pt')

        print('Best Result:')
        print('\tRecall@20:\t%.4f\tMRR@20:\t%.4f\tEpoch:\t%d,\t%d' %
                (best_result[0], best_result[1], best_epoch[0], best_epoch[1]))

        bad_counter += 1 - flag

        if bad_counter >= args.patience:
            break

    print('-' * 50)
    end = time.time()
    print("Running time: %f seconds" % (end - start))

    --------------------------------------------------
    Epoch:  0
    Start training:  2021-12-23 11:37:30.078500
    [0/11] Loss: 5.7335
    [3/11] Loss: 5.7263
    [6/11] Loss: 5.7291
    [9/11] Loss: 5.7135
    	Loss Value:	62.968
    Start Prediction:  2021-12-23 11:37:31.807639
    Best Result:
    	Recall@20:	17.3554	MRR@20:	5.2499	Epoch:	0,	0
    --------------------------------------------------
    Epoch:  1
    Start training:  2021-12-23 11:37:31.935884
    [0/11] Loss: 5.7028
    [3/11] Loss: 5.6784
    [6/11] Loss: 5.6666
    [9/11] Loss: 5.6629
    	Loss Value:	62.498
    Start Prediction:  2021-12-23 11:37:33.677782
    Best Result:
    	Recall@20:	19.8347	MRR@20:	5.2499	Epoch:	1,	0
    --------------------------------------------------
    Epoch:  2
    Start training:  2021-12-23 11:37:33.802865
    [0/11] Loss: 5.6609
    [3/11] Loss: 5.6498
    [6/11] Loss: 5.6462
    [9/11] Loss: 5.6439
    	Loss Value:	62.090
    Start Prediction:  2021-12-23 11:37:35.502085
    Best Result:
    	Recall@20:	20.6612	MRR@20:	5.2499	Epoch:	2,	0
    --------------------------------------------------
    Epoch:  3
    Start training:  2021-12-23 11:37:35.604826
    [0/11] Loss: 5.6461
    [3/11] Loss: 5.6324
    [6/11] Loss: 5.6430
    [9/11] Loss: 5.6417
    	Loss Value:	61.997
    Start Prediction:  2021-12-23 11:37:37.213415
    Best Result:
    	Recall@20:	21.4876	MRR@20:	5.2499	Epoch:	3,	0
    --------------------------------------------------
    Epoch:  4
    Start training:  2021-12-23 11:37:37.333273
    [0/11] Loss: 5.6551
    [3/11] Loss: 5.6529
    [6/11] Loss: 5.5994
    [9/11] Loss: 5.6067
    	Loss Value:	61.898
    Start Prediction:  2021-12-23 11:37:38.855207
    Best Result:
    	Recall@20:	21.4876	MRR@20:	5.2499	Epoch:	4,	0
    --------------------------------------------------
    Running time: 8.897622 seconds

{% endraw %}