Skip to content

Instantly share code, notes, and snippets.

View eggie5's full-sized avatar
💭
Working on TensorFlow Ranking

Alex Egg eggie5

💭
Working on TensorFlow Ranking
View GitHub Profile
def bpr_mf(user_count, item_count, hidden_dim):
u = tf.placeholder(tf.int32, [None])
i = tf.placeholder(tf.int32, [None])
j = tf.placeholder(tf.int32, [None])
with tf.device("/cpu:0"):
user_emb_w = tf.get_variable("user_emb_w", [user_count+1, hidden_dim],
initializer=tf.random_normal_initializer(0, 0.1))
item_emb_w = tf.get_variable("item_emb_w", [item_count+1, hidden_dim],
initializer=tf.random_normal_initializer(0, 0.1))
# Generalized Matrix Factorization
def GMF(num_users, num_items, k, reg):
"""Models the interaction between user/item LV w/ linear dot-product"""
print("using dot-product interaction term")
seed = 1
uid = Input((1,), name="uid")
iid = Input((1,), name="iid")
item_bias = Embedding(
import pandas as pd
import tensorflow as tf
import dask.dataframe as dd
from dask.distributed import Client, LocalCluster
from tensorflow.python.saved_model import loader
def encode_factory(sess, export_path:str):
"""Loads TF SavedModel and returns a callable"""
def plot_confusion_matrix(y_true, y_pred, classes,
normalize=False,
title=None,
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if not title:
if normalize:
@eggie5
eggie5 / clr.py
Created November 11, 2019 22:00
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.ops import math_ops
from tensorflow.python.eager import context
def cyclic_learning_rate(global_step,
learning_rate=0.01,
max_lr=0.1,
step_size=20.,
gamma=0.99994,
@eggie5
eggie5 / verify_tfrecords.py
Created August 7, 2019 21:16 — forked from LeegleechN/verify_tfrecords.py
Check TFRecords
"""Checks if a set of TFRecords appear to be valid.
Specifically, this checks whether the provided record sizes are consistent and
that the file does not end in the middle of a record. It does not verify the
CRCs.
"""
import struct
from multiprocessing import Pool
import tensorflow as tf

The saved model CLI can't handle string input w/ the input_examples flag RE: tensorflow/tensorflow#27662 :

saved_model_cli run \
--dir . \
--tag_set serve \
--signature_def predict \
--input_examples 'examples=[{"menu_item":["this is a sentence"]}]'
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.Tensors;
import org.tensorflow.TensorFlow;
import org.tensorflow.SavedModelBundle;
import org.tensorflow.SavedModelBundle.Loader;
import org.tensorflow.framework.SignatureDef;
import org.tensorflow.framework.MetaGraphDef;
import org.tensorflow.framework.TensorInfo;

Learning to Rank

A common method to rank a set of items is to pass all items through a scoring function and then sorting the scores to get an overall rank. Traditionally this space has been domianted by ordinal regression techniques on point-wise data. However, there are serious advantages to exploit by learning a scoring function on pair-wise data instead. This technique commonly called RankNet was originally explored by the seminal Learning to Rank by Gradient Descent[^1] paper by Microsoft.

In this talk we will discuss:

  • Theory behind point-wise and pair-wise data

  • Ordinal Regression: ranking point-wise data

  • how to crowd-source pair-wise data

@eggie5
eggie5 / fm.py
Created September 27, 2018 13:20
import numpy as np
from sklearn.base import BaseEstimator
from keras.layers import Input, Embedding, Dense,Flatten ,Activation, Add, Dot
from keras.models import Model
from keras.regularizers import l2 as l2_reg
from keras import initializers
import itertools
def build_model(max_features,K=8,solver='adam',l2=0.0,l2_fm = 0.0):