diff --git a/site/en/tutorials/structured_data/preprocessing_layers.ipynb b/site/en/tutorials/structured_data/preprocessing_layers.ipynb index ead524ca13..d05df3c6d2 100644 --- a/site/en/tutorials/structured_data/preprocessing_layers.ipynb +++ b/site/en/tutorials/structured_data/preprocessing_layers.ipynb @@ -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", @@ -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:" ] @@ -589,7 +589,7 @@ }, "outputs": [], "source": [ - "all_inputs = []\n", + "all_inputs = {}\n", "encoded_features = []\n", "\n", "# Numerical features.\n", @@ -597,7 +597,7 @@ " 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)" ] }, @@ -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)" ] }, @@ -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)" ] }, @@ -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, @@ -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)" ] }, { @@ -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\")" ] }, { @@ -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)" ] }, { @@ -869,7 +881,6 @@ ], "metadata": { "colab": { - "collapsed_sections": [], "name": "preprocessing_layers.ipynb", "toc_visible": true },