Example #1
0
  def test_classwise_covariance_2class_batches_float32(self):
    from tensorflow_gan.python.eval import eval_utils
    """Checks value consistency of streaming_covariance."""
    if tf.executing_eagerly():
      # tf.placeholder() is not compatible with eager execution.
      return
    np.random.seed(0)

    data = [[1., 2.], [2., 1.], [2., 4.], [4., 2.]]
    labels = [0,0, 1, 1]

    placeholder = tf.compat.v1.placeholder(dtype=tf.float64, shape=(2,2))
    placeholder_labs = tf.compat.v1.placeholder(dtype=tf.int32, shape=(2))
    value, update_op = eval_utils.streaming_classwise_autocovariance(x=placeholder, labels=placeholder_labs, nclass=2)

    expected_result0 = np.cov(data[:2], rowvar=False)
    expected_result1 = np.cov(data[2:], rowvar=False)
    expected_result = np.stack([expected_result0, expected_result1])
    with self.cached_session() as sess:
      sess.run(tf.compat.v1.initializers.local_variables())
      # batch 1/2
      update_op_result = sess.run(update_op, feed_dict={placeholder: data[:2], placeholder_labs: labels[:2]})
      result = sess.run(value)
      self.assertAllClose(update_op_result, result)
      # batch 2/2
      update_op_result = sess.run(update_op, feed_dict={placeholder: data[2:], placeholder_labs: labels[2:]})
      result = sess.run(value)
      self.assertAllClose(update_op_result, result)
      # overall
      self.assertAllClose(expected_result, result)
Example #2
0
  def test_classwise_covariance_2class_random_singlet_batches_float32(self):
    from tensorflow_gan.python.eval import eval_utils
    """Checks value consistency of streaming_covariance."""
    if tf.executing_eagerly():
      # tf.placeholder() is not compatible with eager execution.
      return
    np.random.seed(0)

    n_egs_per_class = 8
    data = np.random.randn(n_egs_per_class*2,2)
    labels = ([0]*n_egs_per_class) + ([1]*n_egs_per_class)

    placeholder = tf.compat.v1.placeholder(dtype=tf.float64, shape=(1,2))
    placeholder_labs = tf.compat.v1.placeholder(dtype=tf.int32, shape=(1,))
    value, update_op = eval_utils.streaming_classwise_autocovariance(x=placeholder, labels=placeholder_labs, nclass=2)

    expected_result0 = np.cov(data[:n_egs_per_class], rowvar=False)
    expected_result1 = np.cov(data[n_egs_per_class:], rowvar=False)
    expected_result = np.stack([expected_result0, expected_result1])
    with self.cached_session() as sess:
      sess.run(tf.compat.v1.initializers.local_variables())
      for i in range(n_egs_per_class*2):
        update_op_result = sess.run(update_op, feed_dict={placeholder: [data[i]], placeholder_labs: [labels[i]]})
        result = sess.run(value)
        self.assertAllClose(update_op_result, result)
      # overall
      self.assertAllClose(expected_result, result)
Example #3
0
 def test_classwise_covariance_1class_simple_float32(self):
   from tensorflow_gan.python.eval import eval_utils
   """Checks handling of float32 values in test_classwise_covariance_float32."""
   
   if tf.executing_eagerly():
     # streaming_covariance is not supported when eager execution is enabled.
     return
   x = [[1., 2.], [2., 1.]]
   result, update_op = eval_utils.streaming_classwise_autocovariance(
       x=tf.constant(x, dtype=tf.float32), labels=tf.constant([0,0]), nclass=1)
   
   expected_result = np.expand_dims(np.cov(x, rowvar=False), axis=0)
   with self.cached_session() as sess:
     sess.run(tf.compat.v1.initializers.local_variables())
     self.assertAllClose(expected_result, update_op)
     self.assertAllClose(expected_result, result)