TensorFlow:查找后重塑嵌入向量?

时间:2017-11-26 19:50:24

标签: python tensorflow word-embedding

我第一次使用TensorFlow,并且无法理解embedding_lookup功能。我有一系列代表单个特征向量的索引,比方说[0,3,2,5]。我想用相应的嵌入替换这些索引,所以我将嵌入和索引向量放在embedding_lookup中。然后它返回一个4 X n张量,其中n是每次嵌入的长度。我的想法,也许是我误解的事情,是我反而希望这是一个长度为4 * n的单行向量,我应该能够通过重塑来完成。

如果不是发送embedding_lookup单个实例的特征索引,我想发送数据集中每个实例的索引,假设每个列表的长度为4,它将返回X 4 X n张量(其中m是集合的大小),我想重塑为m X n * 4。假设我已经不在这里,我正在使用以下代码:

... # Stuff

word_embeddings = tf.Variable(self.word_embeddings, name="embeddings")

feature_ids = tf.placeholder(
    tf.int32, shape=(None, input_size), name="feature_ids")
X = tf.reshape(
    tf.nn.embedding_lookup(word_embeddings, feature_ids), [len(features),-1], name="X")
y = tf.placeholder(tf.int64, shape=(None), name='y')

with tf.name_scope("nn"):
    hidden1 = fully_connected(X, hidden_size1, scope="hidden1")
    hidden2 = fully_connected(hidden1, hidden_size2, scope="hidden2")
    output = fully_connected(hidden2, output_size, activation_fn=None, scope="output")

... # More stuff

但我得到的是以下内容:

Traceback (most recent call last):
  File "....py", line 84, in test
    hidden1 = fully_connected(X, hidden_size1, scope="hidden1")
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
    return func(*args, **current_args)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1469, in fully_connected
    outputs = layer.apply(inputs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/python/layers/base.py", line 492, in apply
    return self.__call__(inputs, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/python/layers/base.py", line 434, in __call__
    self.build(input_shapes[0])
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/tensorflow/python/layers/core.py", line 109, in build
    raise ValueError('The last dimension of the inputs to `Dense` '
ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.

关于我哪里出错的任何想法?

1 个答案:

答案 0 :(得分:0)

首先,您应该使用tf.expand_dims将形状从(4,n)扩展为(m,4,n)

并使用tf.transpose将形状从(m,4,n)更改为(m,n,4)。

在这种情况下,转置函数的perm参数将是[0,2,1]

(转置时不要使用tf.reshape !!!)