code: def explained_variance(
      I,
      private_images,
      lambdas,
      encoded_images,
      public_to_private,
      return_mat=False,
  ):
      # private images: 100x32x32x3
      # encoded images: 5000x32x32x3
  
      public_to_private = jax.nn.softmax(public_to_private, axis=-1)
  
      # Compute the components from each of the images we know should map onto the same original image.
      component_1 = jn.dot(
          public_to_private[0], private_images.reshape((100, -1))
      ).reshape((5000, 32, 32, 3))
      component_2 = jn.dot(
          public_to_private[1], private_images.reshape((100, -1))
      ).reshape((5000, 32, 32, 3))
  
      # Now combine them together to get the variance we can explain
      merged = (
          component_1 * lambdas[:, 0][:, None, None, None]
          + component_2 * lambdas[:, 1][:, None, None, None]
      )
  
      # And now get the variance we can't explain.
      # This is the contribution of the public images.
      # We want this value to be small.
  
      def keep_smallest_abs(xx1, xx2):
          t = 0
          which = (jn.abs(xx1 + t) < jn.abs(xx2 + t)) + 0.0
          return xx1 * which + xx2 * (1 - which)
  
      xx1 = jn.abs(encoded) - merged
      xx2 = -(jn.abs(encoded) + merged)
  
      xx = keep_smallest_abs(xx1, xx2)
      unexplained_variance = xx
  
      if return_mat:
          return unexplained_variance, xx1, xx2
  
      extra = (1 - jn.abs(private_images)).mean() * 0.05
  
      return extra + (unexplained_variance**2).mean()
  
file_path: privacy/research/instahide_attack_2020/step_5_reconstruct.py
imports: import json
  import numpy
  import jax.numpy
  import jax
  import collections
  from PIL import Image
  import jax.example_libraries.optimizers
  import matplotlib.pyplot
label_desc: Use image node_id 2 for calling a global function with example usage: explained_variance(I, private_images, lambdas, encoded_images, public_to_private, return_mat) and returns: unknown, unknown, unexplained_variance,  xx1,  xx2