<commit_before># Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for SavedModelEstimator."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import shutil
import tempfile

from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import metrics as metrics_lib
from tensorflow.python.ops import state_ops
from tensorflow.python.ops import variables
from tensorflow.python.platform import test
from tensorflow.python.training import monitored_session
from tensorflow.python.training import training
from tensorflow_estimator.python.estimator import estimator
from tensorflow_estimator.python.estimator import model_fn as model_fn_lib
from tensorflow_estimator.python.estimator.canned import saved_model_estimator
from tensorflow_estimator.python.estimator.export import export
from tensorflow_estimator.python.estimator.export import export_output


def dummy_input_fn():
  return dataset_ops.Dataset.from_tensors(({
      'x': constant_op.constant([[1], [-2]], name='feature_x')
  }, constant_op.constant([[4], [-3]], name='truth'))).repeat()


def _serving_feature_dict():
  return {'x': constant_op.constant([[5], [6]], name='feature_x')}


def dummy_input_fn_features_only():
  return dataset_ops.Dataset.from_tensors(_serving_feature_dict()).repeat()


def dummy_supervised_receiver_fn():
  return export.build_supervised_input_receiver_fn_from_input_fn(dummy_input_fn)


def dummy_serving_receiver_fn():
  return export.build_raw_serving_input_receiver_fn(_serving_feature_dict())


def model_fn_diff_modes(features, labels, mode):
  _, _ = features, labels
  v = variables.Variable(21, name='some_var')
  train_op = None
  loss = constant_op.constant(104)
  if mode == model_fn_lib.ModeKeys.TRAIN:
    loss = constant_op.constant(105)
    predictions = constant_op.constant([501])
    train_op = control_flow_ops.group(
        state_ops.assign_add(training.get_global_step(), 1),
        state_ops.assign_add(v, 3))
  elif mode == model_fn_lib.ModeKeys.EVAL:
    loss = constant_op.constant(106)
    predictions = constant_op.constant([502])
  else:
    loss = constant_op.constant(107)
    predictions = constant_op.constant([503])
  return model_fn_lib.EstimatorSpec(
      mode,
      loss=loss,
      train_op=train_op,
      eval_metric_ops={
          'abs_err': metrics_lib.mean_absolute_error(
              constant_op.constant(0), predictions)},
      predictions=predictions)


@test_util.run_v1_only('Tests v1 only symbols')
class SavedModelEstimatorTest(test.TestCase):

  def setUp(self):
    super(SavedModelEstimatorTest, self).setUp()
    self.tmpdirs = []

  def tearDown(self):
    for tmpdir in self.tmpdirs:
      # gfile.DeleteRecursively fails in the windows cmake test, so use shutil.
      shutil.rmtree(tmpdir, ignore_errors=True)
    self.tmpdirs = []
    super(SavedModelEstimatorTest, self).tearDown()

  def _get_tmp_dir(self):
    tmpdir = tempfile.mkdtemp()
    self.tmpdirs.append(tmpdir)
    return tmpdir<commit_msg>Set the reason for run_v1_only on SavedModelEstimatorTest to the relevant bug<commit_after># Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for SavedModelEstimator."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import shutil
import tempfile

from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import metrics as metrics_lib
from tensorflow.python.ops import state_ops
from tensorflow.python.ops import variables
from tensorflow.python.platform import test
from tensorflow.python.training import monitored_session
from tensorflow.python.training import training
from tensorflow_estimator.python.estimator import estimator
from tensorflow_estimator.python.estimator import model_fn as model_fn_lib
from tensorflow_estimator.python.estimator.canned import saved_model_estimator
from tensorflow_estimator.python.estimator.export import export
from tensorflow_estimator.python.estimator.export import export_output


def dummy_input_fn():
  return dataset_ops.Dataset.from_tensors(({
      'x': constant_op.constant([[1], [-2]], name='feature_x')
  }, constant_op.constant([[4], [-3]], name='truth'))).repeat()


def _serving_feature_dict():
  return {'x': constant_op.constant([[5], [6]], name='feature_x')}


def dummy_input_fn_features_only():
  return dataset_ops.Dataset.from_tensors(_serving_feature_dict()).repeat()


def dummy_supervised_receiver_fn():
  return export.build_supervised_input_receiver_fn_from_input_fn(dummy_input_fn)


def dummy_serving_receiver_fn():
  return export.build_raw_serving_input_receiver_fn(_serving_feature_dict())


def model_fn_diff_modes(features, labels, mode):
  _, _ = features, labels
  v = variables.Variable(21, name='some_var')
  train_op = None
  loss = constant_op.constant(104)
  if mode == model_fn_lib.ModeKeys.TRAIN:
    loss = constant_op.constant(105)
    predictions = constant_op.constant([501])
    train_op = control_flow_ops.group(
        state_ops.assign_add(training.get_global_step(), 1),
        state_ops.assign_add(v, 3))
  elif mode == model_fn_lib.ModeKeys.EVAL:
    loss = constant_op.constant(106)
    predictions = constant_op.constant([502])
  else:
    loss = constant_op.constant(107)
    predictions = constant_op.constant([503])
  return model_fn_lib.EstimatorSpec(
      mode,
      loss=loss,
      train_op=train_op,
      eval_metric_ops={
          'abs_err': metrics_lib.mean_absolute_error(
              constant_op.constant(0), predictions)},
      predictions=predictions)


@test_util.run_v1_only('b/122480158')
class SavedModelEstimatorTest(test.TestCase):

  def setUp(self):
    super(SavedModelEstimatorTest, self).setUp()
    self.tmpdirs = []

  def tearDown(self):
    for tmpdir in self.tmpdirs:
      # gfile.DeleteRecursively fails in the windows cmake test, so use shutil.
      shutil.rmtree(tmpdir, ignore_errors=True)
    self.tmpdirs = []
    super(SavedModelEstimatorTest, self).tearDown()

  def _get_tmp_dir(self):
    tmpdir = tempfile.mkdtemp()
    self.tmpdirs.append(tmpdir)
    return tmpdir
{'fraction_non_alphanumeric': 0.10579565326005495, 'fraction_numerical': 0.011616287784161879, 'mean_word_length': 5.2021688613477926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '420901', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2355, 'n_words': 606}