推荐系统精排模型中的ID特征

一、ID特征包括什么?

单ID特征包括:用户ID物品ID

ID序列特征主要指的是:用户有过行为的物品ID List。比如:用户最近点击过的物品ID集合。

二、物品ID特征怎么加进推荐系统?

2.1 StrHash——将物品ID映射到bucket大小的int空间

hash = ((hash << 5) + hash) + c;

其中,hash的初始值为5381,c代表输入值。

djb2:一个产生简单的随机分布的哈希函数 - 范加索尔拉 - 博客园

2.2 通过Hash值查找embedding矩阵,作为模型输入。

三、用户ID特征

PreHash

四、用户行为序列特征

也是将各个ItemID通过StrHash映射成HashID,然后查找embedding矩阵,然后基于attention建模。这里不止是要用到行为序列ID,还要用到Target Item ID,attention的目的其实就是去计算targetID和用户历史行为ID的相似度,来提取用户的兴趣点。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基于关键词的文本排序检索系统的简单实现,使用了 TF-IDF 模型和倒排索引。代码包含了注释,以便理解。请注意,这只是一个简单的示例,需要根据实际需求和具体情况进行调整和优化。 ```c++ #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <map> #include <algorithm> #include <cmath> using namespace std; // 定义一个结构体,表示文本信息 struct Document { int id; // 文本编号 string title; // 文本标题 string content; // 文本内容 }; class TextSearch { private: vector<Document> documents; // 存储所有文本信息 map<string, map<int, int>> invertedIndex; // 存储倒排索引 public: // 加载文本库 void loadDocuments(const string& filename) { ifstream ifs(filename); if (!ifs) { cerr << "Error: failed to open file " << filename << endl; return; } string line; while (getline(ifs, line)) { stringstream ss(line); int id; string title, content; ss >> id >> title >> content; documents.push_back({id, title, content}); } ifs.close(); } // 创建倒排索引 void createInvertedIndex() { for (const auto& doc : documents) { stringstream ss(doc.title + " " + doc.content); string word; while (ss >> word) { // 去除停用词 if (isStopWord(word)) { continue; } // 将单词转换为小写形式 transform(word.begin(), word.end(), word.begin(), ::tolower); // 更新倒排索引 invertedIndex[word][doc.id]++; } } } // 检索文本 vector<pair<int, double>> search(const string& query) { // 将检索关键词转换为小写形式 string lowerQuery = query; transform(lowerQuery.begin(), lowerQuery.end(), lowerQuery.begin(), ::tolower); // 计算检索关键词的 TF 和 IDF 值 map<string, double> tf; double maxTf = 0.0; stringstream ss(lowerQuery); string word; while (ss >> word) { // 去除停用词 if (isStopWord(word)) { continue; } // 计算 TF 值 tf[word]++; maxTf = max(maxTf, tf[word]); } for (auto& p : tf) { p.second /= maxTf; // 归一化 } map<string, double> idf; double N = documents.size(); for (const auto& p : invertedIndex) { double df = p.second.size(); idf[p.first] = log(N / df); } // 计算文本的 TF-IDF 值,然后计算得分 map<int, double> scores; for (const auto& p : tf) { const string& word = p.first; double tfidf = p.second * idf[word]; for (const auto& q : invertedIndex[word]) { int docId = q.first; int freq = q.second; scores[docId] += tfidf * freq; } } // 对得分进行排序,返回前十个结果 vector<pair<int, double>> results(scores.begin(), scores.end()); sort(results.begin(), results.end(), [](const auto& p, const auto& q) { return p.second > q.second; }); if (results.size() > 10) { results.resize(10); } return results; } // 显示文本 void displayDocument(int id) { const auto& doc = documents[id]; cout << "Title: " << doc.title << endl; cout << "Content: " << doc.content << endl; } private: // 停用词列表 static const vector<string> stopWords; // 判断一个单词是否为停用词 static bool isStopWord(const string& word) { return find(stopWords.begin(), stopWords.end(), word) != stopWords.end(); } }; // 初始化停用词列表 const vector<string> TextSearch::stopWords = { "is", "am", "are", "a", "an", "the" }; int main() { TextSearch search; search.loadDocuments("documents.txt"); search.createInvertedIndex(); while (true) { cout << "Please enter a query (type 'exit' to quit): "; string query; getline(cin, query); if (query == "exit") { break; } auto results = search.search(query); if (results.empty()) { cout << "No results found.\n"; } else { cout << "Top " << results.size() << " results:\n"; for (const auto& p : results) { cout << "Document " << p.first << " (score: " << p.second << ")\n"; search.displayDocument(p.first); } } } return 0; } ``` 这是一个简单的命令行程序,主要包括以下几个部分: 1. 定义了一个文本信息的结构体 `Document`,包括文本编号、标题和内容。 2. 定义了一个 `TextSearch` 类,包含了加载文本库、创建倒排索引、检索文本和显示文本等功能。 3. 加载文本库时,从文件读取每个文本的编号、标题和内容,并存储到 `documents` 向量。 4. 创建倒排索引时,遍历每个文本的标题和内容,将单词转换为小写形式并去除停用词,然后更新倒排索引。 5. 检索文本时,将检索关键词转换为小写形式并去除停用词,然后计算 TF 和 IDF 值,计算文本的 TF-IDF 值,最后计算得分并排序。返回前十个结果,并显示每个结果的得分和文本信息。 6. 显示文本时,根据文本编号从 `documents` 向量查找对应的文本,然后输出标题和内容。 这个程序还有很多可以改进的地方,比如支持动态装载和处理文本库、支持停用词的管理和维护等。但是,这个程序已经可以基本实现基于关键词的文本排序检索系统的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值