【问题标题】:Tensorflow embedding lookup using onehot encoding使用 onehot 编码的 TensorFlow 嵌入查找
【发布时间】:2017-03-22 19:17:21
【问题描述】:

我目前有要使用嵌入的 onehot 编码。但是当我打电话时

embed=tf.nn.embedding_lookup(embeddings, train_data) 
print(embed.get_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

请帮帮我!谢谢。

【问题讨论】:

    标签: python tensorflow one-hot-encoding


    【解决方案1】:

    对您的示例进行小修复:

    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]]
    

    【讨论】:

    • 尝试顶级的不确定是否可以像那样在里面使用 np。
    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多