{"id":1192,"date":"2020-10-12T04:27:55","date_gmt":"2020-10-11T21:27:55","guid":{"rendered":"http:\/\/www.miai.vn\/?p=1192"},"modified":"2020-10-12T04:27:55","modified_gmt":"2020-10-11T21:27:55","slug":"custom-loss-la-cai-giong-gi","status":"publish","type":"post","link":"https:\/\/miai.vn\/?p=1192","title":{"rendered":"Custom Loss l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?"},"content":{"rendered":"\n<p> Ch\u00e0o c\u00e1c anh em M\u00ec AI, h\u00f4m nay ch\u00fang ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 Custom Loss &#8211; h\u00e0m loss t\u1ef1 t\u1ea1o &#8211; trong ML, DL v\u1edbi Keras nh\u00e9.<\/p>\n\n\n\n<p>L\u00e2u l\u1eafm r\u1ed3i m\u1edbi vi\u1ebft b\u00e0i cho anh em \u0111\u00e2y. D\u1ea1o n\u00e0y b\u1eadn n\u00ean tranh th\u1ee7 \u0111\u01b0\u1ee3c th\u1eddi gian l\u00e0 quay video cho nhanh kaka.<\/p>\n\n\n\n<p>Tr\u01b0\u1edbc khi b\u1eaft \u0111\u1ea7u b\u00e0i n\u00e0y m\u00ecnh c\u0169ng xin n\u00f3i lu\u00f4n l\u00e0 m\u00ecnh gi\u1ea3i th\u00edch ho\u00e0n to\u00e0n b\u1eb1ng kinh nghi\u1ec7m, b\u1eb1ng ki\u1ec3u M\u00ec AI cho d\u1ec5 hi\u1ec3u cho c\u00e1c b\u1ea1n newbie. Mong c\u00e1c cao th\u1ee7 \u0111i qua gi\u00fap \u0111\u1ee1 th\u00eam ah.<\/p>\n\n\n\n<p>Okie, let&#8217;s go!<\/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 1 &#8211; Custom Loss l\u00e0 g\u00ec?<\/h2>\n\n\n\n<p>Tr\u01b0\u1edbc khi \u0111i s\u00e2u v\u00e0o t\u00ecm hi\u1ec3u Custom Loss code ra sao? Tri\u1ec3n khai nh\u01b0 n\u00e0o th\u00ec ch\u00fang ta s\u1ebd \u0111i t\u00ecm hi\u1ec3u xem custom loss l\u00e0 g\u00ec?<\/p>\n\n\n\n<p>Custom Loss d\u1ecbch n\u00f4m na l\u00e0 h\u00e0m loss \u0111\u01b0\u1ee3c custome, haha! \u0110\u00f9a ch\u00fat cho vui th\u00f4i, t\u00f3m l\u1ea1i l\u00e0 anh em c\u00f3 th\u1ec3 vi\u1ebft h\u00e0m loss ri\u00eang ho\u1eb7c ch\u1ec9nh s\u1eeda l\u1ea1i h\u00e0m loss s\u1eb5n c\u00f3 cho ph\u00f9 h\u1ee3p v\u1edbi nhu c\u1ea7u \u0111\u1eb7c bi\u1ec7t n\u00e0o \u0111\u00f3 c\u1ee7a anh em.<\/p>\n\n\n\n<p>N\u1ebfu anh em c\u1ea7n t\u00ecm hi\u1ec3m xem Loss l\u00e0 g\u00ec th\u00ec m\u00ecnh \u0111\u00e3 c\u00f3 m\u1ed9t b\u00e0i v\u1ec1 m\u00f3n \u0111\u00f3 r\u1ed3i, anh em \u0111\u1ecdc l\u1ea1i t\u1ea1i link n\u00e0y nh\u00e9: <strong><a href=\"https:\/\/www.miai.vn\/2020\/06\/12\/oanh-gia-model-ai-theo-cach-mi-an-lien-chuong-1-loss-va-accuracy\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.miai.vn\/2020\/06\/12\/oanh-gia-model-ai-theo-cach-mi-an-lien-chuong-1-loss-va-accuracy\/<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ph\u1ea7n 2 &#8211; T\u1ea1i sao ph\u1ea3i custom c\u00e1i h\u00e0m loss cho m\u1ec7t?<\/h2>\n\n\n\n<p>\u0110\u00fang! T\u1ed9i g\u00ec ph\u1ec9a custom loss l\u00e0m g\u00ec cho m\u1ec7t nh\u1ec9? Haha! Tr\u01b0\u1edbc m\u00ecnh c\u0169ng ngh\u0129 nh\u01b0 th\u1ebf cho \u0111\u1ebfn m\u1ed9t ng\u00e0y &#8230;.. nh\u1eadn ra n\u00f3 kh\u00f4ng th\u1ebf \ud83d\ude00<\/p>\n\n\n\n<p>B\u00e2y gi\u1edd \u0111\u1ec3 c\u00e1c b\u1ea1n hi\u1ec3u l\u00fd do ph\u1ea3i Custom Loss th\u00ec ch\u00fang ta c\u00f9ng x\u00e9t qua v\u00e0i v\u00ed d\u1ee5 sau.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5<\/strong> <strong>01<\/strong> <\/p>\n\n\n\n<p>Ch\u00fang ta l\u00e0m m\u1ed9t b\u00e0i to\u00e1n v\u1edbi input X v\u00e0 vector output predict label yhat v\u00e0 vector true label y  c\u1ee7a ch\u00fang ta l\u00e0 m\u1ed9t vector c\u00f3 \u0111\u1ed9 d\u00e0i n. B\u00e2y gi\u1edd khi ch\u00fang ta s\u1eed d\u1ee5ng loss m\u1eb7c \u0111\u1ecbnh (v\u00ed d\u1ee5 Mean Square Error, RMSE hay Cross Entropy&#8230;) th\u00ec h\u00e0m loss s\u1ebd t\u00ednh to\u00e1n \u0111\u1ea7y \u0111\u1ee7 tr\u00ean n ph\u1ea7n t\u1eed c\u1ee7a vector yhat v\u00e0 y. Tuy nhi\u00ean trong m\u1ed9t s\u1ed1 tr\u01b0\u1eddng h\u1ee3p n\u00e0o \u0111\u00f3, v\u00ed d\u1ee5 b\u1eb1ng Data Knowledge c\u1ee7a b\u1ea1n, b\u1ea1n th\u1ea5y r\u1eb1ng n\u00ean lo\u1ea1i 02 ph\u1ea7n t\u1eed \u0111\u1ea7u c\u1ee7a 2 vector yhat v\u00e0 y ra kh\u1ecfi qu\u00e1 tr\u00ecnh t\u00ednh to\u00e1n loss v\u00ec n\u00f3 kh\u00f4ng c\u00f3 \u00fd ngh\u0129a ch\u1eb3ng h\u1ea1n. \u0110\u00f3 l\u00e0 l\u00fac b\u1ea1n ph\u1ea3i custom l\u1ea1i h\u00e0m loss c\u1ee7a b\u1ea1n \u0111\u00f3.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/github.com\/thangnch\/photos\/blob\/master\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true\" alt=\"custom loss\"\/><\/figure>\n\n\n\n<p><strong>V\u00ed d\u1ee5 02. B\u00e0i to\u00e1n Coin Segmentation<\/strong><\/p>\n\n\n\n<p>Gi\u1ea3 s\u1eed gi\u1edd ch\u00fang ta l\u00e0m b\u00e0i to\u00e1n Coin Segmentation &#8211; ph\u00e2n t\u00e1ch c\u00e1c \u0111\u1ed3ng xu nh\u00e9. V\u00e0 gi\u1ea3 s\u1eed input v\u00e0 output c\u1ee7a ch\u00fang ta \u0111\u01b0\u1ee3c y\u00eau c\u1ea7u nh\u01b0 sau:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Input: H\u00ecnh \u1ea3nh c\u00e1c \u0111\u1ed3ng xu<\/li><li>Output: Ph\u00e2n lo\u1ea1i c\u00e1c \u0111i\u1ec3m \u1ea3nh trong input th\u00e0nh 0 &#8211; V\u00f9ng \u1ea3nh n\u1ec1n (m\u00e0u tr\u1eafng), 1 &#8211; V\u00f9ng thu\u1ed9c \u0111\u1ed3ng xu (m\u00e0u v\u00e0ng) v\u00e0 2 &#8211; V\u00f9ng bi\u00ean gi\u1eefa c\u00e1c \u0111\u1ed3ng xu (m\u00e0u xanh).<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/github.com\/thangnch\/photos\/blob\/master\/Screen%20Shot%202020-10-12%20at%2011.05.36.png?raw=true\" alt=\"custom loss in coin segmentation\"\/><\/figure>\n\n\n\n<p>V\u1edbi b\u00e0i to\u00e1n n\u00e0y c\u00f3 nhi\u1ec1u c\u00e1ch l\u00e0m l\u1eafm, nh\u01b0ng m\u00ecnh gi\u1ea3 s\u1eed b\u1ecb \u00e9p l\u00e0m theo c\u00e1ch tr\u00ean \u0111\u1ec3 demo custom loss \u1ea5y m\u00e0 :D. C\u00e1c b\u1ea1n ch\u00fa \u00fd nh\u00e9!<\/p>\n\n\n\n<p>R\u1ed3i, \u0111\u00e2y l\u00e0 m\u1ed9t b\u00e0i to\u00e1n classify th\u00f4ng th\u01b0\u1eddng v\u00e0 gi\u1ea3 s\u1eed loss c\u1ee7a n\u00f3 l\u00e0 $L$, trong \u0111\u00f3:<\/p>\n\n\n\n<p>$$L=(L_{coin} + L_{no coin} + L_{border})$$<\/p>\n\n\n\n<p>B\u00e2y gi\u1edd nh\u00ecn v\u00e0o h\u00ecnh th\u00ec ta s\u1ebd th\u1ea5y \u0111a ph\u1ea7n c\u00e1c pixel trong h\u00ecnh l\u00e0 coin ho\u1eb7c no_coin. \u0110i\u1ec1u \u0111\u00f3 d\u1eabn \u0111\u1ebfn m\u00f4 h\u00ecnh c\u1ee7a ch\u00fang ta s\u1ebd thi\u00ean v\u1ec1 d\u1ef1 \u0111o\u00e1n c\u00e1c pixel v\u00e0o 1 trong 2 l\u1edbp \u0111\u00f3 d\u1eabn \u0111\u1ebfn v\u00f9ng border s\u1ebd \u00edt , c\u00e1c \u0111\u1ed3ng xu s\u1ebd b\u1ecb lem v\u00e0o nhau, kh\u00f4ng \u0111\u00e1p \u1ee9ng \u0111\u01b0\u1ee3c nhu c\u1ea7u c\u1ee7a b\u00e0i to\u00e1n.<\/p>\n\n\n\n<p>V\u00e0 \u0111\u00e2y l\u00e0 l\u00fac ch\u00fang ta c\u1ea7n custom b\u00e0i to\u00e1n \u0111\u00e2y. Ch\u00fang ta s\u1ebd th\u00eam v\u00e0o 2 tham s\u1ed1 $\\alpha$ v\u00e0 $\\beta$ (\u0111\u1ec1u l\u00e0 s\u1ed1 \u0111\u1ee7 nh\u1ecf v\u00e0 &lt; 1) v\u00e0o $L_{coin}$ v\u00e0 $L_{no_coin}$ \u0111\u1ec3 tr\u1eebng ph\u1ea1t m\u1ea1nh h\u01a1n vi\u1ec7c nh\u1eadn di\u1ec7n sai border. V\u00e0 ta c\u00f3 h\u00e0m loss m\u1edbi: <\/p>\n\n\n\n<p>$$L=(\\alpha*L_{coin} + \\beta*L_{no coin} + L_{border})$$<\/p>\n\n\n\n<p>Hi v\u1ecdng c\u00e1c b\u1ea1n \u0111\u00e3 hi\u1ec3u qua qua l\u00fd do c\u1ea7n ph\u1ea3i custom loss, b\u00e2y gi\u1edd ch\u00fang ta s\u1ebd xem th\u1eed code th\u1ef1c t\u1ebf b\u1eb1ng Python cho d\u1ec5 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 3 &#8211; Code v\u1edbi Python, Keras <\/h2>\n\n\n\n<p>V\u1edbi h\u00e0m loss m\u1eb7c \u0111\u1ecbnh th\u00ec ch\u00fang ta c\u1ee9 l\u00e0m m\u1ed7i vi\u1ec7c g\u1ecdi t\u00ean h\u00e0m loss ra th\u00f4i. V\u00ed d\u1ee5 v\u1edbi b\u00e0i to\u00e1n <a href=\"https:\/\/www.miai.vn\/2020\/04\/21\/nhan-dang-tien-viet-nam-voi-transfer-learning-vgg16-cnn-classify\/\" target=\"_blank\" rel=\"noreferrer noopener\">Nh\u1eadn d\u1ea1ng ti\u1ec1n Vi\u1ec7t Nam v\u1edbi Transfer Learning (VGG16 CNN Classify)<\/a> m\u00ecnh v\u1eeba l\u00e0m tr\u00ean M\u00ec AI \u0111i nh\u00e9. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Them cac layer FC va Dropout\n    x = Flatten(name='flatten')(output_vgg16_conv)\n    x = Dense(4096, activation='relu', name='fc1')(x)\n    x = Dropout(0.5)(x)\n    x = Dense(4096, activation='relu', name='fc2')(x)\n    x = Dropout(0.5)(x)\n    x = Dense(4, activation='softmax', name='predictions')(x)\n\n    # Compile\n    my_model = Model(input=input, output=x)\n    my_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=&#91;'accuracy'])<\/code><\/pre>\n\n\n\n<p>\u0110\u00f3 c\u00e1c b\u1ea1n \u0111\u1ec3 \u00fd \u0111o\u1ea1n loss=&#8217;categorical_crossentropy&#8217;, \u0111\u00e2y l\u00e0 h\u00e0m loss crossentropy m\u1eb7c \u0111\u1ecbnh.<\/p>\n\n\n\n<p>H\u00e0m loss n\u00e0y th\u00ec \u0111\u00e3 c\u00f3 nhi\u1ec1u b\u00e0i vi\u1ebft v\u1ec1 n\u00f3 tr\u00ean google, n\u1ebfu b\u1ea1n c\u1ea7n th\u00eam tr\u1ee3 gi\u00fap h\u00e3y post l\u00ean Group trao \u0111\u1ed5i, chia s\u1ebb: <strong><a href=\"https:\/\/facebook.com\/groups\/miaigroup\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/facebook.com\/groups\/miaigroup<\/a><\/strong> \u0111\u1ec3 c\u00f9ng th\u1ea3o lu\u1eadn nh\u00e9!<\/p>\n\n\n\n<p>R\u1ed3i \u0111\u00f3 l\u00e0 v\u00ed d\u1ee5 d\u00f9ng loss m\u1eb7c \u0111\u1ecbnh, gi\u1edd ta th\u1eed code m\u1ed9t b\u00e0i to\u00e1n kh\u00e1c, s\u1ebd d\u00f9ng loss custom nh\u00e9.<\/p>\n\n\n\n<p>Gi\u1ea3 s\u1eeda ch\u00fang ta \u0111ang l\u00e0m b\u00e0i to\u00e1n d\u1ef1 \u0111o\u00e1n h\u1ed3i quy Regression v\u1ec1 nhi\u1ec7t \u0111\u1ed9, \u0111\u1ed5 \u1ea9m v\u00e0 \u00e1p su\u1ea5t c\u1ee7a ph\u00f2ng th\u00ed nghi\u1ec7m d\u1ef1a v\u00e0o 10 gi\u00e1 tr\u1ecb sensor \u0111\u1ea7u v\u00e0o. \u0110i\u1ec1u \u0111\u00f3 c\u00f3 ngh\u0129a l\u00e0 vector input s\u1ebd l\u00e0 m\u1ed9t vector <strong>$x$<\/strong> c\u00f3 \u0111\u1ed9 d\u00e0i 10 v\u00e0 output s\u1ebd l\u00e0 vector <strong>$y$<\/strong> c\u00f3 \u0111\u1ed9 d\u00e0i l\u00e0 3 g\u1ed3m <strong>$y_{1}$<\/strong> &#8211; nhi\u1ec7t \u0111\u1ed9, <strong>$y_{2}$<\/strong> &#8211; \u0111\u1ed9 \u1ea9m v\u00e0 <strong>$y_{3}$<\/strong> &#8211; \u00e1p su\u1ea5t.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/github.com\/thangnch\/photos\/blob\/master\/Screen%20Shot%202020-10-12%20at%2010.53.13.png?raw=true\" alt=\"custom loss\"\/><\/figure>\n\n\n\n<p>V\u00e0 nh\u01b0 bao b\u00e0i to\u00e1n Regression kh\u00e1c, ch\u00fang ta th\u1ef1c hi\u1ec7n x\u00e2y d\u1ef1ng m\u1ea1ng NN (v\u00ed d\u1ee5 th\u1ebf cho ho\u00e0nh) nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model = keras.models.Sequenial()\nmodel.add(Dense(128, input_shape=(10,), activation='relu'))\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dense(16, activation='relu'))\nmodel.add(Dense(3, activation='linear'))\nmodel.compile(loss=\"mse\", optimizer='adam')<\/code><\/pre>\n\n\n\n<p>C\u00e1c b\u1ea1n focus v\u00e0o m\u1ea5y \u0111i\u1ec3m sau:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Input l\u00e0 10, ch\u00ednh l\u00e0 \u0111\u1ed9 d\u00e0i c\u1ee7a vector $x$<\/li><li>Layer dense cu\u1ed1i c\u00f9ng c\u00f3 3 unit v\u00e0 h\u00e0m activation l\u00e0 linear th\u00f4ng th\u01b0\u1eddng v\u00ec \u0111\u00e2y l\u00e0 b\u00e0i to\u00e1n h\u1ed3i quy.<\/li><li>H\u00e0m loss \u0111ang d\u00f9ng MSE <\/li><\/ul>\n\n\n\n<p>Okie, v\u00e0 m\u1ecdi th\u1ee9 ngon l\u00e0nh c\u00e0nh \u0111\u00e0o th\u00f4i! Nh\u01b0ng model n\u00e0o th\u00ec c\u0169ng c\u00f3 sai s\u00f3t v\u00e0 model n\u00e0y c\u0169ng v\u1eady, \u0111\u00f4i khi n\u00f3 s\u1ebd d\u1ef1 \u0111o\u00e1n sai (l\u1ec7ch so v\u1edbi gi\u00e1 tr\u1ecb th\u1ef1c). V\u00e0 b\u1ea1n nh\u1eadn \u0111\u01b0\u1ee3c l\u1ec7nh, okie sai th\u00ec l\u00e0 t\u1ea5t nhi\u00ean nh\u01b0ng \u00f4ng c\u1ea7n \u0111\u1ea3m b\u1ea3o cho t\u00f4i l\u00e0 \u01b0u ti\u00ean d\u1ef1 \u0111o\u00e1n \u0111\u00fang nhi\u1ec7t \u0111\u1ed9 v\u00e0 \u0111\u1ed9 \u1ea9m, \u00e1p su\u1ea5t th\u00ec sai t\u00fd c\u0169ng \u0111\u01b0\u1ee3c. V\u00e0 gi\u1edd b\u1ea1n ph\u1ea3i l\u00e0m sao? <\/p>\n\n\n\n<p>C\u00e2u tr\u1ea3 l\u1eddi l\u00e0 b\u1ea1n c\u1ea7n custom h\u00e0m loss \u0111\u1ec3 ph\u1ea1t n\u1eb7ng h\u01a1n vi\u1ec7c nh\u1eadn sai nhi\u1ec7t \u0111\u1ed9 v\u00e0 \u0111\u1ed9 \u1ea9m, coi nh\u1eb9 h\u01a1n \u00f4ng \u00e1p su\u1ea5t. Let&#8217;s do it!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def custom_mse_loss(y_true, y_pred):\n    mse_loss = K.square(y_pred - y_true)  \n    mse_loss = loss * &#91;0.4, 0.4, 0.2]  \n    mse_loss = K.sum(loss, axis=1)  \n\n    return mse_loss<\/code><\/pre>\n\n\n\n<p>\u0110\u00f3 \u0111\u00f3, ch\u00fang ta t\u00ednh loss xong th\u00ec nh\u00e2n ngay v\u1edbi 1 c\u00e1i vector [0.4, 0.4,0.2] \u0111\u1ec3 \u0111\u00e1nh tr\u1ecdng s\u1ed1 cho c\u00e1i loss c\u1ee7a ch\u00fang ta, \u01b0u ti\u00ean $y_{1}$ &#8211; nhi\u1ec7t \u0111\u1ed9, $y_{2}$ &#8211; \u0111\u1ed9 \u1ea9m v\u00e0 coi nh\u1eb9 h\u01a1n t\u00fd $y_{3}$ &#8211; \u00e1p su\u1ea5t. Okie v\u00e0 b\u00e2y gi\u1edd l\u00e0 gh\u00e9p v\u00e0o code n\u00e0o!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model.compile(loss=\"custom_mse_loss\", optimizer='adam')<\/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>That&#8217;s all. Simple \u0111\u00fang kh\u00f4ng c\u00e1c b\u1ea1n?<\/p>\n\n\n\n<p>B\u00e0i n\u00e0y m\u00ecnh kh\u00f4ng c\u00f3 \u00fd \u0111\u1ecbnh cover t\u1ea5t c\u1ea3 c\u00e1c case c\u1ee7a Custom Loss m\u00e0 m\u00ecnh ch\u1ec9 mu\u1ed1n gi\u1edbi thi\u1ec7u cho c\u00e1c b\u1ea1n bi\u00eat r\u1eb1ng c\u00f3 kh\u00e1i ni\u1ec7m Custom Loss \u0111\u1ec3 c\u00e1c b\u1ea1n d\u00f9ng khi c\u1ea7n thi\u1ebft. Khi n\u00e0o b\u1ea1n c\u1ea7n t\u1ed1i \u01b0u ho\u00e1 model, fine tune, n\u00e2ng cao \u0111\u1ed9 ch\u00ednh x\u00e1c&#8230;. th\u00ec c\u00e1c b\u1ea1n s\u1ebd ngay l\u1eadp t\u1ee9c n\u1ea3y sinh nhu c\u1ea7u custom l\u1ea1i loss v\u00e0 h\u00e3y nh\u1edb t\u1edbi b\u00e0i n\u00e0y nh\u00e9.<\/p>\n\n\n\n<p>C\u00f2n b\u00e2y gi\u1edd, xin t\u1ea1m bi\u1ec7t nh\u00e9!<\/p>\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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ch\u00e0o c\u00e1c anh em M\u00ec AI, h\u00f4m nay ch\u00fang ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 Custom Loss &#8211; h\u00e0m loss t\u1ef1 t\u1ea1o &#8211; trong ML, DL v\u1edbi Keras nh\u00e9. L\u00e2u l\u1eafm r\u1ed3i m\u1edbi vi\u1ebft b\u00e0i cho anh em \u0111\u00e2y. D\u1ea1o n\u00e0y b\u1eadn n\u00ean tranh th\u1ee7 \u0111\u01b0\u1ee3c th\u1eddi gian l\u00e0 quay video cho nhanh kaka. Tr\u01b0\u1edbc [&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":[415,416,243,306,417,418,32],"class_list":["post-1192","post","type-post","status-publish","format-standard","hentry","category-basic","tag-custom-loss","tag-customize-loss","tag-keras","tag-loss","tag-model-custom-loss","tag-model-loss","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Custom Loss, l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?- M\u00ec AI<\/title>\n<meta name=\"description\" content=\"Ch\u00e0o tu\u1ea7n m\u1edbi c\u00e1c anh em M\u00ec AI, h\u00f4m nay ch\u00fang ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 Custom Loss - h\u00e0m loss t\u1ef1 t\u1ea1o - trong ML, DL v\u1edbi Keras 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=1192\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom Loss, l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?- M\u00ec AI\" \/>\n<meta property=\"og:description\" content=\"Ch\u00e0o tu\u1ea7n m\u1edbi c\u00e1c anh em M\u00ec AI, h\u00f4m nay ch\u00fang ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 Custom Loss - h\u00e0m loss t\u1ef1 t\u1ea1o - trong ML, DL v\u1edbi Keras nh\u00e9.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/miai.vn\/?p=1192\" \/>\n<meta property=\"og:site_name\" content=\"M\u00ec AI\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-11T21:27:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/github.com\/thangnch\/photos\/blob\/master\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true\" \/>\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=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1192#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1192\"},\"author\":{\"name\":\"Ch\u1ee7 ti\u1ec7m M\u00ec\",\"@id\":\"https:\\\/\\\/miai.vn\\\/#\\\/schema\\\/person\\\/cc8bc24bb90bd3f596add82f3a59948c\"},\"headline\":\"Custom Loss l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?\",\"datePublished\":\"2020-10-11T21:27:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1192\"},\"wordCount\":1676,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1192#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/github.com\\\/thangnch\\\/photos\\\/blob\\\/master\\\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true\",\"keywords\":[\"custom loss\",\"customize loss\",\"keras\",\"loss\",\"model custom loss\",\"model loss\",\"python\"],\"articleSection\":[\"Basic\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/miai.vn\\\/?p=1192#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1192\",\"url\":\"https:\\\/\\\/miai.vn\\\/?p=1192\",\"name\":\"Custom Loss, l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?- M\u00ec AI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1192#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1192#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/github.com\\\/thangnch\\\/photos\\\/blob\\\/master\\\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true\",\"datePublished\":\"2020-10-11T21:27:55+00:00\",\"description\":\"Ch\u00e0o tu\u1ea7n m\u1edbi c\u00e1c anh em M\u00ec AI, h\u00f4m nay ch\u00fang ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 Custom Loss - h\u00e0m loss t\u1ef1 t\u1ea1o - trong ML, DL v\u1edbi Keras nh\u00e9.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1192#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/miai.vn\\\/?p=1192\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1192#primaryimage\",\"url\":\"https:\\\/\\\/github.com\\\/thangnch\\\/photos\\\/blob\\\/master\\\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true\",\"contentUrl\":\"https:\\\/\\\/github.com\\\/thangnch\\\/photos\\\/blob\\\/master\\\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/miai.vn\\\/?p=1192#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/miai.vn\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Custom Loss l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?\"}]},{\"@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":"Custom Loss, l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?- M\u00ec AI","description":"Ch\u00e0o tu\u1ea7n m\u1edbi c\u00e1c anh em M\u00ec AI, h\u00f4m nay ch\u00fang ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 Custom Loss - h\u00e0m loss t\u1ef1 t\u1ea1o - trong ML, DL v\u1edbi Keras 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=1192","og_locale":"en_US","og_type":"article","og_title":"Custom Loss, l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?- M\u00ec AI","og_description":"Ch\u00e0o tu\u1ea7n m\u1edbi c\u00e1c anh em M\u00ec AI, h\u00f4m nay ch\u00fang ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 Custom Loss - h\u00e0m loss t\u1ef1 t\u1ea1o - trong ML, DL v\u1edbi Keras nh\u00e9.","og_url":"https:\/\/miai.vn\/?p=1192","og_site_name":"M\u00ec AI","article_published_time":"2020-10-11T21:27:55+00:00","og_image":[{"url":"https:\/\/github.com\/thangnch\/photos\/blob\/master\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true","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":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/miai.vn\/?p=1192#article","isPartOf":{"@id":"https:\/\/miai.vn\/?p=1192"},"author":{"name":"Ch\u1ee7 ti\u1ec7m M\u00ec","@id":"https:\/\/miai.vn\/#\/schema\/person\/cc8bc24bb90bd3f596add82f3a59948c"},"headline":"Custom Loss l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?","datePublished":"2020-10-11T21:27:55+00:00","mainEntityOfPage":{"@id":"https:\/\/miai.vn\/?p=1192"},"wordCount":1676,"commentCount":0,"publisher":{"@id":"https:\/\/miai.vn\/#organization"},"image":{"@id":"https:\/\/miai.vn\/?p=1192#primaryimage"},"thumbnailUrl":"https:\/\/github.com\/thangnch\/photos\/blob\/master\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true","keywords":["custom loss","customize loss","keras","loss","model custom loss","model loss","python"],"articleSection":["Basic"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/miai.vn\/?p=1192#respond"]}]},{"@type":"WebPage","@id":"https:\/\/miai.vn\/?p=1192","url":"https:\/\/miai.vn\/?p=1192","name":"Custom Loss, l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?- M\u00ec AI","isPartOf":{"@id":"https:\/\/miai.vn\/#website"},"primaryImageOfPage":{"@id":"https:\/\/miai.vn\/?p=1192#primaryimage"},"image":{"@id":"https:\/\/miai.vn\/?p=1192#primaryimage"},"thumbnailUrl":"https:\/\/github.com\/thangnch\/photos\/blob\/master\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true","datePublished":"2020-10-11T21:27:55+00:00","description":"Ch\u00e0o tu\u1ea7n m\u1edbi c\u00e1c anh em M\u00ec AI, h\u00f4m nay ch\u00fang ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u v\u1ec1 Custom Loss - h\u00e0m loss t\u1ef1 t\u1ea1o - trong ML, DL v\u1edbi Keras nh\u00e9.","breadcrumb":{"@id":"https:\/\/miai.vn\/?p=1192#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/miai.vn\/?p=1192"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/miai.vn\/?p=1192#primaryimage","url":"https:\/\/github.com\/thangnch\/photos\/blob\/master\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true","contentUrl":"https:\/\/github.com\/thangnch\/photos\/blob\/master\/Screen%20Shot%202020-10-12%20at%2010.31.57.png?raw=true"},{"@type":"BreadcrumbList","@id":"https:\/\/miai.vn\/?p=1192#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/miai.vn\/"},{"@type":"ListItem","position":2,"name":"Custom Loss l\u00e0 c\u00e1i gi\u1ed1ng g\u00ec? V\u00ec sao ph\u1ea3i d\u00f9ng?"}]},{"@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\/1192","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=1192"}],"version-history":[{"count":0,"href":"https:\/\/miai.vn\/index.php?rest_route=\/wp\/v2\/posts\/1192\/revisions"}],"wp:attachment":[{"href":"https:\/\/miai.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/miai.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/miai.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}