|
| 1 | +# coding=utf-8 |
| 2 | + |
| 3 | +import time |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import tensorflow as tf |
| 7 | +import pandas as pd |
| 8 | + |
| 9 | +from caicloud.clever.tensorflow import dist_base |
| 10 | +from caicloud.clever.tensorflow import model_exporter |
| 11 | + |
| 12 | +tf.app.flags.DEFINE_string("export_dir", |
| 13 | + "/tmp/saved_model/movie", |
| 14 | + "model export directory path.") |
| 15 | + |
| 16 | +tf.app.flags.DEFINE_string("batch_size", 128, "training batch size.") |
| 17 | +tf.app.flags.DEFINE_string("embedding_dim", 50, "embedding dimension.") |
| 18 | + |
| 19 | +FLAGS = tf.app.flags.FLAGS |
| 20 | +USER_NUM = 6040 |
| 21 | +ITEM_NUM = 3952 |
| 22 | + |
| 23 | +def get_data(): |
| 24 | + col_names = ["user", "item", "rate", "st"] |
| 25 | + df = pd.read_csv("/tmp/movielens/ml-1m/ratings.dat", sep="::", header=None, names=col_names, engine='python') |
| 26 | + |
| 27 | + df["user"] -= 1 |
| 28 | + df["item"] -= 1 |
| 29 | + for col in ("user", "item"): |
| 30 | + df[col] = df[col].astype(np.int32) |
| 31 | + df["rate"] = df["rate"].astype(np.float32) |
| 32 | + |
| 33 | + rows = len(df) |
| 34 | + print "Total number of instances: ", rows |
| 35 | + df = df.iloc[np.random.permutation(rows)].reset_index(drop=True) |
| 36 | + split_index = int(rows * 0.9) |
| 37 | + return df[0:split_index], df[split_index:] |
| 38 | + |
| 39 | +class ShuffleIterator(object): |
| 40 | + def __init__(self, inputs, batch_size=10): |
| 41 | + self.inputs = inputs |
| 42 | + self.batch_size = batch_size |
| 43 | + self.num_cols = len(self.inputs) |
| 44 | + self.len = len(self.inputs[0]) |
| 45 | + self.inputs = np.transpose(np.vstack([np.array(self.inputs[i]) for i in range(self.num_cols)])) |
| 46 | + |
| 47 | + def __len__(self): |
| 48 | + return self.len |
| 49 | + |
| 50 | + def __iter__(self): |
| 51 | + return self |
| 52 | + |
| 53 | + def __next__(self): |
| 54 | + return self.next() |
| 55 | + |
| 56 | + def next(self): |
| 57 | + ids = np.random.randint(0, self.len, (self.batch_size,)) |
| 58 | + out = self.inputs[ids, :] |
| 59 | + return [out[:, i] for i in range(self.num_cols)] |
| 60 | + |
| 61 | +_train, _test = get_data() |
| 62 | +_iter_train = ShuffleIterator([_train["user"], _train["item"], _train["rate"]], batch_size=FLAGS.batch_size) |
| 63 | +_train_op = None |
| 64 | +_infer = None |
| 65 | +_global_step = None |
| 66 | +_user_batch = None |
| 67 | +_item_batch = None |
| 68 | +_rate_batch = None |
| 69 | +_cost = None |
| 70 | +_rmse = None |
| 71 | +_local_step = 0 |
| 72 | + |
| 73 | +def inference(user_batch, item_batch, dim): |
| 74 | + w_user = tf.get_variable("embd_user", shape=[USER_NUM, dim], |
| 75 | + initializer=tf.truncated_normal_initializer(stddev=0.02)) |
| 76 | + w_item = tf.get_variable("embd_item", shape=[ITEM_NUM, dim], |
| 77 | + initializer=tf.truncated_normal_initializer(stddev=0.02)) |
| 78 | + |
| 79 | + input1 = tf.nn.embedding_lookup(w_user, user_batch) |
| 80 | + input2 = tf.nn.embedding_lookup(w_item, item_batch) |
| 81 | + input = tf.concat([input1, input2], 1) |
| 82 | + |
| 83 | + w = tf.get_variable("w", shape=[2*dim, 1], initializer=tf.truncated_normal_initializer(stddev=0.02)) |
| 84 | + b = tf.get_variable("b", shape=[1], initializer=tf.constant_initializer(1)) |
| 85 | + infer = tf.transpose(tf.matmul(input, w) + b, name="infer") |
| 86 | + return infer |
| 87 | + |
| 88 | +def model_fn(sync, num_replicas): |
| 89 | + global _train_op, _infer, _user_batch, _item_batch, _rate_batch, _rmse, _cost, _global_step |
| 90 | + |
| 91 | + _user_batch = tf.placeholder(tf.int32, shape=[None], name="user") |
| 92 | + _item_batch = tf.placeholder(tf.int32, shape=[None], name="item") |
| 93 | + _rate_batch = tf.placeholder(tf.float32, shape=[None], name="rate") |
| 94 | + |
| 95 | + _infer = inference(_user_batch, _item_batch, FLAGS.embedding_dim) |
| 96 | + _global_step = tf.contrib.framework.get_or_create_global_step() |
| 97 | + |
| 98 | + _cost = tf.square(_infer - _rate_batch) |
| 99 | + optimizer = tf.train.AdamOptimizer(0.001) |
| 100 | + _train_op = optimizer.minimize(_cost, global_step=_global_step) |
| 101 | + |
| 102 | + _rmse = tf.sqrt(tf.reduce_mean(_cost)) |
| 103 | + |
| 104 | + def rmse_evalute_fn(session): |
| 105 | + return session.run(_rmse, feed_dict={ |
| 106 | + _user_batch: _test["user"], _item_batch: _test["item"], _rate_batch: _test["rate"]}) |
| 107 | + |
| 108 | + # 定义模型导出配置 |
| 109 | + model_export_spec = model_exporter.ModelExportSpec( |
| 110 | + export_dir=FLAGS.export_dir, |
| 111 | + input_tensors={"user": _user_batch, "item": _item_batch}, |
| 112 | + output_tensors={"infer": _infer}) |
| 113 | + |
| 114 | + # 定义模型评测(准确率)的计算方法 |
| 115 | + model_metric_ops = { |
| 116 | + "rmse": rmse_evalute_fn |
| 117 | + } |
| 118 | + |
| 119 | + return dist_base.ModelFnHandler( |
| 120 | + global_step=_global_step, |
| 121 | + optimizer=optimizer, |
| 122 | + model_metric_ops=model_metric_ops, |
| 123 | + model_export_spec=model_export_spec, |
| 124 | + summary_op=None) |
| 125 | + |
| 126 | +def train_fn(session, num_global_step): |
| 127 | + global _train_op, _infer, _user_batch, _item_batch, _rate_batch, _rmse, _local_step, _cost |
| 128 | + |
| 129 | + users, items, rates = next(_iter_train) |
| 130 | + session.run(_train_op, feed_dict={_user_batch: users, _item_batch: items, _rate_batch: rates}) |
| 131 | + |
| 132 | + if _local_step % 2000 == 0: |
| 133 | + rmse, infer, cost = session.run([_rmse, _infer, _cost], feed_dict={_user_batch: _test["user"], _item_batch: _test["item"], _rate_batch: _test["rate"]}) |
| 134 | + print("Eval RMSE at round {} is: {}".format(num_global_step, rmse)) |
| 135 | + |
| 136 | + _local_step += 1 |
| 137 | + return False |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + distTfRunner = dist_base.DistTensorflowRunner(model_fn = model_fn, gen_init_fn=None) |
| 141 | + distTfRunner.run(train_fn) |
0 commit comments