|
6 | 6 | "id": "ZwZNOAMZcxl3" |
7 | 7 | }, |
8 | 8 | "source": [ |
9 | | - "##### Copyright 2019 Google LLC" |
| 9 | + "##### Copyright 2019 The TensorFlow Neural Structured Learning Authors" |
10 | 10 | ] |
11 | 11 | }, |
12 | 12 | { |
|
56 | 56 | " \u003ctd\u003e\n", |
57 | 57 | " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/neural-structured-learning/blob/master/g3doc/tutorials/adversarial_keras_cnn_mnist.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", |
58 | 58 | " \u003c/td\u003e\n", |
| 59 | + " \u003ctd\u003e\n", |
| 60 | + " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/neural-structured-learning/g3doc/tutorials/adversarial_keras_cnn_mnist.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", |
| 61 | + " \u003c/td\u003e\n", |
59 | 62 | "\u003c/table\u003e" |
60 | 63 | ] |
61 | 64 | }, |
|
404 | 407 | " x = tf.keras.layers.Flatten()(x)\n", |
405 | 408 | " for num_units in hparams.num_fc_units:\n", |
406 | 409 | " x = tf.keras.layers.Dense(num_units, activation='relu')(x)\n", |
407 | | - " pred = tf.keras.layers.Dense(hparams.num_classes, activation='softmax')(x)\n", |
| 410 | + " pred = tf.keras.layers.Dense(hparams.num_classes)(x)\n", |
408 | 411 | " model = tf.keras.Model(inputs=inputs, outputs=pred)\n", |
409 | 412 | " return model" |
410 | 413 | ] |
|
438 | 441 | }, |
439 | 442 | "outputs": [], |
440 | 443 | "source": [ |
441 | | - "base_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',\n", |
442 | | - " metrics=['acc'])\n", |
| 444 | + "base_model.compile(\n", |
| 445 | + " optimizer='adam',\n", |
| 446 | + " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", |
| 447 | + " metrics=['acc'])\n", |
443 | 448 | "base_model.fit(train_dataset, epochs=HPARAMS.epochs)" |
444 | 449 | ] |
445 | 450 | }, |
|
565 | 570 | }, |
566 | 571 | "outputs": [], |
567 | 572 | "source": [ |
568 | | - "adv_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',\n", |
569 | | - " metrics=['acc'])\n", |
| 573 | + "adv_model.compile(\n", |
| 574 | + " optimizer='adam',\n", |
| 575 | + " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", |
| 576 | + " metrics=['acc'])\n", |
570 | 577 | "adv_model.fit(train_set_for_adv_model, epochs=HPARAMS.epochs)" |
571 | 578 | ] |
572 | 579 | }, |
|
620 | 627 | "outputs": [], |
621 | 628 | "source": [ |
622 | 629 | "reference_model = nsl.keras.AdversarialRegularization(\n", |
623 | | - " base_model,\n", |
624 | | - " label_keys=[LABEL_INPUT_NAME],\n", |
625 | | - " adv_config=adv_config)\n", |
| 630 | + " base_model, label_keys=[LABEL_INPUT_NAME], adv_config=adv_config)\n", |
626 | 631 | "reference_model.compile(\n", |
627 | 632 | " optimizer='adam',\n", |
628 | | - " loss='sparse_categorical_crossentropy',\n", |
| 633 | + " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", |
629 | 634 | " metrics=['acc'])" |
630 | 635 | ] |
631 | 636 | }, |
|
685 | 690 | "for batch in test_set_for_adv_model:\n", |
686 | 691 | " perturbed_batch = reference_model.perturb_on_batch(batch)\n", |
687 | 692 | " # Clipping makes perturbed examples have the same range as regular ones.\n", |
688 | | - " perturbed_batch[IMAGE_INPUT_NAME] = tf.clip_by_value( \n", |
| 693 | + " perturbed_batch[IMAGE_INPUT_NAME] = tf.clip_by_value(\n", |
689 | 694 | " perturbed_batch[IMAGE_INPUT_NAME], 0.0, 1.0)\n", |
690 | 695 | " y_true = perturbed_batch.pop(LABEL_INPUT_NAME)\n", |
691 | 696 | " perturbed_images.append(perturbed_batch[IMAGE_INPUT_NAME].numpy())\n", |
|
742 | 747 | "\n", |
743 | 748 | "batch_size = HPARAMS.batch_size\n", |
744 | 749 | "n_col = 4\n", |
745 | | - "n_row = (batch_size + n_col - 1) / n_col\n", |
| 750 | + "n_row = (batch_size + n_col - 1) // n_col\n", |
746 | 751 | "\n", |
747 | 752 | "print('accuracy in batch %d:' % batch_index)\n", |
748 | 753 | "for name, pred in batch_pred.items():\n", |
|
0 commit comments