• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15
16"""TensorFlow Ops to work with embeddings (deprecated).
17
18This module and all its submodules are deprecated. See
19[contrib/learn/README.md](https://www.tensorflow.org/code/tensorflow/contrib/learn/README.md)
20for migration instructions.
21
22Note: categorical variables are handled via embeddings in many cases.
23For example, in case of words.
24"""
25
26from __future__ import absolute_import
27from __future__ import division
28from __future__ import print_function
29
30from tensorflow.contrib.framework import deprecated
31from tensorflow.python.framework import ops
32from tensorflow.python.ops import array_ops as array_ops_
33from tensorflow.python.ops import math_ops
34from tensorflow.python.ops import nn
35from tensorflow.python.ops import variable_scope as vs
36
37
38@deprecated('2016-12-01', 'Use `tf.embedding_lookup` instead.')
39def embedding_lookup(params, ids, name='embedding_lookup'):
40  """Provides a N dimensional version of tf.embedding_lookup.
41
42  Ids are flattened to a 1d tensor before being passed to embedding_lookup
43  then, they are unflattend to match the original ids shape plus an extra
44  leading dimension of the size of the embeddings.
45
46  Args:
47    params: List of tensors of size D0 x D1 x ... x Dn-2 x Dn-1.
48    ids: N-dimensional tensor of B0 x B1 x .. x Bn-2 x Bn-1.
49      Must contain indexes into params.
50    name: Optional name for the op.
51
52  Returns:
53    A tensor of size B0 x B1 x .. x Bn-2 x Bn-1 x D1 x ... x Dn-2 x Dn-1
54    containing the values from the params tensor(s) for indecies in ids.
55
56  Raises:
57    ValueError: if some parameters are invalid.
58  """
59  with ops.name_scope(name, 'embedding_lookup', [params, ids]):
60    params = ops.convert_to_tensor(params)
61    ids = ops.convert_to_tensor(ids)
62    shape = array_ops_.shape(ids)
63    ids_flat = array_ops_.reshape(
64        ids, math_ops.reduce_prod(shape, keepdims=True))
65    embeds_flat = nn.embedding_lookup(params, ids_flat, name)
66    embed_shape = array_ops_.concat([shape, [-1]], 0)
67    embeds = array_ops_.reshape(embeds_flat, embed_shape)
68    embeds.set_shape(ids.get_shape().concatenate(params.get_shape()[1:]))
69    return embeds
70
71
72@deprecated('2016-12-01', 'Use `tf.contrib.layers.embed_sequence` instead.')
73def categorical_variable(tensor_in, n_classes, embedding_size, name):
74  """Creates an embedding for categorical variable with given number of classes.
75
76  Args:
77    tensor_in: Input tensor with class identifier (can be batch or
78      N-dimensional).
79    n_classes: Number of classes.
80    embedding_size: Size of embedding vector to represent each class.
81    name: Name of this categorical variable.
82  Returns:
83    Tensor of input shape, with additional dimension for embedding.
84
85  Example:
86    Calling categorical_variable([1, 2], 5, 10, "my_cat"), will return 2 x 10
87    tensor, where each row is representation of the class.
88  """
89  with vs.variable_scope(name):
90    embeddings = vs.get_variable(name + '_embeddings',
91                                 [n_classes, embedding_size])
92    return embedding_lookup(embeddings, tensor_in)
93