def __call__(self, support_set, input_image, name, training=False):
"""
This module calculates the cosine distance between each of the support set embeddings and the target
image embeddings.
:param support_set: The embeddings of the support set images, tensor of shape [sequence_length, batch_size, 64]
:param input_image: The embedding of the target image, tensor of shape [batch_size, 64]
:param name: Name of the op to appear on the graph
:param training: Flag indicating training or evaluation (True/False)
:return: A tensor with cosine similarities of shape [batch_size, sequence_length, 1]
"""
with tf.name_scope('distance-module' + name), tf.variable_scope('distance-module', reuse=self.reuse):
eps = 1e-10
similarities = []
for support_image in tf.unstack(support_set, axis=0):
sum_support = tf.reduce_sum(tf.square(support_image), 1, keep_dims=True)
support_magnitude = tf.rsqrt(tf.clip_by_value(sum_support, eps, float("inf")))
dot_product = tf.matmul(tf.expand_dims(input_image, 1), tf.expand_dims(support_image, 2))
dot_product = tf.squeeze(dot_product, [1, ])
cosine_similarity = dot_product * support_magnitude
similarities.append(cosine_similarity)
similarities = tf.concat(axis=1, values=similarities)
self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='distance-module')
return similarities
one_shot_learning_network.py 文件源码
python
阅读 87
收藏 0
点赞 0
评论 0
评论列表
文章目录