|
447 | 447 | "source": [
|
448 | 448 | "### Categorical columns\n",
|
449 | 449 | "\n",
|
450 |
| - "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", |
| 450 | + "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", |
451 | 451 | "\n",
|
452 | 452 | "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:"
|
453 | 453 | ]
|
|
589 | 589 | },
|
590 | 590 | "outputs": [],
|
591 | 591 | "source": [
|
592 |
| - "all_inputs = []\n", |
| 592 | + "all_inputs = {}\n", |
593 | 593 | "encoded_features = []\n",
|
594 | 594 | "\n",
|
595 | 595 | "# Numerical features.\n",
|
596 | 596 | "for header in ['PhotoAmt', 'Fee']:\n",
|
597 | 597 | " numeric_col = tf.keras.Input(shape=(1,), name=header)\n",
|
598 | 598 | " normalization_layer = get_normalization_layer(header, train_ds)\n",
|
599 | 599 | " encoded_numeric_col = normalization_layer(numeric_col)\n",
|
600 |
| - " all_inputs.append(numeric_col)\n", |
| 600 | + " all_inputs[header] = numeric_col\n", |
601 | 601 | " encoded_features.append(encoded_numeric_col)"
|
602 | 602 | ]
|
603 | 603 | },
|
|
625 | 625 | " dtype='int64',\n",
|
626 | 626 | " max_tokens=5)\n",
|
627 | 627 | "encoded_age_col = encoding_layer(age_col)\n",
|
628 |
| - "all_inputs.append(age_col)\n", |
| 628 | + "all_inputs['Age'] = age_col\n", |
629 | 629 | "encoded_features.append(encoded_age_col)"
|
630 | 630 | ]
|
631 | 631 | },
|
|
656 | 656 | " dtype='string',\n",
|
657 | 657 | " max_tokens=5)\n",
|
658 | 658 | " encoded_categorical_col = encoding_layer(categorical_col)\n",
|
659 |
| - " all_inputs.append(categorical_col)\n", |
| 659 | + " all_inputs[header] = categorical_col\n", |
660 | 660 | " encoded_features.append(encoded_categorical_col)"
|
661 | 661 | ]
|
662 | 662 | },
|
|
678 | 678 | "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`."
|
679 | 679 | ]
|
680 | 680 | },
|
| 681 | + { |
| 682 | + "cell_type": "code", |
| 683 | + "execution_count": null, |
| 684 | + "metadata": { |
| 685 | + "id": "EtkwHC-akvcv" |
| 686 | + }, |
| 687 | + "outputs": [], |
| 688 | + "source": [ |
| 689 | + "encoded_features" |
| 690 | + ] |
| 691 | + }, |
681 | 692 | {
|
682 | 693 | "cell_type": "code",
|
683 | 694 | "execution_count": null,
|
|
713 | 724 | "source": [
|
714 | 725 | "model.compile(optimizer='adam',\n",
|
715 | 726 | " loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n",
|
716 |
| - " metrics=[\"accuracy\"])" |
| 727 | + " metrics=[\"accuracy\"],\n", |
| 728 | + " run_eagerly=True)" |
717 | 729 | ]
|
718 | 730 | },
|
719 | 731 | {
|
|
734 | 746 | "outputs": [],
|
735 | 747 | "source": [
|
736 | 748 | "# Use `rankdir='LR'` to make the graph horizontal.\n",
|
737 |
| - "tf.keras.utils.plot_model(model, show_shapes=True, rankdir=\"LR\")" |
| 749 | + "tf.keras.utils.plot_model(model, show_shapes=True, show_layer_names=True, rankdir=\"LR\")" |
738 | 750 | ]
|
739 | 751 | },
|
740 | 752 | {
|
|
765 | 777 | },
|
766 | 778 | "outputs": [],
|
767 | 779 | "source": [
|
768 |
| - "loss, accuracy = model.evaluate(test_ds)\n", |
769 |
| - "print(\"Accuracy\", accuracy)" |
| 780 | + "result = model.evaluate(test_ds, return_dict=True)\n", |
| 781 | + "print(result)" |
770 | 782 | ]
|
771 | 783 | },
|
772 | 784 | {
|
|
869 | 881 | ],
|
870 | 882 | "metadata": {
|
871 | 883 | "colab": {
|
872 |
| - "collapsed_sections": [], |
873 | 884 | "name": "preprocessing_layers.ipynb",
|
874 | 885 | "toc_visible": true
|
875 | 886 | },
|
|
0 commit comments