Beispiel #1
0
    def forward(self, inputs):
        text = inputs[0]
        pos_tag = inputs[1]
        neg_tag = inputs[2]

        text_emb = self.text_embedding(text)
        text_emb = paddle.reshape(
            text_emb, shape=[-1, self.text_len, self.emb_dim])
        pos_tag_emb = self.tag_embedding(pos_tag)
        pos_tag_emb = paddle.reshape(pos_tag_emb, shape=[-1, self.emb_dim])
        neg_tag_emb = self.tag_embedding(neg_tag)
        neg_tag_emb = paddle.reshape(
            neg_tag_emb, shape=[-1, self.neg_size, self.emb_dim])

        conv_1d = self.conv(text_emb)
        act = paddle.tanh(conv_1d)
        maxpool = paddle.max(act, axis=1)
        maxpool = paddle.reshape(maxpool, shape=[-1, self.hid_dim])
        text_hid = self.hid_fc(maxpool)
        cos_pos = F.cosine_similarity(
            pos_tag_emb, text_hid, axis=1).reshape([-1, 1])
        # fluid.layers.Print(cos_pos)
        neg_tag_emb = paddle.max(neg_tag_emb, axis=1)
        neg_tag_emb = paddle.reshape(neg_tag_emb, shape=[-1, self.emb_dim])
        cos_neg = F.cosine_similarity(
            neg_tag_emb, text_hid, axis=1).reshape([-1, 1])
        # fluid.layers.Print(cos_neg)
        return cos_pos, cos_neg
Beispiel #2
0
    def forward(self, input_data, is_infer):
        query_fc = input_data[0]
        for n_layer in self._query_layers:
            query_fc = n_layer(query_fc)

        doc_pos_fc = input_data[1]
        for n_layer in self._pos_layers:
            doc_pos_fc = n_layer(doc_pos_fc)

        R_Q_D_p = F.cosine_similarity(query_fc, doc_pos_fc, axis=1,
                                      eps=0).reshape([-1, 1])

        if is_infer:
            return R_Q_D_p, paddle.ones(shape=[self.slice_end, 1])

        R_Q_D_ns = []
        for i in range(len(input_data) - 2):
            doc_neg_fc_i = input_data[i + 2]
            for n_layer in self._neg_layers:
                doc_neg_fc_i = n_layer(doc_neg_fc_i)
            R_Q_D_n = F.cosine_similarity(query_fc,
                                          doc_neg_fc_i,
                                          axis=1,
                                          eps=0).reshape([-1, 1])
            R_Q_D_ns.append(R_Q_D_n)
        concat_Rs = paddle.concat(x=[R_Q_D_p] + R_Q_D_ns, axis=1)
        prob = F.softmax(concat_Rs, axis=1)
        hit_prob = paddle.slice(prob,
                                axes=[0, 1],
                                starts=[0, 0],
                                ends=[self.slice_end, -1])
        return R_Q_D_p, hit_prob
Beispiel #3
0
    def forward(self, inputs, is_infer=False):
        self.q_slots = inputs[0]
        self.pt_slots = inputs[1]
        if not is_infer:
            self.nt_slots = inputs[2]

        q_embs = [self.embedding(query) for query in self.q_slots]
        q_encodes = []
        for emb in q_embs:
            emb = paddle.reshape(
                emb, shape=[-1, self.query_len, self.query_encode_dim])
            gru = self.gru(emb)
            maxpool = paddle.max(gru[0], axis=1)
            maxpool = paddle.reshape(maxpool,
                                     shape=[-1, self.query_encode_dim])
            q_encodes.append(maxpool)
        q_concat = paddle.concat(q_encodes, axis=1)
        q_hid = self.q_fc(q_concat)

        pt_embs = [self.embedding(title) for title in self.pt_slots]
        pt_encodes = []
        for emb in pt_embs:
            emb = paddle.reshape(
                emb, shape=[-1, self.pos_len, self.title_encode_dim])
            gru = self.gru(emb)
            maxpool = paddle.max(gru[0], axis=1)
            maxpool = paddle.reshape(maxpool,
                                     shape=[-1, self.title_encode_dim])
            pt_encodes.append(maxpool)
        pt_concat = paddle.concat(pt_encodes, axis=1)
        pt_hid = self.t_fc(pt_concat)

        cos_pos = F.cosine_similarity(q_hid, pt_hid, axis=1).reshape([-1, 1])
        if is_infer:
            return cos_pos, paddle.ones(shape=[1, 1])

        nt_embs = [self.embedding(title) for title in self.nt_slots]
        nt_encodes = []
        for emb in nt_embs:
            emb = paddle.reshape(
                emb, shape=[-1, self.neg_len, self.title_encode_dim])
            gru = self.gru(emb)
            maxpool = paddle.max(gru[0], axis=1)
            maxpool = paddle.reshape(maxpool,
                                     shape=[-1, self.title_encode_dim])
            nt_encodes.append(maxpool)
        nt_concat = paddle.concat(nt_encodes, axis=1)
        nt_hid = self.t_fc(nt_concat)

        cos_neg = F.cosine_similarity(q_hid, nt_hid, axis=1).reshape([-1, 1])
        return cos_pos, cos_neg
Beispiel #4
0
    def forward(self, batch_size, user_sparse_inputs, mov_sparse_inputs,
                label_input):

        user_sparse_embed_seq = []
        for s_input in user_sparse_inputs:
            emb = self.embedding(s_input)
            emb = paddle.reshape(emb, shape=[-1, self.sparse_feature_dim])
            user_sparse_embed_seq.append(emb)

        mov_sparse_embed_seq = []
        for s_input in mov_sparse_inputs:
            s_input = paddle.reshape(s_input, shape=[batch_size, -1])
            emb = self.embedding(s_input)
            emb = paddle.sum(emb, axis=1)
            emb = paddle.reshape(emb, shape=[-1, self.sparse_feature_dim])
            mov_sparse_embed_seq.append(emb)

        user_features = paddle.concat(user_sparse_embed_seq, axis=1)
        mov_features = paddle.concat(mov_sparse_embed_seq, axis=1)

        for n_layer in self._user_layers:
            user_features = n_layer(user_features)

        for n_layer in self._movie_layers:
            mov_features = n_layer(mov_features)

        sim = F.cosine_similarity(user_features, mov_features,
                                  axis=1).reshape([-1, 1])
        predict = paddle.scale(sim, scale=5)

        return predict
Beispiel #5
0
    def forward(self, usr_var, mov_var):
        # 计算用户特征和电影特征
        user_features = self.get_usr_feat(usr_var)
        mov_features = self.get_mov_feat(mov_var)

        #使用余弦相似度算子,计算用户和电影的相似程度
        sim = F.cosine_similarity(user_features, mov_features,
                                  axis=1).reshape([-1, 1])
        # 将相似度扩大范围到和电影评分相同数据范围
        res = paddle.scale(sim, scale=5)
        return user_features, mov_features, res
Beispiel #6
0
    def test_dygraph_2(self):
        paddle.disable_static()

        shape = [12, 13]
        axis = 0
        eps = 1e-6
        np.random.seed(1)
        np_x1 = np.random.rand(*shape).astype(np.float32)
        np_x2 = np.random.rand(*shape).astype(np.float32)
        np_out = self._get_numpy_out(np_x1, np_x2, axis=axis, eps=eps)

        tesnor_x1 = paddle.to_tensor(np_x1)
        tesnor_x2 = paddle.to_tensor(np_x2)
        y = F.cosine_similarity(tesnor_x1, tesnor_x2, axis=axis, eps=eps)

        self.assertTrue(np.allclose(y.numpy(), np_out))
Beispiel #7
0
    def test_dygraph_3(self):
        paddle.disable_static()

        shape1 = [10, 12, 10]
        shape2 = [10, 1, 10]
        axis = 2
        eps = 1e-6
        np.random.seed(1)
        np_x1 = np.random.rand(*shape1).astype(np.float32)
        np_x2 = np.random.rand(*shape2).astype(np.float32)
        np_out = self._get_numpy_out(np_x1, np_x2, axis=axis, eps=eps)

        tesnor_x1 = paddle.to_variable(np_x1)
        tesnor_x2 = paddle.to_variable(np_x2)
        y = F.cosine_similarity(tesnor_x1, tesnor_x2, axis=axis, eps=eps)

        self.assertTrue(np.allclose(y.numpy(), np_out))
Beispiel #8
0
    def check_static_result(self, place):
        paddle.enable_static()

        with program_guard(Program(), Program()):
            shape = [10, 15]
            axis = 1
            eps = 1e-8
            np.random.seed(0)
            np_x1 = np.random.rand(*shape).astype(np.float32)
            np_x2 = np.random.rand(*shape).astype(np.float32)

            x1 = paddle.fluid.data(name="x1", shape=shape)
            x2 = paddle.fluid.data(name="x2", shape=shape)
            result = F.cosine_similarity(x1, x2, axis=axis, eps=eps)
            exe = Executor(place)
            fetches = exe.run(default_main_program(),
                              feed={"x1": np_x1,
                                    "x2": np_x2},
                              fetch_list=[result])

            np_out = self._get_numpy_out(np_x1, np_x2, axis=axis, eps=eps)
            self.assertTrue(np.allclose(fetches[0], np_out))