code: def compute_sampled_logits(
      positive_keys,
      inputs,
      num_samples: int,
      de_config: de_config_pb2.DynamicEmbeddingConfig,
      var_name: typing.Text,
      service_address: typing.Text = "",
      timeout_ms: int = -1,
  ):
      """Computes sampled logits from given positive labels.
  
      Args:
        positive_keys: A string `Tensor` of shape `[batch_size, None]` representing
          input positive keys.
        inputs: A float `Tensor` of shape `[batch_size, dim]` representing the
          forward activations of the input network.
        num_samples: An int denoting the returned positive and negative samples.
        de_config: A DynamicEmbeddingConfig for configuring the dynamic embedding.
        var_name: A unique name for the operation.
        service_address: The address of a dynamic embedding service. If empty, the
          value passed from --kbs_address flag will be used instead.
        timeout_ms: Timeout millseconds for the connection. If negative, never
          timout.
  
      Returns:
        logits: A float `Tensor` of shape `[batch_size, num_samples]` representing
            the logits for sampled labels.
        labels: A float `Tensor` of shape `[batch_size, num_samples]` with values
            in {0, 1} indicating if the sample is positive or negative.
        keys: A string `Tensor` of shape `[batch_size, num_samples]` representing
            the keys for each sample.
        mask: A float `Tensor` of shape `[batch_size]` representing the 0/1 mask
            of each batch. For example, if all keys in positive_keys[i] are empty,
            mask[i] = 0; otherwise mask[i] = 1.
        weights: A float `Tensor` representing the embeddings of the sampled keys.
  
      Raises:
        ValueError: If var_name is not specified.
        TypeError: If de_config is an instance of DynamicEmbeddingConfig.
      """
      if not var_name:
          raise ValueError(
              "Must specify a valid name, got %s" % var_name
          )
      if num_samples < 1:
          raise ValueError("Invalid num_samples: %d" % num_samples)
  
      context.add_to_collection(var_name, de_config)
      resource = gen_carls_ops.dynamic_embedding_manager_resource(
          de_config.SerializeToString(),
          var_name,
          service_address,
          timeout_ms,
      )
  
      # Create a dummy variable so that the gradients can be passed in.
      grad_placeholder = tf.Variable(0.0)
  
      (
          keys,
          labels,
          expected_counts,
          mask,
          weights,
      ) = gen_carls_ops.sampled_logits_lookup(
          positive_keys, inputs, num_samples, grad_placeholder, resource
      )
  
      # Compute sampled logits.
      # Shape of weights: [d1, d2, dn-1, num_samples, embed_dim]
      # Shape of inputs: [d1, d2, dn-1, embed_dim]
      # Shape of output logits: [d1, d2, dn-1, num_samples]
  
      # [d1, d2, dn-1, embed_dim] -> [d1, d2, dn-1, 1, embed_dim]
      tiled_inputs = tf.expand_dims(inputs, axis=-2)
      # [d1, d2, dn-1, embed_dim] -> [d1, d2, dn-1, num_samples, embed_dim]
      multiples = [1] * (inputs.ndim + 1)
      multiples[-2] = num_samples
      tiled_inputs = tf.tile(tiled_inputs, multiples)
      # [d1, d2, dn-1, num_samples, embed_dim] -> [d1, d2, dn-1, num_samples]
      logits = tf.reduce_sum(weights * tiled_inputs, -1)
      # Sampled logits.
      logits -= tf.math.log(expected_counts)
  
      return logits, labels, keys, mask, weights
  
file_path: neural-structured-learning/research/carls/candidate_sampling_ops.py
imports: import typing
  from research.carls import context
  from research.carls import dynamic_embedding_config_pb2
  from research.carls.kernels import gen_carls_ops
  import tensorflow
label_desc: Use image node_id 4 for calling a global function with example usage: compute_sampled_logits(positive_keys, inputs, num_samples, de_config, var_name, service_address, timeout_ms) and returns: logits,  labels,  keys,  mask,  weights