Skip to content

Commit 117c1d3

Browse files
committed
debug
1 parent 55c0d21 commit 117c1d3

File tree

1 file changed

+66
-48
lines changed

1 file changed

+66
-48
lines changed

site/en/tutorials/load_data/pandas_dataframe.ipynb

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -447,79 +447,77 @@
447447
"cell_type": "code",
448448
"execution_count": null,
449449
"metadata": {
450-
"id": "U3QDo-jwHYXc"
450+
"id": "voDoA447GBC3"
451451
},
452452
"outputs": [],
453453
"source": [
454-
"numeric_dict_ds = tf.data.Dataset.from_tensor_slices((dict(numeric_features), target))"
454+
"numeric_features_dict = {key: value.to_numpy()[:, tf.newaxis] for key, value in dict(numeric_features).items()}\n",
455+
"target_array = target.to_numpy()[:, tf.newaxis]"
455456
]
456457
},
457458
{
458-
"cell_type": "markdown",
459+
"cell_type": "code",
460+
"execution_count": null,
459461
"metadata": {
460-
"id": "yyEERK9ldIi_"
462+
"id": "U3QDo-jwHYXc"
461463
},
464+
"outputs": [],
462465
"source": [
463-
"Here are the first three examples from that dataset:"
466+
"numeric_dict_ds = tf.data.Dataset.from_tensor_slices((numeric_features_dict , target_array))"
464467
]
465468
},
466469
{
467470
"cell_type": "code",
468471
"execution_count": null,
469472
"metadata": {
470-
"id": "q0tDwk0VdH6D"
473+
"id": "HL4Bf1b7M7DT"
471474
},
472475
"outputs": [],
473476
"source": [
474-
"for row in numeric_dict_ds.take(3):\n",
475-
" print(row)"
477+
"len(numeric_features_dict)"
476478
]
477479
},
478480
{
479481
"cell_type": "markdown",
480482
"metadata": {
481-
"id": "DEAM6HAFxlMy"
483+
"id": "yyEERK9ldIi_"
482484
},
483485
"source": [
484-
"### Dictionaries with Keras"
486+
"Here are the first three examples from that dataset:"
485487
]
486488
},
487489
{
488-
"cell_type": "markdown",
490+
"cell_type": "code",
491+
"execution_count": null,
489492
"metadata": {
490-
"id": "dnoyoWLWx07i"
493+
"id": "q0tDwk0VdH6D"
491494
},
495+
"outputs": [],
492496
"source": [
493-
"Typically, Keras models and layers expect a single input tensor, but these classes can accept and return nested structures of dictionaries, tuples and tensors. These structures are known as \"nests\" (refer to the `tf.nest` module for details).\n",
494-
"\n",
495-
"There are two equivalent ways you can write a Keras model that accepts a dictionary as input."
497+
"for row in numeric_dict_ds.take(3):\n",
498+
" print(row)"
496499
]
497500
},
498501
{
499502
"cell_type": "markdown",
500503
"metadata": {
501-
"id": "5xUTrm0apDTr"
504+
"id": "dnoyoWLWx07i"
502505
},
503506
"source": [
504-
"#### 1. The Model-subclass style\n",
507+
"Typically, Keras models and layers expect a single input tensor, but these classes can accept and return nested structures of dictionaries, tuples and tensors. These structures are known as \"nests\" (refer to the `tf.nest` module for details).\n",
505508
"\n",
506-
"You write a subclass of `tf.keras.Model` (or `tf.keras.Layer`). You directly handle the inputs, and create the outputs:"
509+
"There are two equivalent ways you can write a Keras model that accepts a dictionary as input."
507510
]
508511
},
509512
{
510-
"cell_type": "code",
511-
"execution_count": null,
513+
"cell_type": "markdown",
512514
"metadata": {
513-
"id": "Zc3HV99CFRWL"
515+
"id": "5xUTrm0apDTr"
514516
},
515-
"outputs": [],
516517
"source": [
517-
" def stack_dict(inputs, fun=tf.stack):\n",
518-
" values = []\n",
519-
" for key in sorted(inputs.keys()):\n",
520-
" values.append(tf.cast(inputs[key], tf.float32))\n",
518+
"### 1. The Model-subclass style\n",
521519
"\n",
522-
" return fun(values, axis=-1)"
520+
"You write a subclass of `tf.keras.Model` (or `tf.keras.Layer`). You directly handle the inputs, and create the outputs:"
523521
]
524522
},
525523
{
@@ -545,22 +543,31 @@
545543
" tf.keras.layers.Dense(1)\n",
546544
" ])\n",
547545
"\n",
546+
" self.concat = tf.keras.layers.Concatenate(axis=1)\n",
547+
"\n",
548+
" def _stack(self, input_dict):\n",
549+
" values = []\n",
550+
" for key, value in sorted(input_dict.items()):\n",
551+
" values.append(value)\n",
552+
"\n",
553+
" return self.concat(values)\n",
554+
"\n",
548555
" def adapt(self, inputs):\n",
549556
" # Stack the inputs and `adapt` the normalization layer.\n",
550-
" inputs = stack_dict(inputs)\n",
557+
" inputs = self._stack(inputs)\n",
551558
" self.normalizer.adapt(inputs)\n",
552559
"\n",
553560
" def call(self, inputs):\n",
554561
" # Stack the inputs\n",
555-
" inputs = stack_dict(inputs)\n",
562+
" inputs = self._stack(inputs)\n",
556563
" # Run them through all the layers.\n",
557564
" result = self.seq(inputs)\n",
558565
"\n",
559566
" return result\n",
560567
"\n",
561568
"model = MyModel()\n",
562569
"\n",
563-
"model.adapt(dict(numeric_features))\n",
570+
"model.adapt(numeric_features_dict)\n",
564571
"\n",
565572
"model.compile(optimizer='adam',\n",
566573
" loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n",
@@ -585,7 +592,7 @@
585592
},
586593
"outputs": [],
587594
"source": [
588-
"model.fit(dict(numeric_features), target, epochs=5, batch_size=BATCH_SIZE)"
595+
"model.fit(numeric_features_dict, target_array, epochs=5, batch_size=BATCH_SIZE)"
589596
]
590597
},
591598
{
@@ -626,7 +633,7 @@
626633
"id": "QIIdxIYm13Ik"
627634
},
628635
"source": [
629-
"#### 2. The Keras functional style"
636+
"### 2. The Keras functional style"
630637
]
631638
},
632639
{
@@ -653,10 +660,13 @@
653660
},
654661
"outputs": [],
655662
"source": [
656-
"x = stack_dict(inputs, fun=tf.concat)\n",
663+
"xs = [value for key, value in sorted(inputs.items())]\n",
664+
"\n",
665+
"concat = tf.keras.layers.Concatenate(axis=1)\n",
666+
"x = concat(xs)\n",
657667
"\n",
658668
"normalizer = tf.keras.layers.Normalization(axis=-1)\n",
659-
"normalizer.adapt(stack_dict(dict(numeric_features)))\n",
669+
"normalizer.adapt(np.concatenate([value for key, value in sorted(numeric_features_dict.items())], axis=1))\n",
660670
"\n",
661671
"x = normalizer(x)\n",
662672
"x = tf.keras.layers.Dense(10, activation='relu')(x)\n",
@@ -679,7 +689,7 @@
679689
},
680690
"outputs": [],
681691
"source": [
682-
"tf.keras.utils.plot_model(model, rankdir=\"LR\", show_shapes=True)"
692+
"tf.keras.utils.plot_model(model, rankdir=\"LR\", show_shapes=True, show_layer_names=True)"
683693
]
684694
},
685695
{
@@ -699,7 +709,7 @@
699709
},
700710
"outputs": [],
701711
"source": [
702-
"model.fit(dict(numeric_features), target, epochs=5, batch_size=BATCH_SIZE)"
712+
"model.fit(numeric_features_dict, target, epochs=5, batch_size=BATCH_SIZE)"
703713
]
704714
},
705715
{
@@ -807,7 +817,7 @@
807817
" else:\n",
808818
" dtype = tf.float32\n",
809819
"\n",
810-
" inputs[name] = tf.keras.Input(shape=(), name=name, dtype=dtype)"
820+
" inputs[name] = tf.keras.Input(shape=(1,), name=name, dtype=dtype)"
811821
]
812822
},
813823
{
@@ -853,9 +863,7 @@
853863
"\n",
854864
"for name in binary_feature_names:\n",
855865
" inp = inputs[name]\n",
856-
" inp = inp[:, tf.newaxis]\n",
857-
" float_value = tf.cast(inp, tf.float32)\n",
858-
" preprocessed.append(float_value)\n",
866+
" preprocessed.append(inp)\n",
859867
"\n",
860868
"preprocessed"
861869
]
@@ -880,7 +888,7 @@
880888
"outputs": [],
881889
"source": [
882890
"normalizer = tf.keras.layers.Normalization(axis=-1)\n",
883-
"normalizer.adapt(stack_dict(dict(numeric_features)))"
891+
"normalizer.adapt(np.concatenate([value for key, value in sorted(numeric_features_dict.items())], axis=1))"
884892
]
885893
},
886894
{
@@ -900,11 +908,11 @@
900908
},
901909
"outputs": [],
902910
"source": [
903-
"numeric_inputs = {}\n",
911+
"numeric_inputs = []\n",
904912
"for name in numeric_feature_names:\n",
905-
" numeric_inputs[name]=inputs[name]\n",
913+
" numeric_inputs.append(inputs[name])\n",
906914
"\n",
907-
"numeric_inputs = stack_dict(numeric_inputs)\n",
915+
"numeric_inputs = tf.keras.layers.Concatenate(axis=-1)(numeric_inputs)\n",
908916
"numeric_normalized = normalizer(numeric_inputs)\n",
909917
"\n",
910918
"preprocessed.append(numeric_normalized)\n",
@@ -986,7 +994,7 @@
986994
" else:\n",
987995
" lookup = tf.keras.layers.IntegerLookup(vocabulary=vocab, output_mode='one_hot')\n",
988996
"\n",
989-
" x = inputs[name][:, tf.newaxis]\n",
997+
" x = inputs[name]\n",
990998
" x = lookup(x)\n",
991999
" preprocessed.append(x)"
9921000
]
@@ -1037,7 +1045,7 @@
10371045
},
10381046
"outputs": [],
10391047
"source": [
1040-
"preprocessed_result = tf.concat(preprocessed, axis=-1)\n",
1048+
"preprocessed_result = tf.keras.layers.Concatenate(axis=1)(preprocessed)\n",
10411049
"preprocessed_result"
10421050
]
10431051
},
@@ -1069,7 +1077,7 @@
10691077
},
10701078
"outputs": [],
10711079
"source": [
1072-
"tf.keras.utils.plot_model(preprocessor, rankdir=\"LR\", show_shapes=True)"
1080+
"tf.keras.utils.plot_model(preprocessor, rankdir=\"LR\", show_shapes=True, show_layer_names=True)"
10731081
]
10741082
},
10751083
{
@@ -1184,6 +1192,17 @@
11841192
" metrics=['accuracy'])"
11851193
]
11861194
},
1195+
{
1196+
"cell_type": "code",
1197+
"execution_count": null,
1198+
"metadata": {
1199+
"id": "i_Z2C2ZcZ3oC"
1200+
},
1201+
"outputs": [],
1202+
"source": [
1203+
"tf.keras.utils.plot_model(model, show_shapes=True, show_layer_names=True)"
1204+
]
1205+
},
11871206
{
11881207
"cell_type": "markdown",
11891208
"metadata": {
@@ -1259,7 +1278,6 @@
12591278
],
12601279
"metadata": {
12611280
"colab": {
1262-
"collapsed_sections": [],
12631281
"name": "pandas_dataframe.ipynb",
12641282
"toc_visible": true
12651283
},

0 commit comments

Comments
 (0)