|
254 | 254 | "outputs": [],
|
255 | 255 | "source": [
|
256 | 256 | "def create_model():\n",
|
| 257 | + " regularizer = tf.keras.regularizers.L2(1e-5)\n", |
257 | 258 | " return tf.keras.Sequential(\n",
|
258 |
| - " [tf.keras.layers.Conv2D(256, 3, activation='relu', input_shape=(28, 28, 1)),\n", |
259 |
| - " tf.keras.layers.Conv2D(256, 3, activation='relu'),\n", |
| 259 | + " [tf.keras.layers.Conv2D(256, 3, input_shape=(28, 28, 1),\n", |
| 260 | + " activation='relu',\n", |
| 261 | + " kernel_regularizer=regularizer),\n", |
| 262 | + " tf.keras.layers.Conv2D(256, 3,\n", |
| 263 | + " activation='relu',\n", |
| 264 | + " kernel_regularizer=regularizer),\n", |
260 | 265 | " tf.keras.layers.Flatten(),\n",
|
261 |
| - " tf.keras.layers.Dense(256, activation='relu'),\n", |
262 |
| - " tf.keras.layers.Dense(128, activation='relu'),\n", |
263 |
| - " tf.keras.layers.Dense(10)])" |
| 266 | + " tf.keras.layers.Dense(256,\n", |
| 267 | + " activation='relu',\n", |
| 268 | + " kernel_regularizer=regularizer),\n", |
| 269 | + " tf.keras.layers.Dense(128,\n", |
| 270 | + " activation='relu',\n", |
| 271 | + " kernel_regularizer=regularizer),\n", |
| 272 | + " tf.keras.layers.Dense(10,\n", |
| 273 | + " kernel_regularizer=regularizer)])" |
| 274 | + ] |
| 275 | + }, |
| 276 | + { |
| 277 | + "cell_type": "markdown", |
| 278 | + "metadata": { |
| 279 | + "id": "h-2qaXgfyONQ" |
| 280 | + }, |
| 281 | + "source": [ |
| 282 | + "This model puts L2 regularization terms on the weights of each layer, so that the custom training loop below can show how you pick them up from `Model.losses`." |
264 | 283 | ]
|
265 | 284 | },
|
266 | 285 | {
|
|
442 | 461 | " images, labels = inputs\n",
|
443 | 462 | " with tf.GradientTape() as tape:\n",
|
444 | 463 | " logits = model(images, training=True)\n",
|
445 |
| - " loss = tf.keras.losses.sparse_categorical_crossentropy(\n", |
| 464 | + " per_example_loss = tf.keras.losses.sparse_categorical_crossentropy(\n", |
446 | 465 | " labels, logits, from_logits=True)\n",
|
447 |
| - " loss = tf.nn.compute_average_loss(loss, global_batch_size=batch_size)\n", |
| 466 | + " loss = tf.nn.compute_average_loss(per_example_loss)\n", |
| 467 | + " model_losses = model.losses\n", |
| 468 | + " if model_losses:\n", |
| 469 | + " loss += tf.nn.scale_regularization_loss(tf.add_n(model_losses))\n", |
| 470 | + "\n", |
448 | 471 | " grads = tape.gradient(loss, model.trainable_variables)\n",
|
449 | 472 | " optimizer.apply_gradients(list(zip(grads, model.trainable_variables)))\n",
|
450 | 473 | " training_loss.update_state(loss * strategy.num_replicas_in_sync)\n",
|
|
478 | 501 | "\n",
|
479 | 502 | " for step in range(steps_per_epoch):\n",
|
480 | 503 | " train_step(train_iterator)\n",
|
481 |
| - " print('Current step: {}, training loss: {}, accuracy: {}%'.format(\n", |
| 504 | + " print('Current step: {}, training loss: {}, training accuracy: {}%'.format(\n", |
482 | 505 | " optimizer.iterations.numpy(),\n",
|
483 | 506 | " round(float(training_loss.result()), 4),\n",
|
484 | 507 | " round(float(training_accuracy.result()) * 100, 2)))\n",
|
|
516 | 539 | " images, labels = inputs\n",
|
517 | 540 | " with tf.GradientTape() as tape:\n",
|
518 | 541 | " logits = model(images, training=True)\n",
|
519 |
| - " loss = tf.keras.losses.sparse_categorical_crossentropy(\n", |
| 542 | + " per_example_loss = tf.keras.losses.sparse_categorical_crossentropy(\n", |
520 | 543 | " labels, logits, from_logits=True)\n",
|
521 |
| - " loss = tf.nn.compute_average_loss(loss, global_batch_size=batch_size)\n", |
| 544 | + " loss = tf.nn.compute_average_loss(per_example_loss)\n", |
| 545 | + " model_losses = model.losses\n", |
| 546 | + " if model_losses:\n", |
| 547 | + " loss += tf.nn.scale_regularization_loss(tf.add_n(model_losses))\n", |
522 | 548 | " grads = tape.gradient(loss, model.trainable_variables)\n",
|
523 | 549 | " optimizer.apply_gradients(list(zip(grads, model.trainable_variables)))\n",
|
524 | 550 | " training_loss.update_state(loss * strategy.num_replicas_in_sync)\n",
|
|
531 | 557 | "# retraced if the value changes.\n",
|
532 | 558 | "train_multiple_steps(train_iterator, tf.convert_to_tensor(steps_per_epoch))\n",
|
533 | 559 | "\n",
|
534 |
| - "print('Current step: {}, training loss: {}, accuracy: {}%'.format(\n", |
| 560 | + "print('Current step: {}, training loss: {}, training accuracy: {}%'.format(\n", |
535 | 561 | " optimizer.iterations.numpy(),\n",
|
536 | 562 | " round(float(training_loss.result()), 4),\n",
|
537 | 563 | " round(float(training_accuracy.result()) * 100, 2)))"
|
|
0 commit comments