tensorflow 用矩阵运算替换for循环 用tf.tile而不写for的方法

tensorflow 用矩阵运算替换for循环 用tf.tile而不写for的方法,第1张

tensorflow 用矩阵运算替换for循环 用tf.tile而不写for的方法

如下所示:

# u [32,30,200]
# u_logits [400,32,30]
q_j_400 = [] 
for j in range(400):
 q_j_400.append(tf.squeeze(tf.matmul(tf.transpose(u,[0,2,1]),tf.expand_dims(tf.nn.softmax(u_logits[j]),-1)),[2])) # tf.matmul [32,200,30],[32,30,1]
test_result = tf.stack(q_j_400)
test_result = tf.transpose(test_result,[1,0,2])

可以通过tf.tile实现更高速的版本

# u [32,30,200]
# u_logits [32,400,30]
u_tile = tf.tile(tf.expand_dims(u,1),[1,400,1,1])
u_logits = tf.expand_dims(tf.nn.softmax(u_logits,-1),-1)
test_result = tf.reduce_sum(u_logits * u_tile,-2) # [32,400,30,1]*[32,400,30,200]

以上这篇tensorflow 用矩阵运算替换for循环 用tf.tile而不写for的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/3289360.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-05
下一篇 2022-10-05

发表评论

登录后才能评论

评论列表(0条)

保存