Skip to content

Commit d5b2907

Browse files
Neural-Link Teamtensorflow-copybara
authored andcommitted
Updates NSL tutorials to ensure they work in the open source environment.
PiperOrigin-RevId: 266400651
1 parent fae990c commit d5b2907

File tree

2 files changed

+29
-36
lines changed

2 files changed

+29
-36
lines changed

g3doc/tutorials/graph_keras_lstm_imdb.ipynb

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
"\n",
4646
"\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n",
4747
" \u003ctd\u003e\n",
48-
" \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/neural_structured_learning/tutorials/graph_keras_lstm_imdb.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n",
48+
" \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/neural_structured_learning/tutorials/graph_keras_lstm_imdb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n",
4949
" \u003c/td\u003e\n",
5050
" \u003ctd\u003e\n",
51-
" \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/neural-structured-_learning/blob/master/g3doc/tutorials/graph_keras_lstm_imdb.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
51+
" \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/neural-structured-learning/blob/master/g3doc/tutorials/graph_keras_lstm_imdb.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
5252
" \u003c/td\u003e\n",
5353
" \u003ctd\u003e\n",
5454
" \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/neural-structured-learning/blob/master/g3doc/tutorials/graph_keras_lstm_imdb.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n",
@@ -114,8 +114,9 @@
114114
"source": [
115115
"## Requirements\n",
116116
"\n",
117-
"1. Install the Neural Structured Learning package.\n",
118-
"2. Install tensorflow-hub"
117+
"1. Install TensorFlow 2.x to create an interactive developing environment with eager execution.\n",
118+
"2. Install the Neural Structured Learning package.\n",
119+
"3. Install tensorflow-hub."
119120
]
120121
},
121122
{
@@ -128,8 +129,9 @@
128129
},
129130
"outputs": [],
130131
"source": [
132+
"!pip install --quiet tensorflow==2.0.0-rc0\n",
131133
"!pip install --quiet neural-structured-learning\n",
132-
"!pip install --quit tensorflow-hub"
134+
"!pip install --quiet tensorflow-hub"
133135
]
134136
},
135137
{
@@ -162,14 +164,18 @@
162164
"\n",
163165
"import neural_structured_learning as nsl\n",
164166
"\n",
165-
"### Enable Tensorflow v2 behavior.\n",
166167
"import tensorflow as tf\n",
167168
"tf.compat.v1.enable_v2_behavior()\n",
168-
"tf.keras.backend.clear_session() # Resets notebook state\n",
169169
"\n",
170170
"import tensorflow_hub as hub\n",
171171
"\n",
172-
"print(tf.__version__)"
172+
"# Resets notebook state\n",
173+
"tf.keras.backend.clear_session()\n",
174+
"\n",
175+
"print(\"Version: \", tf.__version__)\n",
176+
"print(\"Eager mode: \", tf.executing_eagerly())\n",
177+
"print(\"Hub version: \", hub.__version__)\n",
178+
"print(\"GPU is\", \"available\" if tf.test.is_gpu_available() else \"NOT AVAILABLE\")"
173179
]
174180
},
175181
{
@@ -436,6 +442,10 @@
436442
},
437443
"outputs": [],
438444
"source": [
445+
"# This is necessary because hub.KerasLayer assumes tensor hashability, which\n",
446+
"# is not supported in eager mode.\n",
447+
"tf.compat.v1.disable_tensor_equality()\n",
448+
"\n",
439449
"pretrained_embedding = 'https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1'\n",
440450
"\n",
441451
"hub_layer = hub.KerasLayer(\n",
@@ -730,7 +740,7 @@
730740
" self.num_classes = 2\n",
731741
" self.max_seq_length = 256\n",
732742
" self.vocab_size = 10000\n",
733-
" ### Neural Graph Learning parameters\n",
743+
" ### neural graph learning parameters\n",
734744
" self.distance_type = nsl.configs.DistanceType.L2\n",
735745
" self.graph_regularization_multiplier = 0.1\n",
736746
" self.num_neighbors = 1\n",
@@ -1030,6 +1040,7 @@
10301040
"validation_fraction = 0.9\n",
10311041
"validation_size = int(validation_fraction *\n",
10321042
" int(training_samples_count / HPARAMS.batch_size))\n",
1043+
"print(validation_size)\n",
10331044
"validation_dataset = train_dataset.take(validation_size)\n",
10341045
"train_dataset = train_dataset.skip(validation_size)"
10351046
]

g3doc/tutorials/graph_keras_mlp_cora.ipynb

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -109,38 +109,15 @@
109109
"\n"
110110
]
111111
},
112-
{
113-
"cell_type": "markdown",
114-
"metadata": {
115-
"colab_type": "text",
116-
"id": "v_39VWm4yIgt"
117-
},
118-
"source": [
119-
"Install Tensorflow 2.0 to create an interactive developing environment with\n",
120-
"eager execution."
121-
]
122-
},
123-
{
124-
"cell_type": "code",
125-
"execution_count": 0,
126-
"metadata": {
127-
"colab": {},
128-
"colab_type": "code",
129-
"id": "2m7Sp74HyFVS"
130-
},
131-
"outputs": [],
132-
"source": [
133-
"!pip install --quiet tensorflow==2.0.0-rc0"
134-
]
135-
},
136112
{
137113
"cell_type": "markdown",
138114
"metadata": {
139115
"colab_type": "text",
140116
"id": "hgSLDi0SyBuO"
141117
},
142118
"source": [
143-
"Install the Neural Structured Learning package."
119+
"1. Install TensorFlow 2.x to create an interactive developing environment with eager execution.\n",
120+
"2. Install the Neural Structured Learning package."
144121
]
145122
},
146123
{
@@ -153,6 +130,7 @@
153130
},
154131
"outputs": [],
155132
"source": [
133+
"!pip install --quiet tensorflow==2.0.0-rc0\n",
156134
"!pip install --quiet neural-structured-learning"
157135
]
158136
},
@@ -183,7 +161,11 @@
183161
"import tensorflow as tf\n",
184162
"\n",
185163
"# Resets notebook state\n",
186-
"tf.keras.backend.clear_session()"
164+
"tf.keras.backend.clear_session()\n",
165+
"\n",
166+
"print(\"Version: \", tf.__version__)\n",
167+
"print(\"Eager mode: \", tf.executing_eagerly())\n",
168+
"print(\"GPU is\", \"available\" if tf.test.is_gpu_available() else \"NOT AVAILABLE\")"
187169
]
188170
},
189171
{
@@ -324,7 +306,7 @@
324306
"id": "2gYWAqJqZ76I"
325307
},
326308
"source": [
327-
"## Hyperparameters and constants\n",
309+
"## Hyperparameters\n",
328310
"\n",
329311
"We will use an instance of `HParams` to inclue various hyperparameters and\n",
330312
"constants used for training and evaluation. We briefly describe each of them\n",

0 commit comments

Comments
 (0)