forked from PaddlePaddle/PaddleRec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_infer.py
141 lines (121 loc) · 5.48 KB
/
reader_infer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Copyright (c) 2022 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 pickle
from paddle.io import IterableDataset
class RecDataset(IterableDataset):
def __init__(self, file_list, config):
super(RecDataset, self).__init__()
self.file_list = file_list
self.config = config
self.task_count = config.get("hyper_parameters.task_count")
self.batchsize = config.get("hyper_parameters.batch_size")
self.static_context_col = [
'carrier',
'consumptionAbility',
'LBS',
'age',
'education',
'gender',
'house',
]
self.dynamic_context_col = [
'interest1',
'interest2',
'interest3',
'kw1',
'kw2',
'topic1',
'topic2',
]
self.ad_col = [
'advertiserId',
'campaignId',
'creativeSize',
'adCategoryId',
'productId',
'productType',
]
self.col_length_name = [
x + '_length' for x in self.dynamic_context_col
]
self.label_col = 'label'
self.train_col = self.static_context_col + self.dynamic_context_col + self.col_length_name + self.ad_col
self.all_col = [
self.label_col, 'aid'
] + self.static_context_col + self.dynamic_context_col + self.col_length_name + self.ad_col
def __iter__(self):
np.random.seed(2021)
self.file_list.sort()
print(self.file_list)
with open(self.file_list[0], "rb") as data_test_stage1:
with open(self.file_list[1], "rb") as data_test_stage2:
data_test_stage1 = pickle.load(data_test_stage1)[self.all_col]
data_test_stage2 = pickle.load(data_test_stage2)[self.all_col]
aid_set = set(data_test_stage1.aid)
for aid in aid_set:
task_test_stage1 = data_test_stage1[data_test_stage1.aid ==
aid]
task_test_stage2 = data_test_stage2[data_test_stage2.aid ==
aid]
data_train = task_test_stage1.sample(frac=1)
data_test = task_test_stage2
output_list = list()
batch_sup_x = []
batch_sup_x.append(
np.array(data_train[self.static_context_col])
[:]) #shape=[*, 7]
# data_stage1中dynamic部分
temp_list = list()
for k in range(len(self.dynamic_context_col)):
dy_np = np.array(data_train[self.dynamic_context_col[
k]])[:]
dy_np = np.vstack(dy_np)
temp_list.append(dy_np)
temp_np = np.concatenate(temp_list, axis=1)
batch_sup_x.append(temp_np) #shape = [*, 50]
batch_sup_x.append(
np.array(data_train[self.col_length_name])
[:]) #shape = [*,7]
batch_sup_x.append(
np.array(data_train[self.ad_col])[:]) #shape = [*,6]
batch_sup_y = np.array(data_train[self.label_col]
.values)[:] #shape = [*,1]
batch_qry_x = []
batch_qry_x.append(
np.array(data_test[self.static_context_col])
[:]) #shape=[*, 7]
# data_stage2中dynamic部分
temp_list = list()
for k in range(len(self.dynamic_context_col)):
dy_np = np.array(data_test[self.dynamic_context_col[
k]])[:]
dy_np = np.vstack(dy_np)
temp_list.append(dy_np)
temp_np = np.concatenate(temp_list, axis=1)
batch_qry_x.append(temp_np) #shape = [*, 50]
batch_qry_x.append(
np.array(data_test[self.col_length_name])
[:]) #shape = [*,7]
batch_qry_x.append(
np.array(data_test[self.ad_col])[:]) #shape = [*,6]
batch_qry_y = np.array(data_test[self.label_col]
.values)[:] #shape = [*,1]
output_list.append(np.array([aid])) #本次子任务的aid
output_list.append(batch_sup_x) # shape = [*, 7+50+7+6]
output_list.append(batch_sup_y) # shape = [*, 1]
output_list.append(batch_qry_x) # shape = [*, 7+50+7+6]
output_list.append(batch_qry_y) # shape = [*, 1]
yield output_list