提交 8407391f 编写于 作者: F frankwhzhang

fix ncf

上级 93e5453c
此差异已折叠。
...@@ -12,42 +12,56 @@ ...@@ -12,42 +12,56 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
evaluate: workspace: "paddlerec.models.recall.ncf"
reader:
batch_size: 1
class: "{workspace}/movielens_infer_reader.py"
test_data_path: "{workspace}/data/test"
train: dataset:
trainer: - name: dataset_train
# for cluster training batch_size: 5
strategy: "async" type: QueueDataset
data_path: "{workspace}/data/train"
data_converter: "{workspace}/movielens_reader.py"
- name: dataset_infer
batch_size: 5
type: QueueDataset
data_path: "{workspace}/data/test"
data_converter: "{workspace}/movielens_infer_reader.py"
epochs: 3 hyper_parameters:
workspace: "paddlerec.models.recall.ncf" num_users: 6040
device: cpu num_items: 3706
latent_dim: 8
layers: [64, 32, 16, 8]
optimizer:
class: adam
learning_rate: 0.001
strategy: async
reader: #use infer_runner mode and modify 'phase' below if infer
batch_size: 2 mode: train_runner
class: "{workspace}/movielens_reader.py" #mode: infer_runner
train_data_path: "{workspace}/data/train"
model: runner:
models: "{workspace}/model.py" - name: train_runner
hyper_parameters: class: single_train
num_users: 6040 device: cpu
num_items: 3706 epochs: 3
latent_dim: 8 save_checkpoint_interval: 2
layers: [64, 32, 16, 8] save_inference_interval: 4
learning_rate: 0.001 save_checkpoint_path: "increment"
optimizer: adam save_inference_path: "inference"
print_interval: 10
- name: infer_runner
class: single_infer
init_model_path: "increment/0"
device: cpu
epochs: 3
save: phase:
increment: - name: train
dirname: "increment" model: "{workspace}/model.py"
epoch_interval: 2 dataset_name: dataset_train
save_last: True thread_num: 1
inference: #- name: infer
dirname: "inference" # model: "{workspace}/model.py"
epoch_interval: 4 # dataset_name: dataset_infer
save_last: True # thread_num: 1
...@@ -24,7 +24,13 @@ class Model(ModelBase): ...@@ -24,7 +24,13 @@ class Model(ModelBase):
def __init__(self, config): def __init__(self, config):
ModelBase.__init__(self, config) ModelBase.__init__(self, config)
def input_data(self, is_infer=False): def _init_hyper_parameters(self):
self.num_users = envs.get_global_env("hyper_parameters.num_users")
self.num_items = envs.get_global_env("hyper_parameters.num_items")
self.latent_dim = envs.get_global_env("hyper_parameters.latent_dim")
self.layers = envs.get_global_env("hyper_parameters.layers")
def input_data(self, is_infer=False, **kwargs):
user_input = fluid.data( user_input = fluid.data(
name="user_input", shape=[-1, 1], dtype="int64", lod_level=0) name="user_input", shape=[-1, 1], dtype="int64", lod_level=0)
item_input = fluid.data( item_input = fluid.data(
...@@ -35,45 +41,35 @@ class Model(ModelBase): ...@@ -35,45 +41,35 @@ class Model(ModelBase):
inputs = [user_input] + [item_input] inputs = [user_input] + [item_input]
else: else:
inputs = [user_input] + [item_input] + [label] inputs = [user_input] + [item_input] + [label]
self._data_var = inputs
return inputs return inputs
def net(self, inputs, is_infer=False): def net(self, inputs, is_infer=False):
num_users = envs.get_global_env("hyper_parameters.num_users", None, num_layer = len(self.layers) #Number of layers in the MLP
self._namespace)
num_items = envs.get_global_env("hyper_parameters.num_items", None,
self._namespace)
latent_dim = envs.get_global_env("hyper_parameters.latent_dim", None,
self._namespace)
layers = envs.get_global_env("hyper_parameters.layers", None,
self._namespace)
num_layer = len(layers) #Number of layers in the MLP
MF_Embedding_User = fluid.embedding( MF_Embedding_User = fluid.embedding(
input=inputs[0], input=inputs[0],
size=[num_users, latent_dim], size=[self.num_users, self.latent_dim],
param_attr=fluid.initializer.Normal( param_attr=fluid.initializer.Normal(
loc=0.0, scale=0.01), loc=0.0, scale=0.01),
is_sparse=True) is_sparse=True)
MF_Embedding_Item = fluid.embedding( MF_Embedding_Item = fluid.embedding(
input=inputs[1], input=inputs[1],
size=[num_items, latent_dim], size=[self.num_items, self.latent_dim],
param_attr=fluid.initializer.Normal( param_attr=fluid.initializer.Normal(
loc=0.0, scale=0.01), loc=0.0, scale=0.01),
is_sparse=True) is_sparse=True)
MLP_Embedding_User = fluid.embedding( MLP_Embedding_User = fluid.embedding(
input=inputs[0], input=inputs[0],
size=[num_users, int(layers[0] / 2)], size=[self.num_users, int(self.layers[0] / 2)],
param_attr=fluid.initializer.Normal( param_attr=fluid.initializer.Normal(
loc=0.0, scale=0.01), loc=0.0, scale=0.01),
is_sparse=True) is_sparse=True)
MLP_Embedding_Item = fluid.embedding( MLP_Embedding_Item = fluid.embedding(
input=inputs[1], input=inputs[1],
size=[num_items, int(layers[0] / 2)], size=[self.num_items, int(self.layers[0] / 2)],
param_attr=fluid.initializer.Normal( param_attr=fluid.initializer.Normal(
loc=0.0, scale=0.01), loc=0.0, scale=0.01),
is_sparse=True) is_sparse=True)
...@@ -94,7 +90,7 @@ class Model(ModelBase): ...@@ -94,7 +90,7 @@ class Model(ModelBase):
for i in range(1, num_layer): for i in range(1, num_layer):
mlp_vector = fluid.layers.fc( mlp_vector = fluid.layers.fc(
input=mlp_vector, input=mlp_vector,
size=layers[i], size=self.layers[i],
act='relu', act='relu',
param_attr=fluid.ParamAttr( param_attr=fluid.ParamAttr(
initializer=fluid.initializer.TruncatedNormal( initializer=fluid.initializer.TruncatedNormal(
...@@ -126,16 +122,3 @@ class Model(ModelBase): ...@@ -126,16 +122,3 @@ class Model(ModelBase):
self._cost = avg_cost self._cost = avg_cost
self._metrics["cost"] = avg_cost self._metrics["cost"] = avg_cost
def train_net(self):
input_data = self.input_data()
self.net(input_data)
def infer_net(self):
self._infer_data_var = self.input_data(is_infer=True)
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)
...@@ -19,7 +19,7 @@ from collections import defaultdict ...@@ -19,7 +19,7 @@ from collections import defaultdict
import numpy as np import numpy as np
class EvaluateReader(Reader): class TrainReader(Reader):
def init(self): def init(self):
pass pass
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册