|
12 | 12 | },
|
13 | 13 | {
|
14 | 14 | "cell_type": "code",
|
15 |
| - "execution_count": 0, |
| 15 | + "execution_count": null, |
16 | 16 | "metadata": {
|
17 | 17 | "cellView": "form",
|
18 | 18 | "colab": {},
|
|
101 | 101 | },
|
102 | 102 | {
|
103 | 103 | "cell_type": "code",
|
104 |
| - "execution_count": 0, |
| 104 | + "execution_count": null, |
105 | 105 | "metadata": {
|
106 | 106 | "colab": {},
|
107 | 107 | "colab_type": "code",
|
|
125 | 125 | },
|
126 | 126 | {
|
127 | 127 | "cell_type": "code",
|
128 |
| - "execution_count": 0, |
| 128 | + "execution_count": null, |
129 | 129 | "metadata": {
|
130 | 130 | "cellView": "both",
|
131 | 131 | "colab": {},
|
|
136 | 136 | "source": [
|
137 | 137 | "import tensorflow as tf\n",
|
138 | 138 | "\n",
|
| 139 | + "import copy\n", |
139 | 140 | "import logging\n",
|
140 | 141 | "import numpy as np\n",
|
141 | 142 | "import pandas as pd\n",
|
|
157 | 158 | },
|
158 | 159 | {
|
159 | 160 | "cell_type": "code",
|
160 |
| - "execution_count": 0, |
| 161 | + "execution_count": null, |
161 | 162 | "metadata": {
|
162 | 163 | "cellView": "both",
|
163 | 164 | "colab": {},
|
|
190 | 191 | },
|
191 | 192 | {
|
192 | 193 | "cell_type": "code",
|
193 |
| - "execution_count": 0, |
| 194 | + "execution_count": null, |
194 | 195 | "metadata": {
|
195 | 196 | "cellView": "both",
|
196 | 197 | "colab": {},
|
|
219 | 220 | },
|
220 | 221 | {
|
221 | 222 | "cell_type": "code",
|
222 |
| - "execution_count": 0, |
| 223 | + "execution_count": null, |
223 | 224 | "metadata": {
|
224 | 225 | "colab": {},
|
225 | 226 | "colab_type": "code",
|
|
285 | 286 | },
|
286 | 287 | {
|
287 | 288 | "cell_type": "code",
|
288 |
| - "execution_count": 0, |
| 289 | + "execution_count": null, |
289 | 290 | "metadata": {
|
290 | 291 | "colab": {},
|
291 | 292 | "colab_type": "code",
|
|
341 | 342 | },
|
342 | 343 | {
|
343 | 344 | "cell_type": "code",
|
344 |
| - "execution_count": 0, |
| 345 | + "execution_count": null, |
345 | 346 | "metadata": {
|
346 | 347 | "colab": {},
|
347 | 348 | "colab_type": "code",
|
|
450 | 451 | },
|
451 | 452 | {
|
452 | 453 | "cell_type": "code",
|
453 |
| - "execution_count": 0, |
| 454 | + "execution_count": null, |
454 | 455 | "metadata": {
|
455 | 456 | "colab": {},
|
456 | 457 | "colab_type": "code",
|
|
499 | 500 | },
|
500 | 501 | {
|
501 | 502 | "cell_type": "code",
|
502 |
| - "execution_count": 0, |
| 503 | + "execution_count": null, |
503 | 504 | "metadata": {
|
504 | 505 | "colab": {},
|
505 | 506 | "colab_type": "code",
|
|
559 | 560 | },
|
560 | 561 | {
|
561 | 562 | "cell_type": "code",
|
562 |
| - "execution_count": 0, |
| 563 | + "execution_count": null, |
563 | 564 | "metadata": {
|
564 | 565 | "colab": {},
|
565 | 566 | "colab_type": "code",
|
|
568 | 569 | "outputs": [],
|
569 | 570 | "source": [
|
570 | 571 | "# This is random lattice ensemble model with separate calibration:\n",
|
571 |
| - "# model output is the average output of separatly calibrated lattices.\n", |
| 572 | + "# model output is the average output of separately calibrated lattices.\n", |
572 | 573 | "model_config = tfl.configs.CalibratedLatticeEnsembleConfig(\n",
|
573 | 574 | " feature_configs=feature_configs,\n",
|
574 | 575 | " num_lattices=5,\n",
|
|
589 | 590 | "tfl.visualization.draw_model_graph(model_graph, calibrator_dpi=15)"
|
590 | 591 | ]
|
591 | 592 | },
|
| 593 | + { |
| 594 | + "cell_type": "markdown", |
| 595 | + "metadata": { |
| 596 | + "colab_type": "text", |
| 597 | + "id": "7uyO8s97FGJM" |
| 598 | + }, |
| 599 | + "source": [ |
| 600 | + "### RTL Layer Random Lattice Ensemble\n", |
| 601 | + "\n", |
| 602 | + "The following model config uses a `tfl.layers.RTL` layer that uses a random subset of features for each lattice. We note that `tfl.layers.RTL` only supports monotonicity constraints and must have the same lattice size for all features and no per-feature regularization. Note that using a `tfl.layers.RTL` layer lets you scale to much larger ensembles than using separate `tfl.layers.Lattice` instances." |
| 603 | + ] |
| 604 | + }, |
| 605 | + { |
| 606 | + "cell_type": "code", |
| 607 | + "execution_count": null, |
| 608 | + "metadata": { |
| 609 | + "colab": {}, |
| 610 | + "colab_type": "code", |
| 611 | + "id": "8v7dKg-FF7iz" |
| 612 | + }, |
| 613 | + "outputs": [], |
| 614 | + "source": [ |
| 615 | + "# Make sure our feature configs have the same lattice size, no per-feature\n", |
| 616 | + "# regularization, and only monotonicity constraints.\n", |
| 617 | + "rtl_layer_feature_configs = copy.deepcopy(feature_configs)\n", |
| 618 | + "for feature_config in rtl_layer_feature_configs:\n", |
| 619 | + " feature_config.lattice_size = 2\n", |
| 620 | + " feature_config.unimodality = 'none'\n", |
| 621 | + " feature_config.reflects_trust_in = None\n", |
| 622 | + " feature_config.dominates = None\n", |
| 623 | + " feature_config.regularizer_configs = None\n", |
| 624 | + "# This is RTL layer ensemble model with separate calibration:\n", |
| 625 | + "# model output is the average output of separately calibrated lattices.\n", |
| 626 | + "model_config = tfl.configs.CalibratedLatticeEnsembleConfig(\n", |
| 627 | + " lattices='rtl_layer',\n", |
| 628 | + " feature_configs=rtl_layer_feature_configs,\n", |
| 629 | + " num_lattices=5,\n", |
| 630 | + " lattice_rank=3)\n", |
| 631 | + "# A CannedClassifier is constructed from the given model config.\n", |
| 632 | + "estimator = tfl.estimators.CannedClassifier(\n", |
| 633 | + " feature_columns=feature_columns,\n", |
| 634 | + " model_config=model_config,\n", |
| 635 | + " feature_analysis_input_fn=feature_analysis_input_fn,\n", |
| 636 | + " optimizer=tf.keras.optimizers.Adam(LEARNING_RATE),\n", |
| 637 | + " config=tf.estimator.RunConfig(tf_random_seed=42))\n", |
| 638 | + "estimator.train(input_fn=train_input_fn)\n", |
| 639 | + "results = estimator.evaluate(input_fn=test_input_fn)\n", |
| 640 | + "print('Random ensemble test AUC: {}'.format(results['auc']))\n", |
| 641 | + "saved_model_path = estimator.export_saved_model(estimator.model_dir,\n", |
| 642 | + " serving_input_fn)\n", |
| 643 | + "model_graph = tfl.estimators.get_model_graph(saved_model_path)\n", |
| 644 | + "tfl.visualization.draw_model_graph(model_graph, calibrator_dpi=15)" |
| 645 | + ] |
| 646 | + }, |
592 | 647 | {
|
593 | 648 | "cell_type": "markdown",
|
594 | 649 | "metadata": {
|
|
605 | 660 | },
|
606 | 661 | {
|
607 | 662 | "cell_type": "code",
|
608 |
| - "execution_count": 0, |
| 663 | + "execution_count": null, |
609 | 664 | "metadata": {
|
610 | 665 | "colab": {},
|
611 | 666 | "colab_type": "code",
|
|
634 | 689 | },
|
635 | 690 | {
|
636 | 691 | "cell_type": "code",
|
637 |
| - "execution_count": 0, |
| 692 | + "execution_count": null, |
638 | 693 | "metadata": {
|
639 | 694 | "colab": {},
|
640 | 695 | "colab_type": "code",
|
|
643 | 698 | "outputs": [],
|
644 | 699 | "source": [
|
645 | 700 | "# This is Crystals ensemble model with separate calibration: model output is\n",
|
646 |
| - "# the average output of separatly calibrated lattices.\n", |
| 701 | + "# the average output of separately calibrated lattices.\n", |
647 | 702 | "model_config = tfl.configs.CalibratedLatticeEnsembleConfig(\n",
|
648 | 703 | " feature_configs=feature_configs,\n",
|
649 | 704 | " lattices='crystals',\n",
|
|
680 | 735 | },
|
681 | 736 | {
|
682 | 737 | "cell_type": "code",
|
683 |
| - "execution_count": 0, |
| 738 | + "execution_count": null, |
684 | 739 | "metadata": {
|
685 | 740 | "colab": {},
|
686 | 741 | "colab_type": "code",
|
|
0 commit comments