{"id":1165,"date":"2020-09-05T10:11:44","date_gmt":"2020-09-05T03:11:44","guid":{"rendered":"http:\/\/www.miai.vn\/?p=1165"},"modified":"2020-09-05T10:11:44","modified_gmt":"2020-09-05T03:11:44","slug":"keras-callbacks-tro-thu-dac-luc-khi-train-models","status":"publish","type":"post","link":"https:\/\/miai.vn\/?p=1165","title":{"rendered":"Keras Callbacks &#8211; tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models"},"content":{"rendered":"\n<p>Hello tu\u1ea7n m\u1edbi anh em M\u00ec AI. H\u00f4m nay ch\u00fang ta c\u00f9ng nhau \u0111i t\u00ecm hi\u1ec3u Keras Callbacks &#8211; tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models nh\u00e9.<\/p>\n\n\n\n<p>Tr\u01b0\u1edbc khi b\u1eaft \u0111\u1ea7u m\u00ecnh c\u0169ng xin n\u00f3i tr\u01b0\u1edbc l\u00e0 \u0111\u00e2y l\u00e0 series d\u00e0nh cho c\u00e1c b\u1ea1n newbie m\u1edbi b\u1ea5t \u0111\u1ea7u th\u00f4i. C\u00f2n v\u1edbi c\u00e1c b\u1ea1n l\u00e0m l\u00e2u r\u1ed3i th\u00ec c\u00f3 l\u1ebd kh\u00e1i ni\u1ec7m n\u00e0y \u0111\u00e3 kh\u00e1 quen thu\u1ed9c r\u1ed3i n\u00ean c\u00e1c b\u1ea1n g\u00f3p \u00fd th\u00eam cho m\u00ecnh nh\u00e9.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/567\/1*6Z8TAefRMxXUNYDILTKOtg.png\" alt=\"keras callbacks\"\/><figcaption>Ngu\u1ed3n: <a href=\"https:\/\/miro.medium.com\/max\/567\/1*6Z8TAefRMxXUNYDILTKOtg.png\" target=\"_blank\" rel=\"noreferrer noopener\">T\u1ea1i \u0111\u00e2y<\/a><\/figcaption><\/figure>\n\n\n\n<p>R\u1ed3i b\u00e2y gi\u1edd ti\u1ebfp t\u1ee5c nha!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ph\u1ea7n 1 &#8211;  Keras Callback l\u00e0 c\u00e1i chi chi?<\/h2>\n\n\n\n<p>Nhi\u1ec1u khi train model b\u1ecb overfit, model k\u00e9m ch\u1ea5t l\u01b0\u1ee3ng m\u00e0 ch\u00fang ta kh\u00f4ng bi\u1ebft \u0111\u01b0\u1ee3c ch\u00ednh x\u00e1c khi n\u00e0o c\u1ea7n d\u1eebng train, khi n\u00e0o n\u00ean l\u01b0u l\u1ea1i weights&#8230; \u0110\u00f3 l\u00e0 l\u00fac ch\u00fang ta c\u1ea7n \u0111\u1ebfn c\u00e1c callback function. T\u00ecm hi\u1ec3u nh\u00e9!<\/p>\n\n\n\n<ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-5095883280136027\" data-ad-slot=\"7735063137\" data-ad-format=\"auto\" data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>Theo nh\u01b0 t\u00e0i li\u1ec7u ch\u00ednh ch\u1ee7 c\u1ee7a Keras th\u00ec:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A callback is a set of functions to be applied at given stages of the training procedure. You can use callbacks to get a view on internal states and statistics of the model during training.<\/code><\/pre>\n\n\n\n<p>Nh\u01b0 v\u1eady gi\u1ea3i ngh\u0129a ra th\u00ec Callback l\u00e0 m\u1ed9t s\u1ed1 h\u00e0m (function), ch\u00fang ta s\u1ebd ch\u1ea1y c\u00e1c h\u00e0m n\u00e0y \u1edf b\u01b0\u1edbc nh\u1ea5t \u0111\u1ecbnh c\u1ee7a qu\u00e1 tr\u00ecnh training \u0111\u1ec3 xem, l\u01b0u l\u1ea1i c\u00e1c tr\u1ea1ng th\u00e1i v\u00e0 th\u00f4ng s\u1ed1 c\u1ee7a model trong qu\u00e1 tr\u00ecnh training.<\/p>\n\n\n\n<p>Ch\u1eafc v\u1ea5n tr\u1eebu t\u01b0\u1ee3ng v\u00e3i ph\u1ea3i kh\u00f4ng c\u00e1c b\u1ea1n? Th\u00f4i gi\u1ea3i th\u00edch theo ki\u1ec3u M\u00ec AI nh\u00e9. C\u00e1c b\u1ea1n d\u00f9ng callbacks \u0111\u1ec3 can thi\u1ec7p v\u00e0o qu\u00e1 tr\u00ecnh training c\u1ee7a models, v\u00ed d\u1ee5<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Stop qu\u00e1 tr\u00ecnh training khi model \u0111\u1ea1t \u0111\u01b0\u1ee3c m\u1ed9t k\u1ebft qu\u1ea3 accuracy\/loss nh\u00e2t \u0111\u1ecbnh n\u00e0o \u0111\u00f3, g\u1ecdi l\u00e0 Early Stop (D\u1eebng s\u1edbm)<\/li><li>L\u01b0u l\u1ea1i weights( ho\u1eb7c model) c\u1ee7a model t\u1ea1i m\u1ed9t th\u1eddi \u0111i\u1ec3m n\u00e0o \u0111\u00f3 (g\u1ecdi l\u00e0 checkpoint) khi b\u1ea1n cho \u0111\u00f3 l\u00e0 m\u1ed9t b\u1ed9 weights t\u1ed1t. <\/li><li>\u0110i\u1ec1u ch\u1ec9nh learning rate (LR) c\u1ee7a model theo s\u1ed1 epochs \u0111\u00e3 train. V\u00ed d\u1ee5 l\u00fac \u0111\u1ea7u c\u00f3 th\u1ec3 \u0111\u1ec3 LR cao, sau \u0111\u00f3 gi\u1ea3m d\u1ea7n&#8230;.<\/li><\/ul>\n\n\n\n<p>C\u00f2n nhi\u1ec1u h\u00e0m callbacks kh\u00e1c n\u1eefa, nh\u01b0ng m\u00ecnh ch\u1ec9 \u0111i 03 h\u00e0m th\u00f4ng d\u1ee5ng b\u00ean tr\u00ean, c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 t\u1ef1 t\u00ecm hi\u1ec3u th\u00eam \u1edf t\u00e0i li\u1ec7u g\u1ed1c c\u1ee7a Keras nh\u00e9.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ph\u1ea7n 2 &#8211; T\u00ecm hi\u1ec3u Early Stop Callback<\/h2>\n\n\n\n<p>N\u00f3i m\u1ed9t c\u00e1ch ng\u1eafn g\u1ecdn l\u00e0 Early Stop Callback d\u00f9ng \u0111\u1ec3 d\u1eebng qu\u00e1 tr\u00ecnh train s\u1edbm, sau khi m\u00e0 th\u00f4ng s\u1ed1 ch\u00fang ta quan s\u00e1t (c\u00f3 th\u1ec3 l\u00e0 validation accuracy hay validaiton loss) kh\u00f4ng &#8220;kh\u00e1 l\u00ean&#8221; sau m\u1ed9t v\u00e0i epochs.<\/p>\n\n\n\n<p>\u0110\u00e2y c\u0169ng l\u00e0 m\u1ed9t k\u1ef9 thu\u1eadt hay \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 tr\u00e1nh Overfit (OF). Kh\u00f4ng ph\u1ea3i c\u1ee9 train v\u1edbi nhi\u1ec1u epochs l\u00e0 t\u1ed1t, ch\u00fang ta train nhi\u1ec1u qu\u00e1 m\u00e0 kh\u00f4ng th\u1ea5y val_loss gi\u1ea3m n\u1eefa ho\u1eb7c val_acc t\u0103ng n\u1eefa m\u00e0 v\u1eabn ti\u1ebfp t\u1ee5c train th\u00ec s\u1ebd d\u1eabn \u0111\u1ebfn c\u00f3 kh\u1ea3 n\u0103ng OF. <\/p>\n\n\n\n<p>B\u1ea1n n\u00e0o ch\u01b0a bi\u1ebft OF l\u00e0 g\u00ec th\u00ec c\u00f3 th\u1ec3 google th\u00eam. \u0110\u1ea1i kh\u00e1i l\u00e0 khi \u0111\u00f3 model qu\u00e1 kh\u1edbp v\u00e0o d\u1eef li\u1ec7u train, t\u1ea1o ra train acc v\u00e0 train loss r\u1ea5t t\u1ed1t nh\u01b0ng k\u1ebft qu\u1ea3 validation th\u00ec ng\u01b0\u1ee3c l\u1ea1i, ng\u00e0y c\u00e0ng t\u1ec7 (v\u00e0 c\u0169ng l\u00e0 kh\u00f4ng &#8220;kh\u00e1 l\u00ean&#8221;).<\/p>\n\n\n\n<p>\u0110\u00f3 l\u00e0 khi ch\u00fang ta n\u00ean \u00e1p d\u1ee5ng Early Stop Callback.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tf.keras.callbacks.EarlyStopping(\n    monitor=\"val_loss\",\n    min_delta=0,\n    mode=\"auto\",\n    restore_best_weights=False,\n)<\/code><\/pre>\n\n\n\n<ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-5095883280136027\" data-ad-slot=\"7735063137\" data-ad-format=\"auto\" data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>Gi\u1ea3i th\u00edch c\u00e1c tham s\u1ed1 c\u1ee7a EarlyStopping:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>monitor: Ch\u00fang ta s\u1ebd quan s\u00e1t tham s\u1ed1 g\u00ec? Nh\u01b0 \u1edf \u0111\u00e2y l\u00e0 val_loss, ch\u00fang ta c\u00f3 th\u1ec3 \u0111\u1ed5i th\u00e0nh : loss, val_accuracy, &#8230;.<\/li><li>min_delta: m\u1ee9c thay \u0111\u1ed5i t\u1ed1i thi\u1ec3u \u0111\u1ec3 tham s\u1ed1 quan s\u00e1t \u1edf tr\u00ean \u0111\u01b0\u1ee3c xem xet l\u00e0 \u0111\u00e3 &#8220;kh\u00e1 l\u00ean&#8221;. N\u1ebfu \u0111\u1ec3 min_delta=0 th\u00ec ch\u1ec9 c\u1ea7n c\u00f3 thay \u0111\u1ed5i t\u00edch c\u1ef1c d\u00f9 nh\u1ecf nh\u1ea5t c\u0169ng \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 &#8220;kh\u00e1 l\u00ean&#8221; r\u1ed3i. C\u00f2n n\u1ebfu \u0111\u1ec3 min_delta=0.5 ch\u1eb3ng h\u1ea1n th\u00ec tham s\u1ed1 ph\u1ea3i thay \u0111\u1ed5i t\u00edch c\u1ef1c m\u1ed9t l\u01b0\u1ee3ng 0.5 th\u00ec m\u1edbi \u0111\u01b0\u1ee3c coi l\u00e0 c\u00f3 &#8220;kh\u00e1 l\u00ean&#8221;. Tham s\u1ed1 n\u00e0y m\u1eb7c \u0111\u1ecbnh l\u00e0 0 nh\u00e9.<\/li><li>patience (m\u1eb7c \u0111\u1ecbnh = 0): d\u00e2y l\u00e0 s\u1ed1 nguy\u00ean (g\u1ecdi l\u00e0 N). Model s\u1ebd d\u1eebng train n\u1ebfu tham s\u1ed1 quan s\u00e1t kh\u00f4ng &#8220;kh\u00e1 l\u00ean&#8221; sau  N epochs.<\/li><li>mode: l\u00e0 ch\u1ebf \u0111\u1ed9 xem x\u00e9t tham s\u1ed1 quan s\u00e1t. N\u1ebfu \u0111\u1ec3 l\u00e0 &#8220;min&#8221; th\u00ec tham s\u1ed1 quan s\u00e1t c\u00e0ng gi\u1ea3m c\u00e0ng &#8220;kh\u00e1&#8221; (v\u00ed d\u1ee5 loss), n\u1ebfu \u0111\u1ec3 l\u00e0 &#8220;max&#8221; th\u00ec tham s\u1ed1 quan s\u00e1t c\u00e0ng t\u0103ng c\u00e0ng &#8220;kh\u00e1&#8221; (v\u00ed d\u1ee5 accuracy). N\u1ebfu \u0111\u1ec3 &#8220;auto&#8221; l\u00e0 ngon l\u00e0nh nh\u1ea5t, mode s\u1ebd t\u1ef1 chuy\u1ec3n th\u00e0nh min ho\u1eb7c max tu\u1ef3 v\u00e0o tham s\u1ed1 c\u1ee7a ch\u00fang ta l\u00e0 loss hay accuracy.<\/li><li>restore_best_weights: Tham s\u1ed1 n\u00e0y quy \u0111\u1ecbnh r\u1eb7ng model c\u00f3 restore l\u1ea1i weights t\u1ed1t nh\u1ea5t khi stop hay kh\u00f4ng? V\u00ed d\u1ee5, khi train \u0111\u1ebfn epoch 50 th\u00ec model nh\u1eadn th\u1ea5y val_loss th\u1ea5p nh\u1ea5t, v\u00e0 sau khi train 3 epoch 51,52,53 th\u00ec th\u1ea5y val_loss b\u1eaft \u0111\u1ea7u gi\u1ea3m (model k\u00e9m \u0111i) n\u00ean EarlyStopping Callback ra l\u1ec7nh &#8220;D\u1eebng anh em \u01a1i!&#8221;. L\u00fac n\u00e0y n\u1ebfu ta \u0111\u1ec3 restore_best_weights=True th\u00ec model s\u1ebd restore l\u1ea1i weights \u1edf epoch 50 xong m\u1edbi stop, c\u00f2n ng\u01b0\u1ee3c l\u1ea1i th\u00ec n\u00f3 s\u1ebd gi\u1eef nguy\u00ean weights \u1edf b\u01b0\u1edbc 53.<\/li><\/ul>\n\n\n\n<p>V\u00ed d\u1ee5 ch\u00fang ta train m\u1ed9t model v\u00e0 mu\u1ed1n r\u1eb1ng sau 5 epoch m\u00e0 loss kh\u00f4ng gi\u1ea3m n\u1eefa th\u00ec stop v\u00e0 l\u1ea5y weights t\u1ed1t nh\u1ea5t \u0111\u1ec3 l\u01b0u ra file model. Ta s\u1ebd vi\u1ebft source nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import tensorflow as tf\nimport numpy as np\n\n# Thi\u1ebft l\u1eadp h\u00e0m call back Early Stopping\ncallback = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=5, restore_best_weights=True)\n\n# T\u1ea1o l\u1eadp model\nmodel = tf.keras.models.Sequential(&#91;tf.keras.layers.Dense(512)])\nmodel.compile(tf.keras.optimizers.SGD(), loss='mse')\n\n# Sinh d\u1eef li\u1ec7u fake \u0111\u1ec3 train th\u1eed\nX = np.arange(100).reshape(5, 20).astype(float)\ny = np.zeros(5)\n\n# Train model 10 epochs, batch_sizes = 1\nhistory = model.fit( X, y , epochs=10, batch_size=1, callbacks=&#91;callback])\n\n# In history ra xem train m\u1ea5y epochs\nprint(\"S\u1ed1 epoch \u0111\u00e3 train = \", len(history.history&#91;'loss']))<\/code><\/pre>\n\n\n\n<p>V\u00e0 ch\u00fang ta \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5\/5 &#91;==============================] - 0s 37ms\/sample - loss: 543.4534\nEpoch 2\/10\n5\/5 &#91;==============================] - 0s 766us\/sample - loss: 52498.2139\nEpoch 3\/10\n5\/5 &#91;==============================] - 0s 982us\/sample - loss: 1131009.8172\nEpoch 4\/10\n5\/5 &#91;==============================] - 0s 934us\/sample - loss: 1418184.8703\nEpoch 5\/10\n5\/5 &#91;==============================] - 0s 697us\/sample - loss: 129574012.1000\nEpoch 6\/10\n5\/5 &#91;==============================] - 0s 3ms\/sample - loss: 179616147.2000\nS\u1ed1 epoch \u0111\u00e3 train =  6<\/code><\/pre>\n\n\n\n<p>Ch\u1ec9 c\u00f3 06 epoch \u0111\u01b0\u1ee3c train v\u00e0 model \u0111\u00e3 stop v\u00ec loss kh\u00f4ng gi\u1ea3m n\u1eefa. Th\u1ef1c ra b\u00e0i n\u00e0y loss t\u0103ng v\u00ea l\u1edd lu\u00f4n anh em. Th\u1eed nghi\u1ec7m \u0111\u1ec3 anh em hi\u1ec3u nh\u01b0 n\u00e0o l\u00e0 Early Stop th\u00f4i m\u00e0.<\/p>\n\n\n\n<p>Anh em c\u00f3 th\u1ec3 tham kh\u1ea3o r\u00f5 h\u01a1n qua \u0111\u1ed3 th\u1ecb sau:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/www.researchgate.net\/profile\/Giuseppina_Gini\/publication\/4310358\/figure\/fig2\/AS:279627110076459@1443679706731\/The-early-stopping-criterion.png\" alt=\"keras callbacks\"\/><figcaption>Ngu\u1ed3n: <a href=\"https:\/\/www.researchgate.net\/profile\/Giuseppina_Gini\/publication\/4310358\/figure\/fig2\/AS:279627110076459@1443679706731\/The-early-stopping-criterion.png\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">T\u1ea1i \u0111\u00e2y<\/a><\/figcaption><\/figure><\/div>\n\n\n\n<ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-5095883280136027\" data-ad-slot=\"7735063137\" data-ad-format=\"auto\" data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Ph\u1ea7n 3 &#8211; Checkpoint Keras Callbacks<\/h2>\n\n\n\n<p>V\u1edbi Checkpoint callbacks th\u00ec kh\u00e1 \u0111\u01a1n gi\u1ea3n, n\u00f3 ch\u1ec9 \u0111\u01a1n gi\u1ea3n l\u00e0m nhi\u1ec7m v\u1ee5 l\u01b0u l\u1ea1i b\u1ed9 weights t\u1ed1t nh\u1ea5t cho ch\u00fang ta. Ch\u00fang ta quan t\u00e2m 02 th\u1ee9 \u1edf \u0111\u00e2y:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Th\u1ee9 nh\u1ea5t, h\u01b0 n\u00e0o l\u00e0 weights t\u1ed1t? C\u00e2u tr\u1ea3 l\u1eddi l\u00e0 tu\u1ef3 ta ch\u1ecdn tham s\u1ed1 n\u00e0o \u0111\u1ec3 quan s\u00e1t (val_loss, val_accuracy&#8230;) nh\u01b0 \u1edf EarlyStop. Loss th\u00ec c\u00e0ng th\u1ea5p c\u00e0ng t\u1ed1t, accuracy th\u00ec c\u00e0ng cao c\u00e0ng t\u1ed1t.<\/li><li>Th\u1ee9 hai, checkpoint callback s\u1ebd \u0111\u01b0\u1ee3c g\u1ecdi th\u1ef1c thi khi n\u00e0o? C\u00e2u tr\u1ea3 l\u1eddi l\u00e0 n\u00f3 \u0111\u01b0\u1ee3c g\u1ecdi th\u1ef1c thi sau m\u1ed7i epoch. Khi m\u1ed9t epoch k\u1ebft th\u00fac n\u00f3 s\u1ebd ki\u1ec3m tra xem b\u1ed9 weights hi\u1ec7n t\u1ea1i c\u00f3 \u0111\u01b0\u1ee3c goi l\u00e0 &#8220;t\u1ed1t nh\u1ea5t&#8221; kh\u00f4ng? N\u1ebfu c\u00f3 n\u00f3 s\u1ebd \u0111\u01b0\u1ee3c l\u01b0u l\u1ea1i.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/nusit.nus.edu.sg\/wp-content\/uploads\/2019\/09\/hpc-checkpointing-04.gif\" alt=\"\"\/><figcaption>Ngu\u1ed3n: <a href=\"https:\/\/nusit.nus.edu.sg\/wp-content\/uploads\/2019\/09\/hpc-checkpointing-04.gif\" target=\"_blank\" rel=\"noreferrer noopener\">T\u1ea1i \u0111\u00e2y<\/a><\/figcaption><\/figure>\n\n\n\n<p>N\u00e0o, c\u00f9ng xem c\u00e1i h\u00e0m Checkpoint m\u1eb7t m\u0169i n\u00f3 ra l\u00e0m sao n\u00e0o:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tf.keras.callbacks.ModelCheckpoint(\n    filepath,\n    monitor=\"val_loss\",\n    verbose=0,\n    save_best_only=False,\n    save_weights_only=False,\n    mode=\"auto\",\n    save_freq=\"epoch\"\n)<\/code><\/pre>\n\n\n\n<p>C\u00e1c tham s\u1ed1 c\u1ee7a keras callbacks n\u00e0y nh\u01b0 sau:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>filepath: \u0110\u01b0\u1eddng d\u1eabn l\u01b0u file weights\/model.<\/li><li>monitor: tham s\u1ed1 quan s\u00e1t, t\u01b0\u01a1ng t\u1ef1 nh\u01b0 \u1edf tr\u00ean Early Stop.<\/li><li>save_best_only: N\u1ebfu \u0111\u1ec3 l\u00e0 true th\u00ec model ch\u1ec9 l\u01b0u l\u1ea1i m\u1ed9t checkpoint t\u1ed1t nh\u1ea5t v\u00e0 ng\u01b0\u1ee3c l\u1ea1i. C\u00e1i n\u00e0y ch\u00fang ta n\u00ean \u0111\u1ec3 l\u00e0 true.<\/li><li>save_weights_only: N\u1ebfu \u0111\u1ec3 l\u00e0 True th\u00ec callback s\u1ebd ch\u1ec9 l\u01b0u l\u1ea1i weights, kh\u00f4ng l\u01b0u l\u1ea1i c\u1ea5u tr\u00fac model, c\u00f2n ng\u01b0\u1ee3c l\u1ea1i th\u00ec s\u1ebd l\u01b0u c\u1ea3 ki\u1ebfn tr\u00fac model trong filepath. C\u00e1c b\u1ea1n tu\u1ef3 \u00fd s\u1eed d\u1ee5ng nh\u00e9. <\/li><li>mode: T\u01b0\u01a1ng t\u1ef1 nh\u01b0 \u1edf EarlyStop, c\u00e1c b\u1ea1n k\u00e9o l\u00ean xem nha.<\/li><li>save_freq: N\u1ebfu \u0111\u1ec3 l\u00e0 &#8220;epoch&#8221; th\u00ec model s\u1ebd ki\u1ec3m tra tham s\u1ed1 quan s\u00e1t sau m\u1ed7i epoch \u0111\u1ec3 t\u1eeb \u0111\u00f3 \u0111\u00e1nh gi\u00e1 xem weights hi\u1eb9n t\u1ea1i c\u00f3 &#8220;t\u1ed1t nh\u1ea5t&#8221; (\u0111\u1ec3 l\u01b0u l\u1ea1i) hay kh\u00f4ng? C\u00f2n n\u1ebfu l\u00e0 s\u1ed1 nguy\u00ean d\u01b0\u01a1ng N th\u00ec model s\u1ebd ki\u1ec3m tra sau N batches.<\/li><\/ul>\n\n\n\n<ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-5095883280136027\" data-ad-slot=\"7735063137\" data-ad-format=\"auto\" data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>V\u00ed d\u1ee5 m\u1ed9t ch\u00fat cho d\u1ec5 hi\u1ec3u nh\u00e9. B\u00e2y gi\u1edd ch\u00fang ta c\u00f3 b\u00e0i to\u00e1n d\u1ef1 \u0111o\u00e1n xem m\u1ed9t ng\u01b0\u1eddi c\u00f3 b\u1ecb ti\u1ec3u \u0111\u01b0\u1eddng hay kh\u00f4ng d\u1ef1a v\u00e0o c\u00e1c tham s\u1ed1 \u0111o \u0111\u01b0\u1ee3c c\u1ee7a ng\u01b0\u1eddi \u0111\u00f3. M\u00ecnh d\u00f9ng d\u1eef li\u1ec7u t\u1eeb Kaggle (https:\/\/www.kaggle.com\/uciml\/pima-indians-diabetes-database) nh\u00e9.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.callbacks import ModelCheckpoint\nimport numpy as np\n\n# Load d\u1eef li\u1ec7u\ndataset = np.loadtxt(\"pima-indians-diabetes.data.txt\", delimiter=\",\")\n\n# Chia ra input X v\u00e0 output y\nX = dataset&#91;:,0:8]\nY = dataset&#91;:,8]\n\n# T\u1ea1o model\nmodel = Sequential()\nmodel.add(Dense(32, input_dim=8, activation='relu'))\nmodel.add(Dense(16, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid'))\nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=&#91;'accuracy'])\n\n# T\u1ea1o callback\nfilepath=\"checkpoint.hdf5\"\ncallback = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='auto')\n\n# Train model\nmodel.fit(X, Y, validation_split=0.2, epochs=100, batch_size=8, callbacks=&#91;callback])<\/code><\/pre>\n\n\n\n<p>V\u00e0 sau khi train 100 epochs th\u00ec ch\u00fang ta s\u1ebd th\u1ea5y xu\u1ea5t hi\u1ec7n m\u1ed9t file checkpoint.hdf5 trong th\u01b0 m\u1ee5c hi\u1ec7n t\u1ea1i. \u0110\u00f3 ch\u00ednh l\u00e0 file l\u01b0u l\u1ea1i weights t\u1ed1t nh\u1ea5t c\u1ee7a ch\u00fang ta. V\u00e0 n\u1ebfu quan s\u00e1t log in ra, c\u00e1c b\u1ea1n s\u1ebd th\u1ea5y:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Epoch 00096: val_accuracy did not improve from 0.74675\nEpoch 97\/100\n614\/614 &#91;==============================] - 0s 112us\/step - loss: 0.4925 - accuracy: 0.7655 - val_loss: 0.6825 - val_accuracy: 0.6948\n\nEpoch 00097: val_accuracy did not improve from 0.74675\nEpoch 98\/100\n614\/614 &#91;==============================] - 0s 107us\/step - loss: 0.5555 - accuracy: 0.7508 - val_loss: 0.8247 - val_accuracy: 0.6558\n\nEpoch 00098: val_accuracy did not improve from 0.74675\nEpoch 99\/100\n614\/614 &#91;==============================] - 0s 112us\/step - loss: 0.5101 - accuracy: 0.7573 - val_loss: 0.7197 - val_accuracy: 0.6948\n\nEpoch 00099: val_accuracy did not improve from 0.74675\nEpoch 100\/100\n614\/614 &#91;==============================] - 0s 110us\/step - loss: 0.4815 - accuracy: 0.7687 - val_loss: 0.6195 - val_accuracy: 0.7273\n\nEpoch 00100: val_accuracy did not improve from 0.74675\n<\/code><\/pre>\n\n\n\n<p>Nh\u01b0 v\u1eady val_accuracy t\u1ed1t nh\u1ea5t l\u00e0 0.74675 v\u00e0 \u0111\u00e3 \u0111\u01b0\u1ee3c l\u01b0u l\u1ea1i! Ch\u00fang ta c\u0169ng c\u00f3 th\u1ec3 nh\u00e9t th\u00eam c\u00e1c th\u00f4ng s\u1ed1 nh\u01b0: epoch, gi\u00e1 tr\u1ecb loss, accuracy trong t\u00ean file check point nh\u00e9. V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>filepath=\"checkpoint-{epoch}-{val_accuracy}.hdf5\"<\/code><\/pre>\n\n\n\n<p>R\u1ed3i, nh\u01b0 v\u1eady c\u00f3 th\u1ec3 train 100 hay 1000 epochs \u0111i ch\u0103ng n\u1eefa th\u00ec weights t\u1ed1t nh\u1ea5t lu\u00f4n \u0111\u01b0\u1ee3c l\u01b0u l\u1ea1i cho ch\u00fang ta! Y\u00ean t\u00e2m nh\u00e9!<\/p>\n\n\n\n<ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-5095883280136027\" data-ad-slot=\"7735063137\" data-ad-format=\"auto\" data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Ph\u1ea7n 4 &#8211; LearningRateScheduler keras callbacks<\/h2>\n\n\n\n<p>C\u00e1i m\u00f3n keras callbacks n\u00e0y th\u00ec \u0111\u01a1n gi\u1ea3n l\u1eafm lu\u00f4n, n\u00f3 ch\u1ec9 c\u00f3 m\u1ed9t nhi\u1ec7m v\u1ee5 \u0111i\u1ec1u ch\u1ec9nh Learning Rate (LR) cho qu\u00e1 tr\u00ecnh train. B\u1ea1n n\u00e0o ch\u01b0a bi\u1ebft LR l\u00e0 g\u00ec th\u00ec <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Learning_rate\">\u0111\u1ecdc \u0111\u00e2y<\/a><\/strong> nh\u00e9!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.math.purdue.edu\/~nwinovic\/figures\/learning_rates.png\" alt=\"keras callbacks learning rate\"\/><figcaption>Ngu\u1ed3n: <a href=\"https:\/\/www.math.purdue.edu\/~nwinovic\/figures\/learning_rates.png\" target=\"_blank\" rel=\"noreferrer noopener\">T\u1ea1i \u0111\u00e2y<\/a><\/figcaption><\/figure>\n\n\n\n<p>C\u1ea5u tr\u00fac c\u1ee7a m\u00f3n n\u00e0y kh\u00e1 \u0111\u01a1n gi\u1ea3n:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tf.keras.callbacks.LearningRateScheduler(schedule, verbose=0)<\/code><\/pre>\n\n\n\n<p>Trong \u0111\u00f3 schedule l\u00e0 t\u00ean h\u00e0m s\u1ebd tr\u1ea3 v\u1ec1 LR.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 ch\u00ednh ch\u1ee7 c\u1ee7a Keras lu\u00f4n:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import tensorflow as tf\nimport numpy as np\ntf.enable_eager_execution()\n\n# \u0110\u1ecbnh ngh\u0129a h\u00e0m tr\u1ea3 v\u1ec1 LR\ndef scheduler(epoch, lr):\n    # N\u1ebfu d\u01b0\u1edbi 5 epoch\n    if epoch &lt; 5:\n        # Tr\u1ea3 v\u1ec1 lr\n        return float(lr)\n    else:\n        # C\u00f2n kh\u00f4ng th\u00ec tr\u1ea3 v\u1ec1\n        return float(lr * tf.math.exp(-0.1))\n\n\n# \u0110\u1ecbnh ngh\u0129a model\nmodel = tf.keras.models.Sequential(&#91;tf.keras.layers.Dense(10)])\nmodel.compile(tf.keras.optimizers.SGD(), loss='mse')\n\nprint(\"Learning rate ban \u0111\u1ea7u = \", round(model.optimizer.lr.numpy(), 5))\n\n# Train model v\u00e0 xem learning rate\ncallback = tf.keras.callbacks.LearningRateScheduler(scheduler)\nX = np.arange(100).reshape(5, 20).astype(float)\ny = np.zeros(5)\nhistory = model.fit( X, y , epochs=8, callbacks=&#91;callback], verbose=1)\n\nprint(\"Learning rate sau khi train xong 8 epochs = \", round(model.optimizer.lr.numpy(), 5))\n<\/code><\/pre>\n\n\n\n<p>V\u00e0 k\u1ebft qu\u1ea3 \u0111\u00e2y:<\/p>\n\n\n\n<ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-5095883280136027\" data-ad-slot=\"7735063137\" data-ad-format=\"auto\" data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<pre class=\"wp-block-code\"><code>Learning rate ban \u0111\u1ea7u =  0.01\nTrain on 5 samples\nEpoch 1\/8\n5\/5 &#91;==============================] - 1s 186ms\/sample - loss: 3805.0991\nEpoch 2\/8\n5\/5 &#91;==============================] - 0s 208us\/sample - loss: 64118804.0000\nEpoch 3\/8\n5\/5 &#91;==============================] - 0s 243us\/sample - loss: 1083885420544.0000\nEpoch 4\/8\n5\/5 &#91;==============================] - 0s 197us\/sample - loss: 18322360549507072.0000\nEpoch 5\/8\n5\/5 &#91;==============================] - 0s 188us\/sample - loss: 309727253441802141696.0000\nEpoch 6\/8\n5\/5 &#91;==============================] - 0s 175us\/sample - loss: 5235732452911954394087424.0000\nEpoch 7\/8\n5\/5 &#91;==============================] - 0s 211us\/sample - loss: 72345870604726808312717246464.0000\nEpoch 8\/8\n5\/5 &#91;==============================] - 0s 215us\/sample - loss: 816984177027815951967181258883072.0000\nLearning rate sau khi train xong 8 epochs =  0.00741\n<\/code><\/pre>\n\n\n\n<p>\u0110\u00f3, ch\u00fang ta th\u1ea5y r\u00f5 s\u1ef1 thay \u0111\u1ed5i c\u1ee7a LR qua qu\u00e1 tr\u00ecnh train. \u00dd \u0111\u1ed1 \u1edf \u0111\u00e2y l\u00e0 l\u00fac \u0111\u1ea7u m\u1edbi train th\u00ec ta c\u1ea7n LR l\u1edbn \u0111\u1ec3 nhanh ch\u00f3ng t\u00ecm \u0111\u1ebfn \u0111i\u1ec3m c\u1ef1c ti\u1ec3u, nh\u01b0ng khi \u0111\u00e3 g\u1ea7n \u0111i\u1ec3m c\u1ef1c ti\u1ec3u r\u1ed3i th\u00ec ta c\u1ea7n gi\u1ea3m LR \u0111\u1ec3 nhanh ch\u00f3ng d\u1eebng l\u1ea1i tr\u00e1nh &#8220;\u0111i qu\u00e1&#8221; \u0111i\u1ec3m c\u1ef1c ti\u1ec3u c\u1ea7n t\u00ecm.<\/p>\n\n\n\n<p>R\u1ed3i v\u1eady l\u00e0 m\u00ecnh \u0111\u00e3 c\u00f9ng c\u00e1c b\u1ea1n \u0111i qua v\u00e0i keras callbacks function th\u00f4ng d\u1ee5ng trong train model. C\u00e1c b\u1ea1n h\u00e3y v\u1eadn d\u1ee5ng v\u00e0o th\u1ef1c t\u1ebf \u0111\u1ec3 train model t\u1ed1t h\u01a1n v\u00e0 nhanh h\u01a1n nh\u00e9.<\/p>\n\n\n\n<p>C\u00e1c b\u1ea1n c\u00f3 th\u1ec3 tham kh\u1ea3o source full t\u1ea1i \u0111\u00e2y: <strong><a href=\"https:\/\/github.com\/thangnch\/MiAI_Keras_Callback\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/thangnch\/MiAI_Keras_Callback<\/a><\/strong><\/p>\n\n\n\n<p><strong>Ch\u00fac c\u00e1c b\u1ea1n th\u00e0nh c\u00f4ng!<\/strong><\/p>\n\n\n\n<ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"ca-pub-5095883280136027\" data-ad-slot=\"7735063137\" data-ad-format=\"auto\" data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p>#M\u00ecAI<\/p>\n\n\n\n<p>Fanpage:&nbsp;<a href=\"http:\/\/facebook.com\/miaiblog\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>http:\/\/facebook.com\/miaiblog<\/strong><\/a><br>Group trao \u0111\u1ed5i, chia s\u1ebb:&nbsp;<a href=\"https:\/\/www.facebook.com\/groups\/miaigroup\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>https:\/\/www.facebook.com\/groups\/miaigroup<\/strong><\/a><br>Website:&nbsp;<a href=\"http:\/\/miai.vn\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>https:\/\/miai.vn\/<\/strong><\/a><br>Youtube:&nbsp;<a href=\"http:\/\/bit.ly\/miaiyoutube\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>http:\/\/bit.ly\/miaiyoutube<\/strong><\/a><\/p>\n\n\n\n<p>B\u00e0i vi\u1ebft tham kh\u1ea3o: <a href=\"https:\/\/machinelearningmastery.com\/check-point-deep-learning-models-keras\/\" target=\"_blank\" rel=\"noreferrer noopener\">T\u1ea1i \u0111\u00e2y<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello tu\u1ea7n m\u1edbi anh em M\u00ec AI. H\u00f4m nay ch\u00fang ta c\u00f9ng nhau \u0111i t\u00ecm hi\u1ec3u Keras Callbacks &#8211; tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models nh\u00e9. Tr\u01b0\u1edbc khi b\u1eaft \u0111\u1ea7u m\u00ecnh c\u0169ng xin n\u00f3i tr\u01b0\u1edbc l\u00e0 \u0111\u00e2y l\u00e0 series d\u00e0nh cho c\u00e1c b\u1ea1n newbie m\u1edbi b\u1ea5t \u0111\u1ea7u th\u00f4i. C\u00f2n v\u1edbi c\u00e1c b\u1ea1n l\u00e0m l\u00e2u [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[400,401,402,243,403,404,405,406,407,408],"class_list":["post-1165","post","type-post","status-publish","format-standard","hentry","category-basic","tag-callbacks","tag-early-stop","tag-earlystopping","tag-keras","tag-keras-callbacks","tag-keras-model","tag-learning-rate","tag-learning-rate-scheduler","tag-model-checkpoint","tag-modelcheckpoint"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models - M\u00ec AI<\/title>\n<meta name=\"description\" content=\"Hello tu\u1ea7n m\u1edbi anh em M\u00ec AI. H\u00f4m nay ch\u00fang ta c\u00f9ng nhau \u0111i t\u00ecm hi\u1ec3u Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models nh\u00e9.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/miai.vn\/?p=1165\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models - M\u00ec AI\" \/>\n<meta property=\"og:description\" content=\"Hello tu\u1ea7n m\u1edbi anh em M\u00ec AI. H\u00f4m nay ch\u00fang ta c\u00f9ng nhau \u0111i t\u00ecm hi\u1ec3u Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models nh\u00e9.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/miai.vn\/?p=1165\" \/>\n<meta property=\"og:site_name\" content=\"M\u00ec AI\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-05T03:11:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/miro.medium.com\/max\/567\/1*6Z8TAefRMxXUNYDILTKOtg.png\" \/>\n<meta name=\"author\" content=\"Ch\u1ee7 ti\u1ec7m M\u00ec\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ch\u1ee7 ti\u1ec7m M\u00ec\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1165#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1165\"},\"author\":{\"name\":\"Ch\u1ee7 ti\u1ec7m M\u00ec\",\"@id\":\"https:\\\/\\\/miai.vn\\\/#\\\/schema\\\/person\\\/cc8bc24bb90bd3f596add82f3a59948c\"},\"headline\":\"Keras Callbacks &#8211; tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models\",\"datePublished\":\"2020-09-05T03:11:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1165\"},\"wordCount\":2119,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1165#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/miro.medium.com\\\/max\\\/567\\\/1*6Z8TAefRMxXUNYDILTKOtg.png\",\"keywords\":[\"callbacks\",\"early stop\",\"earlystopping\",\"keras\",\"keras callbacks\",\"keras model\",\"learning rate\",\"learning rate scheduler\",\"model checkpoint\",\"modelcheckpoint\"],\"articleSection\":[\"Basic\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/miai.vn\\\/?p=1165#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1165\",\"url\":\"https:\\\/\\\/miai.vn\\\/?p=1165\",\"name\":\"Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models - M\u00ec AI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1165#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1165#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/miro.medium.com\\\/max\\\/567\\\/1*6Z8TAefRMxXUNYDILTKOtg.png\",\"datePublished\":\"2020-09-05T03:11:44+00:00\",\"description\":\"Hello tu\u1ea7n m\u1edbi anh em M\u00ec AI. H\u00f4m nay ch\u00fang ta c\u00f9ng nhau \u0111i t\u00ecm hi\u1ec3u Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models nh\u00e9.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1165#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/miai.vn\\\/?p=1165\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1165#primaryimage\",\"url\":\"https:\\\/\\\/miro.medium.com\\\/max\\\/567\\\/1*6Z8TAefRMxXUNYDILTKOtg.png\",\"contentUrl\":\"https:\\\/\\\/miro.medium.com\\\/max\\\/567\\\/1*6Z8TAefRMxXUNYDILTKOtg.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1165#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/miai.vn\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Keras Callbacks &#8211; tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/miai.vn\\\/#website\",\"url\":\"https:\\\/\\\/miai.vn\\\/\",\"name\":\"M\u00ec AI\",\"description\":\"H\u1ecdc AI theo c\u00e1ch M\u00ec \u0103n li\u1ec1n!\",\"publisher\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/miai.vn\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/miai.vn\\\/#organization\",\"name\":\"M\u00ec AI\",\"url\":\"https:\\\/\\\/miai.vn\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/miai.vn\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/miai.vn\\\/wp-content\\\/uploads\\\/2026\\\/05\\\/cropped-Logo_w_slogan.png\",\"contentUrl\":\"https:\\\/\\\/miai.vn\\\/wp-content\\\/uploads\\\/2026\\\/05\\\/cropped-Logo_w_slogan.png\",\"width\":240,\"height\":193,\"caption\":\"M\u00ec AI\"},\"image\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/miai.vn\\\/#\\\/schema\\\/person\\\/cc8bc24bb90bd3f596add82f3a59948c\",\"name\":\"Ch\u1ee7 ti\u1ec7m M\u00ec\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0b5124b0d2c0a8054d7127d2c236bdc3dc7a50e2d4e8728ab32eee5b122a8d1?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0b5124b0d2c0a8054d7127d2c236bdc3dc7a50e2d4e8728ab32eee5b122a8d1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b0b5124b0d2c0a8054d7127d2c236bdc3dc7a50e2d4e8728ab32eee5b122a8d1?s=96&d=mm&r=g\",\"caption\":\"Ch\u1ee7 ti\u1ec7m M\u00ec\"},\"sameAs\":[\"https:\\\/\\\/miai.vn\"],\"url\":\"https:\\\/\\\/miai.vn\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models - M\u00ec AI","description":"Hello tu\u1ea7n m\u1edbi anh em M\u00ec AI. H\u00f4m nay ch\u00fang ta c\u00f9ng nhau \u0111i t\u00ecm hi\u1ec3u Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models nh\u00e9.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/miai.vn\/?p=1165","og_locale":"en_US","og_type":"article","og_title":"Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models - M\u00ec AI","og_description":"Hello tu\u1ea7n m\u1edbi anh em M\u00ec AI. H\u00f4m nay ch\u00fang ta c\u00f9ng nhau \u0111i t\u00ecm hi\u1ec3u Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models nh\u00e9.","og_url":"https:\/\/miai.vn\/?p=1165","og_site_name":"M\u00ec AI","article_published_time":"2020-09-05T03:11:44+00:00","og_image":[{"url":"https:\/\/miro.medium.com\/max\/567\/1*6Z8TAefRMxXUNYDILTKOtg.png","type":"","width":"","height":""}],"author":"Ch\u1ee7 ti\u1ec7m M\u00ec","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Ch\u1ee7 ti\u1ec7m M\u00ec","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/miai.vn\/?p=1165#article","isPartOf":{"@id":"https:\/\/miai.vn\/?p=1165"},"author":{"name":"Ch\u1ee7 ti\u1ec7m M\u00ec","@id":"https:\/\/miai.vn\/#\/schema\/person\/cc8bc24bb90bd3f596add82f3a59948c"},"headline":"Keras Callbacks &#8211; tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models","datePublished":"2020-09-05T03:11:44+00:00","mainEntityOfPage":{"@id":"https:\/\/miai.vn\/?p=1165"},"wordCount":2119,"commentCount":5,"publisher":{"@id":"https:\/\/miai.vn\/#organization"},"image":{"@id":"https:\/\/miai.vn\/?p=1165#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/max\/567\/1*6Z8TAefRMxXUNYDILTKOtg.png","keywords":["callbacks","early stop","earlystopping","keras","keras callbacks","keras model","learning rate","learning rate scheduler","model checkpoint","modelcheckpoint"],"articleSection":["Basic"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/miai.vn\/?p=1165#respond"]}]},{"@type":"WebPage","@id":"https:\/\/miai.vn\/?p=1165","url":"https:\/\/miai.vn\/?p=1165","name":"Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models - M\u00ec AI","isPartOf":{"@id":"https:\/\/miai.vn\/#website"},"primaryImageOfPage":{"@id":"https:\/\/miai.vn\/?p=1165#primaryimage"},"image":{"@id":"https:\/\/miai.vn\/?p=1165#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/max\/567\/1*6Z8TAefRMxXUNYDILTKOtg.png","datePublished":"2020-09-05T03:11:44+00:00","description":"Hello tu\u1ea7n m\u1edbi anh em M\u00ec AI. H\u00f4m nay ch\u00fang ta c\u00f9ng nhau \u0111i t\u00ecm hi\u1ec3u Keras Callbacks - tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models nh\u00e9.","breadcrumb":{"@id":"https:\/\/miai.vn\/?p=1165#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/miai.vn\/?p=1165"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/miai.vn\/?p=1165#primaryimage","url":"https:\/\/miro.medium.com\/max\/567\/1*6Z8TAefRMxXUNYDILTKOtg.png","contentUrl":"https:\/\/miro.medium.com\/max\/567\/1*6Z8TAefRMxXUNYDILTKOtg.png"},{"@type":"BreadcrumbList","@id":"https:\/\/miai.vn\/?p=1165#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/miai.vn\/"},{"@type":"ListItem","position":2,"name":"Keras Callbacks &#8211; tr\u1ee3 th\u1ee7 \u0111\u1eafc l\u1ef1c khi train models"}]},{"@type":"WebSite","@id":"https:\/\/miai.vn\/#website","url":"https:\/\/miai.vn\/","name":"M\u00ec AI","description":"H\u1ecdc AI theo c\u00e1ch M\u00ec \u0103n li\u1ec1n!","publisher":{"@id":"https:\/\/miai.vn\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/miai.vn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/miai.vn\/#organization","name":"M\u00ec AI","url":"https:\/\/miai.vn\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/miai.vn\/#\/schema\/logo\/image\/","url":"https:\/\/miai.vn\/wp-content\/uploads\/2026\/05\/cropped-Logo_w_slogan.png","contentUrl":"https:\/\/miai.vn\/wp-content\/uploads\/2026\/05\/cropped-Logo_w_slogan.png","width":240,"height":193,"caption":"M\u00ec AI"},"image":{"@id":"https:\/\/miai.vn\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/miai.vn\/#\/schema\/person\/cc8bc24bb90bd3f596add82f3a59948c","name":"Ch\u1ee7 ti\u1ec7m M\u00ec","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b0b5124b0d2c0a8054d7127d2c236bdc3dc7a50e2d4e8728ab32eee5b122a8d1?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b0b5124b0d2c0a8054d7127d2c236bdc3dc7a50e2d4e8728ab32eee5b122a8d1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b0b5124b0d2c0a8054d7127d2c236bdc3dc7a50e2d4e8728ab32eee5b122a8d1?s=96&d=mm&r=g","caption":"Ch\u1ee7 ti\u1ec7m M\u00ec"},"sameAs":["https:\/\/miai.vn"],"url":"https:\/\/miai.vn\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/miai.vn\/index.php?rest_route=\/wp\/v2\/posts\/1165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/miai.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/miai.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/miai.vn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/miai.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1165"}],"version-history":[{"count":0,"href":"https:\/\/miai.vn\/index.php?rest_route=\/wp\/v2\/posts\/1165\/revisions"}],"wp:attachment":[{"href":"https:\/\/miai.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/miai.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/miai.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}