tensorflow replaces for with matrix operations. The method that loops tf.tile without writing for

  • 2020-11-25 07:23:09
  • OfStack

As shown below:


# 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])

A faster version can be achieved with ES4en.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]

Related articles: