tek4

Convolutional Neural Network – Tự Học TensorFlow

by - September. 21, 2021
Kiến thức
<blockquote> <p style="text-align: justify;">Ch&agrave;o mừng c&aacute;c bạn quay trở lại với loạt b&agrave;i&nbsp;<a href="../../tu-hoc-tensorflow-deep-learning-cho-nguoi-moi-bat-dau/" target="_blank" rel="noopener">Tự Học Tensorflow</a>&nbsp;của&nbsp;<a href="../../" target="_blank" rel="noopener">tek4.vn</a>. B&agrave;i viết n&agrave;y sẽ giới thiệu đến c&aacute;c bạn về Convolutional Neural Network (CNN) l&agrave; g&igrave;, kiến tr&uacute;c của mạng cũng như c&aacute;ch đ&agrave;o tạo mạng với TensorFlow. Bắt đầu th&ocirc;i!</p> </blockquote> <p style="text-align: justify;">Xem th&ecirc;m b&agrave;i viết trước:&nbsp;<a href="../../mang-neural-gioi-thieu-va-vi-du-tu-hoc-tensorflow/" target="_blank" rel="noopener">Giới thiệu mạng neural &ndash;&nbsp;V&iacute; dụ về TensorFlow ANN</a></p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-7370 disappear appear" src="../../wp-content/uploads/2021/01/14.png" sizes="(max-width: 1113px) 100vw, 1113px" srcset="https://old.tek4.vn/wp-content/uploads/2021/01/14.png 1113w, https://old.tek4.vn/wp-content/uploads/2021/01/14-300x156.png 300w, https://old.tek4.vn/wp-content/uploads/2021/01/14-1024x534.png 1024w, https://old.tek4.vn/wp-content/uploads/2021/01/14-768x400.png 768w" alt="Convolutional Neural Network" width="1113" height="580" loading="lazy" /></p> <h3 id="ftoc-heading-1" class="ftwp-heading" style="text-align: justify;">Convolutional Neural Network l&agrave; g&igrave;</h3> <p style="text-align: justify;">CNN &ndash; Mạng neural t&iacute;ch chập, l&agrave; một mạng nổi tiếng trong c&aacute;c ứng dụng thị gi&aacute;c m&aacute;y t&iacute;nh. Loại kiến ​​tr&uacute;c n&agrave;y chiếm ưu thế trong việc nhận dạng c&aacute;c đối tượng từ một bức ảnh hoặc video.</p> <p style="text-align: justify;">Trong b&agrave;i viết n&agrave;y, ch&uacute;ng ta sẽ học c&aacute;ch x&acirc;y dựng một mạng t&iacute;ch chập v&agrave; c&aacute;ch sử dụng TensorFlow để giải quyết tập dữ liệu chữ số viết tay.</p> <h3 id="ftoc-heading-2" class="ftwp-heading" style="text-align: justify;">Kiến tr&uacute;c của một mạng neural t&iacute;ch chập</h3> <p style="text-align: justify;">H&atilde;y nghĩ về Facebook v&agrave;i năm trước, sau khi bạn tải một bức ảnh l&ecirc;n trang c&aacute; nh&acirc;n của m&igrave;nh, bạn được y&ecirc;u cầu th&ecirc;m t&ecirc;n cho khu&ocirc;n mặt tr&ecirc;n bức ảnh đ&oacute; theo c&aacute;ch thủ c&ocirc;ng. Ng&agrave;y nay, Facebook sử dụng mạng CNN để tự động gắn thẻ bạn b&egrave; của bạn trong ảnh.</p> <p style="text-align: justify;">Một convolutional neural network kh&ocirc;ng kh&oacute; hiểu lắm. H&igrave;nh ảnh đầu v&agrave;o được xử l&yacute; trong giai đoạn convolution v&agrave; sau đ&oacute; được g&aacute;n cho một nh&atilde;n.</p> <p style="text-align: justify;">C&oacute; thể t&oacute;m tắt một kiến ​​tr&uacute;c chập điển h&igrave;nh trong h&igrave;nh b&ecirc;n dưới. Trước hết, một h&igrave;nh ảnh được truyền v&agrave;o mạng: Đ&acirc;y được gọi l&agrave; h&igrave;nh ảnh đầu v&agrave;o. Sau đ&oacute;, h&igrave;nh ảnh đầu v&agrave;o n&agrave;y trải qua v&ocirc; số bước: Đ&acirc;y l&agrave; phần phức tạp của mạng. Cuối c&ugrave;ng, mạng neural c&oacute; thể dự đo&aacute;n chữ số tr&ecirc;n h&igrave;nh ảnh.</p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-7463 disappear appear" src="../../wp-content/uploads/2021/01/082918_1325_ConvNetConv1.png" sizes="(max-width: 689px) 100vw, 689px" srcset="https://old.tek4.vn/wp-content/uploads/2021/01/082918_1325_ConvNetConv1.png 689w, https://old.tek4.vn/wp-content/uploads/2021/01/082918_1325_ConvNetConv1-300x147.png 300w" alt="" width="689" height="337" loading="lazy" /></p> <p style="text-align: justify;">H&igrave;nh ảnh bao gồm một mảng pixel với chiều cao v&agrave; chiều rộng. Ảnh thang độ x&aacute;m chỉ c&oacute; một k&ecirc;nh m&agrave;u trong khi ảnh m&agrave;u c&oacute; ba k&ecirc;nh m&agrave;u (Red, Green v&agrave; Blue). Một k&ecirc;nh được xếp chồng l&ecirc;n nhau. Trong b&agrave;i n&agrave;y, ch&uacute;ng ta sẽ sử dụng ảnh thang độ x&aacute;m chỉ với một k&ecirc;nh m&agrave;u. Mỗi pixel c&oacute; gi&aacute; trị từ 0 đến 255 để phản &aacute;nh cường độ của m&agrave;u. V&iacute; dụ: pixel bằng 0 sẽ hiển thị m&agrave;u trắng trong khi pixel c&oacute; gi&aacute; trị gần 255 sẽ tối hơn.</p> <p style="text-align: justify;">H&atilde;y xem một h&igrave;nh ảnh được lưu trữ trong tập&nbsp;<a href="http://yann.lecun.com/exdb/mnist/" target="_blank" rel="noopener">dữ liệu MNIST</a>. H&igrave;nh dưới đ&acirc;y cho thấy c&aacute;ch biểu diễn h&igrave;nh b&ecirc;n tr&aacute;i ở dạng ma trận. Lưu &yacute; rằng, ma trận ban đầu đ&atilde; được chuẩn h&oacute;a để nằm trong khoảng từ 0 đến 1. Đối với m&agrave;u tối hơn, gi&aacute; trị trong ma trận l&agrave; khoảng 0,9 trong khi c&aacute;c pixel m&agrave;u trắng c&oacute; gi&aacute; trị bằng 0.</p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-7464 disappear appear" src="../../wp-content/uploads/2021/01/082918_1325_ConvNetConv2.png" sizes="(max-width: 845px) 100vw, 845px" srcset="https://old.tek4.vn/wp-content/uploads/2021/01/082918_1325_ConvNetConv2.png 845w, https://old.tek4.vn/wp-content/uploads/2021/01/082918_1325_ConvNetConv2-300x127.png 300w, https://old.tek4.vn/wp-content/uploads/2021/01/082918_1325_ConvNetConv2-768x326.png 768w" alt="Mnist dataset" width="845" height="359" loading="lazy" /></p> <p style="text-align: justify;">Th&agrave;nh phần quan trọng nhất trong m&ocirc; h&igrave;nh l&agrave; layers chập. Phần n&agrave;y nhằm mục đ&iacute;ch giảm k&iacute;ch thước của h&igrave;nh ảnh để t&iacute;nh to&aacute;n trọng số nhanh hơn v&agrave; cải thiện t&iacute;nh tổng qu&aacute;t của n&oacute;.</p> <p style="text-align: justify;">Trong phần convolutional , mạng giữ c&aacute;c đặc trưng thiết yếu của h&igrave;nh ảnh v&agrave; loại trừ nhiễu kh&ocirc;ng li&ecirc;n quan. V&iacute; dụ: m&ocirc; h&igrave;nh đang học c&aacute;ch nhận ra một con voi từ một bức ảnh với một ngọn n&uacute;i ở background. Nếu ta sử dụng mạng neural truyền thống, m&ocirc; h&igrave;nh sẽ chỉ định trọng số cho tất cả c&aacute;c pixel, bao gồm cả những pixel từ n&uacute;i kh&ocirc;ng cần thiết v&agrave; c&oacute; thể g&acirc;y hiểu nhầm cho mạng.</p> <p style="text-align: justify;">Thay v&agrave;o đ&oacute;, một convolutional neural network sẽ sử dụng một kỹ thuật to&aacute;n học để chỉ tr&iacute;ch xuất c&aacute;c pixel ph&ugrave; hợp nhất. Ph&eacute;p to&aacute;n n&agrave;y được gọi l&agrave; ph&eacute;p chập. Kỹ thuật n&agrave;y cho ph&eacute;p mạng t&igrave;m hiểu c&aacute;c đặc trưng ng&agrave;y c&agrave;ng phức tạp ở mỗi layer. Ph&eacute;p chập chia ma trận th&agrave;nh c&aacute;c phần nhỏ để t&igrave;m hiểu hầu hết c&aacute;c yếu tố cần thiết trong mỗi phần.</p> <h3 id="ftoc-heading-3" class="ftwp-heading" style="text-align: justify;">C&aacute;c th&agrave;nh phần của CNN</h3> <p style="text-align: justify;">C&oacute; bốn th&agrave;nh phần của một CNN.</p> <ol style="text-align: justify;"> <li>Convolution</li> <li>Non Linearity (ReLU)</li> <li>Pooling hoặc Sub Sampling</li> <li>Classification (Fully Connected Layer)</li> </ol> <h3 id="ftoc-heading-4" class="ftwp-heading" style="text-align: justify;">Convolution</h3> <p style="text-align: justify;">Mục đ&iacute;ch của ph&eacute;p chập l&agrave; tr&iacute;ch xuất cục bộ c&aacute;c đặc trưng của đối tượng tr&ecirc;n ảnh. N&oacute; c&oacute; nghĩa l&agrave; mạng sẽ học c&aacute;c mẫu cụ thể trong ảnh v&agrave; c&oacute; thể nhận ra n&oacute; ở mọi nơi trong ảnh.</p> <p style="text-align: justify;">Convolution l&agrave; một ph&eacute;p nh&acirc;n&nbsp;<a href="../../element-wise-lap-trinh-neural-network-voi-pytorch-bai-11/" target="_blank" rel="noopener">element-wise</a>. Kh&aacute;i niệm n&agrave;y rất dễ hiểu. M&aacute;y t&iacute;nh sẽ qu&eacute;t một phần của h&igrave;nh ảnh, thường c&oacute; k&iacute;ch thước l&agrave; 3&times;3 v&agrave; nh&acirc;n n&oacute; với một filter. Đầu ra của ph&eacute;p nh&acirc;nelement-wise được gọi l&agrave; bản đồ đặc trưng (feature map). Bước n&agrave;y được lặp lại cho đến khi tất cả h&igrave;nh ảnh được qu&eacute;t. Lưu &yacute; rằng, sau khi t&iacute;ch chập, k&iacute;ch thước của h&igrave;nh ảnh sẽ giảm.</p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-7465 disappear appear" src="../../wp-content/uploads/2021/01/082918_1325_ConvNetConv3.png" sizes="(max-width: 676px) 100vw, 676px" srcset="https://old.tek4.vn/wp-content/uploads/2021/01/082918_1325_ConvNetConv3.png 676w, https://old.tek4.vn/wp-content/uploads/2021/01/082918_1325_ConvNetConv3-300x194.png 300w, https://old.tek4.vn/wp-content/uploads/2021/01/082918_1325_ConvNetConv3-190x122.png 190w" alt="" width="676" height="437" loading="lazy" /></p> <p style="text-align: justify;">Dưới đ&acirc;y, c&oacute; một GIF để xem c&aacute;ch hoạt động của ph&eacute;p t&iacute;ch chập.</p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-7466 disappear appear" src="../../wp-content/uploads/2021/01/082918_1325_ConvNetConv4.gif" alt="" width="526" height="384" loading="lazy" /></p> <p style="text-align: justify;">C&oacute; rất nhiều k&ecirc;nh c&oacute; sẵn. Dưới đ&acirc;y, liệt k&ecirc; một số k&ecirc;nh. C&oacute; thể thấy rằng mỗi filter c&oacute; một mục đ&iacute;ch cụ thể. Lưu &yacute;, trong h&igrave;nh b&ecirc;n dưới Kernel l&agrave; một từ đồng nghĩa của filter.</p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-7467 disappear appear" src="../../wp-content/uploads/2021/01/082918_1325_ConvNetConv5.png" sizes="(max-width: 669px) 100vw, 669px" srcset="https://old.tek4.vn/wp-content/uploads/2021/01/082918_1325_ConvNetConv5.png 669w, https://old.tek4.vn/wp-content/uploads/2021/01/082918_1325_ConvNetConv5-271x300.png 271w" alt="" width="669" height="740" loading="lazy" /></p> <p style="text-align: justify;">Số học đằng sau t&iacute;ch chập</p> <p style="text-align: justify;">Giai đoạn t&iacute;ch chập sẽ &aacute;p dụng filter tr&ecirc;n một mảng pixel nhỏ trong h&igrave;nh ảnh. Filter sẽ di chuyển dọc theo h&igrave;nh ảnh đầu v&agrave;o c&oacute; h&igrave;nh dạng chung l&agrave; 3&times;3 hoặc 5&times;5. N&oacute; c&oacute; nghĩa l&agrave; mạng sẽ trượt c&aacute;c cửa sổ n&agrave;y tr&ecirc;n tất cả h&igrave;nh ảnh đầu v&agrave;o v&agrave; t&iacute;nh t&iacute;ch chập. H&igrave;nh ảnh dưới đ&acirc;y cho thấy c&aacute;ch hoạt động của t&iacute;ch chập. K&iacute;ch thước của patch l&agrave; 3&times;3 v&agrave; ma trận đầu ra l&agrave; kết quả của hoạt động element-wise giữa ma trận h&igrave;nh ảnh v&agrave; filter.</p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-8370 disappear appear" src="../../wp-content/uploads/2021/02/082918_1325_ConvNetConv6.gif" alt="" width="1000" height="563" loading="lazy" /></p> <p style="text-align: justify;">Bạn lưu &yacute; rằng chiều rộng v&agrave; chiều cao của đầu ra c&oacute; thể kh&aacute;c với chiều rộng v&agrave; chiều cao của đầu v&agrave;o. N&oacute; xảy ra do border effect.</p> <h4 id="ftoc-heading-5" class="ftwp-heading" style="text-align: justify;"><strong>Border effect</strong></h4> <p style="text-align: justify;">H&igrave;nh ảnh c&oacute; features map 5&times;5 v&agrave; &nbsp;filter 3&times;3. Chỉ c&oacute; một cửa sổ ở trung t&acirc;m nơi filter c&oacute; thể hiển thị lưới 3&times;3. Feature đầu ra sẽ thu nhỏ theo hai &ocirc; c&ugrave;ng với k&iacute;ch thước 3&times;3.<img class="aligncenter size-full wp-image-8379 disappear appear" src="../../wp-content/uploads/2021/02/082918_1325_ConvNetConv7.png" sizes="(max-width: 367px) 100vw, 367px" srcset="https://old.tek4.vn/wp-content/uploads/2021/02/082918_1325_ConvNetConv7.png 367w, https://old.tek4.vn/wp-content/uploads/2021/02/082918_1325_ConvNetConv7-300x85.png 300w" alt="" width="367" height="104" loading="lazy" /></p> <p style="text-align: justify;">Để c&oacute; c&ugrave;ng dimension đầu ra với dimension đầu v&agrave;o, bạn cần th&ecirc;m phần padding. Padding bao gồm việc th&ecirc;m đ&uacute;ng số h&agrave;ng v&agrave; cột ở mỗi b&ecirc;n của ma trận. N&oacute; sẽ cho ph&eacute;p t&iacute;ch chập để khớp với mọi &ocirc; đầu v&agrave;o. Trong h&igrave;nh dưới đ&acirc;y, ma trận đầu v&agrave;o / đầu ra c&oacute; c&ugrave;ng k&iacute;ch thước 5&times;5.</p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-8380 disappear appear" src="../../wp-content/uploads/2021/02/082918_1325_ConvNetConv8.png" sizes="(max-width: 592px) 100vw, 592px" srcset="https://old.tek4.vn/wp-content/uploads/2021/02/082918_1325_ConvNetConv8.png 592w, https://old.tek4.vn/wp-content/uploads/2021/02/082918_1325_ConvNetConv8-300x253.png 300w" alt="" width="592" height="499" loading="lazy" /></p> <p style="text-align: justify;">Khi bạn x&aacute;c định mạng, c&aacute;c đặc trưng được đối chiếu được kiểm so&aacute;t bởi ba tham số:</p> <ol style="text-align: justify;"> <li>Depth: N&oacute; x&aacute;c định số lượng filter để &aacute;p dụng trong qu&aacute; tr&igrave;nh t&iacute;ch chập. Trong v&iacute; dụ trước, bạn đ&atilde; thấy độ s&acirc;u l&agrave; 1, nghĩa l&agrave; chỉ một filter được sử dụng. Trong hầu hết c&aacute;c trường hợp, c&oacute; nhiều hơn một filter. H&igrave;nh ảnh dưới đ&acirc;y cho thấy c&aacute;c hoạt động được thực hiện trong một t&igrave;nh huống c&oacute; ba filter.<img class="aligncenter size-full wp-image-8381 disappear appear" src="../../wp-content/uploads/2021/02/082918_1325_ConvNetConv9.gif" alt="" width="1000" height="563" loading="lazy" /></li> <li>Stride:&nbsp; N&oacute; x&aacute;c định số &lsquo;bước nhảy của pixel&rsquo; giữa hai slices. Nếu stride bằng 1, c&aacute;c cửa sổ sẽ di chuyển với độ trải của một pixel. Nếu stride bằng hai, c&aacute;c cửa sổ sẽ nhảy th&ecirc;m 2 pixel. Nếu bạn tăng khoảng c&aacute;ch, bạn sẽ c&oacute; c&aacute;c feature maps nhỏ hơn.</li> </ol> <p style="text-align: justify;">V&iacute; dụ stride 1:</p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-8382 disappear appear" src="../../wp-content/uploads/2021/02/082918_1325_ConvNetConv10.png" alt="" width="291" height="105" loading="lazy" /></p> <p style="text-align: justify;">Stride 2:</p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-8383 disappear appear" src="../../wp-content/uploads/2021/02/082918_1325_ConvNetConv11.png" sizes="(max-width: 305px) 100vw, 305px" srcset="https://old.tek4.vn/wp-content/uploads/2021/02/082918_1325_ConvNetConv11.png 305w, https://old.tek4.vn/wp-content/uploads/2021/02/082918_1325_ConvNetConv11-300x107.png 300w" alt="" width="305" height="109" loading="lazy" /></p> <ol style="text-align: justify;" start="3"> <li>Zero-padding: Khoảng đệm l&agrave; thao t&aacute;c th&ecirc;m một số h&agrave;ng v&agrave; cột tương ứng v&agrave;o mỗi b&ecirc;n của features maps đầu v&agrave;o.Trong trường hợp n&agrave;y, đầu ra c&oacute; c&ugrave;ng dimension với đầu v&agrave;o.</li> <li>Non Linearity (ReLU): Khi kết th&uacute;c hoạt động t&iacute;ch chập, đầu ra c&oacute; h&agrave;m k&iacute;ch hoạt để cho ph&eacute;p non-linearity. H&agrave;m k&iacute;ch hoạt th&ocirc;ng thường cho mạng chuyển đổi l&agrave; Relu. Tất cả pixel c&oacute; gi&aacute; trị &acirc;m sẽ được thay thế bằng 0.</li> </ol> <h4 id="ftoc-heading-6" class="ftwp-heading" style="text-align: justify;"><strong>Max-pooling operation</strong></h4> <p style="text-align: justify;">Bước n&agrave;y rất dễ hiểu. Mục đ&iacute;ch của việc pooling l&agrave; để giảm k&iacute;ch thước của h&igrave;nh ảnh đầu v&agrave;o. C&aacute;c bước được thực hiện để giảm độ phức tạp t&iacute;nh to&aacute;n của hoạt động. Bằng c&aacute;ch giảm k&iacute;ch thước, mạng c&oacute; trọng số thấp hơn để t&iacute;nh to&aacute;n, v&igrave; vậy n&oacute; ngăn chặn việc qu&aacute; khớp.</p> <p style="text-align: justify;">Trong giai đoạn n&agrave;y, bạn cần x&aacute;c định size v&agrave; stride. Một c&aacute;ch ti&ecirc;u chuẩn để pool h&igrave;nh ảnh đầu v&agrave;o l&agrave; sử dụng gi&aacute; trị tối đa của feature map. Nh&igrave;n v&agrave;o h&igrave;nh ảnh dưới đ&acirc;y. &ldquo;Pooling&rdquo; sẽ hiển thị bốn submatrix của feature map 4&times;4 v&agrave; trả về gi&aacute; trị lớn nhất. Việc pooling lấy gi&aacute; trị lớn nhất của mảng 2&times;2 v&agrave; sau đ&oacute; di chuyển c&aacute;c cửa sổ n&agrave;y đi hai pixel. V&iacute; dụ: ma trận con đầu ti&ecirc;n l&agrave; [3,1,3,2], việc gộp sẽ trả về gi&aacute; trị lớn nhất, l&agrave; 3.</p> <p style="text-align: justify;"><img class="aligncenter size-full wp-image-8455 disappear appear" src="../../wp-content/uploads/2021/02/082918_1325_ConvNetConv12.png" sizes="(max-width: 527px) 100vw, 527px" srcset="https://old.tek4.vn/wp-content/uploads/2021/02/082918_1325_ConvNetConv12.png 527w, https://old.tek4.vn/wp-content/uploads/2021/02/082918_1325_ConvNetConv12-300x154.png 300w" alt="" width="527" height="271" loading="lazy" /></p> <p style="text-align: justify;">C&oacute; một hoạt động pooling kh&aacute;c chẳng hạn như gi&aacute; trị trung b&igrave;nh (mean).</p> <p style="text-align: justify;">Thao t&aacute;c n&agrave;y l&agrave;m giảm mạnh k&iacute;ch thước của feature map.</p> <ul style="text-align: justify;"> <li>C&aacute;c layers được kết nối đầy đủ.</li> </ul> <p style="text-align: justify;">Bước cuối c&ugrave;ng bao gồm x&acirc;y dựng mạng nơ-ron nh&acirc;n tạo truyền thống như bạn đ&atilde; l&agrave;m trong hướng dẫn trước. Bạn kết nối tất cả c&aacute;c nơ-ron từ layer trước với layer tiếp theo. Sử dụng h&agrave;m k&iacute;ch hoạt softmax để ph&acirc;n loại số tr&ecirc;n ảnh đầu v&agrave;o.</p> <h4 id="ftoc-heading-7" class="ftwp-heading" style="text-align: justify;"><strong>Recap</strong></h4> <p style="text-align: justify;">Mạng Neural Convolutions bi&ecirc;n dịch c&aacute;c layers kh&aacute;c nhau trước khi đưa ra dự đo&aacute;n. Mạng nơron c&oacute;:</p> <ul style="text-align: justify;"> <li>1 convolutional layer</li> <li>H&agrave;m k&iacute;ch hoạt Relu</li> <li>Pooling layer</li> <li>Densely connected layer</li> </ul> <p style="text-align: justify;">C&aacute;c convolutional layers &aacute;p dụng c&aacute;c filter kh&aacute;c nhau tr&ecirc;n một tiểu v&ugrave;ng của h&igrave;nh ảnh. H&agrave;m k&iacute;ch hoạt Relu th&ecirc;m t&iacute;nh kh&ocirc;ng tuyến t&iacute;nh v&agrave; c&aacute;c pooling layers l&agrave;m giảm k&iacute;ch thước của &nbsp;features maps.</p> <p style="text-align: justify;">Tất cả c&aacute;c layers n&agrave;y tr&iacute;ch xuất th&ocirc;ng tin cần thiết từ h&igrave;nh ảnh. Cuối c&ugrave;ng, features map được cung cấp đến một layer ch&iacute;nh được kết nối đầy đủ với h&agrave;m softmax để đưa ra dự đo&aacute;n.</p> <h3 id="ftoc-heading-8" class="ftwp-heading" style="text-align: justify;">Đ&agrave;o tạo CNN với TensorFlow</h3> <p style="text-align: justify;">Ch&uacute;ng ta sẽ sử dụng bộ dữ liệu MNIST để ph&acirc;n loại h&igrave;nh ảnh.</p> <p style="text-align: justify;">Việc chuẩn bị dữ liệu giống như hướng dẫn trước. Bạn c&oacute; thể chạy m&atilde; v&agrave; chuyển trực tiếp đến kiến ​​tr&uacute;c của CNN.</p> <p style="text-align: justify;">Sẽ l&agrave;m theo c&aacute;c bước sau:</p> <p style="text-align: justify;">Bước 1: Tải l&ecirc;n tập dữ liệu</p> <div style="text-align: justify;">&nbsp;</div> <p style="text-align: justify;">Bước 2: Layer đầu v&agrave;o</p> <p style="text-align: justify;">Bước 3: Convolutional layer</p> <p style="text-align: justify;">Bước 4: Pooling layer</p> <p style="text-align: justify;">Bước 5: Convolutional Layer thứ hai v&agrave; Pooling Layer</p> <p style="text-align: justify;">Bước 6: Dense layer</p> <p style="text-align: justify;">Bước 7: Logit Layer</p> <h4 id="ftoc-heading-9" class="ftwp-heading" style="text-align: justify;">Bước 1: Tải l&ecirc;n tập dữ liệu</h4> <p style="text-align: justify;">Tập dữ liệu MNIST c&oacute; sẵn với scikit learn. Bạn c&oacute; thể tải n&oacute; l&ecirc;n với fetch_mldata(&lsquo;MNIST original&rsquo;).</p> <p style="text-align: justify;"><strong>Tạo một train/test set&nbsp;</strong></p> <p style="text-align: justify;">Bạn cần t&aacute;ch tập dữ liệu bằng train_test_split.</p> <p style="text-align: justify;"><strong>Scale c&aacute;c đặc trưng</strong></p> <p style="text-align: justify;">Cuối c&ugrave;ng, bạn c&oacute; thể scale c&aacute;c đặc trưng với MinMaxScaler.</p> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-7">7</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-8">8</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-9">9</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-10">10</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-11">11</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-12">12</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-13">13</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-14">14</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-15">15</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-16">16</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-17">17</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-18">18</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-19">19</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-20">20</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-21">21</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-22">22</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-23">23</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-24">24</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-25">25</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-26">26</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-27">27</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-28">28</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d640e713871884-29">29</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-1" class="crayon-line"><span class="crayon-e">import </span><span class="crayon-e">numpy </span><span class="crayon-st">as</span> <span class="crayon-e">np</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-2" class="crayon-line crayon-striped-line"><span class="crayon-e">import </span><span class="crayon-e">tensorflow </span><span class="crayon-st">as</span> <span class="crayon-e">tf</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-3" class="crayon-line"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">datasets </span><span class="crayon-e">import </span><span class="crayon-v">fetch</span><span class="crayon-sy">_</span>mldata</div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-4" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-5" class="crayon-line"><span class="crayon-p">#Change USERNAME by the username of your machine</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-6" class="crayon-line crayon-striped-line"><span class="crayon-p">## Windows USER</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-7" class="crayon-line"><span class="crayon-v">mnist</span> <span class="crayon-o">=</span> <span class="crayon-e">fetch_mldata</span><span class="crayon-sy">(</span><span class="crayon-s">'C:\\Users\\USERNAME\\Downloads\\MNIST original'</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-8" class="crayon-line crayon-striped-line"><span class="crayon-p">## Mac User</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-9" class="crayon-line"><span class="crayon-v">mnist</span> <span class="crayon-o">=</span> <span class="crayon-e">fetch_mldata</span><span class="crayon-sy">(</span><span class="crayon-s">'/Users/USERNAME/Downloads/MNIST original'</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-10" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-11" class="crayon-line"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-v">mnist</span><span class="crayon-sy">.</span><span class="crayon-v">data</span><span class="crayon-sy">.</span><span class="crayon-v">shape</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-12" class="crayon-line crayon-striped-line"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-v">mnist</span><span class="crayon-sy">.</span><span class="crayon-v">target</span><span class="crayon-sy">.</span><span class="crayon-v">shape</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-13" class="crayon-line"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">model_selection </span><span class="crayon-e">import </span><span class="crayon-e">train_test_split</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-14" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-15" class="crayon-line"><span class="crayon-v">X_train</span><span class="crayon-sy">,</span> <span class="crayon-v">X_test</span><span class="crayon-sy">,</span> <span class="crayon-v">y_train</span><span class="crayon-sy">,</span> <span class="crayon-v">y_test</span> <span class="crayon-o">=</span> <span class="crayon-e">train_test_split</span><span class="crayon-sy">(</span><span class="crayon-v">mnist</span><span class="crayon-sy">.</span><span class="crayon-v">data</span><span class="crayon-sy">,</span> <span class="crayon-v">mnist</span><span class="crayon-sy">.</span><span class="crayon-v">target</span><span class="crayon-sy">,</span> <span class="crayon-v">test_size</span><span class="crayon-o">=</span><span class="crayon-cn">0.2</span><span class="crayon-sy">,</span> <span class="crayon-v">random_state</span><span class="crayon-o">=</span><span class="crayon-cn">42</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-16" class="crayon-line crayon-striped-line"><span class="crayon-v">y_train</span><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-o">=</span> <span class="crayon-v">y_train</span><span class="crayon-sy">.</span><span class="crayon-e">astype</span><span class="crayon-sy">(</span><span class="crayon-t">int</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-17" class="crayon-line"><span class="crayon-v">y_test</span><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-o">=</span> <span class="crayon-v">y_test</span><span class="crayon-sy">.</span><span class="crayon-e">astype</span><span class="crayon-sy">(</span><span class="crayon-t">int</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-18" class="crayon-line crayon-striped-line"><span class="crayon-v">batch_size</span> <span class="crayon-o">=</span><span class="crayon-e">len</span><span class="crayon-sy">(</span><span class="crayon-v">X_train</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-19" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-20" class="crayon-line crayon-striped-line"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-v">X_train</span><span class="crayon-sy">.</span><span class="crayon-v">shape</span><span class="crayon-sy">,</span> <span class="crayon-v">y_train</span><span class="crayon-sy">.</span><span class="crayon-v">shape</span><span class="crayon-sy">,</span><span class="crayon-v">y_test</span><span class="crayon-sy">.</span><span class="crayon-i">shape</span> <span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-21" class="crayon-line"><span class="crayon-p">## resclae</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-22" class="crayon-line crayon-striped-line"><span class="crayon-e">from </span><span class="crayon-v">sklearn</span><span class="crayon-sy">.</span><span class="crayon-e">preprocessing </span><span class="crayon-e">import </span><span class="crayon-e">MinMaxScaler</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-23" class="crayon-line"><span class="crayon-v">scaler</span> <span class="crayon-o">=</span> <span class="crayon-e">MinMaxScaler</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-24" class="crayon-line crayon-striped-line"><span class="crayon-p"># Train</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-25" class="crayon-line"><span class="crayon-v">X_train_scaled</span> <span class="crayon-o">=</span> <span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit_transform</span><span class="crayon-sy">(</span><span class="crayon-v">X_train</span><span class="crayon-sy">.</span><span class="crayon-e">astype</span><span class="crayon-sy">(</span><span class="crayon-v">np</span><span class="crayon-sy">.</span><span class="crayon-v">float64</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-26" class="crayon-line crayon-striped-line"><span class="crayon-p"># test</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-27" class="crayon-line"><span class="crayon-v">X_test_scaled</span> <span class="crayon-o">=</span> <span class="crayon-v">scaler</span><span class="crayon-sy">.</span><span class="crayon-e">fit_transform</span><span class="crayon-sy">(</span><span class="crayon-v">X_test</span><span class="crayon-sy">.</span><span class="crayon-e">astype</span><span class="crayon-sy">(</span><span class="crayon-v">np</span><span class="crayon-sy">.</span><span class="crayon-v">float64</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-28" class="crayon-line crayon-striped-line"><span class="crayon-v">feature_columns</span> <span class="crayon-o">=</span> <span class="crayon-sy">[</span><span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">feature_column</span><span class="crayon-sy">.</span><span class="crayon-e">numeric_column</span><span class="crayon-sy">(</span><span class="crayon-s">'x'</span><span class="crayon-sy">,</span> <span class="crayon-v">shape</span><span class="crayon-o">=</span><span class="crayon-v">X_train_scaled</span><span class="crayon-sy">.</span><span class="crayon-v">shape</span><span class="crayon-sy">[</span><span class="crayon-cn">1</span><span class="crayon-o">:</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span><span class="crayon-sy">]</span></div> <div id="urvanov-syntax-highlighter-610fefe2d640e713871884-29" class="crayon-line"><span class="crayon-v">X_train_scaled</span><span class="crayon-sy">.</span><span class="crayon-v">shape</span><span class="crayon-sy">[</span><span class="crayon-cn">1</span><span class="crayon-o">:</span><span class="crayon-sy">]</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <h5 id="ftoc-heading-10" class="ftwp-heading" style="text-align: justify;"><strong>X&aacute;c định CNN</strong></h5> <p style="text-align: justify;">Để tạo CNN, bạn cần x&aacute;c định:</p> <ol style="text-align: justify;"> <li>Một convolutional layer: &Aacute;p dụng n số filter cho feature map. Sau khi t&iacute;ch chập, bạn cần sử dụng h&agrave;m k&iacute;ch hoạt Relu để th&ecirc;m t&iacute;nh kh&ocirc;ng tuyến t&iacute;nh v&agrave;o mạng.</li> <li>Pooling layer: Bước tiếp theo sau khi t&iacute;ch chập l&agrave; giảm mẫu tối đa của đặc trưng. Mục đ&iacute;ch l&agrave; để giảm k&iacute;ch thước của feature map để tr&aacute;nh qu&aacute; khớp v&agrave; cải thiện tốc độ t&iacute;nh to&aacute;n.Max pooling l&agrave; kỹ thuật th&ocirc;ng thường, chia c&aacute;c feature maps th&agrave;nh c&aacute;c tiểu v&ugrave;ng (thường c&oacute; k&iacute;ch thước 2&times;2) v&agrave; chỉ giữ c&aacute;c gi&aacute; trị lớn nhất.</li> <li>Fully connected layers: Tất cả c&aacute;c nơ-ron từ c&aacute;c layer trước được kết nối với c&aacute;c layer tiếp theo. CNN sẽ ph&acirc;n loại nh&atilde;n theo c&aacute;c đặc điểm từ c&aacute;c convolutional layers v&agrave; giảm dần theo pooling layer.</li> </ol> <h5 id="ftoc-heading-11" class="ftwp-heading" style="text-align: justify;">Kiến tr&uacute;c CNN</h5> <ul style="text-align: justify;"> <li>Convolutional Layer: &Aacute;p dụng 14 filter 5&times;5 (tr&iacute;ch xuất c&aacute;c v&ugrave;ng con 5&times;5 pixel), với h&agrave;m k&iacute;ch hoạt ReLU.</li> <li>Pooling Layer: Thực hiện pooling tối đa với bộ lọc 2&times;2 v&agrave; stride l&agrave; 2 (chỉ định rằng c&aacute;c v&ugrave;ng được tổng hợp kh&ocirc;ng chồng ch&eacute;o)</li> <li>Convolutional Layer: &Aacute;p dụng 36 filter 5&times;5, với h&agrave;m k&iacute;ch hoạt ReLU.</li> <li>Pooling Layer #2: Một lần nữa, thực hiện pooling tối đa với filter 2&times;2 v&agrave; stride l&agrave; 2.</li> <li>1.764 neurons, với tỷ lệ bỏ học ch&iacute;nh quy l&agrave; 0,4 (x&aacute;c suất 0,4 rằng bất kỳ phần tử nhất định n&agrave;o sẽ bị loại bỏ trong qu&aacute; tr&igrave;nh đ&agrave;o tạo)</li> <li>Dense Layer (Logits Layer): 10 neurons, một cho mỗi layer mục ti&ecirc;u chữ số (0-9).</li> </ul> <p style="text-align: justify;">C&oacute; ba m&ocirc;-đun quan trọng cần sử dụng để tạo CNN:</p> <ul style="text-align: justify;"> <li>conv2d(). X&acirc;y dựng một lớp t&iacute;ch chập hai chiều với số lượng filters, filter kernel size, padding h&agrave;m k&iacute;ch hoạt l&agrave;m đối số.</li> <li>max_pooling2d(). X&acirc;y dựng pooling layer hai chiều bằng c&aacute;ch sử dụng thuật to&aacute;n max-pooling.</li> <li>dense(). X&acirc;y dựng một dense layer với c&aacute;c layer v&agrave; đơn vị ẩn</li> </ul> <p style="text-align: justify;">Bạn sẽ x&aacute;c định một h&agrave;m để x&acirc;y dựng CNN.</p> <h4 id="ftoc-heading-12" class="ftwp-heading" style="text-align: justify;">Bước 2: Layer đầu v&agrave;o</h4> <div id="urvanov-syntax-highlighter-610fefe2d6431802643481" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6431802643481-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6431802643481-2">2</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d6431802643481-1" class="crayon-line"><span class="crayon-e">def </span><span class="crayon-e">cnn_model_fn</span><span class="crayon-sy">(</span><span class="crayon-v">features</span><span class="crayon-sy">,</span> <span class="crayon-v">labels</span><span class="crayon-sy">,</span> <span class="crayon-v">mode</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6431802643481-2" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">input_layer</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-v">tensor</span> <span class="crayon-o">=</span> <span class="crayon-v">features</span><span class="crayon-sy">[</span><span class="crayon-s">"x"</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-v">shape</span> <span class="crayon-o">=</span><span class="crayon-sy">[</span><span class="crayon-o">-</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span> <span class="crayon-cn">28</span><span class="crayon-sy">,</span> <span class="crayon-cn">28</span><span class="crayon-sy">,</span> <span class="crayon-cn">1</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Bạn cần x&aacute;c định một tensor với shape của dữ liệu. Đối với điều đ&oacute;, bạn c&oacute; thể sử dụng module tf.reshape. Đối số đầu ti&ecirc;n l&agrave; c&aacute;c đặc trưng của dữ liệu, được định nghĩa trong đối số của h&agrave;m.</p> <p style="text-align: justify;">H&igrave;nh ảnh c&oacute; chiều cao, chiều rộng v&agrave; k&ecirc;nh. Tập dữ liệu MNIST l&agrave; một bức ảnh đơn sắc c&oacute; k&iacute;ch thước 28&times;28.</p> <h4 id="ftoc-heading-13" class="ftwp-heading" style="text-align: justify;">Bước 3: Convolutional layer</h4> <div id="urvanov-syntax-highlighter-610fefe2d6439557385958" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6439557385958-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6439557385958-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6439557385958-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6439557385958-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6439557385958-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6439557385958-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6439557385958-7">7</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d6439557385958-1" class="crayon-line"><span class="crayon-p"># first Convolutional Layer</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6439557385958-2" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">conv1</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">conv2d</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6439557385958-3" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">input_layer</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6439557385958-4" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">filters</span><span class="crayon-o">=</span><span class="crayon-cn">14</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6439557385958-5" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">kernel_size</span><span class="crayon-o">=</span><span class="crayon-sy">[</span><span class="crayon-cn">5</span><span class="crayon-sy">,</span> <span class="crayon-cn">5</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6439557385958-6" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">padding</span><span class="crayon-o">=</span><span class="crayon-s">"same"</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6439557385958-7" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">nn</span><span class="crayon-sy">.</span><span class="crayon-v">relu</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Lớp chập đầu ti&ecirc;n c&oacute; 14 bộ lọc với kernel size l&agrave; 5&times;5 với c&ugrave;ng một v&ugrave;ng padding. Phần padding giống nhau c&oacute; nghĩa l&agrave; cả tensor đầu ra v&agrave; tensor đầu v&agrave;o phải c&oacute; c&ugrave;ng chiều cao v&agrave; chiều rộng. Tensorflow sẽ th&ecirc;m c&aacute;c số kh&ocirc;ng v&agrave;o c&aacute;c h&agrave;ng v&agrave; cột để đảm bảo c&oacute; c&ugrave;ng k&iacute;ch thước.</p> <p style="text-align: justify;">Sử dụng h&agrave;m k&iacute;ch hoạt Relu. K&iacute;ch thước đầu ra sẽ l&agrave; [28, 28, 14].</p> <h4 id="ftoc-heading-14" class="ftwp-heading" style="text-align: justify;">Bước 4: Pooling layer</h4> <p style="text-align: justify;">Bước tiếp theo sau ph&eacute;p t&iacute;ch chập l&agrave; t&iacute;nh to&aacute;n pooling, gi&uacute;p l&agrave;m giảm k&iacute;ch thước của dữ liệu. Bạn c&oacute; thể sử dụng m&ocirc;-đun max_pooling2d với k&iacute;ch thước 2&times;2 v&agrave; stride l&agrave; 2. Sử dụng layer trước đ&oacute; l&agrave;m đầu v&agrave;o. K&iacute;ch thước đầu ra sẽ l&agrave; [batch_size, 14, 14, 14]</p> <div id="urvanov-syntax-highlighter-610fefe2d643d882756989" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d643d882756989-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d643d882756989-2">2</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d643d882756989-1" class="crayon-line"><span class="crayon-p"># first Pooling Layer </span></div> <div id="urvanov-syntax-highlighter-610fefe2d643d882756989-2" class="crayon-line crayon-striped-line"><span class="crayon-v">pool1</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">max_pooling2d</span><span class="crayon-sy">(</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">conv1</span><span class="crayon-sy">,</span> <span class="crayon-v">pool_size</span><span class="crayon-o">=</span><span class="crayon-sy">[</span><span class="crayon-cn">2</span><span class="crayon-sy">,</span> <span class="crayon-cn">2</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span> <span class="crayon-v">strides</span><span class="crayon-o">=</span><span class="crayon-cn">2</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <h4 id="ftoc-heading-15" class="ftwp-heading" style="text-align: justify;">Bước 5: Convolutional Layer Thứ Hai v&agrave; Pooling Layer</h4> <p style="text-align: justify;">Lớp chập thứ hai c&oacute; 32 &nbsp;filters, với k&iacute;ch thước đầu ra l&agrave; [batch_size, 14, 14, 32]. Pooling layer c&oacute; c&ugrave;ng k&iacute;ch thước như trước v&agrave; shape đầu ra l&agrave; [batch_size, 14, 14, 18].</p> <div id="urvanov-syntax-highlighter-610fefe2d6441515122479" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6441515122479-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6441515122479-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6441515122479-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6441515122479-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6441515122479-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6441515122479-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6441515122479-7">7</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d6441515122479-1" class="crayon-line"><span class="crayon-v">conv2</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">conv2d</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6441515122479-2" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">pool1</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6441515122479-3" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">filters</span><span class="crayon-o">=</span><span class="crayon-cn">36</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6441515122479-4" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">kernel_size</span><span class="crayon-o">=</span><span class="crayon-sy">[</span><span class="crayon-cn">5</span><span class="crayon-sy">,</span> <span class="crayon-cn">5</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6441515122479-5" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">padding</span><span class="crayon-o">=</span><span class="crayon-s">"same"</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6441515122479-6" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">nn</span><span class="crayon-sy">.</span><span class="crayon-v">relu</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6441515122479-7" class="crayon-line"><span class="crayon-v">pool2</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">max_pooling2d</span><span class="crayon-sy">(</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">conv2</span><span class="crayon-sy">,</span> <span class="crayon-v">pool_size</span><span class="crayon-o">=</span><span class="crayon-sy">[</span><span class="crayon-cn">2</span><span class="crayon-sy">,</span> <span class="crayon-cn">2</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span> <span class="crayon-v">strides</span><span class="crayon-o">=</span><span class="crayon-cn">2</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <h4 id="ftoc-heading-16" class="ftwp-heading" style="text-align: justify;">Bước 6:&nbsp;Dense layer</h4> <p style="text-align: justify;">Sau đ&oacute;, bạn cần x&aacute;c định fully-connected layer. Feature map phải được flatten trước khi được kết nối với dense layer. Bạn c&oacute; thể sử dụng reshape lại m&ocirc;-đun với k&iacute;ch thước 7 * 7 * 36.</p> <p style="text-align: justify;">Dense layer sẽ kết nối 1764 neurons. Th&ecirc;m h&agrave;m k&iacute;ch hoạt Relu.</p> <div id="urvanov-syntax-highlighter-610fefe2d6444968011513" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6444968011513-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6444968011513-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6444968011513-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6444968011513-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6444968011513-5">5</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d6444968011513-1" class="crayon-line"><span class="crayon-v">pool2_flat</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-v">pool2</span><span class="crayon-sy">,</span> <span class="crayon-sy">[</span><span class="crayon-o">-</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span> <span class="crayon-cn">7</span> <span class="crayon-o">*</span> <span class="crayon-cn">7</span> <span class="crayon-o">*</span> <span class="crayon-cn">36</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6444968011513-2" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d6444968011513-3" class="crayon-line"><span class="crayon-v">dense</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">dense</span><span class="crayon-sy">(</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">pool2_flat</span><span class="crayon-sy">,</span> <span class="crayon-v">units</span><span class="crayon-o">=</span><span class="crayon-cn">7</span> <span class="crayon-o">*</span> <span class="crayon-cn">7</span> <span class="crayon-o">*</span> <span class="crayon-cn">36</span><span class="crayon-sy">,</span> <span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">nn</span><span class="crayon-sy">.</span><span class="crayon-v">relu</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6444968011513-4" class="crayon-line crayon-striped-line"><span class="crayon-v">dropout</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">dropout</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6444968011513-5" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">dense</span><span class="crayon-sy">,</span> <span class="crayon-v">rate</span><span class="crayon-o">=</span><span class="crayon-cn">0.3</span><span class="crayon-sy">,</span> <span class="crayon-v">training</span><span class="crayon-o">=</span><span class="crayon-v">mode</span> <span class="crayon-o">==</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-v">ModeKeys</span><span class="crayon-sy">.</span><span class="crayon-v">TRAIN</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <h4 id="ftoc-heading-17" class="ftwp-heading" style="text-align: justify;">Bước 7: Logit Layer</h4> <p style="text-align: justify;">Cuối c&ugrave;ng, bạn c&oacute; thể x&aacute;c định layer cuối c&ugrave;ng với dự đo&aacute;n của m&ocirc; h&igrave;nh.</p> <div id="urvanov-syntax-highlighter-610fefe2d644b160489334" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d644b160489334-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d644b160489334-2">2</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d644b160489334-1" class="crayon-line"><span class="crayon-p"># Logits Layer</span></div> <div id="urvanov-syntax-highlighter-610fefe2d644b160489334-2" class="crayon-line crayon-striped-line"><span class="crayon-v">logits</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">dense</span><span class="crayon-sy">(</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">dropout</span><span class="crayon-sy">,</span> <span class="crayon-v">units</span><span class="crayon-o">=</span><span class="crayon-cn">10</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Bạn c&oacute; thể tạo một từ điển chứa c&aacute;c lớp v&agrave; x&aacute;c suất của mỗi lớp.</p> <div id="urvanov-syntax-highlighter-610fefe2d644e611318518" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d644e611318518-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d644e611318518-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d644e611318518-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d644e611318518-4">4</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d644e611318518-1" class="crayon-line"><span class="crayon-v">predictions</span> <span class="crayon-o">=</span> <span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-610fefe2d644e611318518-2" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-p"># Generate predictions </span></div> <div id="urvanov-syntax-highlighter-610fefe2d644e611318518-3" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-s">"classes"</span><span class="crayon-o">:</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-e">argmax</span><span class="crayon-sy">(</span><span class="crayon-v">input</span><span class="crayon-o">=</span><span class="crayon-v">logits</span><span class="crayon-sy">,</span> <span class="crayon-v">axis</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d644e611318518-4" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-s">"probabilities"</span><span class="crayon-o">:</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">nn</span><span class="crayon-sy">.</span><span class="crayon-e">softmax</span><span class="crayon-sy">(</span><span class="crayon-v">logits</span><span class="crayon-sy">,</span> <span class="crayon-v">name</span><span class="crayon-o">=</span><span class="crayon-s">"softmax_tensor"</span><span class="crayon-sy">)</span><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-sy">}</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Bạn chỉ muốn trả về dictionnary prediction khi chế độ được đặt th&agrave;nh dự đo&aacute;n. Bạn th&ecirc;m m&atilde; n&agrave;y để loại bỏ c&aacute;c dự đo&aacute;n</p> <div id="urvanov-syntax-highlighter-610fefe2d6451385231271" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6451385231271-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6451385231271-2">2</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d6451385231271-1" class="crayon-line"><span class="crayon-st">if</span> <span class="crayon-v">mode</span> <span class="crayon-o">==</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-v">ModeKeys</span><span class="crayon-sy">.</span><span class="crayon-v">PREDICT</span><span class="crayon-o">:</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6451385231271-2" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-st">return</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-e">EstimatorSpec</span><span class="crayon-sy">(</span><span class="crayon-v">mode</span><span class="crayon-o">=</span><span class="crayon-v">mode</span><span class="crayon-sy">,</span> <span class="crayon-v">predictions</span><span class="crayon-o">=</span><span class="crayon-v">predictions</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Bước tiếp theo bao gồm t&iacute;nh to&aacute;n tổn thất của m&ocirc; h&igrave;nh. Sự mất m&aacute;t được t&iacute;nh to&aacute;n dễ d&agrave;ng bằng đoạn m&atilde; sau:</p> <div id="urvanov-syntax-highlighter-610fefe2d6454372709288" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6454372709288-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6454372709288-2">2</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d6454372709288-1" class="crayon-line"><span class="crayon-p"># Calculate Loss (for both TRAIN and EVAL modes)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6454372709288-2" class="crayon-line crayon-striped-line"><span class="crayon-v">loss</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">losses</span><span class="crayon-sy">.</span><span class="crayon-e">sparse_softmax_cross_entropy</span><span class="crayon-sy">(</span><span class="crayon-v">labels</span><span class="crayon-o">=</span><span class="crayon-v">labels</span><span class="crayon-sy">,</span> <span class="crayon-v">logits</span><span class="crayon-o">=</span><span class="crayon-v">logits</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Bước cuối c&ugrave;ng l&agrave; tối ưu h&oacute;a m&ocirc; h&igrave;nh, tức l&agrave; t&igrave;m c&aacute;c gi&aacute; trị tốt nhất của c&aacute;c trọng số. Đối với điều đ&oacute;, bạn sử dụng tr&igrave;nh tối ưu h&oacute;a Gradient descent với tốc độ học l&agrave; 0,001. Mục ti&ecirc;u l&agrave; giảm thiểu tổn thất.</p> <div id="urvanov-syntax-highlighter-610fefe2d6457003985883" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6457003985883-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6457003985883-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6457003985883-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6457003985883-4">4</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d6457003985883-1" class="crayon-line"><span class="crayon-v">optimizer</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">train</span><span class="crayon-sy">.</span><span class="crayon-e">GradientDescentOptimizer</span><span class="crayon-sy">(</span><span class="crayon-v">learning_rate</span><span class="crayon-o">=</span><span class="crayon-cn">0.001</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6457003985883-2" class="crayon-line crayon-striped-line"><span class="crayon-v">train_op</span> <span class="crayon-o">=</span> <span class="crayon-v">optimizer</span><span class="crayon-sy">.</span><span class="crayon-e">minimize</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6457003985883-3" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">loss</span><span class="crayon-o">=</span><span class="crayon-v">loss</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6457003985883-4" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">global_step</span><span class="crayon-o">=</span><span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">train</span><span class="crayon-sy">.</span><span class="crayon-e">get_global_step</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Bạn đ&atilde; ho&agrave;n th&agrave;nh với CNN. Tensorflow được trang bị độ ch&iacute;nh x&aacute;c của m&ocirc;-đun với hai đối số, nh&atilde;n v&agrave; gi&aacute; trị dự đo&aacute;n.</p> <div id="urvanov-syntax-highlighter-610fefe2d645a859302137" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645a859302137-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645a859302137-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645a859302137-3">3</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d645a859302137-1" class="crayon-line"><span class="crayon-v">eval_metric_ops</span> <span class="crayon-o">=</span> <span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645a859302137-2" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-s">"accuracy"</span><span class="crayon-o">:</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">metrics</span><span class="crayon-sy">.</span><span class="crayon-e">accuracy</span><span class="crayon-sy">(</span><span class="crayon-v">labels</span><span class="crayon-o">=</span><span class="crayon-v">labels</span><span class="crayon-sy">,</span> <span class="crayon-v">predictions</span><span class="crayon-o">=</span><span class="crayon-v">predictions</span><span class="crayon-sy">[</span><span class="crayon-s">"classes"</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span><span class="crayon-sy">}</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645a859302137-3" class="crayon-line"><span class="crayon-st">return</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-e">EstimatorSpec</span><span class="crayon-sy">(</span><span class="crayon-v">mode</span><span class="crayon-o">=</span><span class="crayon-v">mode</span><span class="crayon-sy">,</span> <span class="crayon-v">loss</span><span class="crayon-o">=</span><span class="crayon-v">loss</span><span class="crayon-sy">,</span> <span class="crayon-v">eval_metric_ops</span><span class="crayon-o">=</span><span class="crayon-v">eval_metric_ops</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Bạn đ&atilde; tạo CNN đầu ti&ecirc;n của m&igrave;nh v&agrave; bạn đ&atilde; sẵn s&agrave;ng g&oacute;i mọi thứ th&agrave;nh một h&agrave;m để sử dụng n&oacute; để đ&agrave;o tạo v&agrave; đ&aacute;nh gi&aacute; m&ocirc; h&igrave;nh.</p> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-7">7</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-8">8</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-9">9</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-10">10</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-11">11</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-12">12</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-13">13</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-14">14</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-15">15</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-16">16</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-17">17</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-18">18</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-19">19</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-20">20</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-21">21</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-22">22</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-23">23</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-24">24</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-25">25</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-26">26</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-27">27</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-28">28</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-29">29</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-30">30</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-31">31</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-32">32</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-33">33</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-34">34</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-35">35</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-36">36</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-37">37</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-38">38</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-39">39</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-40">40</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-41">41</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-42">42</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-43">43</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-44">44</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-45">45</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-46">46</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-47">47</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-48">48</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-49">49</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-50">50</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-51">51</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-52">52</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-53">53</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-54">54</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-55">55</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-56">56</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-57">57</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-58">58</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-59">59</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d645d360627534-60">60</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-1" class="crayon-line"><span class="crayon-e">def </span><span class="crayon-e">cnn_model_fn</span><span class="crayon-sy">(</span><span class="crayon-v">features</span><span class="crayon-sy">,</span> <span class="crayon-v">labels</span><span class="crayon-sy">,</span> <span class="crayon-v">mode</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-2" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-s">""</span><span class="crayon-s">"Model function for CNN."</span><span class="crayon-s">""</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-3" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-p"># Input Layer</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-4" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">input_layer</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-v">features</span><span class="crayon-sy">[</span><span class="crayon-s">"x"</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span> <span class="crayon-sy">[</span><span class="crayon-o">-</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span> <span class="crayon-cn">28</span><span class="crayon-sy">,</span> <span class="crayon-cn">28</span><span class="crayon-sy">,</span> <span class="crayon-cn">1</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-5" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-6" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-p"># Convolutional Layer</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-7" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">conv1</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">conv2d</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-8" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">input_layer</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-9" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">filters</span><span class="crayon-o">=</span><span class="crayon-cn">32</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-10" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">kernel_size</span><span class="crayon-o">=</span><span class="crayon-sy">[</span><span class="crayon-cn">5</span><span class="crayon-sy">,</span> <span class="crayon-cn">5</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-11" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">padding</span><span class="crayon-o">=</span><span class="crayon-s">"same"</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-12" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">nn</span><span class="crayon-sy">.</span><span class="crayon-v">relu</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-13" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-14" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-p"># Pooling Layer</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-15" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">pool1</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">max_pooling2d</span><span class="crayon-sy">(</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">conv1</span><span class="crayon-sy">,</span> <span class="crayon-v">pool_size</span><span class="crayon-o">=</span><span class="crayon-sy">[</span><span class="crayon-cn">2</span><span class="crayon-sy">,</span> <span class="crayon-cn">2</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span> <span class="crayon-v">strides</span><span class="crayon-o">=</span><span class="crayon-cn">2</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-16" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-17" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-p"># Convolutional Layer #2 and Pooling Layer</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-18" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">conv2</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">conv2d</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-19" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">pool1</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-20" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">filters</span><span class="crayon-o">=</span><span class="crayon-cn">36</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-21" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">kernel_size</span><span class="crayon-o">=</span><span class="crayon-sy">[</span><span class="crayon-cn">5</span><span class="crayon-sy">,</span> <span class="crayon-cn">5</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-22" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">padding</span><span class="crayon-o">=</span><span class="crayon-s">"same"</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-23" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">nn</span><span class="crayon-sy">.</span><span class="crayon-v">relu</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-24" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">pool2</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">max_pooling2d</span><span class="crayon-sy">(</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">conv2</span><span class="crayon-sy">,</span> <span class="crayon-v">pool_size</span><span class="crayon-o">=</span><span class="crayon-sy">[</span><span class="crayon-cn">2</span><span class="crayon-sy">,</span> <span class="crayon-cn">2</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span> <span class="crayon-v">strides</span><span class="crayon-o">=</span><span class="crayon-cn">2</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-25" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-26" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-p"># Dense Layer</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-27" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">pool2_flat</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-e">reshape</span><span class="crayon-sy">(</span><span class="crayon-v">pool2</span><span class="crayon-sy">,</span> <span class="crayon-sy">[</span><span class="crayon-o">-</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span> <span class="crayon-cn">7</span> <span class="crayon-o">*</span> <span class="crayon-cn">7</span> <span class="crayon-o">*</span> <span class="crayon-cn">36</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-28" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">dense</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">dense</span><span class="crayon-sy">(</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">pool2_flat</span><span class="crayon-sy">,</span> <span class="crayon-v">units</span><span class="crayon-o">=</span><span class="crayon-cn">7</span> <span class="crayon-o">*</span> <span class="crayon-cn">7</span> <span class="crayon-o">*</span> <span class="crayon-cn">36</span><span class="crayon-sy">,</span> <span class="crayon-v">activation</span><span class="crayon-o">=</span><span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">nn</span><span class="crayon-sy">.</span><span class="crayon-v">relu</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-29" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">dropout</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">dropout</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-30" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">dense</span><span class="crayon-sy">,</span> <span class="crayon-v">rate</span><span class="crayon-o">=</span><span class="crayon-cn">0.4</span><span class="crayon-sy">,</span> <span class="crayon-v">training</span><span class="crayon-o">=</span><span class="crayon-v">mode</span> <span class="crayon-o">==</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-v">ModeKeys</span><span class="crayon-sy">.</span><span class="crayon-v">TRAIN</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-31" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-32" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-p"># Logits Layer</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-33" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">logits</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">layers</span><span class="crayon-sy">.</span><span class="crayon-e">dense</span><span class="crayon-sy">(</span><span class="crayon-v">inputs</span><span class="crayon-o">=</span><span class="crayon-v">dropout</span><span class="crayon-sy">,</span> <span class="crayon-v">units</span><span class="crayon-o">=</span><span class="crayon-cn">10</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-34" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-35" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">predictions</span> <span class="crayon-o">=</span> <span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-36" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-p"># Generate predictions (for PREDICT and EVAL mode)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-37" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-s">"classes"</span><span class="crayon-o">:</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-e">argmax</span><span class="crayon-sy">(</span><span class="crayon-v">input</span><span class="crayon-o">=</span><span class="crayon-v">logits</span><span class="crayon-sy">,</span> <span class="crayon-v">axis</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-38" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-s">"probabilities"</span><span class="crayon-o">:</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">nn</span><span class="crayon-sy">.</span><span class="crayon-e">softmax</span><span class="crayon-sy">(</span><span class="crayon-v">logits</span><span class="crayon-sy">,</span> <span class="crayon-v">name</span><span class="crayon-o">=</span><span class="crayon-s">"softmax_tensor"</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-39" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-sy">}</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-40" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-41" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-st">if</span> <span class="crayon-v">mode</span> <span class="crayon-o">==</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-v">ModeKeys</span><span class="crayon-sy">.</span><span class="crayon-v">PREDICT</span><span class="crayon-o">:</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-42" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-st">return</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-e">EstimatorSpec</span><span class="crayon-sy">(</span><span class="crayon-v">mode</span><span class="crayon-o">=</span><span class="crayon-v">mode</span><span class="crayon-sy">,</span> <span class="crayon-v">predictions</span><span class="crayon-o">=</span><span class="crayon-v">predictions</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-43" class="crayon-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-44" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-p"># Calculate Loss</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-45" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">loss</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">losses</span><span class="crayon-sy">.</span><span class="crayon-e">sparse_softmax_cross_entropy</span><span class="crayon-sy">(</span><span class="crayon-v">labels</span><span class="crayon-o">=</span><span class="crayon-v">labels</span><span class="crayon-sy">,</span> <span class="crayon-v">logits</span><span class="crayon-o">=</span><span class="crayon-v">logits</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-46" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-47" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-p"># Configure the Training Op (for TRAIN mode)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-48" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-st">if</span> <span class="crayon-v">mode</span> <span class="crayon-o">==</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-v">ModeKeys</span><span class="crayon-sy">.</span><span class="crayon-v">TRAIN</span><span class="crayon-o">:</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-49" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">optimizer</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">train</span><span class="crayon-sy">.</span><span class="crayon-e">GradientDescentOptimizer</span><span class="crayon-sy">(</span><span class="crayon-v">learning_rate</span><span class="crayon-o">=</span><span class="crayon-cn">0.001</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-50" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">train_op</span> <span class="crayon-o">=</span> <span class="crayon-v">optimizer</span><span class="crayon-sy">.</span><span class="crayon-e">minimize</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-51" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">loss</span><span class="crayon-o">=</span><span class="crayon-v">loss</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-52" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">global_step</span><span class="crayon-o">=</span><span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">train</span><span class="crayon-sy">.</span><span class="crayon-e">get_global_step</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-53" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-st">return</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-e">EstimatorSpec</span><span class="crayon-sy">(</span><span class="crayon-v">mode</span><span class="crayon-o">=</span><span class="crayon-v">mode</span><span class="crayon-sy">,</span> <span class="crayon-v">loss</span><span class="crayon-o">=</span><span class="crayon-v">loss</span><span class="crayon-sy">,</span> <span class="crayon-v">train_op</span><span class="crayon-o">=</span><span class="crayon-v">train_op</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-54" class="crayon-line crayon-striped-line"></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-55" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-p"># Add evaluation metrics Evaluation mode</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-56" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-v">eval_metric_ops</span> <span class="crayon-o">=</span> <span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-57" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-s">"accuracy"</span><span class="crayon-o">:</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">metrics</span><span class="crayon-sy">.</span><span class="crayon-e">accuracy</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-58" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">labels</span><span class="crayon-o">=</span><span class="crayon-v">labels</span><span class="crayon-sy">,</span> <span class="crayon-v">predictions</span><span class="crayon-o">=</span><span class="crayon-v">predictions</span><span class="crayon-sy">[</span><span class="crayon-s">"classes"</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span><span class="crayon-sy">}</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-59" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;</span><span class="crayon-st">return</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-e">EstimatorSpec</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d645d360627534-60" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">mode</span><span class="crayon-o">=</span><span class="crayon-v">mode</span><span class="crayon-sy">,</span> <span class="crayon-v">loss</span><span class="crayon-o">=</span><span class="crayon-v">loss</span><span class="crayon-sy">,</span> <span class="crayon-v">eval_metric_ops</span><span class="crayon-o">=</span><span class="crayon-v">eval_metric_ops</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">C&aacute;c bước dưới đ&acirc;y giống như c&aacute;c b&agrave;i hướng dẫn trước.</p> <p style="text-align: justify;">Trước hết, bạn x&aacute;c định một c&ocirc;ng cụ ước t&iacute;nh với m&ocirc; h&igrave;nh CNN.</p> <div id="urvanov-syntax-highlighter-610fefe2d6463498155770" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6463498155770-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6463498155770-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6463498155770-3">3</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d6463498155770-1" class="crayon-line"><span class="crayon-p"># Create the Estimator</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6463498155770-2" class="crayon-line crayon-striped-line"><span class="crayon-v">mnist_classifier</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-e">Estimator</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6463498155770-3" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">model_fn</span><span class="crayon-o">=</span><span class="crayon-v">cnn_model_fn</span><span class="crayon-sy">,</span> <span class="crayon-v">model_dir</span><span class="crayon-o">=</span><span class="crayon-s">"train/mnist_convnet_model"</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Một CNN mất nhiều lần để đ&agrave;o tạo, do đ&oacute;, bạn tạo một Logging hook để lưu trữ c&aacute;c gi&aacute; trị của c&aacute;c lớp softmax sau mỗi 50 lần lặp.</p> <div id="urvanov-syntax-highlighter-610fefe2d6469131236392" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6469131236392-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6469131236392-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6469131236392-3">3</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d6469131236392-1" class="crayon-line"><span class="crayon-p"># Set up logging for predictions</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6469131236392-2" class="crayon-line crayon-striped-line"><span class="crayon-v">tensors_to_log</span> <span class="crayon-o">=</span> <span class="crayon-sy">{</span><span class="crayon-s">"probabilities"</span><span class="crayon-o">:</span> <span class="crayon-s">"softmax_tensor"</span><span class="crayon-sy">}</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6469131236392-3" class="crayon-line"><span class="crayon-v">logging_hook</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">train</span><span class="crayon-sy">.</span><span class="crayon-e">LoggingTensorHook</span><span class="crayon-sy">(</span><span class="crayon-v">tensors</span><span class="crayon-o">=</span><span class="crayon-v">tensors_to_log</span><span class="crayon-sy">,</span> <span class="crayon-v">every_n_iter</span><span class="crayon-o">=</span><span class="crayon-cn">50</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Bạn đ&atilde; sẵn s&agrave;ng để ước t&iacute;nh m&ocirc; h&igrave;nh. Đặt batch size l&agrave; 100 v&agrave; x&aacute;o trộn dữ liệu.</p> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-7">7</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-8">8</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-9">9</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-10">10</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d646c641335788-11">11</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-1" class="crayon-line"><span class="crayon-p"># Train the model</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-2" class="crayon-line crayon-striped-line"><span class="crayon-v">train_input_fn</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-v">inputs</span><span class="crayon-sy">.</span><span class="crayon-e">numpy_input_fn</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-3" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">x</span><span class="crayon-o">=</span><span class="crayon-sy">{</span><span class="crayon-s">"x"</span><span class="crayon-o">:</span> <span class="crayon-v">X_train_scaled</span><span class="crayon-sy">}</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-4" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">y</span><span class="crayon-o">=</span><span class="crayon-v">y_train</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-5" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">batch_size</span><span class="crayon-o">=</span><span class="crayon-cn">100</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-6" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">num_epochs</span><span class="crayon-o">=</span><span class="crayon-v">None</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-7" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">shuffle</span><span class="crayon-o">=</span><span class="crayon-t">True</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-8" class="crayon-line crayon-striped-line"><span class="crayon-v">mnist_classifier</span><span class="crayon-sy">.</span><span class="crayon-e">train</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-9" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">input_fn</span><span class="crayon-o">=</span><span class="crayon-v">train_input_fn</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-10" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">steps</span><span class="crayon-o">=</span><span class="crayon-cn">16000</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646c641335788-11" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">hooks</span><span class="crayon-o">=</span><span class="crayon-sy">[</span><span class="crayon-v">logging_hook</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">B&acirc;y giờ m&ocirc; h&igrave;nh đ&atilde; được đ&agrave;o tạo, bạn c&oacute; thể đ&aacute;nh gi&aacute; v&agrave; in kết quả.</p> <div id="urvanov-syntax-highlighter-610fefe2d646f355303674" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d646f355303674-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d646f355303674-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d646f355303674-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d646f355303674-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d646f355303674-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d646f355303674-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d646f355303674-7">7</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d646f355303674-8">8</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d646f355303674-1" class="crayon-line"><span class="crayon-p"># Evaluate the model and print results</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646f355303674-2" class="crayon-line crayon-striped-line"><span class="crayon-v">eval_input_fn</span> <span class="crayon-o">=</span> <span class="crayon-v">tf</span><span class="crayon-sy">.</span><span class="crayon-v">estimator</span><span class="crayon-sy">.</span><span class="crayon-v">inputs</span><span class="crayon-sy">.</span><span class="crayon-e">numpy_input_fn</span><span class="crayon-sy">(</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646f355303674-3" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">x</span><span class="crayon-o">=</span><span class="crayon-sy">{</span><span class="crayon-s">"x"</span><span class="crayon-o">:</span> <span class="crayon-v">X_test_scaled</span><span class="crayon-sy">}</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646f355303674-4" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">y</span><span class="crayon-o">=</span><span class="crayon-v">y_test</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646f355303674-5" class="crayon-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">num_epochs</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646f355303674-6" class="crayon-line crayon-striped-line"><span class="crayon-h">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="crayon-v">shuffle</span><span class="crayon-o">=</span><span class="crayon-t">False</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646f355303674-7" class="crayon-line"><span class="crayon-v">eval_results</span> <span class="crayon-o">=</span> <span class="crayon-v">mnist_classifier</span><span class="crayon-sy">.</span><span class="crayon-e">evaluate</span><span class="crayon-sy">(</span><span class="crayon-v">input_fn</span><span class="crayon-o">=</span><span class="crayon-v">eval_input_fn</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-610fefe2d646f355303674-8" class="crayon-line crayon-striped-line"><span class="crayon-e">print</span><span class="crayon-sy">(</span><span class="crayon-v">eval_results</span><span class="crayon-sy">)</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182" class="urvanov-syntax-highlighter-syntax crayon-theme-classic urvanov-syntax-highlighter-font-monaco urvanov-syntax-highlighter-os-pc print-yes notranslate" style="text-align: justify;" data-settings=" minimize scroll-mouseover"> <div class="urvanov-syntax-highlighter-plain-wrap">&nbsp;</div> <div class="urvanov-syntax-highlighter-main"> <table class="crayon-table"> <tbody> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content"> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6472301929182-1">1</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6472301929182-2">2</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6472301929182-3">3</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6472301929182-4">4</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6472301929182-5">5</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6472301929182-6">6</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6472301929182-7">7</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6472301929182-8">8</div> <div class="crayon-num" data-line="urvanov-syntax-highlighter-610fefe2d6472301929182-9">9</div> <div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-610fefe2d6472301929182-10">10</div> </div> </td> <td class="urvanov-syntax-highlighter-code"> <div class="crayon-pre"> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182-1" class="crayon-line"><span class="crayon-v">INFO</span><span class="crayon-o">:</span><span class="crayon-v">tensorflow</span><span class="crayon-o">:</span><span class="crayon-e">Calling </span><span class="crayon-v">model_fn</span><span class="crayon-sy">.</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182-2" class="crayon-line crayon-striped-line"><span class="crayon-v">INFO</span><span class="crayon-o">:</span><span class="crayon-v">tensorflow</span><span class="crayon-o">:</span><span class="crayon-e">Done </span><span class="crayon-e">calling </span><span class="crayon-v">model_fn</span><span class="crayon-sy">.</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182-3" class="crayon-line"><span class="crayon-v">INFO</span><span class="crayon-o">:</span><span class="crayon-v">tensorflow</span><span class="crayon-o">:</span><span class="crayon-e">Starting </span><span class="crayon-e">evaluation </span><span class="crayon-i">at</span> <span class="crayon-cn">2018</span><span class="crayon-o">-</span><span class="crayon-cn">08</span><span class="crayon-o">-</span><span class="crayon-cn">05</span><span class="crayon-o">-</span><span class="crayon-cn">12</span><span class="crayon-o">:</span><span class="crayon-cn">52</span><span class="crayon-o">:</span><span class="crayon-cn">41</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182-4" class="crayon-line crayon-striped-line"><span class="crayon-v">INFO</span><span class="crayon-o">:</span><span class="crayon-v">tensorflow</span><span class="crayon-o">:</span><span class="crayon-e">Graph </span><span class="crayon-e">was </span><span class="crayon-v">finalized</span><span class="crayon-sy">.</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182-5" class="crayon-line"><span class="crayon-v">INFO</span><span class="crayon-o">:</span><span class="crayon-v">tensorflow</span><span class="crayon-o">:</span><span class="crayon-e">Restoring </span><span class="crayon-e">parameters </span><span class="crayon-e">from </span><span class="crayon-v">train</span><span class="crayon-o">/</span><span class="crayon-v">mnist_convnet_model</span><span class="crayon-o">/</span><span class="crayon-v">model</span><span class="crayon-sy">.</span><span class="crayon-v">ckpt</span><span class="crayon-o">-</span><span class="crayon-cn">15652</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182-6" class="crayon-line crayon-striped-line"><span class="crayon-v">INFO</span><span class="crayon-o">:</span><span class="crayon-v">tensorflow</span><span class="crayon-o">:</span><span class="crayon-e">Running </span><span class="crayon-v">local_init_op</span><span class="crayon-sy">.</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182-7" class="crayon-line"><span class="crayon-v">INFO</span><span class="crayon-o">:</span><span class="crayon-v">tensorflow</span><span class="crayon-o">:</span><span class="crayon-e">Done </span><span class="crayon-e">running </span><span class="crayon-v">local_init_op</span><span class="crayon-sy">.</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182-8" class="crayon-line crayon-striped-line"><span class="crayon-v">INFO</span><span class="crayon-o">:</span><span class="crayon-v">tensorflow</span><span class="crayon-o">:</span><span class="crayon-e">Finished </span><span class="crayon-e">evaluation </span><span class="crayon-i">at</span> <span class="crayon-cn">2018</span><span class="crayon-o">-</span><span class="crayon-cn">08</span><span class="crayon-o">-</span><span class="crayon-cn">05</span><span class="crayon-o">-</span><span class="crayon-cn">12</span><span class="crayon-o">:</span><span class="crayon-cn">52</span><span class="crayon-o">:</span><span class="crayon-cn">56</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182-9" class="crayon-line"><span class="crayon-v">INFO</span><span class="crayon-o">:</span><span class="crayon-v">tensorflow</span><span class="crayon-o">:</span><span class="crayon-e">Saving </span><span class="crayon-e">dict </span><span class="crayon-st">for</span> <span class="crayon-m">global</span> <span class="crayon-i">step</span> <span class="crayon-cn">15652</span><span class="crayon-o">:</span> <span class="crayon-v">accuracy</span> <span class="crayon-o">=</span> <span class="crayon-cn">0.9589286</span><span class="crayon-sy">,</span> <span class="crayon-v">global_step</span> <span class="crayon-o">=</span> <span class="crayon-cn">15652</span><span class="crayon-sy">,</span> <span class="crayon-v">loss</span> <span class="crayon-o">=</span> <span class="crayon-cn">0.13894269</span></div> <div id="urvanov-syntax-highlighter-610fefe2d6472301929182-10" class="crayon-line crayon-striped-line"><span class="crayon-sy">{</span><span class="crayon-s">'accuracy'</span><span class="crayon-o">:</span> <span class="crayon-cn">0.9689286</span><span class="crayon-sy">,</span> <span class="crayon-s">'loss'</span><span class="crayon-o">:</span> <span class="crayon-cn">0.13894269</span><span class="crayon-sy">,</span> <span class="crayon-s">'global_step'</span><span class="crayon-o">:</span> <span class="crayon-cn">15652</span><span class="crayon-sy">}</span></div> </div> </td> </tr> </tbody> </table> </div> </div> <p style="text-align: justify;">Với kiến ​​tr&uacute;c hiện tại, bạn nhận được độ ch&iacute;nh x&aacute;c l&agrave; 97%. Bạn c&oacute; thể thay đổi kiến ​​tr&uacute;c, batch size v&agrave; số lần lặp để cải thiện độ ch&iacute;nh x&aacute;c. Mạng nơ-ron CNN đ&atilde; hoạt động tốt hơn nhiều so với ANN hoặc hồi quy logistic.</p> <p style="text-align: justify;">B&agrave;i viết tiếp theo:&nbsp;<a href="../../autoencoder-trong-deep-learning-tu-hoc-tensorflow/" target="_blank" rel="noopener">Autoencoder trong Deep Learning &ndash; V&iacute; dụ với TensorFlow</a></p>