(labels, embeddings, weights, biases,
                                prototype_multiplier)
return [
      weights.assign(output_weights),
      biases.assign(output_biases),
  ]
Return a list of operations for reparameterized ProtoNet initialization.
Return a list of operations for reparameterized ProtoNet initialization.
Return a list of operations for reparameterized ProtoNet initialization .
def proto_maml_fc_layer_init_fn(labels, embeddings, weights, biases,
                                prototype_multiplier):
  """Return a list of operations for reparameterized ProtoNet initialization."""

  # This is robust to classes missing from the training set, but assumes that
  # the last class is present.
  num_ways = tf.cast(
      tf.math.reduce_max(input_tensor=tf.unique(labels)[0]) + 1, tf.int32)

  # When there are no examples for a given class, we default its prototype to
  # zeros, per the implementation of `tf.math.unsorted_segment_mean`.
  prototypes = tf.math.unsorted_segment_mean(embeddings, labels, num_ways)

  # Scale the prototypes, which acts as a regularizer on the weights and biases.
  prototypes *= prototype_multiplier

  # logit = -<squared Euclidian distance to prototype>
  #       = -(x - p)^T.(x - p)
  #       = 2 x^T.p - p^T.p - x^T.x
  #       = x^T.w + b
  #         where w = 2p, b = -p^T.p
  output_weights = tf.transpose(a=2 * prototypes)
  output_biases = -tf.reduce_sum(input_tensor=prototypes * prototypes, axis=1)

  # We zero-pad to align with the original weights and biases.
  output_weights = tf.pad(
      tensor=output_weights,
      paddings=[[
          0, 0
      ], [0, tf.shape(input=weights)[1] - tf.shape(input=output_weights)[1]]],
      mode='CONSTANT',
      constant_values=0)
  output_biases = tf.pad(
      tensor=output_biases,
      paddings=[[
          0, tf.shape(input=biases)[0] - tf.shape(input=output_biases)[0]
      ]],
      mode='CONSTANT',
      constant_values=0)

  return [
      weights.assign(output_weights),
      biases.assign(output_biases),
  ]
def proto_maml_fc_layer_init_fn ( labels , embeddings , weights , biases , prototype_multiplier ) : # This is robust to classes missing from the training set, but assumes that # the last class is present. num_ways = tf . cast ( tf . math . reduce_max ( input_tensor = tf . unique ( labels ) [ 0 ] ) + 1 , tf . int32 ) # When there are no examples for a given class, we default its prototype to # zeros, per the implementation of `tf.math.unsorted_segment_mean`. prototypes = tf . math . unsorted_segment_mean ( embeddings , labels , num_ways ) # Scale the prototypes, which acts as a regularizer on the weights and biases. prototypes *= prototype_multiplier # logit = -<squared Euclidian distance to prototype> #       = -(x - p)^T.(x - p) #       = 2 x^T.p - p^T.p - x^T.x #       = x^T.w + b #         where w = 2p, b = -p^T.p output_weights = tf . transpose ( a = 2 * prototypes ) output_biases = - tf . reduce_sum ( input_tensor = prototypes * prototypes , axis = 1 ) # We zero-pad to align with the original weights and biases. output_weights = tf . pad ( tensor = output_weights , paddings = [ [ 0 , 0 ] , [ 0 , tf . shape ( input = weights ) [ 1 ] - tf . shape ( input = output_weights ) [ 1 ] ] ] , mode = 'CONSTANT' , constant_values = 0 ) output_biases = tf . pad ( tensor = output_biases , paddings = [ [ 0 , tf . shape ( input = biases ) [ 0 ] - tf . shape ( input = output_biases ) [ 0 ] ] ] , mode = 'CONSTANT' , constant_values = 0 ) return [ weights . assign ( output_weights ) , biases . assign ( output_biases ) , ]