Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

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

来源:中文源码网    浏览:294 次    日期:2024-05-09 12:42:46
【下载文档:  tensorflow 用矩阵运算替换for循环 用tf.tile而不写for的方法.txt 】


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的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持中文源码网。

相关内容