From 38aa1162eff8e98fd742eba1ed039b62a05eda31 Mon Sep 17 00:00:00 2001 From: frankwhzhang Date: Fri, 29 May 2020 16:01:03 +0800 Subject: [PATCH] fix esmm --- models/multitask/esmm/config.yaml | 79 +++++++++++++++----------- models/multitask/esmm/esmm_reader.py | 3 - models/multitask/esmm/model.py | 84 ++++++++++++---------------- 3 files changed, 83 insertions(+), 83 deletions(-) diff --git a/models/multitask/esmm/config.yaml b/models/multitask/esmm/config.yaml index f40b967c..b1412515 100644 --- a/models/multitask/esmm/config.yaml +++ b/models/multitask/esmm/config.yaml @@ -12,40 +12,55 @@ # See the License for the specific language governing permissions and # limitations under the License. -evaluate: - reader: - batch_size: 1 - class: "{workspace}/esmm_infer_reader.py" - test_data_path: "{workspace}/data/train" -train: - trainer: - # for cluster training - strategy: "async" +workspace: "paddlerec.models.multitask.esmm" - epochs: 3 - workspace: "paddlerec.models.multitask.esmm" - device: cpu +dataset: +- name: dataset_train + batch_size: 1 + type: QueueDataset + data_path: "{workspace}/data/train" + data_converter: "{workspace}/esmm_reader.py" +- name: dataset_infer + batch_size: 1 + type: QueueDataset + data_path: "{workspace}/data/test" + data_converter: "{workspace}/esmm_reader.py" - reader: - batch_size: 2 - class: "{workspace}/esmm_reader.py" - train_data_path: "{workspace}/data/train" +hyper_parameters: + vocab_size: 10000 + embed_size: 128 + optimizer: + class: adam + learning_rate: 0.001 + strategy: async - model: - models: "{workspace}/model.py" - hyper_parameters: - vocab_size: 10000 - embed_size: 128 - learning_rate: 0.001 - optimizer: adam +#use infer_runner mode and modify 'phase' below if infer +mode: train_runner +#mode: infer_runner + +runner: +- name: train_runner + class: single_train + device: cpu + epochs: 3 + save_checkpoint_interval: 2 + save_inference_interval: 4 + save_checkpoint_path: "increment" + save_inference_path: "inference" + print_interval: 10 +- name: infer_runner + class: single_infer + init_model_path: "increment/0" + device: cpu + epochs: 3 - save: - increment: - dirname: "increment" - epoch_interval: 2 - save_last: True - inference: - dirname: "inference" - epoch_interval: 4 - save_last: True +phase: +- name: train + model: "{workspace}/model.py" + dataset_name: dataset_train + thread_num: 1 + #- name: infer + # model: "{workspace}/model.py" + # dataset_name: dataset_infer + # thread_num: 1 diff --git a/models/multitask/esmm/esmm_reader.py b/models/multitask/esmm/esmm_reader.py index 036e146e..5a3f3f91 100644 --- a/models/multitask/esmm/esmm_reader.py +++ b/models/multitask/esmm/esmm_reader.py @@ -40,8 +40,6 @@ class TrainReader(Reader): This function needs to be implemented by the user, based on data format """ features = line.strip().split(',') - # ctr = list(map(int, features[1])) - # cvr = list(map(int, features[2])) ctr = int(features[1]) cvr = int(features[2]) @@ -54,7 +52,6 @@ class TrainReader(Reader): continue self.all_field_id_dict[field_id][0] = True index = self.all_field_id_dict[field_id][1] - # feat_id = list(map(int, feat_id)) output[index][1].append(int(feat_id)) for field_id in self.all_field_id_dict: diff --git a/models/multitask/esmm/model.py b/models/multitask/esmm/model.py index 71c65395..8a59e9ea 100644 --- a/models/multitask/esmm/model.py +++ b/models/multitask/esmm/model.py @@ -23,28 +23,11 @@ class Model(ModelBase): def __init__(self, config): ModelBase.__init__(self, config) - def fc(self, tag, data, out_dim, active='prelu'): + def _init_hyper_parameters(self): + self.vocab_size = envs.get_global_env("hyper_parameters.vocab_size") + self.embed_size = envs.get_global_env("hyper_parameters.embed_size") - init_stddev = 1.0 - scales = 1.0 / np.sqrt(data.shape[1]) - - p_attr = fluid.param_attr.ParamAttr( - name='%s_weight' % tag, - initializer=fluid.initializer.NormalInitializer( - loc=0.0, scale=init_stddev * scales)) - - b_attr = fluid.ParamAttr( - name='%s_bias' % tag, initializer=fluid.initializer.Constant(0.1)) - - out = fluid.layers.fc(input=data, - size=out_dim, - act=active, - param_attr=p_attr, - bias_attr=b_attr, - name=tag) - return out - - def input_data(self): + def input_data(self, is_infer=False, **kwargs): sparse_input_ids = [ fluid.data( name="field_" + str(i), @@ -55,26 +38,23 @@ class Model(ModelBase): label_ctr = fluid.data(name="ctr", shape=[-1, 1], dtype="int64") label_cvr = fluid.data(name="cvr", shape=[-1, 1], dtype="int64") inputs = sparse_input_ids + [label_ctr] + [label_cvr] - self._data_var.extend(inputs) - - return inputs + if is_infer: + return inputs + else: + return inputs def net(self, inputs, is_infer=False): - vocab_size = envs.get_global_env("hyper_parameters.vocab_size", None, - self._namespace) - embed_size = envs.get_global_env("hyper_parameters.embed_size", None, - self._namespace) emb = [] for data in inputs[0:-2]: feat_emb = fluid.embedding( input=data, - size=[vocab_size, embed_size], + size=[self.vocab_size, self.embed_size], param_attr=fluid.ParamAttr( name='dis_emb', learning_rate=5, initializer=fluid.initializer.Xavier( - fan_in=embed_size, fan_out=embed_size)), + fan_in=self.embed_size, fan_out=self.embed_size)), is_sparse=True) field_emb = fluid.layers.sequence_pool( input=feat_emb, pool_type='sum') @@ -83,14 +63,14 @@ class Model(ModelBase): # ctr active = 'relu' - ctr_fc1 = self.fc('ctr_fc1', concat_emb, 200, active) - ctr_fc2 = self.fc('ctr_fc2', ctr_fc1, 80, active) - ctr_out = self.fc('ctr_out', ctr_fc2, 2, 'softmax') + ctr_fc1 = self._fc('ctr_fc1', concat_emb, 200, active) + ctr_fc2 = self._fc('ctr_fc2', ctr_fc1, 80, active) + ctr_out = self._fc('ctr_out', ctr_fc2, 2, 'softmax') # cvr - cvr_fc1 = self.fc('cvr_fc1', concat_emb, 200, active) - cvr_fc2 = self.fc('cvr_fc2', cvr_fc1, 80, active) - cvr_out = self.fc('cvr_out', cvr_fc2, 2, 'softmax') + cvr_fc1 = self._fc('cvr_fc1', concat_emb, 200, active) + cvr_fc2 = self._fc('cvr_fc2', cvr_fc1, 80, active) + cvr_out = self._fc('cvr_out', cvr_fc2, 2, 'softmax') ctr_clk = inputs[-2] ctcvr_buy = inputs[-1] @@ -127,15 +107,23 @@ class Model(ModelBase): self._metrics["AUC_ctcvr"] = auc_ctcvr self._metrics["BATCH_AUC_ctcvr"] = batch_auc_ctcvr - def train_net(self): - input_data = self.input_data() - self.net(input_data) - - def infer_net(self): - self._infer_data_var = self.input_data() - self._infer_data_loader = fluid.io.DataLoader.from_generator( - feed_list=self._infer_data_var, - capacity=64, - use_double_buffer=False, - iterable=False) - self.net(self._infer_data_var, is_infer=True) + def _fc(self, tag, data, out_dim, active='prelu'): + + init_stddev = 1.0 + scales = 1.0 / np.sqrt(data.shape[1]) + + p_attr = fluid.param_attr.ParamAttr( + name='%s_weight' % tag, + initializer=fluid.initializer.NormalInitializer( + loc=0.0, scale=init_stddev * scales)) + + b_attr = fluid.ParamAttr( + name='%s_bias' % tag, initializer=fluid.initializer.Constant(0.1)) + + out = fluid.layers.fc(input=data, + size=out_dim, + act=active, + param_attr=p_attr, + bias_attr=b_attr, + name=tag) + return out -- GitLab