当前位置: 移动技术网 > IT编程>脚本编程>Python > tensorflow 用矩阵运算替换for循环 用tf.tile而不写for的方法

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

2018年08月08日  | 移动技术网IT编程  | 我要评论

重庆科技学院教务在线,我爱芳邻电影,东莞旗峰山

如下所示:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网