From f4cb25b41385ae6ea9e1979c766c243982f9e8da Mon Sep 17 00:00:00 2001 From: frankwhzhang Date: Fri, 29 May 2020 17:41:13 +0800 Subject: [PATCH] fix youtube_dnn --- models/recall/youtube_dnn/config.yaml | 65 ++++++++++-------- models/recall/youtube_dnn/model.py | 79 +++++++++++----------- models/recall/youtube_dnn/random_reader.py | 12 ++-- 3 files changed, 79 insertions(+), 77 deletions(-) diff --git a/models/recall/youtube_dnn/config.yaml b/models/recall/youtube_dnn/config.yaml index 6cffbaba..5bbc41a9 100644 --- a/models/recall/youtube_dnn/config.yaml +++ b/models/recall/youtube_dnn/config.yaml @@ -13,37 +13,42 @@ # limitations under the License. -train: - trainer: - # for cluster training - strategy: "async" +workspace: "paddlerec.models.recall.youtube_dnn" - epochs: 3 - workspace: "paddlerec.models.recall.youtube_dnn" - device: cpu +dataset: +- name: dataset_train + batch_size: 5 + type: DataLoader + #type: QueueDataset + data_path: "{workspace}/data/train" + data_converter: "{workspace}/random_reader.py" + +hyper_parameters: + watch_vec_size: 64 + search_vec_size: 64 + other_feat_size: 64 + output_size: 100 + layers: [128, 64, 32] + optimizer: + class: adam + learning_rate: 0.001 + strategy: async - reader: - batch_size: 2 - class: "{workspace}/random_reader.py" - train_data_path: "{workspace}/data/train" +mode: train_runner - model: - models: "{workspace}/model.py" - hyper_parameters: - watch_vec_size: 64 - search_vec_size: 64 - other_feat_size: 64 - output_size: 100 - layers: [128, 64, 32] - learning_rate: 0.01 - optimizer: sgd +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 - 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 diff --git a/models/recall/youtube_dnn/model.py b/models/recall/youtube_dnn/model.py index 22953764..a1203447 100644 --- a/models/recall/youtube_dnn/model.py +++ b/models/recall/youtube_dnn/model.py @@ -13,39 +13,64 @@ # limitations under the License. import math +import numpy as np import paddle.fluid as fluid from paddlerec.core.utils import envs from paddlerec.core.model import Model as ModelBase -import numpy as np class Model(ModelBase): def __init__(self, config): ModelBase.__init__(self, config) - def input_data(self, is_infer=False): + def _init_hyper_parameters(self): + self.watch_vec_size = envs.get_global_env( + "hyper_parameters.watch_vec_size") + self.search_vec_size = envs.get_global_env( + "hyper_parameters.search_vec_size") + self.other_feat_size = envs.get_global_env( + "hyper_parameters.other_feat_size") + self.output_size = envs.get_global_env("hyper_parameters.output_size") + self.layers = envs.get_global_env("hyper_parameters.layers") - watch_vec_size = envs.get_global_env("hyper_parameters.watch_vec_size", - None, self._namespace) - search_vec_size = envs.get_global_env( - "hyper_parameters.search_vec_size", None, self._namespace) - other_feat_size = envs.get_global_env( - "hyper_parameters.other_feat_size", None, self._namespace) + def input_data(self, is_infer=False, **kwargs): watch_vec = fluid.data( - name="watch_vec", shape=[None, watch_vec_size], dtype="float32") + name="watch_vec", + shape=[None, self.watch_vec_size], + dtype="float32") search_vec = fluid.data( - name="search_vec", shape=[None, search_vec_size], dtype="float32") + name="search_vec", + shape=[None, self.search_vec_size], + dtype="float32") other_feat = fluid.data( - name="other_feat", shape=[None, other_feat_size], dtype="float32") + name="other_feat", + shape=[None, self.other_feat_size], + dtype="float32") label = fluid.data(name="label", shape=[None, 1], dtype="int64") inputs = [watch_vec] + [search_vec] + [other_feat] + [label] - self._data_var = inputs return inputs - def fc(self, tag, data, out_dim, active='relu'): + def net(self, inputs, is_infer=False): + concat_feats = fluid.layers.concat(input=inputs[:-1], axis=-1) + + l1 = self._fc('l1', concat_feats, self.layers[0], 'relu') + l2 = self._fc('l2', l1, self.layers[1], 'relu') + l3 = self._fc('l3', l2, self.layers[2], 'relu') + l4 = self._fc('l4', l3, self.output_size, 'softmax') + + num_seqs = fluid.layers.create_tensor(dtype='int64') + acc = fluid.layers.accuracy(input=l4, label=inputs[-1], total=num_seqs) + + cost = fluid.layers.cross_entropy(input=l4, label=inputs[-1]) + avg_cost = fluid.layers.mean(cost) + + self._cost = avg_cost + self._metrics["acc"] = acc + + def _fc(self, tag, data, out_dim, active='relu'): init_stddev = 1.0 scales = 1.0 / np.sqrt(data.shape[1]) @@ -67,31 +92,3 @@ class Model(ModelBase): bias_attr=b_attr, name=tag) return out - - def net(self, inputs): - output_size = envs.get_global_env("hyper_parameters.output_size", None, - self._namespace) - layers = envs.get_global_env("hyper_parameters.layers", None, - self._namespace) - concat_feats = fluid.layers.concat(input=inputs[:-1], axis=-1) - - l1 = self.fc('l1', concat_feats, layers[0], 'relu') - l2 = self.fc('l2', l1, layers[1], 'relu') - l3 = self.fc('l3', l2, layers[2], 'relu') - l4 = self.fc('l4', l3, output_size, 'softmax') - - num_seqs = fluid.layers.create_tensor(dtype='int64') - acc = fluid.layers.accuracy(input=l4, label=inputs[-1], total=num_seqs) - - cost = fluid.layers.cross_entropy(input=l4, label=inputs[-1]) - avg_cost = fluid.layers.mean(cost) - - self._cost = avg_cost - self._metrics["acc"] = acc - - def train_net(self): - input_data = self.input_data() - self.net(input_data) - - def infer_net(self): - pass diff --git a/models/recall/youtube_dnn/random_reader.py b/models/recall/youtube_dnn/random_reader.py index 30df6d1d..cdb0add6 100644 --- a/models/recall/youtube_dnn/random_reader.py +++ b/models/recall/youtube_dnn/random_reader.py @@ -13,22 +13,22 @@ # limitations under the License. from __future__ import print_function +import numpy as np + from paddlerec.core.reader import Reader from paddlerec.core.utils import envs from collections import defaultdict -import numpy as np class TrainReader(Reader): def init(self): self.watch_vec_size = envs.get_global_env( - "hyper_parameters.watch_vec_size", None, "train.model") + "hyper_parameters.watch_vec_size") self.search_vec_size = envs.get_global_env( - "hyper_parameters.search_vec_size", None, "train.model") + "hyper_parameters.search_vec_size") self.other_feat_size = envs.get_global_env( - "hyper_parameters.other_feat_size", None, "train.model") - self.output_size = envs.get_global_env("hyper_parameters.output_size", - None, "train.model") + "hyper_parameters.other_feat_size") + self.output_size = envs.get_global_env("hyper_parameters.output_size") def generate_sample(self, line): """ -- GitLab