-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add aitm model #756
Merged
Merged
Add aitm model #756
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0552236
Add aitm model
renmada 5ba0784
Fix code style
renmada 660d0db
Fix code style
renmada f8347a7
Fix code style
renmada bdfb610
Merge remote-tracking branch 'upstream/master' into aitm
renmada bf9a3c0
Fix
renmada 198aa25
Fix
renmada 580c98d
move to datasets loader
renmada b6cfc37
move to datasets loader
renmada 99e15bf
Update
renmada fa1dd3d
Fix tipc data path
renmada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
''' | ||
process the Ali-CCP (Alibaba Click and Conversion Prediction) dataset. | ||
https://tianchi.aliyun.com/datalab/dataSet.html?dataId=408 | ||
|
||
@The author: | ||
Dongbo Xi (xidongbo@meituan.com) | ||
''' | ||
import numpy as np | ||
import joblib | ||
import re | ||
import random | ||
random.seed(2020) | ||
np.random.seed(2020) | ||
data_path = 'data/sample_skeleton_{}.csv' | ||
common_feat_path = 'data/common_features_{}.csv' | ||
enum_path = 'data/ctrcvr_enum.pkl' | ||
write_path = 'data/ctr_cvr' | ||
use_columns = [ | ||
'101', '121', '122', '124', '125', '126', '127', '128', '129', '205', | ||
'206', '207', '216', '508', '509', '702', '853', '301' | ||
] | ||
|
||
|
||
class process(object): | ||
def __init__(self): | ||
pass | ||
|
||
def process_train(self): | ||
c = 0 | ||
common_feat_dict = {} | ||
with open(common_feat_path.format('train'), 'r') as fr: | ||
for line in fr: | ||
line_list = line.strip().split(',') | ||
kv = np.array(re.split('\x01|\x02|\x03', line_list[2])) | ||
key = kv[range(0, len(kv), 3)] | ||
value = kv[range(1, len(kv), 3)] | ||
feat_dict = dict(zip(key, value)) | ||
common_feat_dict[line_list[0]] = feat_dict | ||
c += 1 | ||
if c % 100000 == 0: | ||
print(c) | ||
print('join feats...') | ||
c = 0 | ||
vocabulary = dict( | ||
zip(use_columns, [{} for _ in range(len(use_columns))])) | ||
with open(data_path.format('train') + '.tmp', 'w') as fw: | ||
fw.write('click,purchase,' + ','.join(use_columns) + '\n') | ||
with open(data_path.format('train'), 'r') as fr: | ||
for line in fr: | ||
line_list = line.strip().split(',') | ||
if line_list[1] == '0' and line_list[2] == '1': | ||
continue | ||
kv = np.array(re.split('\x01|\x02|\x03', line_list[5])) | ||
key = kv[range(0, len(kv), 3)] | ||
value = kv[range(1, len(kv), 3)] | ||
feat_dict = dict(zip(key, value)) | ||
feat_dict.update(common_feat_dict[line_list[3]]) | ||
feats = line_list[1:3] | ||
for k in use_columns: | ||
feats.append(feat_dict.get(k, '0')) | ||
fw.write(','.join(feats) + '\n') | ||
for k, v in feat_dict.items(): | ||
if k in use_columns: | ||
if v in vocabulary[k]: | ||
vocabulary[k][v] += 1 | ||
else: | ||
vocabulary[k][v] = 0 | ||
c += 1 | ||
if c % 100000 == 0: | ||
print(c) | ||
print('before filter low freq:') | ||
for k, v in vocabulary.items(): | ||
print(k + ':' + str(len(v))) | ||
new_vocabulary = dict( | ||
zip(use_columns, [set() for _ in range(len(use_columns))])) | ||
for k, v in vocabulary.items(): | ||
for k1, v1 in v.items(): | ||
if v1 > 10: | ||
new_vocabulary[k].add(k1) | ||
vocabulary = new_vocabulary | ||
print('after filter low freq:') | ||
for k, v in vocabulary.items(): | ||
print(k + ':' + str(len(v))) | ||
joblib.dump(vocabulary, enum_path, compress=3) | ||
|
||
print('encode feats...') | ||
vocabulary = joblib.load(enum_path) | ||
feat_map = {} | ||
for feat in use_columns: | ||
feat_map[feat] = dict( | ||
zip(vocabulary[feat], range(1, len(vocabulary[feat]) + 1))) | ||
c = 0 | ||
with open(write_path + '.train', 'w') as fw1: | ||
with open(write_path + '.dev', 'w') as fw2: | ||
fw1.write('click,purchase,' + ','.join(use_columns) + '\n') | ||
fw2.write('click,purchase,' + ','.join(use_columns) + '\n') | ||
with open(data_path.format('train') + '.tmp', 'r') as fr: | ||
fr.readline() # remove header | ||
for line in fr: | ||
line_list = line.strip().split(',') | ||
new_line = line_list[:2] | ||
for value, feat in zip(line_list[2:], use_columns): | ||
new_line.append( | ||
str(feat_map[feat].get(value, '0'))) | ||
if random.random() >= 0.9: | ||
fw2.write(','.join(new_line) + '\n') | ||
else: | ||
fw1.write(','.join(new_line) + '\n') | ||
c += 1 | ||
if c % 100000 == 0: | ||
print(c) | ||
|
||
def process_test(self): | ||
c = 0 | ||
common_feat_dict = {} | ||
with open(common_feat_path.format('test'), 'r') as fr: | ||
for line in fr: | ||
line_list = line.strip().split(',') | ||
kv = np.array(re.split('\x01|\x02|\x03', line_list[2])) | ||
key = kv[range(0, len(kv), 3)] | ||
value = kv[range(1, len(kv), 3)] | ||
feat_dict = dict(zip(key, value)) | ||
common_feat_dict[line_list[0]] = feat_dict | ||
c += 1 | ||
if c % 100000 == 0: | ||
print(c) | ||
print('join feats...') | ||
c = 0 | ||
with open(data_path.format('test') + '.tmp', 'w') as fw: | ||
fw.write('click,purchase,' + ','.join(use_columns) + '\n') | ||
with open(data_path.format('test'), 'r') as fr: | ||
for line in fr: | ||
line_list = line.strip().split(',') | ||
if line_list[1] == '0' and line_list[2] == '1': | ||
continue | ||
kv = np.array(re.split('\x01|\x02|\x03', line_list[5])) | ||
key = kv[range(0, len(kv), 3)] | ||
value = kv[range(1, len(kv), 3)] | ||
feat_dict = dict(zip(key, value)) | ||
feat_dict.update(common_feat_dict[line_list[3]]) | ||
feats = line_list[1:3] | ||
for k in use_columns: | ||
feats.append(str(feat_dict.get(k, '0'))) | ||
fw.write(','.join(feats) + '\n') | ||
c += 1 | ||
if c % 100000 == 0: | ||
print(c) | ||
|
||
print('encode feats...') | ||
vocabulary = joblib.load(enum_path) | ||
feat_map = {} | ||
for feat in use_columns: | ||
feat_map[feat] = dict( | ||
zip(vocabulary[feat], range(1, len(vocabulary[feat]) + 1))) | ||
c = 0 | ||
with open(write_path + '.test', 'w') as fw: | ||
fw.write('click,purchase,' + ','.join(use_columns) + '\n') | ||
with open(data_path.format('test') + '.tmp', 'r') as fr: | ||
fr.readline() # remove header | ||
for line in fr: | ||
line_list = line.strip().split(',') | ||
new_line = line_list[:2] | ||
for value, feat in zip(line_list[2:], use_columns): | ||
new_line.append(str(feat_map[feat].get(value, '0'))) | ||
fw.write(','.join(new_line) + '\n') | ||
c += 1 | ||
if c % 100000 == 0: | ||
print(c) | ||
|
||
|
||
if __name__ == "__main__": | ||
pros = process() | ||
pros.process_train() | ||
pros.process_test() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
mkdir data | ||
mkdir data/whole_data && mkdir data/whole_data/train && mkdir data/whole_data/test | ||
train_source_path="./data/sample_train.tar.gz" | ||
train_target_path="train_data" | ||
test_source_path="./data/sample_test.tar.gz" | ||
test_target_path="test_data" | ||
cd data | ||
echo "downloading sample_train.tar.gz......" | ||
curl -# 'http://jupter-oss.oss-cn-hangzhou.aliyuncs.com/file/opensearch/documents/408/sample_train.tar.gz?Expires=1586435769&OSSAccessKeyId=LTAIGx40tjZWxj6q&Signature=ahUDqhvKT1cGjC4%2FIER2EWtq7o4%3D&response-content-disposition=attachment%3B%20' -H 'Proxy-Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'Accept-Language: zh-CN,zh;q=0.9' --compressed --insecure -o sample_train.tar.gz | ||
cd .. | ||
echo "unzipping sample_train.tar.gz......" | ||
tar -xzvf ${train_source_path} -C data && rm -rf ${train_source_path} | ||
cd data | ||
echo "downloading sample_test.tar.gz......" | ||
curl -# 'http://jupter-oss.oss-cn-hangzhou.aliyuncs.com/file/opensearch/documents/408/sample_test.tar.gz?Expires=1586435821&OSSAccessKeyId=LTAIGx40tjZWxj6q&Signature=OwLMPjt1agByQtRVi8pazsAliNk%3D&response-content-disposition=attachment%3B%20' -H 'Proxy-Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'Accept-Language: zh-CN,zh;q=0.9' --compressed --insecure -o sample_test.tar.gz | ||
cd .. | ||
echo "unzipping sample_test.tar.gz......" | ||
tar -xzvf ${test_source_path} -C data && rm -rf ${test_source_path} | ||
echo "preprocessing data......" | ||
python process_public_data.py | ||
mv data/ctr_cvr.train data/whole_data/train | ||
mv data/ctr_cvr.test data/whole_data/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# AITM模型的点击率预估模型 | ||
|
||
代码请参考:[AITM](https://github.com/PaddlePaddle/PaddleRec/tree/master/models/rank/aitm) | ||
如果我们的代码对您有用,还请点个star啊~ | ||
|
||
## 内容 | ||
|
||
- [模型简介](#模型简介) | ||
- [数据准备](#数据准备) | ||
- [运行环境](#运行环境) | ||
- [快速开始](#快速开始) | ||
- [效果复现](#效果复现) | ||
- [进阶使用](#进阶使用) | ||
- [FAQ](#FAQ) | ||
|
||
## 模型简介 | ||
在推荐场景里,用户的转化链路往往有多个中间步骤(曝光->点击->转化),而有些行业转化链路很长,如金融-信用卡业务,它包括曝光->点击->表单(application)->信用核准(approval)->信用卡激活(activation)。处于链路后端的节点(如approval/activation),因为转化时间久,获取难度较大,导致转化数据少,训练时类别不平衡的问题很严重。 | ||
|
||
作者设计了一种多任务模型框架,充分利用了链路上各个节点的样本,提升模型对后端节点转化率的预估 | ||
## 数据准备 | ||
|
||
数据为[Ali-CCP click](https://tianchi.aliyun.com/datalab/dataSet.html?dataId=408) | ||
在模型目录的data目录下为您准备了快速运行的示例数据,若需要使用全量数据可以参考下方[效果复现](#效果复现)部分。 | ||
|
||
## 运行环境 | ||
PaddlePaddle>=2.0 | ||
|
||
python 2.7/3.5/3.6/3.7 | ||
|
||
os : windows/linux/macos | ||
|
||
## 快速开始 | ||
本文提供了样例数据可以供您快速体验,在任意目录下均可执行。在aitm模型目录的快速执行命令如下: | ||
```bash | ||
# 进入模型目录 | ||
# cd models/rank/aitm # 在任意目录均可运行 | ||
# 动态图训练 | ||
python -u ../../../tools/trainer.py -m config.yaml | ||
|
||
# 动态图预测 | ||
python -u ../../../tools/infer.py -m config.yaml | ||
``` | ||
## 效果复现 | ||
为了方便使用者能够快速的跑通每一个模型,我们在每个模型下都提供了样例数据。如果需要复现readme中的效果,请按如下步骤依次操作即可。 | ||
在全量数据下模型的指标如下: | ||
| 模型 | click auc | purchase auc |batch_size | epoch_num| Time of each epoch | | ||
| :------| :------ | :------ | :------ | :------| :------ | | ||
| aitm | 0.6186 |0.6525 | 2000 | 6| 约3小时 | | ||
|
||
1. 确认您当前所在目录为PaddleRec/models/rank/aitm | ||
2. 进入Paddlerec/datasets/ali-cpp_aitm | ||
3. 执行命令运行全量数据 | ||
|
||
``` bash | ||
cd ../../../datasets/ali-cpp_aitm | ||
sh run.sh | ||
``` | ||
```bash | ||
cd - # 切回模型目录 | ||
# 动态图训练 | ||
python -u ../../../tools/trainer.py -m config_bigdata.yaml | ||
python -u ../../../tools/infer.py -m config_bigdata.yaml | ||
``` | ||
## 进阶使用 | ||
|
||
## FAQ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import print_function | ||
import numpy as np | ||
import paddle | ||
from paddle.io import Dataset | ||
|
||
|
||
class RecDataset(Dataset): | ||
def __init__(self, file_list, config): | ||
super(RecDataset, self).__init__() | ||
self.feature_names = [] | ||
self.datafile = file_list[0] | ||
self.data = [] | ||
self._load_data() | ||
|
||
def _load_data(self): | ||
print("start load data from: {}".format(self.datafile)) | ||
count = 0 | ||
with open(self.datafile) as f: | ||
self.feature_names = f.readline().strip().split(',')[2:] | ||
for line in f: | ||
count += 1 | ||
line = line.strip().split(',') | ||
line = [int(v) for v in line] | ||
self.data.append(line) | ||
print("load data from {} finished".format(self.datafile)) | ||
|
||
def __len__(self, ): | ||
return len(self.data) | ||
|
||
def __getitem__(self, idx): | ||
line = self.data[idx] | ||
click = line[0] | ||
conversion = line[1] | ||
# features = dict(zip(self.feature_names, line[2:])) | ||
features = line[2:] | ||
return click, conversion, features |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
multitask