Example #1
0
    def testReduceDataset(self, stop):
        def init_fn(_):
            return np.int64(0)

        def reduce_fn(state, value):
            return state + value

        def finalize_fn(state):
            return state

        sum_reducer = grouping.Reducer(init_fn, reduce_fn, finalize_fn)

        dataset = dataset_ops.Dataset.range(stop)
        element = get_single_element.reduce_dataset(dataset, sum_reducer)

        self.assertEqual(stop * (stop - 1) / 2, self.evaluate(element))
  def testReduceDataset(self, stop):
    def init_fn(_):
      return np.int64(0)

    def reduce_fn(state, value):
      return state + value

    def finalize_fn(state):
      return state

    sum_reducer = grouping.Reducer(init_fn, reduce_fn, finalize_fn)

    stop_t = array_ops.placeholder(dtypes.int64, shape=[])
    dataset = dataset_ops.Dataset.range(stop_t)
    element = get_single_element.reduce_dataset(dataset, sum_reducer)

    with self.cached_session() as sess:
      value = sess.run(element, feed_dict={stop_t: stop})
      self.assertEqual(stop * (stop - 1) / 2, value)
Example #3
0
    def testReduceDataset(self, stop):
        def init_fn(_):
            return np.int64(0)

        def reduce_fn(state, value):
            return state + value

        def finalize_fn(state):
            return state

        sum_reducer = grouping.Reducer(init_fn, reduce_fn, finalize_fn)

        stop_t = array_ops.placeholder(dtypes.int64, shape=[])
        dataset = dataset_ops.Dataset.range(stop_t)
        element = get_single_element.reduce_dataset(dataset, sum_reducer)

        with self.cached_session() as sess:
            value = sess.run(element, feed_dict={stop_t: stop})
            self.assertEqual(stop * (stop - 1) / 2, value)
  def testReduceDatasetAverage(self, stop):
    def init_fn(_):
      return 0.0, 0.0

    def reduce_fn(x, y):
      return (x[0] * x[1] + math_ops.cast(y, dtypes.float32)) / (
          x[1] + 1), x[1] + 1

    def finalize_fn(x, _):
      return x

    average_reducer = grouping.Reducer(init_fn, reduce_fn, finalize_fn)

    stop_t = array_ops.placeholder(dtypes.int64, shape=[])
    dataset = dataset_ops.Dataset.range(stop_t)
    element = get_single_element.reduce_dataset(dataset, average_reducer)

    with self.cached_session() as sess:
      value = sess.run(element, feed_dict={stop_t: stop})
      if stop == 0:
        self.assertEqual(0.0, value)
      else:
        self.assertEqual(stop * (stop - 1) / (2 * stop), value)