def get_hit_rate_and_ndcg(self, predicted_scores_by_user, items_by_user, top_k=rconst.TOP_K, match_mlperf=False): rconst.TOP_K = top_k rconst.NUM_EVAL_NEGATIVES = predicted_scores_by_user.shape[1] - 1 batch_size = items_by_user.shape[0] users = np.repeat(np.arange(batch_size)[:, np.newaxis], rconst.NUM_EVAL_NEGATIVES + 1, axis=1) users, items, duplicate_mask = \ data_pipeline.BaseDataConstructor._assemble_eval_batch( users, items_by_user[:, -1:], items_by_user[:, :-1], batch_size) g = tf.Graph() with g.as_default(): logits = tf.convert_to_tensor( predicted_scores_by_user.reshape((-1, 1)), tf.float32) softmax_logits = tf.concat([tf.zeros(logits.shape, dtype=logits.dtype), logits], axis=1) duplicate_mask = tf.convert_to_tensor(duplicate_mask, tf.float32) metric_ops = neumf_model._get_estimator_spec_with_metrics( logits=logits, softmax_logits=softmax_logits, duplicate_mask=duplicate_mask, num_training_neg=NUM_TRAIN_NEG, match_mlperf=match_mlperf).eval_metric_ops hr = metric_ops[rconst.HR_KEY] ndcg = metric_ops[rconst.NDCG_KEY] init = [tf.compat.v1.global_variables_initializer(), tf.compat.v1.local_variables_initializer()] with self.session(graph=g) as sess: sess.run(init) return sess.run([hr[1], ndcg[1]])
def get_hit_rate_and_ndcg(self, predicted_scores_by_user, items_by_user, top_k=rconst.TOP_K, match_mlperf=False): rconst.TOP_K = top_k rconst.NUM_EVAL_NEGATIVES = predicted_scores_by_user.shape[1] - 1 batch_size = items_by_user.shape[0] users = np.repeat(np.arange(batch_size)[:, np.newaxis], rconst.NUM_EVAL_NEGATIVES + 1, axis=1) users, items, duplicate_mask = \ data_pipeline.BaseDataConstructor._assemble_eval_batch( users, items_by_user[:, -1:], items_by_user[:, :-1], batch_size) g = tf.Graph() with g.as_default(): logits = tf.convert_to_tensor( predicted_scores_by_user.reshape((-1, 1)), tf.float32) softmax_logits = tf.concat([tf.zeros(logits.shape, dtype=logits.dtype), logits], axis=1) duplicate_mask = tf.convert_to_tensor(duplicate_mask, tf.float32) metric_ops = neumf_model._get_estimator_spec_with_metrics( logits=logits, softmax_logits=softmax_logits, duplicate_mask=duplicate_mask, num_training_neg=NUM_TRAIN_NEG, match_mlperf=match_mlperf).eval_metric_ops hr = metric_ops[rconst.HR_KEY] ndcg = metric_ops[rconst.NDCG_KEY] init = [tf.global_variables_initializer(), tf.local_variables_initializer()] with self.test_session(graph=g) as sess: sess.run(init) return sess.run([hr[1], ndcg[1]])