Skip to content

Instantly share code, notes, and snippets.

@victorkohler
Created March 14, 2019 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorkohler/35b0d5e0d685b7bbb50b8955463c22e8 to your computer and use it in GitHub Desktop.
Save victorkohler/35b0d5e0d685b7bbb50b8955463c22e8 to your computer and use it in GitHub Desktop.
#-------------------------
# TENSORFLOW GRAPH
#-------------------------
# Set up our Tensorflow graph
graph = tf.Graph()
def init_variable(size, dim, name=None):
'''
Helper function to initialize a new variable with
uniform random values.
'''
std = np.sqrt(2 / dim)
return tf.Variable(tf.random_uniform([size, dim], -std, std), name=name)
def embed(inputs, size, dim, name=None):
'''
Helper function to get a Tensorflow variable and create
an embedding lookup to map our user and item
indices to vectors.
'''
emb = init_variable(size, dim, name)
return tf.nn.embedding_lookup(emb, inputs)
def get_variable(graph, session, name):
'''
Helper function to get the value of a
Tensorflow variable by name.
'''
v = graph.get_operation_by_name(name)
v = v.values()[0]
v = v.eval(session=session)
return v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment