【问题标题】:Create random trainable indices to build two 2_D tensor from one 2_D tensor in tensorflow创建随机可训练索引以从张量流中的一个 2_D 张量构建两个 2_D 张量
【发布时间】:2019-08-20 13:53:37
【问题描述】:

我正在尝试从一个形状为 AAA 且 shape=(30) 的张量创建两个形状为 shape=(30, 4200) 的张量, 4201) 和 type= float32。

运行代码进行多次不同更改后,此错误将再次出现。

如何选择正确的张量形状?

【问题讨论】:

    标签: python tensorflow tensor


    【解决方案1】:

    这就是您将如何使用 tf.gather_nd 来处理您的案例:

    # If V1_emb and V2_emb have different shapes you need to take the shape of each one
    s = tf.shape(V1, out_type=V1_emb.dtype)
    batch_idx = tf.tile(tf.expand_dims(tf.range(s[0]), 1), [1, s[1]])
    V1 = tf.gather_nd(params=AAA, indices=tf.stack([batch_idx, V1_emb], axis=-1))
    V2 = tf.gather_nd(params=AAA, indices=tf.stack([batch_idx, V2_emb], axis=-1))
    

    【讨论】:

    • 我可以向您保证,V1_emb不会被训练,因为无法训练整数变量。对于可训练的变量,它必须是浮点类型,并且从该变量到损失值的所有操作都必须是浮点且可微的(这里是整数,变量用于tf.gather_nd,它不是可微的索引的操作)。
    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多