Exemplo n.º 1
0
    def load_model(self):
        self.movielens = init_movielens(self.dataset,
                                        min_rating=self.min_rating,
                                        k=self.k,
                                        # item_features=None,
                                        item_features=['clusters'],
                                        model='resnet50'
                                        )

        self.train_mapping = self.movielens['train-mapping']

        train = self.movielens['train']
        item_features = self.movielens['item_features']

        king_rec = KingRec(no_components=self.optimal_no_components,
                           learning_rate=self.optimal_learning_rate,
                           alpha=self.optimal_alpha,
                           scale=self.optimal_scaling,
                           loss='warp')

        self.model = king_rec.model
        self.model.fit_partial(interactions=train, item_features=item_features,
                               epochs=self.optimal_epochs, verbose=True, num_threads=self.threads)

        return self.model
Exemplo n.º 2
0
        optimal_learning_rate = 0.0570326091236193
        optimal_no_components = 68
        optimal_alpha = 0.0029503539747277366
        optimal_scaling = 0.02563602355611453

        return optimal_epochs, optimal_learning_rate, optimal_no_components, optimal_alpha, optimal_scaling


k = 5
threads = 16
dataset = '../king-rec-dataset/ml-latest-small'
epochs, learning_rate, no_components, alpha, scaling = load_params(
    optimized_for='auc_clusters')

movielens = init_movielens(dataset,
                           min_rating=3.5,
                           k=k,
                           item_features=['clusters'])

train = movielens['train']
test = movielens['test']
item_features = movielens['item_features']

king_rec = KingRec(no_components=no_components,
                   learning_rate=learning_rate,
                   alpha=alpha,
                   scale=scaling,
                   loss='warp')

model = king_rec.model
model.fit_partial(interactions=train,
                  item_features=item_features,
Exemplo n.º 3
0
    mapatk = np.mean(patks)

    # Make negative because we want to minimize objective
    out = -mapatk

    # Handle some weird numerical shit going on
    if np.abs(out + 1) < 0.01 or out < -1.0:
        return 0.0
    else:
        return out


results = open('../../optimized-params-' + str(time.time()), 'a')
for model in ['vgg19', 'inception_v3', 'resnet50']:
    for features in [None, ['genres'], ['clusters'], ['genres', 'clusters']]:
        movielens = init_movielens(dataset, min_rating=3.5, k=k, item_features=features, model=model)
        train = movielens['train']
        test = movielens['test']
        item_features = movielens['item_features']

        for loss in ['warp', 'bpr']:  # 'warp-kos'
            for function_to_optimize in [precision_at_k, auc_score]:
                space = [(1, 250),  # epochs
                         (10 ** -4, 1.0, 'log-uniform'),  # learning_rate
                         (20, 200),  # no_components
                         (10 ** -6, 10 ** -1, 'log-uniform'),  # item_alpha
                         (0.001, 1., 'log-uniform'),  # user_scaling
                         # (1, 5),  # k for k-OS training
                         ]

                current_fixed_params = '\n==> Optimize with features: ' + str(features) + '   loss: ' + loss + \
Exemplo n.º 4
0
def clusters_models(params_to_load_clusters=load_auc_params_clusters):
    for model in ['vgg19', 'inception_v3', 'resnet50']:
        # features = ['clusters']
        features = ['genres', 'clusters']
        movielens = init_movielens(dataset, min_rating=3.5, k=k, item_features=features, model=model)

        if len(features) == 1:
            epochs, learning_rate, no_components, alpha, scaling = params_to_load_clusters(optimized_for=features[0], model=model)
        elif len(features) == 2:
            epochs, learning_rate, no_components, alpha, scaling = params_to_load_clusters(optimized_for=features[0] + '_' + features[1], model=model)

        train = movielens['train']
        test = movielens['test']
        item_features = movielens['item_features']

        king_rec = KingRec(no_components=no_components, learning_rate=learning_rate, alpha=alpha, scale=scaling, loss='warp')
        model = king_rec.model

        train_auc_scores = []
        test_auc_scores = []

        train_precision_scores = []
        test_precision_scores = []

        for epoch in range(epochs):
            print('Epoch:', epoch)
            model.fit_partial(train, item_features=item_features, epochs=1)

            train_precision = precision_at_k(model, train, item_features=item_features, k=k, num_threads=threads).mean()
            test_precision = precision_at_k(model, test, item_features=item_features, k=k, num_threads=threads).mean()

            train_precision_scores.append(train_precision)
            test_precision_scores.append(test_precision)

            # train_auc = auc_score(model, train, item_features=item_features, num_threads=threads).mean()
            # test_auc = auc_score(model, test, item_features=item_features, num_threads=threads).mean()

            # train_auc_scores.append(train_auc)
            # test_auc_scores.append(test_auc)

        # final_test_auc.append(test_auc_scores)
        final_test_precision.append(test_precision_scores)

    # plot results
    plt.figure()
    for auc_scores in final_test_auc:
        x = np.arange(len(auc_scores))

        max_value = max(auc_scores)
        max_index = auc_scores.index(max_value)

        # plt.plot(x, auc_scores, '-D', markevery=[max_index])
        plt.plot(x, auc_scores)

    plt.ylabel('Accuracy')
    plt.xlabel('Epochs')
    plt.title('Accuracy per model with genres and clusters metadata')
    plt.legend(['vgg19: ' + str(np.round(max(final_test_auc[0]) * 100, decimals=2)) + '%',
                'inception_v3: ' + str(np.round(max(final_test_auc[1]) * 100, decimals=2)) + '%',
                'resnet50: ' + str(np.round(max(final_test_auc[2]) * 100, decimals=2)) + '%',
                ])
    plt.savefig('auc-comparison.jpg')
    plt.show()

    plt.figure()
    for precision_scores in final_test_precision:
        x = np.arange(len(precision_scores))

        max_value = max(precision_scores)
        max_index = precision_scores.index(max_value)

        # plt.plot(x, precision_scores, '-D', markevery=[max_index])
        plt.plot(x, precision_scores)

    plt.ylabel('Precision')
    plt.xlabel('Epochs')
    plt.title('Precision per model with genres and clusters metadata')
    plt.legend(['vgg19: ' + str(np.round(max(final_test_precision[0]) * 100, decimals=2)) + '%',
                'inception_v3: ' + str(np.round(max(final_test_precision[1]) * 100, decimals=2)) + '%',
                'resnet50: ' + str(np.round(max(final_test_precision[2]) * 100, decimals=2)) + '%',
                ])
    plt.savefig('precision-comparison.jpg')
    plt.show()