tf.nn.embedding_lookup(params, ids, partition_strategy='mod', name=None, validate_indices=True, max_norm=None)
在params中查找与ids对应的表示。
如下代码表示在W
中查找self.input_x
对应的表示。
W = tf.Variable( tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0),name="W")
self.embedded_chars = tf.nn.embedding_lookup(W, self.input_x)
与在numpy中检索arrays类似, E.g.
matrix = np.random.random([1024, 64]) # 64-dimensional embeddings
ids = np.array([0, 5, 17, 33])
print matrix[ids] # prints a matrix of shape [4, 64]
partition_strategy
决定了ids分布的方式,如果partition_strategy
是 “mod”,每个 id 按照 p = id % len(params)
分割. 例如,13 ids 按照 5 的间隔分割成5份: [[0, 5, 10], [1, 6, 11], [2, 7, 12], [3, 8], [4, 9]]
如果 partition_strategy
是 “div”, 每个 id连续地进行分割, 上一个例子分割为: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10], [11, 12]]