reader.py 2.5 KB
Newer Older
1 2 3
import mmh3


Q
Qiao Longfei 已提交
4
class Dataset:
D
dongdaxiang 已提交
5 6
    def __init__(self):
        pass
7

Z
zhang wenhui 已提交
8

D
dongdaxiang 已提交
9
class CriteoDataset(Dataset):
10 11
    def __init__(self, sparse_feature_dim):
        self.cont_min_ = [0, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Z
zhang wenhui 已提交
12 13 14 15 16 17
        self.cont_max_ = [
            20, 600, 100, 50, 64000, 500, 100, 50, 500, 10, 10, 10, 50
        ]
        self.cont_diff_ = [
            20, 603, 100, 50, 64000, 500, 100, 50, 500, 10, 10, 10, 50
        ]
18
        self.hash_dim_ = sparse_feature_dim
D
dongdaxiang 已提交
19
        # here, training data are lines with line_index < train_idx_
20 21 22 23
        self.train_idx_ = 41256555
        self.continuous_range_ = range(1, 14)
        self.categorical_range_ = range(14, 40)

D
dongdaxiang 已提交
24
    def _reader_creator(self, file_list, is_train, trainer_num, trainer_id):
25 26 27 28 29 30 31
        def reader():
            for file in file_list:
                with open(file, 'r') as f:
                    line_idx = 0
                    for line in f:
                        line_idx += 1
                        if is_train and line_idx > self.train_idx_:
Q
Qiao Longfei 已提交
32
                            break
33 34
                        elif not is_train and line_idx <= self.train_idx_:
                            continue
Q
Qiao Longfei 已提交
35
                        if line_idx % trainer_num != trainer_id:
36 37 38 39 40 41 42 43
                            continue
                        features = line.rstrip('\n').split('\t')
                        dense_feature = []
                        sparse_feature = []
                        for idx in self.continuous_range_:
                            if features[idx] == '':
                                dense_feature.append(0.0)
                            else:
Z
zhang wenhui 已提交
44 45 46
                                dense_feature.append((float(features[idx]) -
                                                      self.cont_min_[idx - 1]) /
                                                     self.cont_diff_[idx - 1])
47
                        for idx in self.categorical_range_:
Z
zhang wenhui 已提交
48
                            sparse_feature.append([
49 50
                                mmh3.hash(str(idx) + features[idx]) %
                                self.hash_dim_
Z
zhang wenhui 已提交
51
                            ])
52 53 54

                        label = [int(features[0])]
                        yield [dense_feature] + sparse_feature + [label]
Z
zhang wenhui 已提交
55

56 57
        return reader

D
dongdaxiang 已提交
58 59
    def train(self, file_list, trainer_num, trainer_id):
        return self._reader_creator(file_list, True, trainer_num, trainer_id)
60 61

    def test(self, file_list):
Q
Qiao Longfei 已提交
62
        return self._reader_creator(file_list, False, 1, 0)
63 64

    def infer(self, file_list):
Q
Qiao Longfei 已提交
65
        return self._reader_creator(file_list, False, 1, 0)