【发布时间】:2019-08-20 13:53:37
【问题描述】:
我正在尝试从一个形状为 AAA 且 shape=(30) 的张量创建两个形状为 shape=(30, 4200) 的张量, 4201) 和 type= float32。
运行代码进行多次不同更改后,此错误将再次出现。
如何选择正确的张量形状?
【问题讨论】:
标签: python tensorflow tensor
我正在尝试从一个形状为 AAA 且 shape=(30) 的张量创建两个形状为 shape=(30, 4200) 的张量, 4201) 和 type= float32。
运行代码进行多次不同更改后,此错误将再次出现。
如何选择正确的张量形状?
【问题讨论】:
标签: python tensorflow tensor
这就是您将如何使用 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,它不是可微的索引的操作)。