Skip to content

Commit 9b5600b

Browse files
Neural-Link Teamtensorflow-copybara
authored andcommitted
File renames:
input_maker.py + input_maker_lib.py -> pack_nbrs.py input_maker_test.py -> pack_nbrs_test.py graph_builder.py + graph_builder_lib.py -> build_graph.py graph_builder_test.py -> build_graph_test.py This change does NOT change the names of these functions: nsl.tools.build_graph nsl.tools.pack_nbrs PiperOrigin-RevId: 274638570
1 parent 4650b68 commit 9b5600b

File tree

8 files changed

+157
-189
lines changed

8 files changed

+157
-189
lines changed

neural_structured_learning/tools/BUILD

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ py_library(
3030
srcs = ["__init__.py"],
3131
srcs_version = "PY2AND3",
3232
deps = [
33-
":graph_builder_lib",
33+
":build_graph_lib",
3434
":graph_utils",
35-
":input_maker_lib",
35+
":pack_nbrs_lib",
3636
],
3737
)
3838

@@ -57,11 +57,13 @@ py_test(
5757
)
5858

5959
py_library(
60-
name = "graph_builder_lib",
61-
srcs = ["graph_builder_lib.py"],
60+
name = "build_graph_lib",
61+
srcs = ["build_graph.py"],
6262
srcs_version = "PY2AND3",
6363
deps = [
6464
":graph_utils",
65+
# package absl:app
66+
# package absl/flags
6567
# package absl/logging
6668
# package numpy
6769
# package six
@@ -70,11 +72,11 @@ py_library(
7072
)
7173

7274
py_test(
73-
name = "graph_builder_lib_test",
74-
srcs = ["graph_builder_lib_test.py"],
75+
name = "build_graph_test",
76+
srcs = ["build_graph_test.py"],
7577
srcs_version = "PY2AND3",
7678
deps = [
77-
":graph_builder_lib",
79+
":build_graph_lib",
7880
":graph_utils",
7981
# package protobuf,
8082
# package absl/testing:absltest
@@ -83,50 +85,47 @@ py_test(
8385
)
8486

8587
py_binary(
86-
name = "graph_builder",
87-
srcs = ["graph_builder.py"],
88+
name = "build_graph",
89+
srcs = ["build_graph.py"],
8890
python_version = "PY3",
8991
deps = [
90-
":graph_builder_lib",
91-
# package absl:app
92-
# package absl/flags
93-
# package tensorflow
92+
":build_graph_lib",
9493
],
9594
)
9695

9796
py_library(
98-
name = "input_maker_lib",
99-
srcs = ["input_maker_lib.py"],
97+
name = "pack_nbrs_lib",
98+
srcs = ["pack_nbrs.py"],
10099
srcs_version = "PY2AND3",
101100
deps = [
102101
":graph_utils",
102+
# package absl:app
103+
# package absl/flags
103104
# package absl/logging
104105
# package six
105106
# package tensorflow
106107
],
107108
)
108109

109110
py_test(
110-
name = "input_maker_lib_test",
111-
srcs = ["input_maker_lib_test.py"],
111+
name = "pack_nbrs_test",
112+
srcs = ["pack_nbrs_test.py"],
112113
srcs_version = "PY2AND3",
113114
deps = [
114115
":graph_utils",
115-
":input_maker_lib",
116+
":pack_nbrs_lib",
116117
# package protobuf,
117118
# package absl/testing:absltest
118119
# package tensorflow
119120
],
120121
)
121122

122123
py_binary(
123-
name = "input_maker",
124-
srcs = ["input_maker.py"],
124+
name = "pack_nbrs",
125+
srcs = ["pack_nbrs.py"],
125126
python_version = "PY3",
126127
deps = [
127-
":input_maker_lib",
128-
# package absl:app
129-
# package absl/flags
128+
":pack_nbrs_lib",
130129
# package tensorflow
131130
],
132131
)
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
"""Tools and APIs for preparing data for Neural Structured Learning."""
1+
"""Tools and APIs for preparing data for Neural Structured Learning.
22
3-
from neural_structured_learning.tools.graph_builder_lib import build_graph
3+
In addition to the functions exported here, two of the modules can be invoked
4+
from the command-line as follows:
5+
6+
```sh
7+
$ python -m neural_structured_learning.tools.build_graph.py ...
8+
$ python -m neural_structured_learning.tools.pack_nbrs.py ...
9+
```
10+
11+
For details on the command-line usage for these programs, see the
12+
`nsl.tools.build_graph` and `nsl.tools.pack_nbrs` documentation.
13+
"""
14+
15+
from neural_structured_learning.tools.build_graph import build_graph
416
from neural_structured_learning.tools.graph_utils import add_edge
517
from neural_structured_learning.tools.graph_utils import add_undirected_edges
618
from neural_structured_learning.tools.graph_utils import read_tsv_graph
719
from neural_structured_learning.tools.graph_utils import write_tsv_graph
8-
from neural_structured_learning.tools.input_maker_lib import pack_nbrs
20+
from neural_structured_learning.tools.pack_nbrs import pack_nbrs

neural_structured_learning/tools/graph_builder_lib.py renamed to neural_structured_learning/tools/build_graph.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
r"""Library to build a graph based on dense input features (embeddings).
16-
17-
A python-based program for graph building also exists on
18-
[GitHub](https://github.com/tensorflow/neural-structured-learning/tree/master/neural_structured_learning/tools/graph_builder.py).
19-
"""
15+
r"""Program & library to build a graph from dense features (embeddings)."""
2016

2117
from __future__ import absolute_import
2218
from __future__ import division
@@ -26,6 +22,8 @@
2622
import itertools
2723
import time
2824

25+
from absl import app
26+
from absl import flags
2927
from absl import logging
3028
from neural_structured_learning.tools import graph_utils
3129
import numpy as np
@@ -165,6 +163,18 @@ def build_graph(embedding_files,
165163
All edges in the output will be symmetric (i.e., if edge `A--w-->B` exists in
166164
the output, then so will edge `B--w-->A`).
167165
166+
Note that this function can also be invoked as a binary from a shell. Sample
167+
usage:
168+
169+
`python -m neural_structured_learning.tools.build_graph` [*flags*]
170+
*embedding_file.tfr... output_graph.tsv*
171+
172+
For details about this program's flags, run:
173+
174+
```
175+
python -m neural_structured_learning.tools.build_graph --help
176+
```
177+
168178
Args:
169179
embedding_files: A list of names of TFRecord files containing
170180
`tf.train.Example` objects, which in turn contain dense embeddings.
@@ -182,3 +192,35 @@ def build_graph(embedding_files,
182192
graph = collections.defaultdict(dict)
183193
_add_edges(embeddings, similarity_threshold, graph)
184194
graph_utils.write_tsv_graph(output_graph_path, graph)
195+
196+
197+
def _main(argv):
198+
"""Main function for invoking the `nsl.tools.build_graph` function."""
199+
flag = flags.FLAGS
200+
flag.showprefixforinfo = False
201+
if len(argv) < 3:
202+
raise app.UsageError(
203+
'Invalid number of arguments; expected 2 or more, got %d' %
204+
(len(argv) - 1))
205+
206+
build_graph(argv[1:-1], argv[-1], flag.similarity_threshold,
207+
flag.id_feature_name, flag.embedding_feature_name)
208+
209+
210+
if __name__ == '__main__':
211+
flags.DEFINE_string(
212+
'id_feature_name', 'id',
213+
"""Name of the singleton bytes_list feature in each input Example
214+
whose value is the Example's ID.""")
215+
flags.DEFINE_string(
216+
'embedding_feature_name', 'embedding',
217+
"""Name of the float_list feature in each input Example
218+
whose value is the Example's (dense) embedding.""")
219+
flags.DEFINE_float(
220+
'similarity_threshold', 0.8,
221+
"""Lower bound on the cosine similarity required for an edge
222+
to be created between two nodes.""")
223+
224+
# Ensure TF 2.0 behavior even if TF 1.X is installed.
225+
tf.compat.v1.enable_v2_behavior()
226+
app.run(_main)

neural_structured_learning/tools/graph_builder_lib_test.py renamed to neural_structured_learning/tools/build_graph_test.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
"""Tests for neural_structured_learning.tools.graph_builder_lib."""
14+
"""Tests for neural_structured_learning.tools.build_graph."""
1515

1616
from __future__ import absolute_import
1717
from __future__ import division
1818
from __future__ import print_function
1919

2020
from absl.testing import absltest
21-
from neural_structured_learning.tools import graph_builder_lib
21+
from neural_structured_learning.tools import build_graph as build_graph_lib
2222
from neural_structured_learning.tools import graph_utils
2323
import tensorflow as tf
2424

2525
from google.protobuf import text_format
2626

2727

28-
class BuildGraphLibTest(absltest.TestCase):
28+
class BuildGraphTest(absltest.TestCase):
2929

3030
def _create_embedding_file(self):
3131
return self.create_tempfile('embeddings.tfr').full_path
@@ -82,9 +82,9 @@ def testGraphBuildingNoThresholding(self):
8282
embedding_path = self._create_embedding_file()
8383
self._write_embeddings(embedding_path)
8484
graph_path = self._create_graph_file()
85-
graph_builder_lib.build_graph([embedding_path],
86-
graph_path,
87-
similarity_threshold=0)
85+
build_graph_lib.build_graph([embedding_path],
86+
graph_path,
87+
similarity_threshold=0)
8888
g_actual = graph_utils.read_tsv_graph(graph_path)
8989
self.assertDictEqual(
9090
g_actual, {
@@ -107,9 +107,9 @@ def testGraphBuildingWithThresholding(self):
107107
embedding_path = self._create_embedding_file()
108108
self._write_embeddings(embedding_path)
109109
graph_path = self._create_graph_file()
110-
graph_builder_lib.build_graph([embedding_path],
111-
graph_path,
112-
similarity_threshold=0.51)
110+
build_graph_lib.build_graph([embedding_path],
111+
graph_path,
112+
similarity_threshold=0.51)
113113
g_actual = graph_utils.read_tsv_graph(graph_path)
114114
self.assertDictEqual(g_actual, {})
115115

neural_structured_learning/tools/graph_builder.py

Lines changed: 0 additions & 66 deletions
This file was deleted.

neural_structured_learning/tools/input_maker.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)