Example #1
0
 def testName(self):
   with ops.name_scope("scope"):
     queue = data_flow_ops.FIFOQueue(10, dtypes.float32, name="queue")
   qr = queue_runner_impl.QueueRunner(queue, [control_flow_ops.no_op()])
   self.assertEqual("scope/queue", qr.name)
   queue_runner_impl.add_queue_runner(qr)
   self.assertEqual(
       1, len(ops.get_collection(ops.GraphKeys.QUEUE_RUNNERS, "scope")))
def boston_input_fn_with_queue(num_epochs=None):
    features, labels = boston_input_fn(num_epochs=num_epochs)

    # Create a minimal queue runner.
    fake_queue = data_flow_ops.FIFOQueue(30, dtypes.int32)
    queue_runner = queue_runner_impl.QueueRunner(fake_queue,
                                                 [constant_op.constant(0)])
    queue_runner_impl.add_queue_runner(queue_runner)

    return features, labels
Example #3
0
def boston_input_fn_with_queue(num_epochs=None):
  features, labels = boston_input_fn(num_epochs=num_epochs)

  # Create a minimal queue runner.
  fake_queue = data_flow_ops.FIFOQueue(30, dtypes.int32)
  queue_runner = queue_runner_impl.QueueRunner(fake_queue,
                                               [constant_op.constant(0)])
  queue_runner_impl.add_queue_runner(queue_runner)

  return features, labels
Example #4
0
 def testStartQueueRunnersRaisesIfNotASession(self):
   zero64 = constant_op.constant(0, dtype=dtypes.int64)
   var = variables.VariableV1(zero64)
   count_up_to = var.count_up_to(3)
   queue = data_flow_ops.FIFOQueue(10, dtypes.float32)
   init_op = variables.global_variables_initializer()
   qr = queue_runner_impl.QueueRunner(queue, [count_up_to])
   queue_runner_impl.add_queue_runner(qr)
   with self.cached_session():
     init_op.run()
     with self.assertRaisesRegexp(TypeError, "tf.Session"):
       queue_runner_impl.start_queue_runners("NotASession")
Example #5
0
 def testStartQueueRunnersIgnoresMonitoredSession(self):
   zero64 = constant_op.constant(0, dtype=dtypes.int64)
   var = variables.VariableV1(zero64)
   count_up_to = var.count_up_to(3)
   queue = data_flow_ops.FIFOQueue(10, dtypes.float32)
   init_op = variables.global_variables_initializer()
   qr = queue_runner_impl.QueueRunner(queue, [count_up_to])
   queue_runner_impl.add_queue_runner(qr)
   with self.cached_session():
     init_op.run()
     threads = queue_runner_impl.start_queue_runners(
         monitored_session.MonitoredSession())
     self.assertFalse(threads)
  def _testScopedExportWithQueue(self, test_dir, exported_filename):
    graph = ops.Graph()
    with graph.as_default():
      with ops.name_scope("queue1"):
        input_queue = data_flow_ops.FIFOQueue(10, dtypes.float32)
        enqueue = input_queue.enqueue((9876), name="enqueue")
        close = input_queue.close(name="close")
        qr = queue_runner_impl.QueueRunner(input_queue, [enqueue], close)
        queue_runner_impl.add_queue_runner(qr)
        input_queue.dequeue(name="dequeue")

      orig_meta_graph, _ = meta_graph.export_scoped_meta_graph(
          filename=os.path.join(test_dir, exported_filename),
          graph=ops.get_default_graph(),
          export_scope="queue1")

    return orig_meta_graph
Example #7
0
 def testStartQueueRunners(self):
   # CountUpTo will raise OUT_OF_RANGE when it reaches the count.
   zero64 = constant_op.constant(0, dtype=dtypes.int64)
   var = variables.VariableV1(zero64)
   count_up_to = var.count_up_to(3)
   queue = data_flow_ops.FIFOQueue(10, dtypes.float32)
   init_op = variables.global_variables_initializer()
   qr = queue_runner_impl.QueueRunner(queue, [count_up_to])
   queue_runner_impl.add_queue_runner(qr)
   with self.cached_session() as sess:
     init_op.run()
     threads = queue_runner_impl.start_queue_runners(sess)
     for t in threads:
       t.join()
     self.assertEqual(0, len(qr.exceptions_raised))
     # The variable should be 3.
     self.assertEqual(3, var.eval())
  def _testScopedExportWithQueue(self, test_dir, exported_filename):
    graph = ops.Graph()
    with graph.as_default():
      with ops.name_scope("queue1"):
        input_queue = data_flow_ops.FIFOQueue(10, dtypes.float32)
        enqueue = input_queue.enqueue((9876), name="enqueue")
        close = input_queue.close(name="close")
        qr = queue_runner_impl.QueueRunner(input_queue, [enqueue], close)
        queue_runner_impl.add_queue_runner(qr)
        input_queue.dequeue(name="dequeue")

      orig_meta_graph, _ = meta_graph.export_scoped_meta_graph(
          filename=os.path.join(test_dir, exported_filename),
          graph=ops.get_default_graph(),
          export_scope="queue1")

    return orig_meta_graph
  def testMultiThreadedEstimateDataDistribution(self):
    num_classes = 10

    # Set up graph.
    random_seed.set_random_seed(1234)
    label = math_ops.cast(
        math_ops.round(random_ops.random_uniform([1]) * num_classes),
        dtypes_lib.int32)

    prob_estimate = sampling_ops._estimate_data_distribution(  # pylint: disable=protected-access
        label, num_classes)
    # Check that prob_estimate is well-behaved in a multithreaded context.
    _, _, [prob_estimate] = sampling_ops._verify_input(  # pylint: disable=protected-access
        [], label, [prob_estimate])

    # Use queues to run multiple threads over the graph, each of which
    # fetches `prob_estimate`.
    queue = data_flow_ops.FIFOQueue(
        capacity=25,
        dtypes=[prob_estimate.dtype],
        shapes=[prob_estimate.get_shape()])
    enqueue_op = queue.enqueue([prob_estimate])
    queue_runner_impl.add_queue_runner(
        queue_runner_impl.QueueRunner(queue, [enqueue_op] * 25))
    out_tensor = queue.dequeue()

    # Run the multi-threaded session.
    with self.cached_session() as sess:
      # Need to initialize variables that keep running total of classes seen.
      variables.global_variables_initializer().run()

      coord = coordinator.Coordinator()
      threads = queue_runner_impl.start_queue_runners(coord=coord)

      for _ in range(25):
        sess.run([out_tensor])

      coord.request_stop()
      coord.join(threads)
Example #10
0
    def testMultiThreadedEstimateDataDistribution(self):
        num_classes = 10

        # Set up graph.
        random_seed.set_random_seed(1234)
        label = math_ops.cast(
            math_ops.round(random_ops.random_uniform([1]) * num_classes),
            dtypes_lib.int32)

        prob_estimate = sampling_ops._estimate_data_distribution(  # pylint: disable=protected-access
            label, num_classes)
        # Check that prob_estimate is well-behaved in a multithreaded context.
        _, _, [prob_estimate] = sampling_ops._verify_input(  # pylint: disable=protected-access
            [], label, [prob_estimate])

        # Use queues to run multiple threads over the graph, each of which
        # fetches `prob_estimate`.
        queue = data_flow_ops.FIFOQueue(capacity=25,
                                        dtypes=[prob_estimate.dtype],
                                        shapes=[prob_estimate.get_shape()])
        enqueue_op = queue.enqueue([prob_estimate])
        queue_runner_impl.add_queue_runner(
            queue_runner_impl.QueueRunner(queue, [enqueue_op] * 25))
        out_tensor = queue.dequeue()

        # Run the multi-threaded session.
        with self.cached_session() as sess:
            # Need to initialize variables that keep running total of classes seen.
            variables.global_variables_initializer().run()

            coord = coordinator.Coordinator()
            threads = queue_runner_impl.start_queue_runners(coord=coord)

            for _ in range(25):
                sess.run([out_tensor])

            coord.request_stop()
            coord.join(threads)
    def model_fn(self, features, labels, mode, params):
        # forward
        wide_fea_cols = params['feature_columns']['wide_feas']
        fm_and_dnn_fea_cols = params['feature_columns']['fm_and_dnn_feas']

        wide_net = tf.feature_column.input_layer(features, wide_fea_cols)
        fm_and_dnn_net = tf.feature_column.input_layer(features,
                                                       fm_and_dnn_fea_cols)

        y_pred = tf.Variable(0.0, name='y_pred')
        y_pred += self.linear(wide_net)
        y_pred += self.dnn(fm_and_dnn_net)
        if self.model == 'deepfm':
            y_pred += self.fm(fm_and_dnn_net)
        y_pred = tf.sigmoid(y_pred)

        # predict
        if mode == tf.estimator.ModeKeys.PREDICT:
            predictions = {'probabilities': y_pred, 'logits': y_pred}
            return tf.estimator.EstimatorSpec(mode, predictions=predictions)

        loss = tf.losses.log_loss(labels=labels, predictions=y_pred)

        # eval
        auc_ = tf.metrics.auc(labels=labels, predictions=y_pred, name='auc_op')
        metrics = {'auc': auc_}
        tf.summary.scalar('auc', auc_[1])
        if mode == tf.estimator.ModeKeys.EVAL:
            return tf.estimator.EstimatorSpec(mode,
                                              loss=loss,
                                              eval_metric_ops=metrics)

        # train
        if params['optimizer'] == 'Adam':
            optimizer = tf.train.AdamOptimizer(learning_rate=params['lr'],
                                               beta1=0.9,
                                               beta2=0.999,
                                               epsilon=1e-8)
        elif params['optimizer'] == 'Adagrad':
            optimizer = tf.train.AdagradOptimizer(
                learning_rate=params['lr'], initial_accumulator_value=1e-8)
        elif params['optimizer'] == 'Momentum':
            optimizer = tf.train.MomentumOptimizer(learning_rate=params['lr'],
                                                   momentum=0.95)
        elif params['optimizer'] == 'ftrl':
            optimizer = tf.train.FtrlOptimizer(params['lr'])
        else:
            optimizer = tf.train.GradientDescentOptimizer(params['lr'])

        # SyncReplicasOptimizer for distribution
        optimizer = tf.train.SyncReplicasOptimizer(
            optimizer,
            replicas_to_aggregate=self.worker_num,
            total_num_replicas=self.worker_num,
            use_locking=False)
        print("hook_sync_replicas is set")
        self.hook_sync_replicas = optimizer.make_session_run_hook(
            is_chief=self.is_chief, num_tokens=0)
        train_op = optimizer.minimize(loss,
                                      global_step=tf.train.get_global_step())
        chief_queue_runner = optimizer.get_chief_queue_runner()
        queue_runner_impl.add_queue_runner(chief_queue_runner)
        self.sync_init_op = optimizer.get_init_tokens_op()
        return tf.estimator.EstimatorSpec(
            mode,
            loss=loss,
            train_op=train_op,
            training_hooks=[self.hook_sync_replicas])