def calculate_allocation_weighting(self, usage_vector):
"""
:param: usage vector: tensor of shape [batch_size, memory_size]
:return: allocation tensor of shape [batch_size, memory_size]
"""
usage_vector = Memory.epsilon + (1 - Memory.epsilon) * usage_vector
# We're sorting the "-self.usage_vector" because top_k returns highest values and we need the lowest
highest_usage, inverse_indices = tf.nn.top_k(-usage_vector, k=self.memory_size)
lowest_usage = -highest_usage
allocation_scrambled = (1 - lowest_usage) * tf.cumprod(lowest_usage, axis=1, exclusive=True)
# allocation is not in the correct order. alloation[i] contains the sorted[i] value
# reversing the already inversed indices for each batch
indices = tf.stack([tf.invert_permutation(batch_indices) for batch_indices in tf.unstack(inverse_indices)])
allocation = tf.stack([tf.gather(mem, ind)
for mem, ind in
zip(tf.unstack(allocation_scrambled), tf.unstack(indices))])
return allocation
评论列表
文章目录