Skip to content

Commit 3a5e0fc

Browse files
qlzh727csferng
authored andcommitted
Explicitly import estimator from tensorflow as a separate import instead of accessing it via tf.estimator and depend on the tensorflow estimator target.
PiperOrigin-RevId: 436596645
1 parent cdfd8f3 commit 3a5e0fc

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

neural_structured_learning/estimator/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ py_library(
4040
"//neural_structured_learning/configs",
4141
"//neural_structured_learning/lib",
4242
# package tensorflow
43+
# package tensorflow:tensorflow_estimator
4344
],
4445
)
4546

@@ -54,6 +55,7 @@ py_test(
5455
"//neural_structured_learning/configs",
5556
# package numpy
5657
# package tensorflow
58+
# package tensorflow:tensorflow_estimator
5759
# package tensorflow framework_test_lib,
5860
],
5961
)
@@ -67,6 +69,7 @@ py_library(
6769
"//neural_structured_learning/lib:distances",
6870
"//neural_structured_learning/lib:utils",
6971
# package tensorflow
72+
# package tensorflow:tensorflow_estimator
7073
],
7174
)
7275

@@ -81,6 +84,7 @@ py_test(
8184
"//neural_structured_learning/configs",
8285
# package numpy
8386
# package tensorflow
87+
# package tensorflow:tensorflow_estimator
8488
# package tensorflow framework_test_lib,
8589
],
8690
)

neural_structured_learning/estimator/adversarial_regularization.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import neural_structured_learning.configs as nsl_configs
2525
import neural_structured_learning.lib as nsl_lib
2626
import tensorflow as tf
27+
from tensorflow import estimator as tf_estimator
2728

2829

2930
def add_adversarial_regularization(estimator,
@@ -106,7 +107,7 @@ def adv_model_fn(features, labels, mode, params=None, config=None):
106107
original_spec = base_fn(features, labels)
107108

108109
# Adversarial regularization only happens in training.
109-
if mode != tf.estimator.ModeKeys.TRAIN:
110+
if mode != tf_estimator.ModeKeys.TRAIN:
110111
return original_spec
111112

112113
adv_neighbor, _ = nsl_lib.gen_adv_neighbor(

neural_structured_learning/estimator/adversarial_regularization_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import neural_structured_learning.estimator as nsl_estimator
2727
import numpy as np
2828
import tensorflow as tf
29+
from tensorflow import estimator as tf_estimator
2930

3031
from tensorflow.python.framework import test_util # pylint: disable=g-direct-tensorflow-import
3132

@@ -66,7 +67,7 @@ def build_linear_regressor(self, weight, bias):
6667

6768
fc = tf.feature_column.numeric_column(FEATURE_NAME,
6869
shape=np.array(weight).shape)
69-
return tf.estimator.LinearRegressor(
70+
return tf_estimator.LinearRegressor(
7071
feature_columns=(fc,), model_dir=self.model_dir, optimizer='SGD')
7172

7273
@test_util.run_v1_only('Requires tf.GraphKeys')
@@ -132,7 +133,7 @@ def test_adversarial_wrapper_saving_batch_statistics(self):
132133
y0 = np.array([1, 0, 1, 0])
133134
input_fn = single_batch_input_fn({FEATURE_NAME: x0}, y0)
134135
fc = tf.feature_column.numeric_column(FEATURE_NAME, shape=[2])
135-
base_est = tf.estimator.DNNClassifier(
136+
base_est = tf_estimator.DNNClassifier(
136137
hidden_units=[4],
137138
feature_columns=[fc],
138139
model_dir=self.model_dir,

neural_structured_learning/estimator/graph_regularization.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from neural_structured_learning.lib import utils
2525

2626
import tensorflow as tf
27+
from tensorflow import estimator as tf_estimator
2728

2829

2930
def add_graph_regularization(estimator,
@@ -108,7 +109,7 @@ def graph_reg_model_fn(features, labels, mode, params=None, config=None):
108109
auxiliary_name_scope=False):
109110
nbr_features = dict()
110111
nbr_weights = None
111-
if mode == tf.estimator.ModeKeys.TRAIN:
112+
if mode == tf_estimator.ModeKeys.TRAIN:
112113
# Extract sample features, neighbor features, and neighbor weights if we
113114
# are in training mode.
114115
sample_features, nbr_features, nbr_weights = (
@@ -130,7 +131,7 @@ def graph_reg_model_fn(features, labels, mode, params=None, config=None):
130131
# - neighbor inputs exist
131132
# - the graph regularization multiplier is greater than zero.
132133
# So, return early if any of these conditions is false.
133-
if (not has_nbr_inputs or mode != tf.estimator.ModeKeys.TRAIN or
134+
if (not has_nbr_inputs or mode != tf_estimator.ModeKeys.TRAIN or
134135
graph_reg_config.multiplier <= 0):
135136
return base_spec
136137

neural_structured_learning/estimator/graph_regularization_test.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import numpy as np
2828
import tensorflow as tf
29+
from tensorflow import estimator as tf_estimator
2930

3031
from google.protobuf import text_format
3132
from tensorflow.python.framework import test_util # pylint: disable=g-direct-tensorflow-import
@@ -136,7 +137,7 @@ def build_linear_regressor(self, weight, weight_shape, bias, bias_shape):
136137

137138
fc = tf.feature_column.numeric_column(
138139
FEATURE_NAME, shape=np.array(weight).shape)
139-
return tf.estimator.LinearRegressor(
140+
return tf_estimator.LinearRegressor(
140141
feature_columns=(fc,), model_dir=self.model_dir, optimizer='SGD')
141142

142143
@test_util.run_v1_only('Requires tf.get_variable')
@@ -452,7 +453,7 @@ def embedding_fn(features, mode, params=None):
452453
# The always-positive activation funciton is to make sure the batch mean
453454
# is non-zero.
454455
batch_norm_layer = tf.compat.v1.layers.batch_normalization(
455-
hidden_layer, training=(mode == tf.estimator.ModeKeys.TRAIN))
456+
hidden_layer, training=(mode == tf_estimator.ModeKeys.TRAIN))
456457
return batch_norm_layer
457458

458459
def model_fn(features, labels, mode, params=None, config=None):
@@ -461,25 +462,25 @@ def model_fn(features, labels, mode, params=None, config=None):
461462
with tf.compat.v1.variable_scope('logit', reuse=tf.AUTO_REUSE):
462463
logits = tf.compat.v1.layers.dense(embeddings, units=1)
463464
predictions = tf.argmax(logits, 1)
464-
if mode == tf.estimator.ModeKeys.PREDICT:
465-
return tf.estimator.EstimatorSpec(
465+
if mode == tf_estimator.ModeKeys.PREDICT:
466+
return tf_estimator.EstimatorSpec(
466467
mode=mode,
467468
predictions={
468469
'logits': logits,
469470
'predictions': predictions
470471
})
471472

472473
loss = tf.losses.sigmoid_cross_entropy(labels, logits)
473-
if mode == tf.estimator.ModeKeys.EVAL:
474-
return tf.estimator.EstimatorSpec(mode=mode, loss=loss)
474+
if mode == tf_estimator.ModeKeys.EVAL:
475+
return tf_estimator.EstimatorSpec(mode=mode, loss=loss)
475476

476477
optimizer = optimizer_fn()
477478
train_op = optimizer.minimize(
478479
loss, global_step=tf.compat.v1.train.get_global_step())
479480
update_ops = tf.compat.v1.get_collection(
480481
tf.compat.v1.GraphKeys.UPDATE_OPS)
481482
train_op = tf.group(train_op, *update_ops)
482-
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
483+
return tf_estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
483484

484485
def input_fn():
485486
nbr_feature = '{}{}_{}'.format(NBR_FEATURE_PREFIX, 0, FEATURE_NAME)
@@ -492,7 +493,7 @@ def input_fn():
492493
labels = tf.constant([[1], [0], [1]])
493494
return tf.data.Dataset.from_tensor_slices((features, labels)).batch(3)
494495

495-
base_est = tf.estimator.Estimator(
496+
base_est = tf_estimator.Estimator(
496497
model_fn, model_dir=self.model_dir, params=None)
497498
graph_reg_config = nsl_configs.make_graph_reg_config(
498499
max_neighbors=1, multiplier=1)

0 commit comments

Comments
 (0)