Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update preprocessing_layers.ipynb #2285

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions site/en/tutorials/structured_data/preprocessing_layers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
"def df_to_dataset(dataframe, shuffle=True, batch_size=32):\n",
" df = dataframe.copy()\n",
" labels = df.pop('target')\n",
" df = {key: value.values[:,tf.newaxis] for key, value in dataframe.items()}\n",
" df = {key: value.to_numpy()[:,tf.newaxis] for key, value in dataframe.items()}\n",
" ds = tf.data.Dataset.from_tensor_slices((dict(df), labels))\n",
" if shuffle:\n",
" ds = ds.shuffle(buffer_size=len(dataframe))\n",
Expand Down Expand Up @@ -447,7 +447,7 @@
"source": [
"### Categorical columns\n",
"\n",
"Pet `Type`s in the dataset are represented as strings—`Dog`s and `Cat`s—which need to be multi-hot encoded before being fed into the model. The `Age` feature \n",
"Pet `Type`s in the dataset are represented as strings—`Dog`s and `Cat`s—which need to be multi-hot encoded before being fed into the model. The `Age` feature\n",
"\n",
"Define another new utility function that returns a layer which maps values from a vocabulary to integer indices and multi-hot encodes the features using the `tf.keras.layers.StringLookup`, `tf.keras.layers.IntegerLookup`, and `tf.keras.CategoryEncoding` preprocessing layers:"
]
Expand Down Expand Up @@ -589,15 +589,15 @@
},
"outputs": [],
"source": [
"all_inputs = []\n",
"all_inputs = {}\n",
"encoded_features = []\n",
"\n",
"# Numerical features.\n",
"for header in ['PhotoAmt', 'Fee']:\n",
" numeric_col = tf.keras.Input(shape=(1,), name=header)\n",
" normalization_layer = get_normalization_layer(header, train_ds)\n",
" encoded_numeric_col = normalization_layer(numeric_col)\n",
" all_inputs.append(numeric_col)\n",
" all_inputs[header] = numeric_col\n",
" encoded_features.append(encoded_numeric_col)"
]
},
Expand Down Expand Up @@ -625,7 +625,7 @@
" dtype='int64',\n",
" max_tokens=5)\n",
"encoded_age_col = encoding_layer(age_col)\n",
"all_inputs.append(age_col)\n",
"all_inputs['Age'] = age_col\n",
"encoded_features.append(encoded_age_col)"
]
},
Expand Down Expand Up @@ -656,7 +656,7 @@
" dtype='string',\n",
" max_tokens=5)\n",
" encoded_categorical_col = encoding_layer(categorical_col)\n",
" all_inputs.append(categorical_col)\n",
" all_inputs[header] = categorical_col\n",
" encoded_features.append(encoded_categorical_col)"
]
},
Expand All @@ -678,6 +678,17 @@
"The next step is to create a model using the [Keras Functional API](https://www.tensorflow.org/guide/keras/functional). For the first layer in your model, merge the list of feature inputs—`encoded_features`—into one vector via concatenation with `tf.keras.layers.concatenate`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "EtkwHC-akvcv"
},
"outputs": [],
"source": [
"encoded_features"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -713,7 +724,8 @@
"source": [
"model.compile(optimizer='adam',\n",
" loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n",
" metrics=[\"accuracy\"])"
" metrics=[\"accuracy\"],\n",
" run_eagerly=True)"
]
},
{
Expand All @@ -734,7 +746,7 @@
"outputs": [],
"source": [
"# Use `rankdir='LR'` to make the graph horizontal.\n",
"tf.keras.utils.plot_model(model, show_shapes=True, rankdir=\"LR\")"
"tf.keras.utils.plot_model(model, show_shapes=True, show_layer_names=True, rankdir=\"LR\")"
]
},
{
Expand Down Expand Up @@ -765,8 +777,8 @@
},
"outputs": [],
"source": [
"loss, accuracy = model.evaluate(test_ds)\n",
"print(\"Accuracy\", accuracy)"
"result = model.evaluate(test_ds, return_dict=True)\n",
"print(result)"
]
},
{
Expand Down Expand Up @@ -869,7 +881,6 @@
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "preprocessing_layers.ipynb",
"toc_visible": true
},
Expand Down
Loading