Beispiel #1
0
 def _get_entropy(samples):
     # TODO(b/123985779): Swith to tf.unique_with_counts_v2 when exposed
     count = gen_array_ops.unique_with_counts_v2(samples,
                                                 axis=[0]).count
     prob = count / self.num_samples
     entropy = tf.reduce_sum(-prob * tf.log(prob))
     return entropy
Beispiel #2
0
 def _get_entropy(samples):
     # TODO(b/123985779): Switch to tf.unique_with_counts_v2 when exposed
     count = gen_array_ops.unique_with_counts_v2(samples,
                                                 axis=[0]).count
     prob = tf.cast(count / num_samples, dtype=self.dtype)
     entropy = tf.reduce_sum(-prob * tf.math.log(prob))
     return entropy
Beispiel #3
0
 def testInt32Axis(self):
   for dtype in [np.int32, np.int64]:
     x = np.array([[1, 0, 0], [1, 0, 0], [2, 0, 0]])
     with self.cached_session() as sess:
       y0, idx0, count0 = gen_array_ops.unique_with_counts_v2(
           x, axis=np.array([0], dtype))
       tf_y0, tf_idx0, tf_count0 = self.evaluate([y0, idx0, count0])
       y1, idx1, count1 = gen_array_ops.unique_with_counts_v2(
           x, axis=np.array([1], dtype))
       tf_y1, tf_idx1, tf_count1 = self.evaluate([y1, idx1, count1])
     self.assertAllEqual(tf_y0, np.array([[1, 0, 0], [2, 0, 0]]))
     self.assertAllEqual(tf_idx0, np.array([0, 0, 1]))
     self.assertAllEqual(tf_count0, np.array([2, 1]))
     self.assertAllEqual(tf_y1, np.array([[1, 0], [1, 0], [2, 0]]))
     self.assertAllEqual(tf_idx1, np.array([0, 1, 1]))
     self.assertAllEqual(tf_count1, np.array([1, 2]))
 def testInt32Axis(self):
   for dtype in [np.int32, np.int64]:
     x = np.array([[1, 0, 0], [1, 0, 0], [2, 0, 0]])
     with self.cached_session() as sess:
       y0, idx0, count0 = gen_array_ops.unique_with_counts_v2(
           x, axis=np.array([0], dtype))
       tf_y0, tf_idx0, tf_count0 = self.evaluate([y0, idx0, count0])
       y1, idx1, count1 = gen_array_ops.unique_with_counts_v2(
           x, axis=np.array([1], dtype))
       tf_y1, tf_idx1, tf_count1 = self.evaluate([y1, idx1, count1])
     self.assertAllEqual(tf_y0, np.array([[1, 0, 0], [2, 0, 0]]))
     self.assertAllEqual(tf_idx0, np.array([0, 0, 1]))
     self.assertAllEqual(tf_count0, np.array([2, 1]))
     self.assertAllEqual(tf_y1, np.array([[1, 0], [1, 0], [2, 0]]))
     self.assertAllEqual(tf_idx1, np.array([0, 1, 1]))
     self.assertAllEqual(tf_count1, np.array([1, 2]))
Beispiel #5
0
def tf_get_sorted_unique_color(tf_img):
    """
    Find unique colors in `tf_img`, and sort by number of colors.

    Parameters
    ----------
    tf_img : `Tensor` of image
        Input `Tensor` image `tf_img`. Should be color image.

    Returns
    -------
    `Tensor` array of colors
        All colors in `Tensor` image, sorted by number of colors.

    Examples
    --------
    >>> test_file_name: str = "lenna.png"
    >>> test_image_fullpath: str = os.path.join(
    ...     self.test_path, self.test_resource_folder_name, test_file_name
    ... )
    >>> tf_image = tf_images.decode_png(test_image_fullpath, 3)
    >>> #.
    >>> tf_images.tf_get_sorted_unique_color(tf_image)[:5]
    tf.Tensor(
    [[ 88.  18.  60.]
     [176.  67.  79.]
     [205.  96.  97.]
     [178.  69.  80.]
     [206. 100.  94.]], shape=(5, 3), dtype=float32)
    """
    scs = tf.reshape(tf_img, (-1, 3))
    scs, _, count = gen_array_ops.unique_with_counts_v2(scs, axis=[-2])
    scs_arg = tf.argsort(count, direction="DESCENDING")
    return tf.gather(scs, scs_arg)
Beispiel #6
0
    def train(self, states, supervise_actions, advantage, mode, beta):
        """
        the batch_size here combines the state_batch_size and action samples.

        Args:
            states: (batch_size, state_dim)
            supervise_actions: (batch_size, action_dim)
            advantage: (batch_size, 1)
            mode (string): the type of distribution being used
            beta (float): update rate for the Actor
        """

        taus = tf.random.uniform(tf.shape(supervise_actions))
        # ODRPO mode requires previous policy probability
        unique_actions, idx, count = gen_array_ops.unique_with_counts_v2(
            supervise_actions, [0])
        num_action_samples = len(idx)
        prob = tf.Variable(tf.zeros(num_action_samples, 1))
        for i in range(num_action_samples):
            prob = prob[i].assign(
                tf.cast(count[idx[i]] / num_action_samples, tf.float32))

        weights = self.target_density(mode, advantage, beta, prob)

        with tf.GradientTape() as tape:
            actions = self(states, taus,
                           supervise_actions)  #(batch_size, action_dim)
            loss = self.huber_quantile_loss(actions, supervise_actions, taus,
                                            weights)
        gradients = tape.gradient(loss, self.trainable_variables)
        self.optimizer.apply_gradients(zip(gradients,
                                           self.trainable_variables))
Beispiel #7
0
 def testInt32Axis(self):
     for dtype in [np.int32, np.int64]:
         with self.subTest(dtype=dtype):
             x = np.array([[1, 0, 0], [1, 0, 0], [2, 0, 0]])
             y0, idx0, count0 = gen_array_ops.unique_with_counts_v2(
                 x, axis=np.array([0], dtype))
             self.assertEqual(y0.shape.rank, 2)
             tf_y0, tf_idx0, tf_count0 = self.evaluate([y0, idx0, count0])
             y1, idx1, count1 = gen_array_ops.unique_with_counts_v2(
                 x, axis=np.array([1], dtype))
             self.assertEqual(y1.shape.rank, 2)
             tf_y1, tf_idx1, tf_count1 = self.evaluate([y1, idx1, count1])
             self.assertAllEqual(tf_y0, np.array([[1, 0, 0], [2, 0, 0]]))
             self.assertAllEqual(tf_idx0, np.array([0, 0, 1]))
             self.assertAllEqual(tf_count0, np.array([2, 1]))
             self.assertAllEqual(tf_y1, np.array([[1, 0], [1, 0], [2, 0]]))
             self.assertAllEqual(tf_idx1, np.array([0, 1, 1]))
             self.assertAllEqual(tf_count1, np.array([1, 2]))
Beispiel #8
0
  def testBoolV2(self):
    x = np.random.choice([True, False], size=7000)
    y, idx, count = gen_array_ops.unique_with_counts_v2(
        x, axis=np.array([], np.int32))
    tf_y, tf_idx, tf_count = self.evaluate([y, idx, count])

    self.assertEqual(len(x), len(tf_idx))
    self.assertEqual(len(tf_y), len(np.unique(x)))
    for i in range(len(x)):
      self.assertEqual(x[i], tf_y[tf_idx[i]])
    for value, count in zip(tf_y, tf_count):
      self.assertEqual(count, np.sum(x == value))
Beispiel #9
0
  def testInt32V2(self):
    # This test is only temporary, once V2 is used
    # by default, the axis will be wrapped to allow `axis=None`.
    x = np.random.randint(2, high=10, size=7000)
    y, idx, count = gen_array_ops.unique_with_counts_v2(
        x, axis=np.array([], np.int32))
    tf_y, tf_idx, tf_count = self.evaluate([y, idx, count])

    self.assertEqual(len(x), len(tf_idx))
    self.assertEqual(len(tf_y), len(np.unique(x)))
    for i in range(len(x)):
      self.assertEqual(x[i], tf_y[tf_idx[i]])
    for value, count in zip(tf_y, tf_count):
      self.assertEqual(count, np.sum(x == value))
Beispiel #10
0
  def testInt32V2(self):
    # This test is only temporary, once V2 is used
    # by default, the axis will be wrapped to allow `axis=None`.
    x = np.random.randint(2, high=10, size=7000)
    with self.cached_session() as sess:
      y, idx, count = gen_array_ops.unique_with_counts_v2(
          x, axis=np.array([], np.int32))
      tf_y, tf_idx, tf_count = self.evaluate([y, idx, count])

    self.assertEqual(len(x), len(tf_idx))
    self.assertEqual(len(tf_y), len(np.unique(x)))
    for i in range(len(x)):
      self.assertEqual(x[i], tf_y[tf_idx[i]])
    for value, count in zip(tf_y, tf_count):
      self.assertEqual(count, np.sum(x == value))
Beispiel #11
0
  def testFloat(self):
    # NOTE(mrry): The behavior when a key is NaN is inherited from
    # `std::unordered_map<float, ...>`: each NaN becomes a unique key in the
    # map.
    x = [0.0, 1.0, np.nan, np.nan]
    y, idx, count = gen_array_ops.unique_with_counts_v2(
        x, axis=np.array([], np.int32))
    tf_y, tf_idx, tf_count = self.evaluate([y, idx, count])

    self.assertEqual(len(x), len(tf_idx))
    self.assertEqual(len(tf_y), len(np.unique(x)))
    for i in range(len(x)):
      if np.isnan(x[i]):
        self.assertTrue(np.isnan(tf_y[tf_idx[i]]))
      else:
        self.assertEqual(x[i], tf_y[tf_idx[i]])
    for value, count in zip(tf_y, tf_count):
      if np.isnan(value):
        self.assertEqual(count, 1)
      else:
        self.assertEqual(count, np.sum(x == value))
Beispiel #12
0
 def _get_mode(samples):
     # TODO(b/123985779): Swith to tf.unique_with_counts_v2 when exposed
     count = gen_array_ops.unique_with_counts_v2(samples,
                                                 axis=[0]).count
     return tf.argmax(count)