{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# 池化层" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T22:14:08.041117Z", "start_time": "2019-07-03T22:14:06.519244Z" }, "attributes": { "classes": [], "id": "", "n": "3" } }, "outputs": [], "source": [ "from mxnet import np, npx\n", "from mxnet.gluon import nn\n", "npx.set_np()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "实现二维池化层。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T22:14:08.174881Z", "start_time": "2019-07-03T22:14:08.043310Z" }, "attributes": { "classes": [], "id": "", "n": "4" } }, "outputs": [ { "data": { "text/plain": [ "array([[4., 5.],\n", " [7., 8.]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def pool2d(X, pool_size, mode='max'):\n", " p_h, p_w = pool_size\n", " Y = np.zeros((X.shape[0] - p_h + 1, X.shape[1] - p_w + 1))\n", " for i in range(Y.shape[0]):\n", " for j in range(Y.shape[1]):\n", " if mode == 'max':\n", " Y[i, j] = np.max(X[i: i + p_h, j: j + p_w])\n", " elif mode == 'avg':\n", " Y[i, j] = X[i: i + p_h, j: j + p_w].mean()\n", " return Y\n", "\n", "X = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])\n", "pool2d(X, (2, 2))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "默认填充和步幅。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T22:14:08.188017Z", "start_time": "2019-07-03T22:14:08.176923Z" }, "attributes": { "classes": [], "id": "", "n": "15" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[[ 0. 1. 2. 3.]\n", " [ 4. 5. 6. 7.]\n", " [ 8. 9. 10. 11.]\n", " [12. 13. 14. 15.]]]]\n" ] }, { "data": { "text/plain": [ "array([[[[10.]]]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = np.arange(16).reshape((1, 1, 4, 4))\n", "print(X)\n", "\n", "pool2d = nn.MaxPool2D(3)\n", "pool2d(X)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "指定填充和步幅。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T22:14:08.195734Z", "start_time": "2019-07-03T22:14:08.189887Z" }, "attributes": { "classes": [], "id": "", "n": "7" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[[ 0. 1. 2. 3.]\n", " [ 4. 5. 6. 7.]\n", " [ 8. 9. 10. 11.]\n", " [12. 13. 14. 15.]]]]\n" ] }, { "data": { "text/plain": [ "array([[[[ 5., 7.],\n", " [13., 15.]]]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(X)\n", "pool2d = nn.MaxPool2D(3, padding=1, strides=2)\n", "pool2d(X)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "多通道。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2019-07-03T22:14:08.217621Z", "start_time": "2019-07-03T22:14:08.197280Z" }, "attributes": { "classes": [], "id": "", "n": "9" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[[[ 0. 1. 2. 3.]\n", " [ 4. 5. 6. 7.]\n", " [ 8. 9. 10. 11.]\n", " [12. 13. 14. 15.]]\n", "\n", " [[ 1. 2. 3. 4.]\n", " [ 5. 6. 7. 8.]\n", " [ 9. 10. 11. 12.]\n", " [13. 14. 15. 16.]]]]\n" ] }, { "data": { "text/plain": [ "array([[[[ 5., 7.],\n", " [13., 15.]],\n", "\n", " [[ 6., 8.],\n", " [14., 16.]]]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = np.concatenate((X, X + 1), axis=1)\n", "print(X)\n", "pool2d = nn.MaxPool2D(3, padding=1, strides=2)\n", "pool2d(X)" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }