Exemplo n.º 1
0
def enable_v2_behavior():
  """Enables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 2.x.

  This function is called in the main TensorFlow `__init__.py` file, user should
  not need to call it, except during complex migrations.
  """
  # TF2 behavior is enabled if either 1) enable_v2_behavior() is called or
  # 2) the TF2_BEHAVIOR=1 environment variable is set.  In the latter case,
  # the modules below independently check if tf2.enabled().
  tf2.enable()
  ops.enable_eager_execution()
  tensor_shape.enable_v2_tensorshape()  # Also switched by tf2
  variable_scope.enable_resource_variables()
  ops.enable_tensor_equality()
  # Enables TensorArrayV2 and control flow V2.
  control_flow_v2_toggles.enable_control_flow_v2()
  # Make sure internal uses of tf.data symbols map to V2 versions.
  dataset_ops.Dataset = dataset_ops.DatasetV2
  readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV2
  readers.TFRecordDataset = readers.TFRecordDatasetV2
  readers.TextLineDataset = readers.TextLineDatasetV2
  counter.Counter = counter.CounterV2
  interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v2
  interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v2
  random_ops.RandomDataset = random_ops.RandomDatasetV2
  exp_readers.CsvDataset = exp_readers.CsvDatasetV2
  exp_readers.SqlDataset = exp_readers.SqlDatasetV2
  exp_readers.make_batched_features_dataset = (
      exp_readers.make_batched_features_dataset_v2)
  exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v2
Exemplo n.º 2
0
    def testV2(self):
        with compat.forward_compatibility_horizon(2019, 8, 9):
            control_flow_util.enable_control_flow_v2()
            variable_scope.enable_resource_variables()

            train_input_fn = _make_train_input_fn_dataset(
                is_classification=True)
            predict_input_fn = numpy_io.numpy_input_fn(x=FEATURES_DICT,
                                                       y=None,
                                                       batch_size=1,
                                                       num_epochs=1,
                                                       shuffle=False)

            est = boosted_trees.boosted_trees_classifier_train_in_memory(
                train_input_fn=train_input_fn,
                feature_columns=self._numeric_feature_columns,
                n_trees=1,
                max_depth=5,
                quantile_sketch_epsilon=0.33)

            # It will stop after 5 steps because of the max depth and num trees.
            self._assert_checkpoint(est.model_dir,
                                    global_step=5,
                                    finalized_trees=1,
                                    attempted_layers=5,
                                    bucket_boundaries=[[-2.001, -1.999, 12.5],
                                                       [-3., 0.4995, 2.],
                                                       [-100., 20., 102.75]])
            eval_res = est.evaluate(input_fn=train_input_fn, steps=1)
            self.assertAllClose(eval_res['accuracy'], 1.0)
            predictions = list(est.predict(input_fn=predict_input_fn))
            self.assertAllClose([[0], [1], [1], [0], [0]],
                                [pred['class_ids'] for pred in predictions])
Exemplo n.º 3
0
def enable_v2_behavior():
    """Enables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 2.x.

  This function is called in the main TensorFlow `__init__.py` file, user should
  not need to call it, except during complex migrations.
  """
    tf2.enable()  # Switches TensorArrayV2 and control flow V2
    ops.enable_eager_execution()
    tensor_shape.enable_v2_tensorshape()  # Also switched by tf2
    variable_scope.enable_resource_variables()
Exemplo n.º 4
0
def enable_v2_behavior():
  """Enables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 2.x.

  This function is called in the main TensorFlow `__init__.py` file, user should
  not need to call it, except during complex migrations.
  """
  tf2.enable()  # Switches TensorArrayV2 and control flow V2
  ops.enable_eager_execution()
  tensor_shape.enable_v2_tensorshape()  # Also switched by tf2
  variable_scope.enable_resource_variables()
def main(argv):
    if len(argv) > 1:
        raise app.UsageError('Too many command-line arguments.')

    shutil.rmtree(FLAGS.saved_model_path)

    variable_scope.enable_resource_variables()

    # Create the graph
    table_initializer = lookup_ops.TextFileInitializer(
        write_vocabulary_file(['cat', 'is', 'on', 'the', 'mat']),
        dtypes.string, lookup_ops.TextFileIndex.WHOLE_LINE, dtypes.int64,
        lookup_ops.TextFileIndex.LINE_NUMBER)
    table = lookup_ops.StaticVocabularyTable(table_initializer,
                                             num_oov_buckets=10)

    key = array_ops.placeholder(dtypes.string, shape=(), name='input')
    result = table.lookup(key)

    sess = session.Session()

    sess.run(variables.global_variables_initializer())

    sm_builder = builder.SavedModelBuilder(FLAGS.saved_model_path)
    tensor_info_x = utils.build_tensor_info(key)
    tensor_info_r = utils.build_tensor_info(result)

    toy_signature = (signature_def_utils.build_signature_def(
        inputs={'x': tensor_info_x},
        outputs={'r': tensor_info_r},
        method_name=signature_constants.PREDICT_METHOD_NAME))

    sm_builder.add_meta_graph_and_variables(
        sess, [tag_constants.SERVING],
        signature_def_map={
            signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            toy_signature,
        },
        main_op=lookup_ops.tables_initializer(),
        assets_collection=ops.get_collection(ops.GraphKeys.ASSET_FILEPATHS),
        strip_default_attrs=True)
    sm_builder.save()
Exemplo n.º 6
0
def enable_v2_behavior():
    """Enables TensorFlow 2.x behaviors.

  This function can be called at the beginning of the program (before `Tensors`,
  `Graphs` or other structures have been created, and before devices have been
  initialized. It switches all global behaviors that are different between
  TensorFlow 1.x and 2.x to behave as intended for 2.x.

  This function is called in the main TensorFlow `__init__.py` file, user should
  not need to call it, except during complex migrations.
  """
    # TF2 behavior is enabled if either 1) enable_v2_behavior() is called or
    # 2) the TF2_BEHAVIOR=1 environment variable is set.  In the latter case,
    # the modules below independently check if tf2.enabled().
    tf2.enable()
    ops.enable_eager_execution()
    tensor_shape.enable_v2_tensorshape()  # Also switched by tf2
    variable_scope.enable_resource_variables()
    # Enables TensorArrayV2 and control flow V2.
    control_flow_v2_toggles.enable_control_flow_v2()
Exemplo n.º 7
0
  def testV2BehaviorLogging(self):
    with self.assertLogs(level='INFO') as logs:
      try:
        ops.enable_eager_execution()
      # Ignore this exception to test log output successfully
      except ValueError as e:
        if 'must be called at program startup' not in str(e):
          raise e

    self.assertIn('Enabling eager execution', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      ops.disable_eager_execution()
    self.assertIn('Disabling eager execution', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      tensor_shape.enable_v2_tensorshape()
    self.assertIn('Enabling v2 tensorshape', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      tensor_shape.disable_v2_tensorshape()
    self.assertIn('Disabling v2 tensorshape', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      variable_scope.enable_resource_variables()
    self.assertIn('Enabling resource variables', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      variable_scope.disable_resource_variables()
    self.assertIn('Disabling resource variables', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      ops.enable_tensor_equality()
    self.assertIn('Enabling tensor equality', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      ops.disable_tensor_equality()
    self.assertIn('Disabling tensor equality', ''.join(logs.output))

    with self.assertLogs(level='INFO') as logs:
      control_flow_v2_toggles.enable_control_flow_v2()
    self.assertIn('Enabling control flow v2', ''.join(logs.output))
    with self.assertLogs(level='INFO') as logs:
      control_flow_v2_toggles.disable_control_flow_v2()
    self.assertIn('Disabling control flow v2', ''.join(logs.output))
Exemplo n.º 8
0
 def wrapper(*args, **kwargs):
     variable_scope.disable_resource_variables()
     try:
         f(*args, **kwargs)
     finally:
         variable_scope.enable_resource_variables()