Ejemplo n.º 1
0
 def __init__(self, c, h, w, name):
     super(SaModel, self).__init__()
     self.name = name
     self.l1 = nn.Linear(w, h)
     utils.init_linear(self.l1)
     self.l2 = nn.Linear(h * w, c)
     utils.init_linear(self.l2)
Ejemplo n.º 2
0
 def rand_init(self):
     """
     random initialization
     """
     for i in range(self.num_layers):
         utils.init_linear(self.trans[i])
         utils.init_linear(self.gate[i])
Ejemplo n.º 3
0
    def __init__(self,
                 features_num_embed=[],
                 embed_dim=64,
                 embed_drop_out=0.1):
        super(FeatureEmbed, self).__init__()
        self.features_embed_module = nn.ModuleList()
        for num_embed in features_num_embed:
            self.features_embed_module.append(
                NormalEmbedding(num_embed, embed_dim, embed_drop_out))

        self.linear_out = nn.Linear(
            len(features_num_embed) * embed_dim, embed_dim)
        utils.init_linear(self.linear_out)
    def __init__(self,
                 url_f_embed_module=None,
                 user_f_embed_module=None,
                 embed_drop_out=0.1,
                 num_limit_list=[]):
        super(Model6Features, self).__init__()
        self.feature_model = feature_models.FeatureModel(
            out_conv=True,
            attend_f=False,
            sa=False,
            embed_drop_out=embed_drop_out,
            num_limit_list=num_limit_list)
        if url_f_embed_module is not None:
            self.feature_model.url_features_embed = url_f_embed_module.features_embed_module
            self.feature_model.url = url_f_embed_module.linear_out

        if user_f_embed_module is not None:
            self.feature_model.user_features_embed = user_f_embed_module.features_embed_module
            self.feature_model.user = user_f_embed_module.linear_out

        self.linear_out = nn.Linear(F_DIM * (2 + 2), 2)
        utils.init_linear(self.linear_out)
    def __init__(self,
                 url_f_embed_module=None,
                 user_f_embed_module=None,
                 embed_drop_out=0.1,
                 if_easy=True):
        super(Model6Pre, self).__init__()
        self.if_easy = if_easy

        if if_easy:
            self.user_embed = embed.FeatureEmbed([11576 + 1], F_DIM, 0.1)
            self.url_embed = embed.FeatureEmbed([4732 + 1], F_DIM, 0.1)
        else:
            # int(uid_no), follow_cnt_bin, follow_in_cnt_bin, post_url_bin, post_cnt_bin
            # ,favorite_cate_no, favorite_site_no
            self.user_embed = embed.FeatureEmbed(
                [11576 + 1, 10 + 1, 10 + 1, 10 + 1, 10 + 1, 20 + 1, 6 + 1],
                F_DIM, 0.1)
            #int(url_no), cate2idx[cate], site2idx[url_site], post_user_bin, post_freq_bin
            self.url_embed = embed.FeatureEmbed(
                [4732 + 1, 20 + 1, 6 + 1, 10 + 1, 10 + 1], F_DIM, 0.1)

        self.linear_out = nn.Linear(F_DIM * 2, 2)
        utils.init_linear(self.linear_out)
Ejemplo n.º 6
0
    def __init__(self, word_embed_mat, dim=64):
        super(TextModel, self).__init__()
        ### tweets and url content
        if word_embed_mat is None:
            self.word_embed = embed.NormalEmbedding(231172, 300, 0.1)
        else:
            self.word_embed = embed.FixedEmbedding(word_embed_mat, 0, False)

        self.char_embed = embed.CharacterEmbedding(num_embed=70,
                                                   embed_dim=64,
                                                   out_ch=64,
                                                   kernel_size=3,
                                                   padding=1)
        #self.char_embed = embed.CharacterDepthwiseEmbedding(num_embed=70, embed_dim=64, out_ch=64, kernel_size=3)
        #self.char_embed2 = embed.CharacterEmbedding(num_embed=70, embed_dim=64, out_ch=64, kernel_size=5, padding=2)

        #self.projection = nn.Conv1d(300+64, 128, 1)
        self.hw1 = highway.hw(300 + 64, 2)
        #self.hw2 = highway.hw(300+64, 2)
        '''self.lstm = nn.LSTM(         # if use nn.RNN(), it hardly learns
            input_size=128,
            hidden_size=64,         # rnn hidden unit
            num_layers=1,           # number of rnn layer
            batch_first=True,       # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size)
            bidirectional=True
        )'''

        ### attention
        self.cos = torch.nn.CosineSimilarity(dim=2, eps=1e-6)
        #self.linear_attention = nn.Linear(300+64, 300+64)
        #utils.init_linear(self.linear_attention)

        #self.linear_attention_url = nn.Linear(300+64, 300+64)
        #utils.init_linear(self.linear_attention_url)

        self.linear = nn.Linear(300 + 64, 64)
        utils.init_linear(self.linear)
    def __init__(self, out_conv=True, attend_f=True, sa=False, embed_drop_out=0.1
            ,num_limit_list=[CONSUM_NUM, NEIGHB_URL_NUM, FRIEND_NUM, NEIGHT_USER_NUM], F_DIM=F_DIM):
        super(FeatureModel, self).__init__()
        self.out_conv = out_conv
        self.attend_f = attend_f
        self.sa = sa

        self.user = nn.Linear(7*F_DIM, F_DIM)
        utils.init_linear(self.user)
        self.url = nn.Linear(5*F_DIM, F_DIM)
        utils.init_linear(self.url)
        #self.user_url = nn.Linear(F_DIM, F_DIM)
        #utils.init_linear(self.user_url)

        self.cos = nn.CosineSimilarity(dim=2, eps=1e-6)
        # url features: int(url_no), cate2idx[cate], site2idx[url_site], post_user_bin, post_freq_bin
        self.url_features_embed = nn.ModuleList()
        self.url_features_embed.append(embed.NormalEmbedding(4732+1, F_DIM, embed_drop_out))
        self.url_features_embed.append(embed.NormalEmbedding(20+1, F_DIM, embed_drop_out))
        self.url_features_embed.append(embed.NormalEmbedding(6+1, F_DIM, embed_drop_out))
        self.url_features_embed.append(embed.NormalEmbedding(10+1, F_DIM, embed_drop_out))
        self.url_features_embed.append(embed.NormalEmbedding(10+1, F_DIM, embed_drop_out))

        self.url_consume = user_model.attenModel(in_c=num_limit_list[0], out_c=num_limit_list[0], name='url_consume',
                    attend_f=self.attend_f, out_conv=self.out_conv, sa=self.sa, h=5, w=F_DIM)
        self.url_neighb = user_model.attenModel(in_c=num_limit_list[1], out_c=num_limit_list[1], name='url_neighb',
                    attend_f=self.attend_f, out_conv=self.out_conv, sa=self.sa, h=5, w=F_DIM)
        self.url_gate = nn.Linear(F_DIM*2, F_DIM)
        utils.init_linear(self.url_gate)


        # user features: int(uid_no), follow_cnt_bin, follow_in_cnt_bin, post_url_bin, post_cnt_bin ,favorite_cate_no, favorite_site_no
        self.user_features_embed = nn.ModuleList()
        self.user_features_embed.append(embed.NormalEmbedding(11576+1, F_DIM, embed_drop_out))
        self.user_features_embed.append(embed.NormalEmbedding(10+1, F_DIM, embed_drop_out))
        self.user_features_embed.append(embed.NormalEmbedding(10+1, F_DIM, embed_drop_out))
        self.user_features_embed.append(embed.NormalEmbedding(10+1, F_DIM, embed_drop_out))
        self.user_features_embed.append(embed.NormalEmbedding(10+1, F_DIM, embed_drop_out))
        self.user_features_embed.append(embed.NormalEmbedding(20+1, F_DIM, embed_drop_out))
        self.user_features_embed.append(embed.NormalEmbedding(6+1, F_DIM, embed_drop_out))
        
        self.user_friend = user_model.attenModel(in_c=num_limit_list[2], out_c=num_limit_list[2], name='user_friend', 
                        attend_f=self.attend_f, out_conv=self.out_conv, sa=self.sa, h=7, w=F_DIM)
        self.user_neighb = user_model.attenModel(in_c=num_limit_list[3], out_c=num_limit_list[3], name='user_neighb',
                        attend_f=self.attend_f, out_conv=self.out_conv, sa=self.sa, h=7, w=F_DIM)
        self.user_gate = nn.Linear(F_DIM*2, F_DIM)
        utils.init_linear(self.user_gate)
    def __init__(self, x_dim=64, mem_dim=364, out_dim=64, attention_method='cos'):
        super(memAttention, self).__init__()
        self.method = attention_method
        ### x linear
        self.linear_x = nn.Linear(x_dim, mem_dim)
        utils.init_linear(self.linear_x)

        ### attention
        if self.method == 'cos':
            self.cos = torch.nn.CosineSimilarity(dim=2, eps=1e-6)
        else:
            self.linear_attention = nn.Linear(mem_dim, mem_dim)
            utils.init_linear(self.linear_attention)

        ### out
        self.linear_out = nn.Linear(mem_dim, out_dim)
        utils.init_linear(self.linear_out)
Ejemplo n.º 9
0
    def __init__(self,
                 url_f_embed_module=None,
                 user_f_embed_module=None,
                 embed_drop_out=0.1,
                 if_one_layer=False,
                 num_limit_list=[],
                 F_DIM=64):
        super(Model6Main, self).__init__()
        self.if_one_layer = if_one_layer

        # feature model
        # num_limit_list=[CONSUM_NUM, NEIGHB_URL_NUM, FRIEND_NUM, NEIGHT_USER_NUM]
        self.feature_model = feature_models.FeatureModel(
            out_conv=True,
            attend_f=True,
            sa=False,
            embed_drop_out=embed_drop_out,
            num_limit_list=num_limit_list,
            F_DIM=F_DIM)

        # int(uid_no), follow_cnt_bin, follow_in_cnt_bin, post_url_bin, post_cnt_bin
        # ,favorite_cate_no, favorite_site_no
        self.user_embed = embed.FeatureEmbed(
            [11576 + 1, 10 + 1, 10 + 1, 10 + 1, 10 + 1, 20 + 1, 6 + 1], F_DIM,
            embed_drop_out)
        #int(url_no), cate2idx[cate], site2idx[url_site], post_user_bin, post_freq_bin
        self.url_embed = embed.FeatureEmbed(
            [4732 + 1, 20 + 1, 6 + 1, 10 + 1, 10 + 1], F_DIM, embed_drop_out)

        self.cos2 = torch.nn.CosineSimilarity(dim=2, eps=1e-6)
        self.cos3 = torch.nn.CosineSimilarity(dim=3, eps=1e-6)

        self.linear_update2 = nn.Linear(F_DIM * 2, F_DIM)
        utils.init_linear(self.linear_update2)

        self.linear_update1 = nn.Linear(F_DIM * 2, F_DIM)
        utils.init_linear(self.linear_update1)

        self.linear_out = nn.Linear(F_DIM * (2 + 4), 2)
        utils.init_linear(self.linear_out)
Ejemplo n.º 10
0
    def __init__(self, url_f_embed_module=None, user_f_embed_module=None, embed_drop_out=0.1, if_one_layer=False):
        super(Model6GCN, self).__init__()
        self.if_one_layer = if_one_layer
        # int(uid_no), follow_cnt_bin, follow_in_cnt_bin, post_url_bin, post_cnt_bin
                # ,favorite_cate_no, favorite_site_no
        self.user_embed = embed.FeatureEmbed([11576+1, 10+1, 10+1, 10+1, 10+1, 20+1, 6+1], F_DIM, embed_drop_out)
        #int(url_no), cate2idx[cate], site2idx[url_site], post_user_bin, post_freq_bin
        self.url_embed = embed.FeatureEmbed([4732+1, 20+1, 6+1, 10+1, 10+1], F_DIM, embed_drop_out)

        if url_f_embed_module is not None:
            print(self.url_embed.features_embed_module[2].embed.weight[0:2, :])
            self.url_embed = url_f_embed_module
            print(self.url_embed.features_embed_module[2].embed.weight[0:2, :])
            '''for i in range(len(self.feature_model.url_features_embed)):
                #print(i, 'url', self.feature_model.url_features_embed[i].embed.weight.requires_grad)
                self.feature_model.url_features_embed[i].embed.weight.requires_grad = False
                #print(i, 'url', self.feature_model.url_features_embed[i].embed.weight.requires_grad)'''

        if user_f_embed_module is not None:
            #print(self.user_embed.features_embed_module[2].embed.weight[0:2, :])
            self.user_embed = user_f_embed_module
            #print(self.user_embed.features_embed_module[2].embed.weight[0:2, :])
            '''for i in range(len(self.feature_model.user_features_embed)):
                #print(i, 'user', self.feature_model.user_features_embed[i].embed.weight.requires_grad)
                self.feature_model.user_features_embed[i].embed.weight.requires_grad = False
                #print(i, 'user', self.feature_model.user_features_embed[i].embed.weight.requires_grad)'''

        self.cos2 = torch.nn.CosineSimilarity(dim=2, eps=1e-6)
        self.cos3 = torch.nn.CosineSimilarity(dim=3, eps=1e-6)

        self.linear_update2 = nn.Linear(F_DIM*2, F_DIM)
        utils.init_linear(self.linear_update2)

        self.linear_update1 = nn.Linear(F_DIM*2, F_DIM)
        utils.init_linear(self.linear_update1)

        self.linear_out = nn.Linear(F_DIM*2, 2)
        utils.init_linear(self.linear_out)