def recommend_fm(self, ICM):
        builder = Builder()
        newBuilder = NewBuilder()
        fm = FactorizationMachine()
        fm.fit(self.URM, ICM, self.target_playlists, self.target_tracks)
        target_tracks_i = newBuilder.get_tracks_indices(self.target_tracks)

        r = fm.recommend()
        print(r)
        print(r.shape)

        nontarget_indices = builder.get_nontarget_indices(self.target_tracks)
        dataframe_list = []
        offset = 0

        print('Predicting FM...', flush=True)
        for i in tqdm(range(0, self.num_playlist_to_recommend)):
            # Iterate over indices of target playlists
            index = builder.get_target_playlist_index(self.target_playlists[i])
            owner_indices = []
            known_indices = np.nonzero(self.URM[index].toarray().flatten())

            # Increment offset of prediction array r
            if i != 0 and i % fm.n_tracks == 0:
                offset += fm.n_tracks

            # Weighted average of recommendations
            URM_row_flatten = np.zeros(fm.n_tracks)
            for t_i in target_tracks_i:
                URM_row_flatten[t_i] = r[i + offset]

            # Make prediction
            top_5_indices = builder.get_top_5_indices(URM_row_flatten,
                                                      nontarget_indices,
                                                      known_indices,
                                                      owner_indices)
            top_5_tracks = builder.get_top_5_tracks_from_indices(top_5_indices)
            top_5_tracks_string = ' '.join([str(i) for i in top_5_tracks])

            # Create dataset
            if self.is_test:
                dataframe_list.append([self.target_playlists[i], top_5_tracks])
            else:
                dataframe_list.append(
                    [self.target_playlists[i], top_5_tracks_string])

        dataframe = pd.DataFrame(dataframe_list,
                                 columns=['playlist_id', 'track_ids'])

        return dataframe
Exemplo n.º 2
0
    def __init__(self, review_size, review_length, word_vec_dim, fm_k,
                 conv_length, conv_kernel_num, latent_factor_num,
                 word_weights):

        # :param review_length: 评论单词数
        # :param word_vec_dim: 词向量维度
        # :param conv_length: 卷积核的长度
        # :param conv_kernel_num: 卷积核数量
        # :param latent_factor_num: 全连接输出的特征维度

        super(DeepCoNN, self).__init__()
        self.embedding = nn.Embedding.from_pretrained(word_weights)
        self.conv_u = nn.Sequential(
            nn.Conv1d(  # input shape (batch_size, review_length, word_vec_dim)
                in_channels=word_vec_dim,
                out_channels=conv_kernel_num,
                kernel_size=conv_length,
                padding=(conv_length - 1) // 2
            ),  # output shape (batch_size, conv_kernel_num, review_length)
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(1, review_length)),
            nn.Dropout(p=1.0),
        )
        self.linear_u = nn.Sequential(
            nn.Linear(conv_kernel_num * review_size, latent_factor_num),
            nn.ReLU(),
            nn.Dropout(p=1.0),
        )
        self.conv_i = nn.Sequential(
            nn.Conv1d(  # input shape (batch_size, review_length, word_vec_dim)
                in_channels=word_vec_dim,
                out_channels=conv_kernel_num,
                kernel_size=conv_length,
                padding=(conv_length - 1) // 2
            ),  # output shape (batch_size, conv_kernel_num, review_length)
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(1, review_length)),
            nn.Dropout(p=1.0),
        )
        self.linear_i = nn.Sequential(
            nn.Linear(conv_kernel_num * review_size, latent_factor_num),
            nn.ReLU(),
            nn.Dropout(p=1.0),
        )
        self.out = FactorizationMachine(latent_factor_num * 2, fm_k)
Exemplo n.º 3
0
 def __init__(self, filter_size, latent_factor_num, conv_kernel_num,
              word_vec_dim, att_conv_size, u_id_len, i_id_len, fm_k,
              word_weights, review_size):
     super(DAML, self).__init__()
     self.review_size = review_size
     self.local_att = LocalAttention(word_vec_dim, att_conv_size)
     self.mutual_att = MutualAttention(filter_size, word_vec_dim,
                                       conv_kernel_num)
     self.id_embedding_u = nn.Embedding(u_id_len, latent_factor_num)
     self.id_embedding_i = nn.Embedding(i_id_len, latent_factor_num)
     self.text_embedding = nn.Embedding.from_pretrained(word_weights)
     self.conv_u = nn.Sequential(
         nn.Conv1d(  # input shape (batch_size, review_size, word_vec_dim)
             in_channels=word_vec_dim,
             out_channels=conv_kernel_num,
             kernel_size=filter_size,
             padding=(filter_size - 1) //
             2),  # output shape (batch_size, conv_kernel_num, review_size)
         nn.ReLU(),
         nn.MaxPool2d(kernel_size=(1, review_size)),
         Flatten(),
         nn.Linear(conv_kernel_num, latent_factor_num),
         nn.ReLU(),
     )
     self.conv_i = nn.Sequential(
         nn.Conv1d(  # input shape (batch_size, review_size, word_vec_dim)
             in_channels=word_vec_dim,
             out_channels=conv_kernel_num,
             kernel_size=filter_size,
             padding=(filter_size - 1) //
             2),  # output shape (batch_size, conv_kernel_num, review_size)
         nn.ReLU(),
         nn.MaxPool2d(kernel_size=(1, review_size)),
         Flatten(),
         nn.Linear(conv_kernel_num, latent_factor_num),
         nn.ReLU(),
     )
     self.out = FactorizationMachine(latent_factor_num * 2, fm_k)
     self.drop_u = nn.Dropout(p=1.0)
     self.drop_i = nn.Dropout(p=1.0)
Exemplo n.º 4
0
 def __init__(self, review_size, word_vec_dim, fm_k, conv_length,
              conv_kernel_num, latent_factor_num, word_weights,
              u_id_matrix_len, i_id_matrix_len, id_embedding_dim,
              atten_vec_dim):
     # :param review_size: review句子的数量
     # :param word_vec_dim: 词向量维度
     # :param conv_length: 卷积核的长度
     # :param conv_kernel_num: 卷积核数量
     # :param word_weights: 词向量矩阵权重
     # :param u_id_matrix_len: user_id总个数
     # :param i_id_matrix_len: item_id总个数
     # :param id_embedding_dim: id向量维度
     # :param atten_vec_dim: attention向量的维度
     super(NRPA, self).__init__()
     self.review_size = review_size
     self.word_weights = word_weights
     self.user_reveiw_net = ReviewEncoder(word_vec_dim=word_vec_dim,
                                          conv_length=conv_length,
                                          conv_kernel_num=conv_kernel_num,
                                          word_weights=self.word_weights,
                                          id_matrix_len=u_id_matrix_len,
                                          id_embedding_dim=id_embedding_dim,
                                          atten_vec_dim=atten_vec_dim)
     self.item_review_net = ReviewEncoder(word_vec_dim=word_vec_dim,
                                          conv_length=conv_length,
                                          conv_kernel_num=conv_kernel_num,
                                          word_weights=self.word_weights,
                                          id_matrix_len=i_id_matrix_len,
                                          id_embedding_dim=id_embedding_dim,
                                          atten_vec_dim=atten_vec_dim)
     self.user_net = UIEncoder(conv_kernel_num=conv_kernel_num,
                               id_matrix_len=u_id_matrix_len,
                               id_embedding_dim=id_embedding_dim,
                               atten_vec_dim=atten_vec_dim)
     self.item_net = UIEncoder(conv_kernel_num=conv_kernel_num,
                               id_matrix_len=i_id_matrix_len,
                               id_embedding_dim=id_embedding_dim,
                               atten_vec_dim=atten_vec_dim)
     self.fm = FactorizationMachine(conv_kernel_num * 2, fm_k)
Exemplo n.º 5
0
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname('__file__'))))
sys.path.append(
    os.path.dirname(os.path.abspath(os.path.dirname('__file__'))) + '/FM')
from util import cuda_is_avail
from MovieLens1M import MovieLensDataSet
from algo_common_func import read_rating_1m
import os
from FM import FactorizationMachine
import torch

if cuda_is_avail():
    os.environ["CUDA_VISIBLE_DEVICES"] = '3'

config = {
    'epoch': 100,
    'lr': 0.001,
    'batch_size': 1024,
    'weight_decay': 1e-6,
    'n_embedding': 16,
    'is_classifier': True
}

rating_data = read_rating_1m()[:, :3]
dataset = MovieLensDataSet(
    torch.tensor(rating_data).cuda(), config['is_classifier'])
fm = FactorizationMachine(config).cuda()
FactorizationMachine.run(fm, config, dataset)