2016-11-09 56 views
1

我目前有一种编码我想使用嵌入的编码。然而,当我打电话使用onehot编码的Tensorflow嵌入查找

embed=tf.nn.embedding_lookup(embeddings, train_data) 
print(embed.get_shape()) 

embed data shape (11, 32, 729, 128)

这种形状应为(11,32,128),但它给了我错了尺寸,因为train_data是onehot编码。

train_data2=tf.matmul(train_data,tf.range(729)) 

给我的错误:

ValueError: Shape must be rank 2 but is rank 3 

帮我请了!谢谢。

回答

2

一个小的修复,以你的例子:

encoding_size = 4 
one_hot_batch = tf.constant([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0]]) 
one_hot_indexes = tf.matmul(one_hot_batch, np.array([range(encoding_size)], 
    dtype=np.int32).T) 

with tf.Session() as session: 
    print one_hot_indexes.eval() 

另一种方法:在这两种情况下

batch_size = 3 
one_hot_batch = tf.constant([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0]]) 
one_hot_indexes = tf.where(tf.not_equal(one_hot_batch, 0)) 
one_hot_indexes = one_hot_indexes[:, 1] 
one_hot_indexes = tf.reshape(one_hot_indexes, [batch_size, 1]) 
with tf.Session() as session: 
    print one_hot_indexes.eval() 

结果:

[[3] 
[1] 
[0]] 
+0

试图顶一个不知道你是否可以只使用NP像那样在里面。 – Rik