dup1 0.0.1
My short description for my project.
License: MIT
Requires Python: >=3.6.0

# Deep UserProfile (tensorflow==1.15)


## 1. 使用方式说明
### 1.1 安装使用
```
pip install dup1==0.01
```

### 1.2 模型使用
#### 1.2.1 目前已经集成了DSIN模型相关代码，正在集成TextCNN、MIND、MMoE相关模型.
#### 1.2.2 使用偏好兴趣模型DSIN
##### 1) 配置reader
```
from dtp1.readers.dsin_reader_v1 import DSINReaderV1
# 设置输入模型特征
input_feature = ["cid3_ph", "pwid_ph",
                 "sess_4_cid3_ph", "sess_4_pwid_ph", "sess_length_ph", "label_ph"]
# reader一次batch读取多少条
batch_size = 256
# 通过配置文件把特征映射成idx
kvargs = {"encode_idx_conf": "../../data/encode_idx_conf.json"}
# 输入训练数据文件
data_files = ["../../data/000047_0.gz"]
# 配置reader
dsin_reader = DSINReaderV1(filenames=data_files, 
                           input_feature=input_feature, 
                           batch_size=batch_size, 
                           kvargs=kvargs)
```

##### 2) 配置模型
```
from dtp1.feature.feature_column import SparseFeat, VarLenSparseFeat
from dtp1.models.dsin import DSIN
# 特征是否hash
hash_flag = False
# 稀疏特征
feature_columns = [SparseFeat("cid3_ph", 4891, use_hash=hash_flag),
                   SparseFeat("pwid_ph", 41453, use_hash=hash_flag)]
# ,DenseFeat("price_ph", 1)]
# 序列特征
feature_columns += [
    VarLenSparseFeat(SparseFeat("sess_0_cid3_ph", 4891, embedding_dim=4,
                                use_hash=hash_flag, embedding_name="cid3_ph"), maxlen=10),
    VarLenSparseFeat(SparseFeat('sess_4_pwid_ph', 41453, embedding_dim=4,
                                use_hash=hash_flag, embedding_name='pwid_ph'), maxlen=10)]
# 配置session行为特征
behavior_feature_list = ["cid3_ph", "pwid_ph"]
# session最大多少个
sess_count = 5
# 一个序列最长多少个
sess_len_max = 10
# 配置模型
dsin_model = DSIN(feature_columns, behavior_feature_list, 
                  sess_max_count=sess_count, 
                  dnn_hidden_units=(200, 80),
                  att_head_num=8,
                  att_embedding_size=1, 
                  bias_encoding=False)
```

##### 2) 配置训练，支持单机训练和分布式训练
```
from dtp1.tasks.local_task import LocalTask
# 配置ckpt保存路径
ckpt_path = "../../output/model2ckpt"
local_task = LocalTask(reader=dsin_reader, 
                       model=dsin_model, 
                       ckpt_path=ckpt_path, 
                       steps_log=32)
# steps_log 每32个batch则进行打印输出日志
local_task.train()
```
##### 3) 模型导出，Serving支持
```
from dtp1.export import Export
export = Export()
pb_out_path = "../output/ckpt2pb"
ckpt_in_path = "../output/model2ckpt/"
input_node_names = ["cid3_ph", "pwid_ph",
                    "sess_4_cid3_ph", "sess_4_pwid_ph", "sess_length_ph", "label_ph"]
output_node_names = ["score"]
# 通过ckptpoint导出pb
export.export_pb_from_ckpt(ckpt_in_path, pb_out_path, input_node_names, output_node_names)
input_signature_map = {node: node for node in input_node_names if node not in ["label_ph"]}
output_signature_map = {node: node for node in output_node_names}
pb_in_path = pb_out_path
# 导出serving model
savedmodel_out_path = "../output/saved_model"
export.export_savedmodel_from_pb(pb_in_path, savedmodel_out_path, input_signature_map, output_signature_map)
```
##### 4) 模型测试
```
from dtp1.metirc import metric
from dtp1.predict import ReaderPredict
from dtp1.readers.dsin_reader_v1 import DSINReaderV1
# time_stamp, act, storeid, event_id, cid3, pwid
input_feature = ["cid3_ph", "pwid_ph",
                 "sess_4_cid3_ph", "sess_4_pwid_ph", "sess_length_ph", "label_ph"]
batch_size = 256
kvargs = {"encode_idx_conf": "../data/encode_idx_conf.json"}
data_files = ["../data/000059_0.gz"]
# 配置测试数据参数
dsin_reader = DSINReaderV1(filenames=data_files, input_feature=input_feature, batch_size=batch_size, kvargs=kvargs)
model_file_path = "../output/saved_model"
# 打印测试指标
reader_predict = ReaderPredict(model_file_path)
labels, scores, i = [], [], 0
for x, y in dsin_reader:
    i = i + 1
    labels.extend(x["label_ph"])
    result = reader_predict.predict(x)
    scores.extend(result["score"])
    if i % 100 == 0:
        metric(labels, scores)
metric(labels, scores)
```