赞
踩
- <body class="notebook_app command_mode" data-base-url="/user/bxycpfikvlfnfowcbjepjo/" data-ws-url="" data-notebook-name="Logistic%20Regression%20with%20a%20Neural%20Network%20mindset%20v5.ipynb" data-notebook-path="Week%202/Logistic%20Regression%20as%20a%20Neural%20Network/Logistic%20Regression%20with%20a%20Neural%20Network%20mindset%20v5.ipynb" style=""><div style="visibility: hidden; overflow: hidden; position: absolute; top: 0px; height: 1px; width: auto; padding: 0px; border: 0px; margin: 0px; text-align: left; text-indent: 0px; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal;"><div id="MathJax_Hidden"><span class="MathJax MathJax_Processing" id="MathJax-Element-1-Frame" tabindex="0" style=""><nobr><span class="math" id="MathJax-Span-1" role="math"><span style="display: inline-block; position: relative; width: 0.003em; height: 0px; font-size: 120%;"><span style="position: absolute;"><span class="mrow" id="MathJax-Span-2"><span class="msubsup" id="MathJax-Span-3"></span></span></span></span></span></nobr></span><br></div></div><div id="MathJax_Message" style="">Typesetting math: 0%</div>
-
- <noscript>
- <div id='noscript'>
- Jupyter Notebook requires JavaScript.<br>
- Please enable it to proceed.
- </div>
- </noscript>
-
- <div id="header" style="display: block;">
- <div id="header-container" class="container">
- <div id="ipython_notebook" class="nav navbar-brand pull-left"><a href="/user/bxycpfikvlfnfowcbjepjo/tree" title="dashboard">
- <img src="/hub/logo" alt="Jupyter Notebook">
- </a></div>
-
-
-
-
-
-
- <span id="login_widget">
-
- <button id="logout" class="btn btn-sm navbar-btn">Logout</button>
-
- </span>
-
-
-
-
-
- <a href="/hub/home" class="btn btn-default btn-sm navbar-btn pull-right" style="margin-right: 4px; margin-left: 2px;">
- Control Panel</a>
-
-
-
-
-
- <span id="save_widget" class="pull-left save_widget">
- <span id="notebook_name" class="filename">Logistic Regression with a Neural Network mindset v5</span>
- <span class="checkpoint_status" title="Sun, Aug 12, 2018 11:24 AM">Last Checkpoint: 08/12/2018</span>
- <span class="autosave_status">(autosaved)</span>
- </span>
-
- <span id="kernel_logo_widget">
-
- <img class="current_kernel_logo" src="/user/bxycpfikvlfnfowcbjepjo/kernelspecs/python3/logo-64x64.png" style="display: inline;">
-
- </span>
-
-
- </div>
- <div class="header-bar"></div>
-
-
- <div id="menubar-container" class="container">
- <div id="menubar">
- <div id="menus" class="navbar navbar-default" role="navigation">
- <div class="container-fluid">
- <button type="button" class="btn btn-default navbar-btn navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
- <i class="fa fa-bars"></i>
- <span class="navbar-text">Menu</span>
- </button>
- <p id="kernel_indicator" class="navbar-text indicator_area">
- <span class="kernel_indicator_name">Python 3</span>
- <i id="kernel_indicator_icon" class="kernel_idle_icon" title="Kernel Idle"></i>
- </p>
- <i id="readonly-indicator" class="navbar-text" title="This notebook is read-only" style="display: none;">
- <span class="fa-stack">
- <i class="fa fa-save fa-stack-1x"></i>
- <i class="fa fa-ban fa-stack-2x text-danger"></i>
- </span>
- </i>
- <i id="modal_indicator" class="navbar-text modal_indicator" title="Command Mode"></i>
- <span id="notification_area"><div id="notification_kernel" class="notification_widget btn btn-xs navbar-btn undefined info" style="display: none;"><span></span></div><div id="notification_notebook" class="notification_widget btn btn-xs navbar-btn" style="display: none;"><span></span></div></span>
- <div class="navbar-collapse collapse">
- <ul class="nav navbar-nav">
- <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">File</a>
- <ul id="file_menu" class="dropdown-menu">
- <li id="new_notebook" class="dropdown-submenu">
- <a href="#">New Notebook</a>
- <ul class="dropdown-menu" id="menu-new-notebook-submenu"><li id="new-notebook-submenu-python3"><a href="#">Python 3</a></li></ul>
- </li>
- <li id="open_notebook" title="Opens a new window with the Dashboard view">
- <a href="#">Open...</a></li>
- <!-- <hr/> -->
- <li class="divider"></li>
- <li id="copy_notebook" title="Open a copy of this notebook's contents and start a new kernel">
- <a href="#">Make a Copy...</a></li>
- <li id="rename_notebook"><a href="#">Rename...</a></li>
- <li id="save_checkpoint"><a href="#">Save and Checkpoint</a></li>
- <!-- <hr/> -->
- <li class="divider"></li>
- <li id="restore_checkpoint" class="dropdown-submenu"><a href="#">Revert to Checkpoint</a>
- <ul class="dropdown-menu"><li><a href="#">Sunday, August 12, 2018 11:24 AM</a></li></ul>
- </li>
- <li class="divider"></li>
- <li id="print_preview"><a href="#">Print Preview</a></li>
- <li class="dropdown-submenu"><a href="#">Download as</a>
- <ul class="dropdown-menu">
- <li id="download_ipynb"><a href="#">Notebook (.ipynb)</a></li>
- <li id="download_script"><a href="#">Python (.py)</a></li>
- <li id="download_html"><a href="#">HTML (.html)</a></li>
- <li id="download_markdown"><a href="#">Markdown (.md)</a></li>
- <li id="download_rst"><a href="#">reST (.rst)</a></li>
- <li id="download_pdf"><a href="#">PDF via LaTeX (.pdf)</a></li>
- </ul>
- </li>
- <li class="divider"></li>
- <li id="trust_notebook" title="Trust the output of this notebook">
- <a href="#">Trust Notebook</a></li>
- <li class="divider"></li>
- <li id="kill_and_exit" title="Shutdown this notebook's kernel, and close this window">
- <a href="#">Close and Halt</a></li>
- </ul>
- </li>
- <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Edit</a>
- <ul id="edit_menu" class="dropdown-menu">
- <li id="cut_cell"><a href="#">Cut Cells</a></li>
- <li id="copy_cell"><a href="#">Copy Cells</a></li>
- <li id="paste_cell_above" class=""><a href="#">Paste Cells Above</a></li>
- <li id="paste_cell_below" class=""><a href="#">Paste Cells Below</a></li>
- <li id="paste_cell_replace" class=""><a href="#">Paste Cells & Replace</a></li>
- <li id="delete_cell"><a href="#">Delete Cells</a></li>
- <li id="undelete_cell" class="disabled"><a href="#">Undo Delete Cells</a></li>
- <li class="divider"></li>
- <li id="split_cell"><a href="#">Split Cell</a></li>
- <li id="merge_cell_above"><a href="#">Merge Cell Above</a></li>
- <li id="merge_cell_below"><a href="#">Merge Cell Below</a></li>
- <li class="divider"></li>
- <li id="move_cell_up"><a href="#">Move Cell Up</a></li>
- <li id="move_cell_down"><a href="#">Move Cell Down</a></li>
- <li class="divider"></li>
- <li id="edit_nb_metadata"><a href="#">Edit Notebook Metadata</a></li>
- <li class="divider"></li>
- <li id="find_and_replace"><a href="#"> Find and Replace </a></li>
- </ul>
- </li>
- <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">View</a>
- <ul id="view_menu" class="dropdown-menu">
- <li id="toggle_header" title="Show/Hide the logo and notebook title (above menu bar)">
- <a href="#">Toggle Header</a></li>
- <li id="toggle_toolbar" title="Show/Hide the action icons (below menu bar)">
- <a href="#">Toggle Toolbar</a></li>
- <li id="menu-cell-toolbar" class="dropdown-submenu">
- <a href="#">Cell Toolbar</a>
- <ul class="dropdown-menu" id="menu-cell-toolbar-submenu"><li data-name="None"><a href="#">None</a></li><li data-name="Edit%20Metadata"><a href="#">Edit Metadata</a></li><li data-name="Raw%20Cell%20Format"><a href="#">Raw Cell Format</a></li><li data-name="Slideshow"><a href="#">Slideshow</a></li></ul>
- </li>
- </ul>
- </li>
- <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Insert</a>
- <ul id="insert_menu" class="dropdown-menu">
- <li id="insert_cell_above" title="Insert an empty Code cell above the currently active cell">
- <a href="#">Insert Cell Above</a></li>
- <li id="insert_cell_below" title="Insert an empty Code cell below the currently active cell">
- <a href="#">Insert Cell Below</a></li>
- </ul>
- </li>
- <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Cell</a>
- <ul id="cell_menu" class="dropdown-menu">
- <li id="run_cell" title="Run this cell, and move cursor to the next one">
- <a href="#">Run Cells</a></li>
- <li id="run_cell_select_below" title="Run this cell, select below">
- <a href="#">Run Cells and Select Below</a></li>
- <li id="run_cell_insert_below" title="Run this cell, insert below">
- <a href="#">Run Cells and Insert Below</a></li>
- <li id="run_all_cells" title="Run all cells in the notebook">
- <a href="#">Run All</a></li>
- <li id="run_all_cells_above" title="Run all cells above (but not including) this cell">
- <a href="#">Run All Above</a></li>
- <li id="run_all_cells_below" title="Run this cell and all cells below it">
- <a href="#">Run All Below</a></li>
- <li class="divider"></li>
- <li id="change_cell_type" class="dropdown-submenu" title="All cells in the notebook have a cell type. By default, new cells are created as 'Code' cells">
- <a href="#">Cell Type</a>
- <ul class="dropdown-menu">
- <li id="to_code" title="Contents will be sent to the kernel for execution, and output will display in the footer of cell">
- <a href="#">Code</a></li>
- <li id="to_markdown" title="Contents will be rendered as HTML and serve as explanatory text">
- <a href="#">Markdown</a></li>
- <li id="to_raw" title="Contents will pass through nbconvert unmodified">
- <a href="#">Raw NBConvert</a></li>
- </ul>
- </li>
- <li class="divider"></li>
- <li id="current_outputs" class="dropdown-submenu"><a href="#">Current Outputs</a>
- <ul class="dropdown-menu">
- <li id="toggle_current_output" title="Hide/Show the output of the current cell">
- <a href="#">Toggle</a>
- </li>
- <li id="toggle_current_output_scroll" title="Scroll the output of the current cell">
- <a href="#">Toggle Scrolling</a>
- </li>
- <li id="clear_current_output" title="Clear the output of the current cell">
- <a href="#">Clear</a>
- </li>
- </ul>
- </li>
- <li id="all_outputs" class="dropdown-submenu"><a href="#">All Output</a>
- <ul class="dropdown-menu">
- <li id="toggle_all_output" title="Hide/Show the output of all cells">
- <a href="#">Toggle</a>
- </li>
- <li id="toggle_all_output_scroll" title="Scroll the output of all cells">
- <a href="#">Toggle Scrolling</a>
- </li>
- <li id="clear_all_output" title="Clear the output of all cells">
- <a href="#">Clear</a>
- </li>
- </ul>
- </li>
- </ul>
- </li>
- <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Kernel</a>
- <ul id="kernel_menu" class="dropdown-menu">
- <li id="int_kernel" title="Send KeyboardInterrupt (CTRL-C) to the Kernel">
- <a href="#">Interrupt</a>
- </li>
- <li id="restart_kernel" title="Restart the Kernel">
- <a href="#">Restart</a>
- </li>
- <li id="restart_clear_output" title="Restart the Kernel and clear all output">
- <a href="#">Restart & Clear Output</a>
- </li>
- <li id="restart_run_all" title="Restart the Kernel and re-run the notebook">
- <a href="#">Restart & Run All</a>
- </li>
- <li id="reconnect_kernel" title="Reconnect to the Kernel">
- <a href="#">Reconnect</a>
- </li>
- <li class="divider"></li>
- <li id="menu-change-kernel" class="dropdown-submenu">
- <a href="#">Change kernel</a>
- <ul class="dropdown-menu" id="menu-change-kernel-submenu"><li id="kernel-submenu-python3"><a href="#">Python 3</a></li></ul>
- </li>
- </ul>
- </li>
- <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Help</a>
- <ul id="help_menu" class="dropdown-menu">
-
- <li id="notebook_tour" title="A quick tour of the notebook user interface"><a href="#">User Interface Tour</a></li>
- <li id="keyboard_shortcuts" title="Opens a tooltip with all keyboard shortcuts"><a href="#">Keyboard Shortcuts</a></li>
- <li class="divider"></li>
-
-
-
-
- <li><a rel="noreferrer" href="http://nbviewer.ipython.org/github/ipython/ipython/blob/3.x/examples/Notebook/Index.ipynb" target="_blank" title="Opens in a new window">
-
- <i class="fa fa-external-link menu-icon pull-right"></i>
-
-
- Notebook Help
- </a></li>
-
- <li><a rel="noreferrer" href="https://help.github.com/articles/markdown-basics/" target="_blank" title="Opens in a new window">
-
- <i class="fa fa-external-link menu-icon pull-right"></i>
-
-
- Markdown
- </a></li>
-
-
-
- <li id="kernel-help-links" class="divider"></li><li><a target="_blank" title="Opens in a new window" href="http://docs.python.org/3.5?v=20180921142330"><i class="fa fa-external-link menu-icon pull-right"></i><span>Python</span></a></li><li><a target="_blank" title="Opens in a new window" href="http://ipython.org/documentation.html?v=20180921142330"><i class="fa fa-external-link menu-icon pull-right"></i><span>IPython</span></a></li><li><a target="_blank" title="Opens in a new window" href="http://docs.scipy.org/doc/numpy/reference/?v=20180921142330"><i class="fa fa-external-link menu-icon pull-right"></i><span>NumPy</span></a></li><li><a target="_blank" title="Opens in a new window" href="http://docs.scipy.org/doc/scipy/reference/?v=20180921142330"><i class="fa fa-external-link menu-icon pull-right"></i><span>SciPy</span></a></li><li><a target="_blank" title="Opens in a new window" href="http://matplotlib.org/contents.html?v=20180921142330"><i class="fa fa-external-link menu-icon pull-right"></i><span>Matplotlib</span></a></li><li><a target="_blank" title="Opens in a new window" href="http://docs.sympy.org/latest/index.html?v=20180921142330"><i class="fa fa-external-link menu-icon pull-right"></i><span>SymPy</span></a></li><li><a target="_blank" title="Opens in a new window" href="http://pandas.pydata.org/pandas-docs/stable/?v=20180921142330"><i class="fa fa-external-link menu-icon pull-right"></i><span>pandas</span></a></li><li class="divider"></li>
- <li title="About Jupyter Notebook"><a id="notebook_about" href="#">About</a></li>
-
- </ul>
- </li>
- </ul>
- </div>
- </div>
- </div>
- </div>
-
- <div id="maintoolbar" class="navbar">
- <div class="toolbar-inner navbar-inner navbar-nobg">
- <div id="maintoolbar-container" class="container toolbar"><div class="btn-group" id="save-notbook"><button class="btn btn-default" title="Save and Checkpoint" data-jupyter-action="jupyter-notebook:save-notebook"><i class="fa-save fa"></i></button></div><div class="btn-group" id="insert_above_below"><button class="btn btn-default" title="insert cell below" data-jupyter-action="jupyter-notebook:insert-cell-below"><i class="fa-plus fa"></i></button></div><div class="btn-group" id="cut_copy_paste"><button class="btn btn-default" title="cut selected cells" data-jupyter-action="jupyter-notebook:cut-cell"><i class="fa-cut fa"></i></button><button class="btn btn-default" title="copy selected cells" data-jupyter-action="jupyter-notebook:copy-cell"><i class="fa-copy fa"></i></button><button class="btn btn-default" title="paste cells below" data-jupyter-action="jupyter-notebook:paste-cell-below"><i class="fa-paste fa"></i></button></div><div class="btn-group" id="move_up_down"><button class="btn btn-default" title="move selected cells up" data-jupyter-action="jupyter-notebook:move-cell-up"><i class="fa-arrow-up fa"></i></button><button class="btn btn-default" title="move selected cells down" data-jupyter-action="jupyter-notebook:move-cell-down"><i class="fa-arrow-down fa"></i></button></div><div class="btn-group" id="run_int"><button class="btn btn-default" title="run cell, select below" data-jupyter-action="jupyter-notebook:run-cell-and-select-next"><i class="fa-step-forward fa"></i></button><button class="btn btn-default" title="interrupt kernel" data-jupyter-action="jupyter-notebook:interrupt-kernel"><i class="fa-stop fa"></i></button><button class="btn btn-default" title="restart the kernel (with dialog)" data-jupyter-action="jupyter-notebook:confirm-restart-kernel"><i class="fa-repeat fa"></i></button></div><select id="cell_type" class="form-control select-xs"><option value="code">Code</option><option value="markdown">Markdown</option><option value="raw">Raw NBConvert</option><option value="heading">Heading</option><option value="multiselect" disabled="disabled" style="display: none;">-</option></select><div class="btn-group"><button class="btn btn-default" title="open the command palette" data-jupyter-action="jupyter-notebook:show-command-palette"><i class="fa-keyboard-o fa"></i></button></div><div class="btn-group"><button title="show new celltoolbar selector location" class="btn btn-default">CellToolbar</button></div><div class="btn-group" id="submit-notebook-button-group" style="float:right"><button class="btn btn-default" title="Submit this notebook for grading." id="submit-notebook-button" style="background-color:rgb(42, 115, 204); color:white; padding:4px 8px">Submit Assignment</button></div></div>
- </div>
- </div>
- </div>
-
- <div class="lower-header-bar"></div>
-
- </div>
-
- <div id="site" style="height: 497px; display: block;">
-
-
- <div id="ipython-main-app">
- <div id="notebook_panel">
- <div id="notebook" tabindex="-1"><div class="container" id="notebook-container"><div class="cell text_cell unrendered selected" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 325px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors" style=""><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 22.5px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-1"># Logistic Regression with a Neural Network mindset</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Welcome to your first (required) programming assignment! You will build a logistic regression classifier to recognize cats. This assignment will step you through how to do this with a Neural Network mindset, and so will also hone your intuitions about deep learning.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Instructions:**</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">- Do not use loops (for/while) in your code, unless the instructions explicitly ask you to do so.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**You will learn to:**</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">- Build the general architecture of a learning algorithm, including:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> - Initializing parameters</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> - Calculating the cost function and its gradient</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> - Using an optimization algorithm (gradient descent) </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">- Gather all three functions above into a main model function, in the right order.</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 325px;"></div><div class="CodeMirror-gutters" style="display: none; height: 338px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h1 id="Logistic-Regression-with-a-Neural-Network-mindset">Logistic Regression with a Neural Network mindset<a class="anchor-link" href="#Logistic-Regression-with-a-Neural-Network-mindset">¶</a></h1>
- <p>Welcome to your first (required) programming assignment! You will build a logistic regression classifier to recognize cats. This assignment will step you through how to do this with a Neural Network mindset, and so will also hone your intuitions about deep learning.</p>
- <p><strong>Instructions:</strong></p>
- <ul>
- <li>Do not use loops (for/while) in your code, unless the instructions explicitly ask you to do so.</li>
- </ul>
- <p><strong>You will learn to:</strong></p>
- <ul>
- <li>Build the general architecture of a learning algorithm, including:<ul>
- <li>Initializing parameters</li>
- <li>Calculating the cost function and its gradient</li>
- <li>Using an optimization algorithm (gradient descent) </li>
- </ul>
- </li>
- <li>Gather all three functions above into a main model function, in the right order.</li>
- </ul>
- </div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 150px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 20px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-2">## 1 - Packages ##</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">First, let's run the cell below to import all the packages that you will need during this assignment. </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- </span><span class="cm-link cm-variable-2">[numpy]</span><span class="cm-string cm-url cm-variable-2">(www.numpy.org)</span><span class="cm-variable-2"> is the fundamental package for scientific computing with Python.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- </span><span class="cm-link cm-variable-2">[h5py]</span><span class="cm-string cm-url cm-variable-2">(http://www.h5py.org)</span><span class="cm-variable-2"> is a common package to interact with a dataset that is stored on an H5 file.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- </span><span class="cm-link cm-variable-2">[matplotlib]</span><span class="cm-string cm-url cm-variable-2">(http://matplotlib.org)</span><span class="cm-variable-2"> is a famous library to plot graphs in Python.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- </span><span class="cm-link cm-variable-2">[PIL]</span><span class="cm-string cm-url cm-variable-2">(http://www.pythonware.com/products/pil/)</span><span class="cm-variable-2"> and </span><span class="cm-link cm-variable-2">[scipy]</span><span class="cm-string cm-url cm-variable-2">(https://www.scipy.org/)</span><span class="cm-variable-2"> are used here to test your model with your own picture at the end.</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 150px;"></div><div class="CodeMirror-gutters" style="display: none; height: 163px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h2 id="1---Packages">1 - Packages<a class="anchor-link" href="#1---Packages">¶</a></h2>
- <p>First, let's run the cell below to import all the packages that you will need during this assignment. </p>
- <ul>
- <li><a href="www.numpy.org" target="_blank">numpy</a> is the fundamental package for scientific computing with Python.</li>
- <li><a href="http://www.h5py.org" target="_blank">h5py</a> is a common package to interact with a dataset that is stored on an H5 file.</li>
- <li><a href="http://matplotlib.org" target="_blank">matplotlib</a> is a famous library to plot graphs in Python.</li>
- <li><a href="http://www.pythonware.com/products/pil/" target="_blank">PIL</a> and <a href="https://www.scipy.org/" target="_blank">scipy</a> are used here to test your model with your own picture at the end.</li>
- </ul>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [5]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 236.734px; margin-bottom: -17px; border-right-width: 13px; min-height: 164px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">import</span> <span class="cm-variable">numpy</span> <span class="cm-keyword">as</span> <span class="cm-variable">np</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">import</span> <span class="cm-variable">matplotlib</span>.<span class="cm-variable">pyplot</span> <span class="cm-keyword">as</span> <span class="cm-variable">plt</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">import</span> <span class="cm-variable">h5py</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">import</span> <span class="cm-variable">scipy</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">from</span> <span class="cm-variable">PIL</span> <span class="cm-keyword">import</span> <span class="cm-variable">Image</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">from</span> <span class="cm-variable">scipy</span> <span class="cm-keyword">import</span> <span class="cm-variable">ndimage</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">from</span> <span class="cm-variable">lr_utils</span> <span class="cm-keyword">import</span> <span class="cm-variable">load_dataset</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-operator">%</span><span class="cm-variable">matplotlib</span> <span class="cm-variable">inline</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 164px;"></div><div class="CodeMirror-gutters" style="display: none; height: 177px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stderr"><pre>/opt/conda/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
- warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
- /opt/conda/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
- warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
- </pre></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 218px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-2">## 2 - Overview of the Problem set ##</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Problem Statement**</span>: You are given a dataset ("data.h5") containing:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- a training set of m_train images labeled as cat (y=1) or non-cat (y=0)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- a test set of m_test images labeled as cat or non-cat</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB). Thus, each image is square (height = num_px) and (width = num_px).</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">You will build a simple image-recognition algorithm that can correctly classify pictures as cat or non-cat.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Let's get more familiar with the dataset. Load the data by running the following code.</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 218px;"></div><div class="CodeMirror-gutters" style="display: none; height: 231px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h2 id="2---Overview-of-the-Problem-set">2 - Overview of the Problem set<a class="anchor-link" href="#2---Overview-of-the-Problem-set">¶</a></h2>
- <p><strong>Problem Statement</strong>: You are given a dataset ("data.h5") containing:</p>
- <pre><code>- a training set of m_train images labeled as cat (y=1) or non-cat (y=0)
- - a test set of m_test images labeled as cat or non-cat
- - each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB). Thus, each image is square (height = num_px) and (width = num_px).
- </code></pre><p>You will build a simple image-recognition algorithm that can correctly classify pictures as cat or non-cat.</p>
- <p>Let's get more familiar with the dataset. Load the data by running the following code.</p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [7]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 591px; margin-bottom: -17px; border-right-width: 13px; min-height: 45px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># Loading the data (cat/non-cat)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">train_set_x_orig</span>, <span class="cm-variable">train_set_y</span>, <span class="cm-variable">test_set_x_orig</span>, <span class="cm-variable">test_set_y</span>, <span class="cm-variable">classes</span> = <span class="cm-variable">load_dataset</span>()</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 45px;"></div><div class="CodeMirror-gutters" style="display: none; height: 58px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 96px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 16.5px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">We added "<span class="cm-em">_orig" at the end of image datasets (train and test) because we are going to preprocess them. After preprocessing, we will end up with train_set_x and test_set_x (the labels train_set_y and test_set_y don't need any preprocessing).</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Each line of your train_set_x_orig and test_set_x_orig is an array representing an image. You can visualize an example by running the following code. Feel free also to change the <span class="cm-comment">`index`</span> value and re-run to see other images. </span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 96px;"></div><div class="CodeMirror-gutters" style="display: none; height: 109px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p>We added "_orig" at the end of image datasets (train and test) because we are going to preprocess them. After preprocessing, we will end up with train_set_x and test_set_x (the labels train_set_y and test_set_y don't need any preprocessing).</p>
- <p>Each line of your train_set_x_orig and test_set_x_orig is an array representing an image. You can visualize an example by running the following code. Feel free also to change the <code>index</code> value and re-run to see other images. </p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [6]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 949px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 949.063px; margin-bottom: -17px; border-right-width: 13px; min-height: 79px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># Example of a picture</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">index</span> = <span class="cm-number">25</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">imshow</span>(<span class="cm-variable">train_set_x_orig</span>[<span class="cm-variable">index</span>])</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"y = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">train_set_y</span>[:, <span class="cm-variable">index</span>]) <span class="cm-operator">+</span> <span class="cm-string">", it's a '"</span> <span class="cm-operator">+</span> <span class="cm-variable">classes</span>[<span class="cm-variable">np</span>.<span class="cm-variable">squeeze</span>(<span class="cm-variable">train_set_y</span>[:, <span class="cm-variable">index</span>])].<span class="cm-variable">decode</span>(<span class="cm-string">"utf-8"</span>) <span class="cm-operator">+</span> <span class="cm-string">"' picture."</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 79px;"></div><div class="CodeMirror-gutters" style="display: none; height: 92px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>y = [1], it's a 'cat' picture.
- </pre></div></div><div class="output_area"><div class="prompt"></div><div class="output_subarea output_png"><img src="https://img-blog.csdnimg.cn/2022010707321485443.png"></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 164px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 16.5px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Many software bugs in deep learning come from having matrix/vector dimensions that don't fit. If you can keep your matrix/vector dimensions straight you will go a long way toward eliminating many bugs. </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Exercise:**</span> Find the values for:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- m_train (number of training examples)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- m_test (number of test examples)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- num_px (= height = width of a training image)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Remember that <span class="cm-comment">`train_set_x_orig`</span> is a numpy-array of shape (m_train, num_px, num_px, 3). For instance, you can access <span class="cm-comment">`m_train`</span> by writing <span class="cm-comment">`train_set_x_orig.shape[0]`</span>.</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 164px;"></div><div class="CodeMirror-gutters" style="display: none; height: 177px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p>Many software bugs in deep learning come from having matrix/vector dimensions that don't fit. If you can keep your matrix/vector dimensions straight you will go a long way toward eliminating many bugs. </p>
- <p><strong>Exercise:</strong> Find the values for:</p>
- <pre><code>- m_train (number of training examples)
- - m_test (number of test examples)
- - num_px (= height = width of a training image)
- </code></pre><p>Remember that <code>train_set_x_orig</code> is a numpy-array of shape (m_train, num_px, num_px, 3). For instance, you can access <code>m_train</code> by writing <code>train_set_x_orig.shape[0]</code>.</p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [8]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 550.063px; margin-bottom: -17px; border-right-width: 13px; min-height: 249px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment">### START CODE HERE ### (≈ 3 lines of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">m_train</span> = <span class="cm-variable">train_set_x_orig</span>.<span class="cm-variable">shape</span>[<span class="cm-number">0</span>]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">m_test</span> =<span class="cm-variable">test_set_x_orig</span>.<span class="cm-variable">shape</span>[<span class="cm-number">0</span>]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">num_px</span> = <span class="cm-variable">train_set_x_orig</span>.<span class="cm-variable">shape</span>[<span class="cm-number">1</span>]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"Number of training examples: m_train = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">m_train</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"Number of testing examples: m_test = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">m_test</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"Height/Width of each image: num_px = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">num_px</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"Each image is of size: ("</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">num_px</span>) <span class="cm-operator">+</span> <span class="cm-string">", "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">num_px</span>) <span class="cm-operator">+</span> <span class="cm-string">", 3)"</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"train_set_x shape: "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">train_set_x_orig</span>.<span class="cm-variable">shape</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"train_set_y shape: "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">train_set_y</span>.<span class="cm-variable">shape</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"test_set_x shape: "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">test_set_x_orig</span>.<span class="cm-variable">shape</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"test_set_y shape: "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">test_set_y</span>.<span class="cm-variable">shape</span>))</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 249px;"></div><div class="CodeMirror-gutters" style="display: none; height: 262px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>Number of training examples: m_train = 209
- Number of testing examples: m_test = 50
- Height/Width of each image: num_px = 64
- Each image is of size: (64, 64, 3)
- train_set_x shape: (209, 64, 64, 3)
- train_set_y shape: (1, 209)
- test_set_x shape: (50, 64, 64, 3)
- test_set_y shape: (1, 50)
- </pre></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 334px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Expected Output for m_train, m_test and num_px**</span>: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">table</span> <span class="cm-attribute">style</span>=<span class="cm-string">"width:15%"</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>**m_train**<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 209 <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>**m_test**<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 50 <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>**num_px**<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 64 <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"></</span><span class="cm-tag">table</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 334px;"></div><div class="CodeMirror-gutters" style="display: none; height: 347px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Expected Output for m_train, m_test and num_px</strong>: </p>
- <table style="width: 15%">
- <tbody><tr>
- <td><strong>m_train</strong></td>
- <td> 209 </td>
- </tr>
-
- <tr>
- <td><strong>m_test</strong></td>
- <td> 50 </td>
- </tr>
-
- <tr>
- <td><strong>num_px</strong></td>
- <td> 64 </td>
- </tr>
-
- </tbody></table>
- </div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 198px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 16.5px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">For convenience, you should now reshape images of shape (num_px, num_px, 3) in a numpy-array of shape (num_px <span class="cm-delimit cm-delimit-open">$</span>*<span class="cm-delimit cm-delimit-close">$</span> num_px <span class="cm-delimit cm-delimit-open">$</span>*<span class="cm-delimit cm-delimit-close">$</span> 3, 1). After this, our training (and test) dataset is a numpy-array where each column represents a flattened image. There should be m_train (respectively m_test) columns.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Exercise:**</span> Reshape the training and test data sets so that images of size (num_px, num_px, 3) are flattened into single vectors of shape (num\_px <span class="cm-delimit cm-delimit-open">$</span>*<span class="cm-delimit cm-delimit-close">$</span> num\_px <span class="cm-delimit cm-delimit-open">$</span>*<span class="cm-delimit cm-delimit-close">$</span> 3, 1).</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b<span class="cm-delimit cm-delimit-open">$</span>*<span class="cm-delimit cm-delimit-close">$</span>c<span class="cm-delimit cm-delimit-open">$</span>*<span class="cm-delimit cm-delimit-close">$</span>d, a) is to use: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment">```python</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">X_flatten</span> = <span class="cm-variable">X</span>.<span class="cm-variable">reshape</span>(<span class="cm-variable">X</span>.<span class="cm-variable">shape</span>[<span class="cm-number">0</span>], <span class="cm-operator">-</span><span class="cm-number">1</span>).<span class="cm-variable">T</span> <span class="cm-comment"># X.T is the transpose of X</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment">```</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 198px;"></div><div class="CodeMirror-gutters" style="display: none; height: 211px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p>For convenience, you should now reshape images of shape (num_px, num_px, 3) in a numpy-array of shape (num_px $*$ num_px $*$ 3, 1). After this, our training (and test) dataset is a numpy-array where each column represents a flattened image. There should be m_train (respectively m_test) columns.</p>
- <p><strong>Exercise:</strong> Reshape the training and test data sets so that images of size (num_px, num_px, 3) are flattened into single vectors of shape (num_px $*$ num_px $*$ 3, 1).</p>
- <p>A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b$*$c$*$d, a) is to use: </p>
- <pre><code class="cm-s-ipython language-python"><span class="cm-variable">X_flatten</span> = <span class="cm-variable">X</span>.<span class="cm-variable">reshape</span>(<span class="cm-variable">X</span>.<span class="cm-variable">shape</span>[<span class="cm-number">0</span>], <span class="cm-operator">-</span><span class="cm-number">1</span>).<span class="cm-variable">T</span> <span class="cm-comment"># X.T is the transpose of X</span>
- </code></pre>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [9]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 542.266px; margin-bottom: -17px; border-right-width: 13px; min-height: 215px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># Reshape the training and test examples</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment">### START CODE HERE ### (≈ 2 lines of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">train_set_x_flatten</span> =<span class="cm-variable">train_set_x_orig</span>.<span class="cm-variable">reshape</span>(<span class="cm-variable">train_set_x_orig</span>.<span class="cm-variable">shape</span>[<span class="cm-number">0</span>],<span class="cm-operator">-</span><span class="cm-number">1</span>).<span class="cm-variable">T</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">test_set_x_flatten</span> =<span class="cm-variable">test_set_x_orig</span>.<span class="cm-variable">reshape</span>(<span class="cm-variable">test_set_x_orig</span>.<span class="cm-variable">shape</span>[<span class="cm-number">0</span>],<span class="cm-operator">-</span><span class="cm-number">1</span>).<span class="cm-variable">T</span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"train_set_x_flatten shape: "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">train_set_x_flatten</span>.<span class="cm-variable">shape</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"train_set_y shape: "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">train_set_y</span>.<span class="cm-variable">shape</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"test_set_x_flatten shape: "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">test_set_x_flatten</span>.<span class="cm-variable">shape</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"test_set_y shape: "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">test_set_y</span>.<span class="cm-variable">shape</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"sanity check after reshaping: "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">train_set_x_flatten</span>[<span class="cm-number">0</span>:<span class="cm-number">5</span>,<span class="cm-number">0</span>]))</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 215px;"></div><div class="CodeMirror-gutters" style="display: none; height: 228px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>train_set_x_flatten shape: (12288, 209)
- train_set_y shape: (1, 209)
- test_set_x_flatten shape: (12288, 50)
- test_set_y shape: (1, 50)
- sanity check after reshaping: [17 31 56 22 33]
- </pre></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 419px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Expected Output**</span>: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">table</span> <span class="cm-attribute">style</span>=<span class="cm-string">"width:35%"</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>**train_set_x_flatten shape**<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> (12288, 209)<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>**train_set_y shape**<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>(1, 209)<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>**test_set_x_flatten shape**<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>(12288, 50)<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>**test_set_y shape**<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>(1, 50)<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>**sanity check after reshaping**<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>[17 31 56 22 33]<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"></</span><span class="cm-tag">table</span><span class="cm-tag cm-bracket">></span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 419px;"></div><div class="CodeMirror-gutters" style="display: none; height: 432px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Expected Output</strong>: </p>
- <table style="width: 35%">
- <tbody><tr>
- <td><strong>train_set_x_flatten shape</strong></td>
- <td> (12288, 209)</td>
- </tr>
- <tr>
- <td><strong>train_set_y shape</strong></td>
- <td>(1, 209)</td>
- </tr>
- <tr>
- <td><strong>test_set_x_flatten shape</strong></td>
- <td>(12288, 50)</td>
- </tr>
- <tr>
- <td><strong>test_set_y shape</strong></td>
- <td>(1, 50)</td>
- </tr>
- <tr>
- <td><strong>sanity check after reshaping</strong></td>
- <td>[17 31 56 22 33]</td>
- </tr>
- </tbody></table></div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 215px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 16.5px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">To represent color images, the red, green and blue channels (RGB) must be specified for each pixel, and so the pixel value is actually a vector of three numbers ranging from 0 to 255.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">One common preprocessing step in machine learning is to center and standardize your dataset, meaning that you substract the mean of the whole numpy array from each example, and then divide each example by the standard deviation of the whole numpy array. But for picture datasets, it is simpler and more convenient and works almost as well to just divide every row of the dataset by 255 (the maximum value of a pixel channel).</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"><!-- During the training of your model, you're going to multiply weights and add biases to some initial inputs in order to observe neuron activations. Then you backpropogate with the gradients to train the model. But, it is extremely important for each feature to have a similar range such that our gradients don't explode. You will see that more in detail later in the lectures. !--></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Let's standardize our dataset.</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 215px;"></div><div class="CodeMirror-gutters" style="display: none; height: 228px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p>To represent color images, the red, green and blue channels (RGB) must be specified for each pixel, and so the pixel value is actually a vector of three numbers ranging from 0 to 255.</p>
- <p>One common preprocessing step in machine learning is to center and standardize your dataset, meaning that you substract the mean of the whole numpy array from each example, and then divide each example by the standard deviation of the whole numpy array. But for picture datasets, it is simpler and more convenient and works almost as well to just divide every row of the dataset by 255 (the maximum value of a pixel channel).</p>
-
- <p>Let's standardize our dataset.</p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [10]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 269px; margin-bottom: -17px; border-right-width: 13px; min-height: 45px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">train_set_x</span> = <span class="cm-variable">train_set_x_flatten</span><span class="cm-operator">/</span><span class="cm-number">255.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">test_set_x</span> = <span class="cm-variable">test_set_x_flatten</span><span class="cm-operator">/</span><span class="cm-number">255.</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 45px;"></div><div class="CodeMirror-gutters" style="display: none; height: 58px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 130px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">font</span> <span class="cm-attribute">color</span>=<span class="cm-string">'blue'</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">**What you need to remember:**</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Common steps for pre-processing a new dataset are:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">- Figure out the dimensions and shapes of the problem (m_train, m_test, num_px, ...)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">- Reshape the datasets such that each example is now a vector of size (num_px \* num_px \* 3, 1)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">- "Standardize" the data</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 130px;"></div><div class="CodeMirror-gutters" style="display: none; height: 143px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><font color="blue">
- <strong>What you need to remember:</strong>
- </font></p><p><font color="blue">Common steps for pre-processing a new dataset are:</font></p><font color="blue">
- <ul>
- <li>Figure out the dimensions and shapes of the problem (m_train, m_test, num_px, ...)</li>
- <li>Reshape the datasets such that each example is now a vector of size (num_px * num_px * 3, 1)</li>
- <li>"Standardize" the data</li>
- </ul>
- </font><p></p></div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 439px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 20px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-2">## 3 - General Architecture of the learning algorithm ##</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">It's time to design a simple algorithm to distinguish cat images from non-cat images.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">You will build a Logistic Regression, using a Neural Network mindset. The following Figure explains why <span class="cm-strong">**Logistic Regression is actually a very simple Neural Network!**</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">img</span> <span class="cm-attribute">src</span>=<span class="cm-string">"images/LogReg_kiank.png"</span> <span class="cm-attribute">style</span>=<span class="cm-string">"width:650px;height:400px;"</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Mathematical expression of the algorithm**</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">For one example <span class="cm-delimit cm-delimit-open">$</span>x^<span class="cm-bracket">{</span>(i)<span class="cm-bracket">}</span><span class="cm-delimit cm-delimit-close">$</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span>z^{(i)} = w^T x^{(i)} + b \tag{1}<span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span>\hat{y}^{(i)} = a^{(i)} = sigmoid(z^{(i)})\tag{2}<span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span> \mathcal{L}(a^{(i)}, y^{(i)}) = - y^{(i)} \log(a^{(i)}) - (1-y^{(i)} ) \log(1-a^{(i)})\tag{3}<span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">The cost is then computed by summing over all training examples:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span> J = \frac{1}{m} \sum<span class="cm-em">_{i=1}^m \mathcal{L}(a^{(i)}, y^{(i)})\tag{6}</span><span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Key steps**</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">In this exercise, you will carry out the following steps: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- Initialize the parameters of the model</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- Learn the parameters for the model by minimizing the cost </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- Use the learned parameters to make predictions (on the test set)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- Analyse the results and conclude</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 439px;"></div><div class="CodeMirror-gutters" style="display: none; height: 452px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h2 id="3---General-Architecture-of-the-learning-algorithm">3 - General Architecture of the learning algorithm<a class="anchor-link" href="#3---General-Architecture-of-the-learning-algorithm">¶</a></h2>
- <p>It's time to design a simple algorithm to distinguish cat images from non-cat images.</p>
- <p>You will build a Logistic Regression, using a Neural Network mindset. The following Figure explains why <strong>Logistic Regression is actually a very simple Neural Network!</strong></p>
- <p><img src="images/LogReg_kiank.png" style="width: 650px ; height: 400px"></p>
- <p><strong>Mathematical expression of the algorithm</strong>:</p>
- <p>For one example $x^{(i)}$:
- $$z^{(i)} = w^T x^{(i)} + b \tag{1}$$
- $$\hat{y}^{(i)} = a^{(i)} = sigmoid(z^{(i)})\tag{2}$$
- $$ \mathcal{L}(a^{(i)}, y^{(i)}) = - y^{(i)} \log(a^{(i)}) - (1-y^{(i)} ) \log(1-a^{(i)})\tag{3}$$</p>
- <p>The cost is then computed by summing over all training examples:
- $$ J = \frac{1}{m} \sum_{i=1}^m \mathcal{L}(a^{(i)}, y^{(i)})\tag{6}$$</p>
- <p><strong>Key steps</strong>:
- In this exercise, you will carry out the following steps: </p>
- <pre><code>- Initialize the parameters of the model
- - Learn the parameters for the model by minimizing the cost
- - Use the learned parameters to make predictions (on the test set)
- - Analyse the results and conclude
- </code></pre></div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 287px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 20px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-2">## 4 - Building the parts of our algorithm ## </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">The main steps for building a Neural Network are:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">1. Define the model structure (such as number of input features) </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">2. Initialize the model's parameters</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">3. Loop:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable-3">- Calculate current loss (forward propagation)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable-2">- Calculate current gradient (backward propagation)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable-2">- Update parameters (gradient descent)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">You often build 1-3 separately and integrate them into one function we call <span class="cm-comment">`model()`</span>.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-3">### 4.1 - Helper functions</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Exercise**</span>: Using your code from "Python Basics", implement <span class="cm-comment">`sigmoid()`</span>. As you've seen in the figure above, you need to compute <span class="cm-delimit cm-delimit-open">$</span>sigmoid( w^T x + b) = <span class="cm-tag">\frac</span><span class="cm-bracket">{</span><span class="cm-atom">1</span><span class="cm-bracket">}{</span><span class="cm-atom">1</span> + e^<span class="cm-bracket">{</span>-(w^T x + b)<span class="cm-bracket">}}</span><span class="cm-delimit cm-delimit-close">$</span> to make predictions. Use np.exp().</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 287px;"></div><div class="CodeMirror-gutters" style="display: none; height: 300px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h2 id="4---Building-the-parts-of-our-algorithm">4 - Building the parts of our algorithm<a class="anchor-link" href="#4---Building-the-parts-of-our-algorithm">¶</a></h2>
- <p>The main steps for building a Neural Network are:</p>
- <ol>
- <li>Define the model structure (such as number of input features) </li>
- <li>Initialize the model's parameters</li>
- <li>Loop:<ul>
- <li>Calculate current loss (forward propagation)</li>
- <li>Calculate current gradient (backward propagation)</li>
- <li>Update parameters (gradient descent)</li>
- </ul>
- </li>
- </ol>
- <p>You often build 1-3 separately and integrate them into one function we call <code>model()</code>.</p>
- <h3 id="4.1---Helper-functions">4.1 - Helper functions<a class="anchor-link" href="#4.1---Helper-functions">¶</a></h3>
- <p><strong>Exercise</strong>: Using your code from "Python Basics", implement <code>sigmoid()</code>. As you've seen in the figure above, you need to compute $sigmoid( w^T x + b) = \frac{1}{1 + e^{-(w^T x + b)}}$ to make predictions. Use np.exp().</p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [11]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 332px; margin-bottom: -17px; border-right-width: 13px; min-height: 317px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># GRADED FUNCTION: sigmoid</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">def</span> <span class="cm-def">sigmoid</span>(<span class="cm-variable">z</span>):</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"""</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Compute the sigmoid of z</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Arguments:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> z -- A scalar or numpy array of any size.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Return:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> s -- sigmoid(z)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> """</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### START CODE HERE ### (≈ 1 line of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">s</span> = <span class="cm-number">1</span><span class="cm-operator">/</span>(<span class="cm-number">1</span><span class="cm-operator">+</span><span class="cm-variable">np</span>.<span class="cm-variable">exp</span>(<span class="cm-operator">-</span><span class="cm-variable">z</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">s</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 317px;"></div><div class="CodeMirror-gutters" style="display: none; height: 330px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [12]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 423px; margin-bottom: -17px; border-right-width: 13px; min-height: 28px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"sigmoid([0, 2]) = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">sigmoid</span>(<span class="cm-variable">np</span>.<span class="cm-variable">array</span>([<span class="cm-number">0</span>,<span class="cm-number">2</span>]))))</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 28px;"></div><div class="CodeMirror-gutters" style="display: none; height: 41px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>sigmoid([0, 2]) = [ 0.5 0.88079708]
- </pre></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 147px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Expected Output**</span>: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">table</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>**sigmoid([0, 2])**<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> [ 0.5 0.88079708]<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"></</span><span class="cm-tag">table</span><span class="cm-tag cm-bracket">></span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 147px;"></div><div class="CodeMirror-gutters" style="display: none; height: 160px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Expected Output</strong>: </p>
- <table>
- <tbody><tr>
- <td><strong>sigmoid([0, 2])</strong></td>
- <td> [ 0.5 0.88079708]</td>
- </tr>
- </tbody></table></div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 80px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 18px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-3">### 4.2 - Initializing parameters</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Exercise:**</span> Implement parameter initialization in the cell below. You have to initialize w as a vector of zeros. If you don't know what numpy function to use, look up np.zeros() in the Numpy library's documentation.</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 80px;"></div><div class="CodeMirror-gutters" style="display: none; height: 93px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h3 id="4.2---Initializing-parameters">4.2 - Initializing parameters<a class="anchor-link" href="#4.2---Initializing-parameters">¶</a></h3>
- <p><strong>Exercise:</strong> Implement parameter initialization in the cell below. You have to initialize w as a vector of zeros. If you don't know what numpy function to use, look up np.zeros() in the Numpy library's documentation.</p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [13]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 640px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 640px; margin-bottom: -17px; border-right-width: 13px; min-height: 402px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># GRADED FUNCTION: initialize_with_zeros</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">def</span> <span class="cm-def">initialize_with_zeros</span>(<span class="cm-variable">dim</span>):</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"""</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> This function creates a vector of zeros of shape (dim, 1) for w and initializes b to 0.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Argument:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> dim -- size of the w vector we want (or number of parameters in this case)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Returns:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> w -- initialized vector of shape (dim, 1)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> b -- initialized scalar (corresponds to the bias)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> """</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### START CODE HERE ### (≈ 1 line of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">w</span> = <span class="cm-variable">np</span>.<span class="cm-variable">zeros</span>((<span class="cm-variable">dim</span>,<span class="cm-number">1</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">b</span> = <span class="cm-number">0</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">assert</span>(<span class="cm-variable">w</span>.<span class="cm-variable">shape</span> <span class="cm-operator">==</span> (<span class="cm-variable">dim</span>, <span class="cm-number">1</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">assert</span>(<span class="cm-builtin">isinstance</span>(<span class="cm-variable">b</span>, <span class="cm-builtin">float</span>) <span class="cm-keyword">or</span> <span class="cm-builtin">isinstance</span>(<span class="cm-variable">b</span>, <span class="cm-builtin">int</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">w</span>, <span class="cm-variable">b</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 402px;"></div><div class="CodeMirror-gutters" style="display: none; height: 415px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [14]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 234px; margin-bottom: -17px; border-right-width: 13px; min-height: 79px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">dim</span> = <span class="cm-number">2</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">w</span>, <span class="cm-variable">b</span> = <span class="cm-variable">initialize_with_zeros</span>(<span class="cm-variable">dim</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"w = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">w</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"b = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">b</span>))</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 79px;"></div><div class="CodeMirror-gutters" style="display: none; height: 92px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>w = [[ 0.]
- [ 0.]]
- b = 0
- </pre></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 283px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Expected Output**</span>: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">table</span> <span class="cm-attribute">style</span>=<span class="cm-string">"width:15%"</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> ** w ** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> [[ 0.]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> [ 0.]] <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> ** b ** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 0 <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"></</span><span class="cm-tag">table</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">For image inputs, w will be of shape (num_px <span class="cm-delimit cm-delimit-open">$</span><span class="cm-tag">\times</span><span class="cm-delimit cm-delimit-close">$</span> num_px <span class="cm-delimit cm-delimit-open">$</span><span class="cm-tag">\times</span><span class="cm-delimit cm-delimit-close">$</span> 3, 1).</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 283px;"></div><div class="CodeMirror-gutters" style="display: none; height: 296px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Expected Output</strong>: </p>
- <table style="width: 15%">
- <tbody><tr>
- <td> <strong> w </strong> </td>
- <td> [[ 0.]
- [ 0.]] </td>
- </tr>
- <tr>
- <td> <strong> b </strong> </td>
- <td> 0 </td>
- </tr>
- </tbody></table>
-
- <p>For image inputs, w will be of shape (num_px $\times$ num_px $\times$ 3, 1).</p>
- </div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 301px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 18px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-3">### 4.3 - Forward and Backward propagation</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Now that your parameters are initialized, you can do the "forward" and "backward" propagation steps for learning the parameters.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Exercise:**</span> Implement a function <span class="cm-comment">`propagate()`</span> that computes the cost function and its gradient.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Hints**</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Forward Propagation:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- You get X</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- You compute </span><span class="cm-delimit cm-delimit-open">$</span>A = <span class="cm-tag">\sigma</span>(w^T X + b) = (a^<span class="cm-bracket">{</span>(1)<span class="cm-bracket">}</span>, a^<span class="cm-bracket">{</span>(2)<span class="cm-bracket">}</span>, ..., a^<span class="cm-bracket">{</span>(m-1)<span class="cm-bracket">}</span>, a^<span class="cm-bracket">{</span>(m)<span class="cm-bracket">}</span>)<span class="cm-delimit cm-delimit-close">$</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- You calculate the cost function: </span><span class="cm-delimit cm-delimit-open">$</span>J = -<span class="cm-tag">\frac</span><span class="cm-bracket">{</span><span class="cm-atom">1</span><span class="cm-bracket">}{</span>m<span class="cm-bracket">}</span><span class="cm-tag">\sum</span>_<span class="cm-bracket">{</span>i=1<span class="cm-bracket">}</span>^<span class="cm-bracket">{</span>m<span class="cm-bracket">}</span>y^<span class="cm-bracket">{</span>(i)<span class="cm-bracket">}</span><span class="cm-tag">\log</span>(a^<span class="cm-bracket">{</span>(i)<span class="cm-bracket">}</span>)+(1-y^<span class="cm-bracket">{</span>(i)<span class="cm-bracket">}</span>)<span class="cm-tag">\log</span>(1-a^<span class="cm-bracket">{</span>(i)<span class="cm-bracket">}</span>)<span class="cm-delimit cm-delimit-close">$</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Here are the two formulas you will be using: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span> \frac{\partial J}{\partial w} = \frac{1}{m}X(A-Y)^T\tag{7}<span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span> \frac{\partial J}{\partial b} = \frac{1}{m} \sum<span class="cm-em">_{i=1}^m (a^{(i)}-y^{(i)})\tag{8}</span><span class="cm-delimit cm-delimit-open">$</span><span class="cm-delimit cm-delimit-close">$</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 301px;"></div><div class="CodeMirror-gutters" style="display: none; height: 314px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h3 id="4.3---Forward-and-Backward-propagation">4.3 - Forward and Backward propagation<a class="anchor-link" href="#4.3---Forward-and-Backward-propagation">¶</a></h3>
- <p>Now that your parameters are initialized, you can do the "forward" and "backward" propagation steps for learning the parameters.</p>
- <p><strong>Exercise:</strong> Implement a function <code>propagate()</code> that computes the cost function and its gradient.</p>
- <p><strong>Hints</strong>:</p>
- <p>Forward Propagation:</p>
- <ul>
- <li>You get X</li>
- <li>You compute $A = \sigma(w^T X + b) = (a^{(1)}, a^{(2)}, ..., a^{(m-1)}, a^{(m)})$</li>
- <li>You calculate the cost function: $J = -\frac{1}{m}\sum_{i=1}^{m}y^{(i)}\log(a^{(i)})+(1-y^{(i)})\log(1-a^{(i)})$</li>
- </ul>
- <p>Here are the two formulas you will be using: </p>
- <p>$$ \frac{\partial J}{\partial w} = \frac{1}{m}X(A-Y)^T\tag{7}$$
- $$ \frac{\partial J}{\partial b} = \frac{1}{m} \sum_{i=1}^m (a^{(i)}-y^{(i)})\tag{8}$$</p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [27]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 675px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 675px; margin-bottom: -17px; border-right-width: 13px; min-height: 793px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># GRADED FUNCTION: propagate</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">def</span> <span class="cm-def">propagate</span>(<span class="cm-variable">w</span>, <span class="cm-variable">b</span>, <span class="cm-variable">X</span>, <span class="cm-variable">Y</span>):</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"""</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Implement the cost function and its gradient for the propagation explained above</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Arguments:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> w -- weights, a numpy array of size (num_px * num_px * 3, 1)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> b -- bias, a scalar</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> X -- data of size (num_px * num_px * 3, number of examples)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Y -- true "label" vector (containing 0 if non-cat, 1 if cat) of size (1, number of examples)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Return:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> cost -- negative log-likelihood cost for logistic regression</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> dw -- gradient of the loss with respect to w, thus same shape as w</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> db -- gradient of the loss with respect to b, thus same shape as b</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Tips:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> - Write your code step by step for the propagation. np.log(), np.dot()</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> """</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">m</span> = <span class="cm-variable">X</span>.<span class="cm-variable">shape</span>[<span class="cm-number">1</span>]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># FORWARD PROPAGATION (FROM X TO COST)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### START CODE HERE ### (≈ 2 lines of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">z</span>=<span class="cm-variable">np</span>.<span class="cm-variable">dot</span>(<span class="cm-variable">w</span>.<span class="cm-variable">T</span>,<span class="cm-variable">X</span>)<span class="cm-operator">+</span><span class="cm-variable">b</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">A</span> = <span class="cm-variable">sigmoid</span>(<span class="cm-variable">z</span>) <span class="cm-comment"># compute activation</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">cost</span> = (<span class="cm-operator">-</span><span class="cm-number">1</span><span class="cm-operator">/</span><span class="cm-variable">m</span> )<span class="cm-operator">*</span><span class="cm-variable">np</span>.<span class="cm-builtin">sum</span>(<span class="cm-variable">np</span>.<span class="cm-variable">multiply</span>(<span class="cm-variable">Y</span>,<span class="cm-variable">np</span>.<span class="cm-variable">log</span>(<span class="cm-variable">A</span>))<span class="cm-operator">+</span><span class="cm-variable">np</span>.<span class="cm-variable">multiply</span>((<span class="cm-number">1</span><span class="cm-operator">-</span><span class="cm-variable">Y</span>),<span class="cm-variable">np</span>.<span class="cm-variable">log</span>(<span class="cm-number">1</span><span class="cm-operator">-</span><span class="cm-variable">A</span>)))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment cm-error"># cost = (-1/m )*np.sum(Y*np.logA+(1-Y)*np.log(1-A)) # compute cost</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># BACKWARD PROPAGATION (TO FIND GRAD)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### START CODE HERE ### (≈ 2 lines of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">dw</span> = (<span class="cm-number">1</span><span class="cm-operator">/</span><span class="cm-variable">m</span>)<span class="cm-operator">*</span><span class="cm-variable">np</span>.<span class="cm-variable">dot</span>(<span class="cm-variable">X</span>,(<span class="cm-variable">A</span><span class="cm-operator">-</span><span class="cm-variable">Y</span>).<span class="cm-variable">T</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">db</span> = (<span class="cm-number">1</span><span class="cm-operator">/</span><span class="cm-variable">m</span>)<span class="cm-operator">*</span><span class="cm-variable">np</span>.<span class="cm-builtin">sum</span>(<span class="cm-variable">A</span><span class="cm-operator">-</span><span class="cm-variable">Y</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">assert</span>(<span class="cm-variable">dw</span>.<span class="cm-variable">shape</span> <span class="cm-operator">==</span> <span class="cm-variable">w</span>.<span class="cm-variable">shape</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">assert</span>(<span class="cm-variable">db</span>.<span class="cm-variable">dtype</span> <span class="cm-operator">==</span> <span class="cm-builtin">float</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">cost</span> = <span class="cm-variable">np</span>.<span class="cm-variable">squeeze</span>(<span class="cm-variable">cost</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">assert</span>(<span class="cm-variable">cost</span>.<span class="cm-variable">shape</span> <span class="cm-operator">==</span> ())</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">grads</span> = {<span class="cm-string">"dw"</span>: <span class="cm-variable">dw</span>,</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"db"</span>: <span class="cm-variable">db</span>}</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">grads</span>, <span class="cm-variable">cost</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 793px;"></div><div class="CodeMirror-gutters" style="display: none; height: 806px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [28]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 683px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 682.531px; margin-bottom: -17px; border-right-width: 13px; min-height: 96px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">w</span>, <span class="cm-variable">b</span>, <span class="cm-variable">X</span>, <span class="cm-variable">Y</span> = <span class="cm-variable">np</span>.<span class="cm-variable">array</span>([[<span class="cm-number">1.</span>],[<span class="cm-number">2.</span>]]), <span class="cm-number">2.</span>, <span class="cm-variable">np</span>.<span class="cm-variable">array</span>([[<span class="cm-number">1.</span>,<span class="cm-number">2.</span>,<span class="cm-operator">-</span><span class="cm-number">1.</span>],[<span class="cm-number">3.</span>,<span class="cm-number">4.</span>,<span class="cm-operator">-</span><span class="cm-number">3.2</span>]]), <span class="cm-variable">np</span>.<span class="cm-variable">array</span>([[<span class="cm-number">1</span>,<span class="cm-number">0</span>,<span class="cm-number">1</span>]])</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">grads</span>, <span class="cm-variable">cost</span> = <span class="cm-variable">propagate</span>(<span class="cm-variable">w</span>, <span class="cm-variable">b</span>, <span class="cm-variable">X</span>, <span class="cm-variable">Y</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"dw = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">grads</span>[<span class="cm-string">"dw"</span>]))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"db = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">grads</span>[<span class="cm-string">"db"</span>]))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"cost = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">cost</span>))</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 96px;"></div><div class="CodeMirror-gutters" style="display: none; height: 109px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>dw = [[ 0.99845601]
- [ 2.39507239]]
- db = 0.00145557813678
- cost = 5.80154531939
- </pre></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 317px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Expected Output**</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">table</span> <span class="cm-attribute">style</span>=<span class="cm-string">"width:50%"</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> ** dw ** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> [[ 0.99845601]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> [ 2.39507239]]<span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> ** db ** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 0.00145557813678 <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> ** cost ** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 5.801545319394553 <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"></</span><span class="cm-tag">table</span><span class="cm-tag cm-bracket">></span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 317px;"></div><div class="CodeMirror-gutters" style="display: none; height: 330px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Expected Output</strong>:</p>
- <table style="width: 50%">
- <tbody><tr>
- <td> <strong> dw </strong> </td>
- <td> [[ 0.99845601]
- [ 2.39507239]]</td>
- </tr>
- <tr>
- <td> <strong> db </strong> </td>
- <td> 0.00145557813678 </td>
- </tr>
- <tr>
- <td> <strong> cost </strong> </td>
- <td> 5.801545319394553 </td>
- </tr>
-
- </tbody></table></div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 131px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 18px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-3">### 4.4 - Optimization</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- You have initialized your parameters.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- You are also able to compute a cost function and its gradient.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- Now, you want to update the parameters using gradient descent.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Exercise:**</span> Write down the optimization function. The goal is to learn <span class="cm-delimit cm-delimit-open">$</span>w<span class="cm-delimit cm-delimit-close">$</span> and <span class="cm-delimit cm-delimit-open">$</span>b<span class="cm-delimit cm-delimit-close">$</span> by minimizing the cost function <span class="cm-delimit cm-delimit-open">$</span>J<span class="cm-delimit cm-delimit-close">$</span>. For a parameter <span class="cm-delimit cm-delimit-open">$</span><span class="cm-tag">\theta</span><span class="cm-delimit cm-delimit-close">$</span>, the update rule is <span class="cm-delimit cm-delimit-open">$</span> <span class="cm-tag">\theta</span> = <span class="cm-tag">\theta</span> - <span class="cm-tag">\alpha</span> <span class="cm-tag">\text</span><span class="cm-bracket">{</span> <span class="cm-bracket">}</span> d<span class="cm-tag">\theta</span><span class="cm-delimit cm-delimit-close">$</span>, where <span class="cm-delimit cm-delimit-open">$</span><span class="cm-tag">\alpha</span><span class="cm-delimit cm-delimit-close">$</span> is the learning rate.</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 131px;"></div><div class="CodeMirror-gutters" style="display: none; height: 144px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h3 id="4.4---Optimization">4.4 - Optimization<a class="anchor-link" href="#4.4---Optimization">¶</a></h3>
- <ul>
- <li>You have initialized your parameters.</li>
- <li>You are also able to compute a cost function and its gradient.</li>
- <li>Now, you want to update the parameters using gradient descent.</li>
- </ul>
- <p><strong>Exercise:</strong> Write down the optimization function. The goal is to learn $w$ and $b$ by minimizing the cost function $J$. For a parameter $\theta$, the update rule is $ \theta = \theta - \alpha \text{ } d\theta$, where $\alpha$ is the learning rate.</p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [29]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 801px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 801px; margin-bottom: -17px; border-right-width: 13px; min-height: 1048px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># GRADED FUNCTION: optimize</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">def</span> <span class="cm-def">optimize</span>(<span class="cm-variable">w</span>, <span class="cm-variable">b</span>, <span class="cm-variable">X</span>, <span class="cm-variable">Y</span>, <span class="cm-variable">num_iterations</span>, <span class="cm-variable">learning_rate</span>, <span class="cm-variable">print_cost</span> = <span class="cm-keyword">False</span>):</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"""</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> This function optimizes w and b by running a gradient descent algorithm</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Arguments:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> w -- weights, a numpy array of size (num_px * num_px * 3, 1)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> b -- bias, a scalar</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> X -- data of shape (num_px * num_px * 3, number of examples)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Y -- true "label" vector (containing 0 if non-cat, 1 if cat), of shape (1, number of examples)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> num_iterations -- number of iterations of the optimization loop</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> learning_rate -- learning rate of the gradient descent update rule</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> print_cost -- True to print the loss every 100 steps</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Returns:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> params -- dictionary containing the weights w and bias b</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> grads -- dictionary containing the gradients of the weights and bias with respect to the cost function</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> costs -- list of all the costs computed during the optimization, this will be used to plot the learning curve.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Tips:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> You basically need to write down two steps and iterate through them:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> 1) Calculate the cost and the gradient for the current parameters. Use propagate().</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> 2) Update the parameters using gradient descent rule for w and b.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> """</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">costs</span> = []</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">for</span> <span class="cm-variable">i</span> <span class="cm-keyword">in</span> <span class="cm-builtin">range</span>(<span class="cm-variable">num_iterations</span>):</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># Cost and gradient calculation (≈ 1-4 lines of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### START CODE HERE ### </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">grads</span>, <span class="cm-variable">cost</span> = <span class="cm-variable">propagate</span>(<span class="cm-variable">w</span>, <span class="cm-variable">b</span>, <span class="cm-variable">X</span>, <span class="cm-variable">Y</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># Retrieve derivatives from grads</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">dw</span> = <span class="cm-variable">grads</span>[<span class="cm-string">"dw"</span>]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">db</span> = <span class="cm-variable">grads</span>[<span class="cm-string">"db"</span>]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># update rule (≈ 2 lines of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### START CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">w</span> = <span class="cm-variable">w</span><span class="cm-operator">-</span><span class="cm-variable">learning_rate</span><span class="cm-operator">*</span><span class="cm-variable">dw</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">b</span> = <span class="cm-variable">b</span><span class="cm-operator">-</span><span class="cm-variable">learning_rate</span><span class="cm-operator">*</span><span class="cm-variable">db</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># Record the costs</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> <span class="cm-variable">i</span> <span class="cm-operator">%</span> <span class="cm-number">100</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">costs</span>.<span class="cm-variable">append</span>(<span class="cm-variable">cost</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># Print the cost every 100 training iterations</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> <span class="cm-variable">print_cost</span> <span class="cm-keyword">and</span> <span class="cm-variable">i</span> <span class="cm-operator">%</span> <span class="cm-number">100</span> <span class="cm-operator">==</span> <span class="cm-number">0</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-builtin">print</span> (<span class="cm-string">"Cost after iteration %i: %f"</span> <span class="cm-operator">%</span>(<span class="cm-variable">i</span>, <span class="cm-variable">cost</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">params</span> = {<span class="cm-string">"w"</span>: <span class="cm-variable">w</span>,</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"b"</span>: <span class="cm-variable">b</span>}</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">grads</span> = {<span class="cm-string">"dw"</span>: <span class="cm-variable">dw</span>,</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"db"</span>: <span class="cm-variable">db</span>}</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">params</span>, <span class="cm-variable">grads</span>, <span class="cm-variable">costs</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 1048px;"></div><div class="CodeMirror-gutters" style="display: none; height: 1061px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [30]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 753px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 753.359px; margin-bottom: -17px; border-right-width: 13px; min-height: 113px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">params</span>, <span class="cm-variable">grads</span>, <span class="cm-variable">costs</span> = <span class="cm-variable">optimize</span>(<span class="cm-variable">w</span>, <span class="cm-variable">b</span>, <span class="cm-variable">X</span>, <span class="cm-variable">Y</span>, <span class="cm-variable">num_iterations</span>= <span class="cm-number">100</span>, <span class="cm-variable">learning_rate</span> = <span class="cm-number">0.009</span>, <span class="cm-variable">print_cost</span> = <span class="cm-keyword">False</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"w = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">params</span>[<span class="cm-string">"w"</span>]))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"b = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">params</span>[<span class="cm-string">"b"</span>]))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"dw = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">grads</span>[<span class="cm-string">"dw"</span>]))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"db = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">grads</span>[<span class="cm-string">"db"</span>]))</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 113px;"></div><div class="CodeMirror-gutters" style="display: none; height: 126px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>w = [[ 0.19033591]
- [ 0.12259159]]
- b = 1.92535983008
- dw = [[ 0.67752042]
- [ 1.41625495]]
- db = 0.219194504541
- </pre></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 419px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Expected Output**</span>: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">table</span> <span class="cm-attribute">style</span>=<span class="cm-string">"width:40%"</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> **w** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>[[ 0.19033591]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> [ 0.12259159]] <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> **b** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 1.92535983008 <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> **dw** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> [[ 0.67752042]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> [ 1.41625495]] <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> **db** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 0.219194504541 <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"></</span><span class="cm-tag">table</span><span class="cm-tag cm-bracket">></span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 419px;"></div><div class="CodeMirror-gutters" style="display: none; height: 432px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Expected Output</strong>: </p>
- <table style="width: 40%">
- <tbody><tr>
- <td> <strong>w</strong> </td>
- <td>[[ 0.19033591]
- [ 0.12259159]] </td>
- </tr>
-
- <tr>
- <td> <strong>b</strong> </td>
- <td> 1.92535983008 </td>
- </tr>
- <tr>
- <td> <strong>dw</strong> </td>
- <td> [[ 0.67752042]
- [ 1.41625495]] </td>
- </tr>
- <tr>
- <td> <strong>db</strong> </td>
- <td> 0.219194504541 </td>
- </tr>
-
- </tbody></table></div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 130px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 16.5px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Exercise:**</span> The previous function will output the learned w and b. We are able to use w and b to predict the labels for a dataset X. Implement the <span class="cm-comment">`predict()`</span> function. There are two steps to computing predictions:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">1. Calculate </span><span class="cm-delimit cm-delimit-open">$</span><span class="cm-tag">\hat</span><span class="cm-bracket">{</span>Y<span class="cm-bracket">}</span> = A = <span class="cm-tag">\sigma</span>(w^T X + b)<span class="cm-delimit cm-delimit-close">$</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">2. Convert the entries of a into 0 (if activation <= 0.5) or 1 (if activation > 0.5), stores the predictions in a vector </span><span class="cm-comment cm-variable-2">`Y_prediction`</span><span class="cm-variable-2">. If you wish, you can use an </span><span class="cm-comment cm-variable-2">`if`</span><span class="cm-variable-2">/</span><span class="cm-comment cm-variable-2">`else`</span><span class="cm-variable-2"> statement in a </span><span class="cm-comment cm-variable-2">`for`</span><span class="cm-variable-2"> loop (though there is also a way to vectorize this). </span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 130px;"></div><div class="CodeMirror-gutters" style="display: none; height: 143px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Exercise:</strong> The previous function will output the learned w and b. We are able to use w and b to predict the labels for a dataset X. Implement the <code>predict()</code> function. There are two steps to computing predictions:</p>
- <ol>
- <li><p>Calculate $\hat{Y} = A = \sigma(w^T X + b)$</p>
- </li>
- <li><p>Convert the entries of a into 0 (if activation <= 0.5) or 1 (if activation > 0.5), stores the predictions in a vector <code>Y_prediction</code>. If you wish, you can use an <code>if</code>/<code>else</code> statement in a <code>for</code> loop (though there is also a way to vectorize this). </p>
- </li>
- </ol>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [33]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 682px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 682px; margin-bottom: -17px; border-right-width: 13px; min-height: 640px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># GRADED FUNCTION: predict</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">def</span> <span class="cm-def">predict</span>(<span class="cm-variable">w</span>, <span class="cm-variable">b</span>, <span class="cm-variable">X</span>):</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">'''</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Predict whether the label is 0 or 1 using learned logistic regression parameters (w, b)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Arguments:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> w -- weights, a numpy array of size (num_px * num_px * 3, 1)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> b -- bias, a scalar</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> X -- data of size (num_px * num_px * 3, number of examples)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Returns:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Y_prediction -- a numpy array (vector) containing all predictions (0/1) for the examples in X</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> '''</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">m</span> = <span class="cm-variable">X</span>.<span class="cm-variable">shape</span>[<span class="cm-number">1</span>]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">Y_prediction</span> = <span class="cm-variable">np</span>.<span class="cm-variable">zeros</span>((<span class="cm-number">1</span>,<span class="cm-variable">m</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">w</span> = <span class="cm-variable">w</span>.<span class="cm-variable">reshape</span>(<span class="cm-variable">X</span>.<span class="cm-variable">shape</span>[<span class="cm-number">0</span>], <span class="cm-number">1</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># Compute vector "A" predicting the probabilities of a cat being present in the picture</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### START CODE HERE ### (≈ 1 line of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">A</span> = <span class="cm-variable">sigmoid</span>(<span class="cm-variable">np</span>.<span class="cm-variable">dot</span>(<span class="cm-variable">w</span>.<span class="cm-variable">T</span>,<span class="cm-variable">X</span>)<span class="cm-operator">+</span><span class="cm-variable">b</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">for</span> <span class="cm-variable">i</span> <span class="cm-keyword">in</span> <span class="cm-builtin">range</span>(<span class="cm-variable">A</span>.<span class="cm-variable">shape</span>[<span class="cm-number">1</span>]):</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># Convert probabilities A[0,i] to actual predictions p[0,i]</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### START CODE HERE ### (≈ 4 lines of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">if</span> <span class="cm-variable">A</span>[<span class="cm-number">0</span>,<span class="cm-variable">i</span>]<span class="cm-operator">></span><span class="cm-number">0.5</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">Y_prediction</span>[<span class="cm-number">0</span>,<span class="cm-variable">i</span>]=<span class="cm-number">1</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">else</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">Y_prediction</span>[<span class="cm-number">0</span>,<span class="cm-variable">i</span>]=<span class="cm-number">0</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">assert</span>(<span class="cm-variable">Y_prediction</span>.<span class="cm-variable">shape</span> <span class="cm-operator">==</span> (<span class="cm-number">1</span>, <span class="cm-variable">m</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">Y_prediction</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 640px;"></div><div class="CodeMirror-gutters" style="display: none; height: 653px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [34]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 339px; margin-bottom: -17px; border-right-width: 13px; min-height: 79px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">w</span> = <span class="cm-variable">np</span>.<span class="cm-variable">array</span>([[<span class="cm-number">0.1124579</span>],[<span class="cm-number">0.23106775</span>]])</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">b</span> = <span class="cm-operator">-</span><span class="cm-number">0.3</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">X</span> = <span class="cm-variable">np</span>.<span class="cm-variable">array</span>([[<span class="cm-number">1.</span>,<span class="cm-operator">-</span><span class="cm-number">1.1</span>,<span class="cm-operator">-</span><span class="cm-number">3.2</span>],[<span class="cm-number">1.2</span>,<span class="cm-number">2.</span>,<span class="cm-number">0.1</span>]])</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"predictions = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">predict</span>(<span class="cm-variable">w</span>, <span class="cm-variable">b</span>, <span class="cm-variable">X</span>)))</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 79px;"></div><div class="CodeMirror-gutters" style="display: none; height: 92px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>predictions = [[ 1. 1. 0.]]
- </pre></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 249px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Expected Output**</span>: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">table</span> <span class="cm-attribute">style</span>=<span class="cm-string">"width:30%"</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> **predictions**</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> [[ 1. 1. 0.]]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"></</span><span class="cm-tag">table</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 249px;"></div><div class="CodeMirror-gutters" style="display: none; height: 262px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Expected Output</strong>: </p>
- <br><table style="width: 30%">
- <tbody><tr>
- <td>
- <strong>predictions</strong>
- </td>
- <td>
- [[ 1. 1. 0.]]
- </td> </tr>
-
- </tbody></table>
- </div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 147px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">font</span> <span class="cm-attribute">color</span>=<span class="cm-string">'blue'</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">**What to remember:**</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">You've implemented several functions that:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">- Initialize (w,b)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">- Optimize the loss iteratively to learn parameters (w,b):</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> - computing the cost and its gradient </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> - updating the parameters using gradient descent</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">- Use the learned (w,b) to predict the labels for a given set of examples</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 147px;"></div><div class="CodeMirror-gutters" style="display: none; height: 160px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><font color="blue">
- <strong>What to remember:</strong>
- You've implemented several functions that:
- </font></p><ul><font color="blue">
- <li>Initialize (w,b)</li>
- <li>Optimize the loss iteratively to learn parameters (w,b):<ul>
- <li>computing the cost and its gradient </li>
- <li>updating the parameters using gradient descent</li>
- </ul>
- </li>
- <li>Use the learned (w,b) to predict the labels for a given set of examples</li>
- </font></ul><font color="blue">
- </font><p></p></div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 167px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 20px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-2">## 5 - Merge all functions into a model ##</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">You will now see how the overall model is structured by putting together all the building blocks (functions implemented in the previous parts) together, in the right order.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Exercise:**</span> Implement the model function. Use the following notation:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- Y_prediction_test for your predictions on the test set</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- Y_prediction_train for your predictions on the train set</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- w, costs, grads for the outputs of optimize()</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 167px;"></div><div class="CodeMirror-gutters" style="display: none; height: 180px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h2 id="5---Merge-all-functions-into-a-model">5 - Merge all functions into a model<a class="anchor-link" href="#5---Merge-all-functions-into-a-model">¶</a></h2>
- <p>You will now see how the overall model is structured by putting together all the building blocks (functions implemented in the previous parts) together, in the right order.</p>
- <p><strong>Exercise:</strong> Implement the model function. Use the following notation:</p>
- <pre><code>- Y_prediction_test for your predictions on the test set
- - Y_prediction_train for your predictions on the train set
- - w, costs, grads for the outputs of optimize()
- </code></pre></div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [38]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 761px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 761px; margin-bottom: -17px; border-right-width: 13px; min-height: 878px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># GRADED FUNCTION: model</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">def</span> <span class="cm-def">model</span>(<span class="cm-variable">X_train</span>, <span class="cm-variable">Y_train</span>, <span class="cm-variable">X_test</span>, <span class="cm-variable">Y_test</span>, <span class="cm-variable">num_iterations</span> = <span class="cm-number">2000</span>, <span class="cm-variable">learning_rate</span> = <span class="cm-number">0.5</span>, <span class="cm-variable">print_cost</span> = <span class="cm-keyword">False</span>):</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"""</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Builds the logistic regression model by calling the function you've implemented previously</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Arguments:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> X_train -- training set represented by a numpy array of shape (num_px * num_px * 3, m_train)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Y_train -- training labels represented by a numpy array (vector) of shape (1, m_train)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> X_test -- test set represented by a numpy array of shape (num_px * num_px * 3, m_test)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Y_test -- test labels represented by a numpy array (vector) of shape (1, m_test)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> num_iterations -- hyperparameter representing the number of iterations to optimize the parameters</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> learning_rate -- hyperparameter representing the learning rate used in the update rule of optimize()</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> print_cost -- Set to true to print the cost every 100 iterations</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> Returns:</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> d -- dictionary containing information about the model.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-string"> """</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### START CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># initialize parameters with zeros (≈ 1 line of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">w</span>, <span class="cm-variable">b</span> = <span class="cm-variable">initialize_with_zeros</span>(<span class="cm-variable">X_train</span>.<span class="cm-variable">shape</span>[<span class="cm-number">0</span>])</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># Gradient descent (≈ 1 line of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">parameters</span>, <span class="cm-variable">grads</span>, <span class="cm-variable">costs</span> = <span class="cm-variable">optimize</span>(<span class="cm-variable">w</span>, <span class="cm-variable">b</span>,<span class="cm-variable">X_train</span>, <span class="cm-variable">Y_train</span>, <span class="cm-variable">num_iterations</span>, <span class="cm-variable">learning_rate</span>, <span class="cm-variable">print_cost</span> )</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-null cm-error"> </span><span class="cm-comment"># Retrieve parameters w and b from dictionary "parameters"</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">w</span> = <span class="cm-variable">parameters</span>[<span class="cm-string">"w"</span>]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">b</span> = <span class="cm-variable">parameters</span>[<span class="cm-string">"b"</span>]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># Predict test/train set examples (≈ 2 lines of code)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">Y_prediction_test</span> =<span class="cm-variable">predict</span>(<span class="cm-variable">w</span>, <span class="cm-variable">b</span>,<span class="cm-variable">X_test</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">Y_prediction_train</span> =<span class="cm-variable">predict</span>(<span class="cm-variable">w</span>, <span class="cm-variable">b</span>,<span class="cm-variable">X_train</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">### END CODE HERE ###</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment"># Print train/test Errors</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-builtin">print</span>(<span class="cm-string">"train accuracy: {} %"</span>.<span class="cm-builtin">format</span>(<span class="cm-number">100</span> <span class="cm-operator">-</span> <span class="cm-variable">np</span>.<span class="cm-variable">mean</span>(<span class="cm-variable">np</span>.<span class="cm-builtin">abs</span>(<span class="cm-variable">Y_prediction_train</span> <span class="cm-operator">-</span> <span class="cm-variable">Y_train</span>)) <span class="cm-operator">*</span> <span class="cm-number">100</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-builtin">print</span>(<span class="cm-string">"test accuracy: {} %"</span>.<span class="cm-builtin">format</span>(<span class="cm-number">100</span> <span class="cm-operator">-</span> <span class="cm-variable">np</span>.<span class="cm-variable">mean</span>(<span class="cm-variable">np</span>.<span class="cm-builtin">abs</span>(<span class="cm-variable">Y_prediction_test</span> <span class="cm-operator">-</span> <span class="cm-variable">Y_test</span>)) <span class="cm-operator">*</span> <span class="cm-number">100</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">d</span> = {<span class="cm-string">"costs"</span>: <span class="cm-variable">costs</span>,</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"Y_prediction_test"</span>: <span class="cm-variable">Y_prediction_test</span>, </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"Y_prediction_train"</span> : <span class="cm-variable">Y_prediction_train</span>, </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"w"</span> : <span class="cm-variable">w</span>, </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"b"</span> : <span class="cm-variable">b</span>,</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"learning_rate"</span> : <span class="cm-variable">learning_rate</span>,</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-string">"num_iterations"</span>: <span class="cm-variable">num_iterations</span>}</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-keyword">return</span> <span class="cm-variable">d</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 878px;"></div><div class="CodeMirror-gutters" style="display: none; height: 891px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 28px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Run the following cell to train your model.</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 28px;"></div><div class="CodeMirror-gutters" style="display: none; height: 41px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p>Run the following cell to train your model.</p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [39]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 872px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 872.094px; margin-bottom: -17px; border-right-width: 13px; min-height: 28px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">d</span> = <span class="cm-variable">model</span>(<span class="cm-variable">train_set_x</span>, <span class="cm-variable">train_set_y</span>, <span class="cm-variable">test_set_x</span>, <span class="cm-variable">test_set_y</span>, <span class="cm-variable">num_iterations</span> = <span class="cm-number">2000</span>, <span class="cm-variable">learning_rate</span> = <span class="cm-number">0.005</span>, <span class="cm-variable">print_cost</span> = <span class="cm-keyword">True</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 28px;"></div><div class="CodeMirror-gutters" style="display: none; height: 41px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>Cost after iteration 0: 0.693147
- Cost after iteration 100: 0.584508
- Cost after iteration 200: 0.466949
- Cost after iteration 300: 0.376007
- Cost after iteration 400: 0.331463
- Cost after iteration 500: 0.303273
- Cost after iteration 600: 0.279880
- Cost after iteration 700: 0.260042
- Cost after iteration 800: 0.242941
- Cost after iteration 900: 0.228004
- Cost after iteration 1000: 0.214820
- Cost after iteration 1100: 0.203078
- Cost after iteration 1200: 0.192544
- Cost after iteration 1300: 0.183033
- Cost after iteration 1400: 0.174399
- Cost after iteration 1500: 0.166521
- Cost after iteration 1600: 0.159305
- Cost after iteration 1700: 0.152667
- Cost after iteration 1800: 0.146542
- Cost after iteration 1900: 0.140872
- train accuracy: 99.04306220095694 %
- test accuracy: 70.0 %
- </pre></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 436px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Expected Output**</span>: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">table</span> <span class="cm-attribute">style</span>=<span class="cm-string">"width:40%"</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> **Cost after iteration 0 ** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 0.693147 <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">center</span><span class="cm-tag cm-bracket">></span> <span class="cm-delimit cm-delimit-open">$</span><span class="cm-tag">\vdots</span><span class="cm-delimit cm-delimit-close">$</span> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">center</span><span class="cm-tag cm-bracket">></span> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">center</span><span class="cm-tag cm-bracket">></span> <span class="cm-delimit cm-delimit-open">$</span><span class="cm-tag">\vdots</span><span class="cm-delimit cm-delimit-close">$</span> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">center</span><span class="cm-tag cm-bracket">></span> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> **Train Accuracy** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 99.04306220095694 % <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span>**Test Accuracy** <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"><</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span> 70.0 % <span class="cm-tag cm-bracket"></</span><span class="cm-tag">td</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-tag cm-bracket"></</span><span class="cm-tag">tr</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"></</span><span class="cm-tag">table</span><span class="cm-tag cm-bracket">></span> </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 436px;"></div><div class="CodeMirror-gutters" style="display: none; height: 449px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Expected Output</strong>: </p>
- <br><table style="width: 40%">
-
- <tbody><tr>
- <td> <strong>Cost after iteration 0 </strong> </td>
- <td> 0.693147 </td>
- </tr>
- <tr>
- <td> <center> $\vdots$ </center> </td>
- <td> <center> $\vdots$ </center> </td>
- </tr> <tr>
- <td> <strong>Train Accuracy</strong> </td>
- <td> 99.04306220095694 % </td>
- </tr>
-
- <tr>
- <td><strong>Test Accuracy</strong> </td>
- <td> 70.0 % </td>
- </tr>
- </tbody></table>
-
-
- </div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 130px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 16.5px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Comment**</span>: Training accuracy is close to 100%. This is a good sanity check: your model is working and has high enough capacity to fit the training data. Test error is 68%. It is actually not bad for this simple model, given the small dataset we used and that logistic regression is a linear classifier. But no worries, you'll build an even better classifier next week!</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Also, you see that the model is clearly overfitting the training data. Later in this specialization you will learn how to reduce overfitting, for example by using regularization. Using the code below (and changing the <span class="cm-comment">`index`</span> variable) you can look at predictions on pictures of the test set.</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 130px;"></div><div class="CodeMirror-gutters" style="display: none; height: 143px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Comment</strong>: Training accuracy is close to 100%. This is a good sanity check: your model is working and has high enough capacity to fit the training data. Test error is 68%. It is actually not bad for this simple model, given the small dataset we used and that logistic regression is a linear classifier. But no worries, you'll build an even better classifier next week!</p>
- <p>Also, you see that the model is clearly overfitting the training data. Later in this specialization you will learn how to reduce overfitting, for example by using regularization. Using the code below (and changing the <code>index</code> variable) you can look at predictions on pictures of the test set.</p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [41]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 1075px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 1075.06px; margin-bottom: -17px; border-right-width: 13px; min-height: 79px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># Example of a picture that was wrongly classified.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">index</span> = <span class="cm-number">1</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">imshow</span>(<span class="cm-variable">test_set_x</span>[:,<span class="cm-variable">index</span>].<span class="cm-variable">reshape</span>((<span class="cm-variable">num_px</span>, <span class="cm-variable">num_px</span>, <span class="cm-number">3</span>)))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span> (<span class="cm-string">"y = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">test_set_y</span>[<span class="cm-number">0</span>,<span class="cm-variable">index</span>]) <span class="cm-operator">+</span> <span class="cm-string">", you predicted that it is a \""</span> <span class="cm-operator">+</span> <span class="cm-variable">classes</span>[<span class="cm-variable">d</span>[<span class="cm-string">"Y_prediction_test"</span>][<span class="cm-number">0</span>,<span class="cm-variable">index</span>]].<span class="cm-variable">decode</span>(<span class="cm-string">"utf-8"</span>) <span class="cm-operator">+</span> <span class="cm-string">"\" picture."</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 96px;"></div><div class="CodeMirror-gutters" style="display: none; height: 109px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stderr"><pre>/opt/conda/lib/python3.5/site-packages/ipykernel/__main__.py:4: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
- </pre></div></div><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>y = 1, you predicted that it is a "cat" picture.
- </pre></div></div><div class="output_area"><div class="prompt"></div><div class="output_subarea output_png"><img src="https://img-blog.csdnimg.cn/2022010707321543993.png"></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 28px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Let's also plot the cost function and the gradients.</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 28px;"></div><div class="CodeMirror-gutters" style="display: none; height: 41px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p>Let's also plot the cost function and the gradients.</p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [42]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 381px; margin-bottom: -17px; border-right-width: 13px; min-height: 130px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># Plot learning curve (with costs)</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">costs</span> = <span class="cm-variable">np</span>.<span class="cm-variable">squeeze</span>(<span class="cm-variable">d</span>[<span class="cm-string">'costs'</span>])</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">plot</span>(<span class="cm-variable">costs</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">ylabel</span>(<span class="cm-string">'cost'</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">xlabel</span>(<span class="cm-string">'iterations (per hundreds)'</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">title</span>(<span class="cm-string">"Learning rate ="</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">d</span>[<span class="cm-string">"learning_rate"</span>]))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">show</span>()</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 130px;"></div><div class="CodeMirror-gutters" style="display: none; height: 143px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_png"><img src="https://img-blog.csdnimg.cn/2022010707321540341.png"></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 79px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Interpretation**</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">You can see the cost decreasing. It shows that the parameters are being learned. However, you see that you could train the model even more on the training set. Try to increase the number of iterations in the cell above and rerun the cells. You might see that the training set accuracy goes up, but the test set accuracy goes down. This is called overfitting. </span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 79px;"></div><div class="CodeMirror-gutters" style="display: none; height: 92px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Interpretation</strong>:
- You can see the cost decreasing. It shows that the parameters are being learned. However, you see that you could train the model even more on the training set. Try to increase the number of iterations in the cell above and rerun the cells. You might see that the training set accuracy goes up, but the test set accuracy goes down. This is called overfitting. </p>
- </div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 82px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 20px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-2">## 6 - Further analysis (optional/ungraded exercise) ##</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Congratulations on building your first image classification model. Let's analyze it further, and examine possible choices for the learning rate <span class="cm-delimit cm-delimit-open">$</span><span class="cm-tag">\alpha</span><span class="cm-delimit cm-delimit-close">$</span>. </span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 82px;"></div><div class="CodeMirror-gutters" style="display: none; height: 95px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h2 id="6---Further-analysis-(optional/ungraded-exercise)">6 - Further analysis (optional/ungraded exercise)<a class="anchor-link" href="#6---Further-analysis-(optional/ungraded-exercise)">¶</a></h2>
- <p>Congratulations on building your first image classification model. Let's analyze it further, and examine possible choices for the learning rate $\alpha$. </p>
- </div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 165px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 18px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-4">#### Choice of learning rate ####</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Reminder**</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">In order for Gradient Descent to work you must choose the learning rate wisely. The learning rate <span class="cm-delimit cm-delimit-open">$</span><span class="cm-tag">\alpha</span><span class="cm-delimit cm-delimit-close">$</span> determines how rapidly we update the parameters. If the learning rate is too large we may "overshoot" the optimal value. Similarly, if it is too small we will need too many iterations to converge to the best values. That's why it is crucial to use a well-tuned learning rate.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Let's compare the learning curve of our model with several choices of learning rates. Run the cell below. This should take about 1 minute. Feel free also to try different values than the three we have initialized the <span class="cm-comment">`learning_rates`</span> variable to contain, and see what happens. </span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 165px;"></div><div class="CodeMirror-gutters" style="display: none; height: 178px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h4 id="Choice-of-learning-rate">Choice of learning rate<a class="anchor-link" href="#Choice-of-learning-rate">¶</a></h4>
- <p><strong>Reminder</strong>:
- In order for Gradient Descent to work you must choose the learning rate wisely. The learning rate $\alpha$ determines how rapidly we update the parameters. If the learning rate is too large we may "overshoot" the optimal value. Similarly, if it is too small we will need too many iterations to converge to the best values. That's why it is crucial to use a well-tuned learning rate.</p>
- <p>Let's compare the learning curve of our model with several choices of learning rates. Run the cell below. This should take about 1 minute. Feel free also to try different values than the three we have initialized the <code>learning_rates</code> variable to contain, and see what happens. </p>
- </div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [43]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 970px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 970.359px; margin-bottom: -17px; border-right-width: 13px; min-height: 300px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">learning_rates</span> = [<span class="cm-number">0.01</span>, <span class="cm-number">0.001</span>, <span class="cm-number">0.0001</span>]</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">models</span> = {}</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">for</span> <span class="cm-variable">i</span> <span class="cm-keyword">in</span> <span class="cm-variable">learning_rates</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-builtin">print</span> (<span class="cm-string">"learning rate is: "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">i</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">models</span>[<span class="cm-builtin">str</span>(<span class="cm-variable">i</span>)] = <span class="cm-variable">model</span>(<span class="cm-variable">train_set_x</span>, <span class="cm-variable">train_set_y</span>, <span class="cm-variable">test_set_x</span>, <span class="cm-variable">test_set_y</span>, <span class="cm-variable">num_iterations</span> = <span class="cm-number">1500</span>, <span class="cm-variable">learning_rate</span> = <span class="cm-variable">i</span>, <span class="cm-variable">print_cost</span> = <span class="cm-keyword">False</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-builtin">print</span> (<span class="cm-string">'\n'</span> <span class="cm-operator">+</span> <span class="cm-string">"-------------------------------------------------------"</span> <span class="cm-operator">+</span> <span class="cm-string">'\n'</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-keyword">for</span> <span class="cm-variable">i</span> <span class="cm-keyword">in</span> <span class="cm-variable">learning_rates</span>:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable">plt</span>.<span class="cm-variable">plot</span>(<span class="cm-variable">np</span>.<span class="cm-variable">squeeze</span>(<span class="cm-variable">models</span>[<span class="cm-builtin">str</span>(<span class="cm-variable">i</span>)][<span class="cm-string">"costs"</span>]), <span class="cm-variable">label</span>= <span class="cm-builtin">str</span>(<span class="cm-variable">models</span>[<span class="cm-builtin">str</span>(<span class="cm-variable">i</span>)][<span class="cm-string">"learning_rate"</span>]))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">ylabel</span>(<span class="cm-string">'cost'</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">xlabel</span>(<span class="cm-string">'iterations (hundreds)'</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">legend</span> = <span class="cm-variable">plt</span>.<span class="cm-variable">legend</span>(<span class="cm-variable">loc</span>=<span class="cm-string">'upper center'</span>, <span class="cm-variable">shadow</span>=<span class="cm-keyword">True</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">frame</span> = <span class="cm-variable">legend</span>.<span class="cm-variable">get_frame</span>()</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">frame</span>.<span class="cm-variable">set_facecolor</span>(<span class="cm-string">'0.90'</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">show</span>()</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 300px;"></div><div class="CodeMirror-gutters" style="display: none; height: 313px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to expand output; double click to hide output" style=""></div><div class="output" style=""><div class="output_area"><div class="prompt"></div><div class="output_subarea output_text output_stream output_stdout"><pre>learning rate is: 0.01
- train accuracy: 99.52153110047847 %
- test accuracy: 68.0 %
-
- -------------------------------------------------------
-
- learning rate is: 0.001
- train accuracy: 88.99521531100478 %
- test accuracy: 64.0 %
-
- -------------------------------------------------------
-
- learning rate is: 0.0001
- train accuracy: 68.42105263157895 %
- test accuracy: 36.0 %
-
- -------------------------------------------------------
-
- </pre></div></div><div class="output_area"><div class="prompt"></div><div class="output_subarea output_png"><img src="https://img-blog.csdnimg.cn/2022010707321549893.png"></div></div></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 181px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-strong">**Interpretation**</span>: </span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- Different learning rates give different costs and thus different predictions results.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- If the learning rate is too large (0.01), the cost may oscillate up and down. It may even diverge (though in this example, using 0.01 still eventually ends up at a good value for the cost). </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- A lower cost doesn't mean a better model. You have to check if there is possibly overfitting. It happens when the training accuracy is a lot higher than the test accuracy.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- In deep learning, we usually recommend that you: </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable-3">- Choose the learning rate that better minimizes the cost function.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-variable-2">- If your model overfits, use other techniques to reduce overfitting. (We'll talk about this in later videos.) </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 181px;"></div><div class="CodeMirror-gutters" style="display: none; height: 194px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><strong>Interpretation</strong>: </p>
- <ul>
- <li>Different learning rates give different costs and thus different predictions results.</li>
- <li>If the learning rate is too large (0.01), the cost may oscillate up and down. It may even diverge (though in this example, using 0.01 still eventually ends up at a good value for the cost). </li>
- <li>A lower cost doesn't mean a better model. You have to check if there is possibly overfitting. It happens when the training accuracy is a lot higher than the test accuracy.</li>
- <li>In deep learning, we usually recommend that you: <ul>
- <li>Choose the learning rate that better minimizes the cost function.</li>
- <li>If your model overfits, use other techniques to reduce overfitting. (We'll talk about this in later videos.) </li>
- </ul>
- </li>
- </ul>
- </div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 133px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 20px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-header cm-header-2">## 7 - Test with your own image (optional/ungraded exercise) ##</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Congratulations on finishing this assignment. You can use your own image and see the output of your model. To do that:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">1. Click on "File" in the upper bar of this notebook, then click "Open" to go on your Coursera Hub.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">2. Add your image to this Jupyter Notebook's directory, in the "images" folder</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">3. Change your image's name in the following code</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">4. Run the code and check if the algorithm is right (1 = cat, 0 = non-cat)!</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 133px;"></div><div class="CodeMirror-gutters" style="display: none; height: 146px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><h2 id="7---Test-with-your-own-image-(optional/ungraded-exercise)">7 - Test with your own image (optional/ungraded exercise)<a class="anchor-link" href="#7---Test-with-your-own-image-(optional/ungraded-exercise)">¶</a></h2>
- <p>Congratulations on finishing this assignment. You can use your own image and see the output of your model. To do that:</p>
- <pre><code>1. Click on "File" in the upper bar of this notebook, then click "Open" to go on your Coursera Hub.
- 2. Add your image to this Jupyter Notebook's directory, in the "images" folder
- 3. Change your image's name in the following code
- 4. Run the code and check if the algorithm is right (1 = cat, 0 = non-cat)!
- </code></pre></div></div></div><div class="cell code_cell unselected rendered" tabindex="2"><div class="input"><div class="prompt input_prompt">In [ ]:</div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-ipython"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 17px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true" style="display: block; right: 0px; left: 0px;"><div style="height: 100%; min-height: 1px; width: 1173px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true" style="height: 17px; width: 17px;"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; min-width: 1173.06px; margin-bottom: -17px; border-right-width: 13px; min-height: 215px; padding-right: 0px; padding-bottom: 17px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment">## START CODE HERE ## (PUT YOUR IMAGE NAME) </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">my_image</span> = <span class="cm-string">"my_image.jpg"</span> <span class="cm-comment"># change this to the name of your image file </span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment">## END CODE HERE ##</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-comment"># We preprocess the image to fit your algorithm.</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">fname</span> = <span class="cm-string">"images/"</span> <span class="cm-operator">+</span> <span class="cm-variable">my_image</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">image</span> = <span class="cm-variable">np</span>.<span class="cm-variable">array</span>(<span class="cm-variable">ndimage</span>.<span class="cm-variable">imread</span>(<span class="cm-variable">fname</span>, <span class="cm-variable">flatten</span>=<span class="cm-keyword">False</span>))</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">my_image</span> = <span class="cm-variable">scipy</span>.<span class="cm-variable">misc</span>.<span class="cm-variable">imresize</span>(<span class="cm-variable">image</span>, <span class="cm-variable">size</span>=(<span class="cm-variable">num_px</span>,<span class="cm-variable">num_px</span>)).<span class="cm-variable">reshape</span>((<span class="cm-number">1</span>, <span class="cm-variable">num_px</span><span class="cm-operator">*</span><span class="cm-variable">num_px</span><span class="cm-operator">*</span><span class="cm-number">3</span>)).<span class="cm-variable">T</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">my_predicted_image</span> = <span class="cm-variable">predict</span>(<span class="cm-variable">d</span>[<span class="cm-string">"w"</span>], <span class="cm-variable">d</span>[<span class="cm-string">"b"</span>], <span class="cm-variable">my_image</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span cm-text=""></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable">plt</span>.<span class="cm-variable">imshow</span>(<span class="cm-variable">image</span>)</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-builtin">print</span>(<span class="cm-string">"y = "</span> <span class="cm-operator">+</span> <span class="cm-builtin">str</span>(<span class="cm-variable">np</span>.<span class="cm-variable">squeeze</span>(<span class="cm-variable">my_predicted_image</span>)) <span class="cm-operator">+</span> <span class="cm-string">", your algorithm predicts a \""</span> <span class="cm-operator">+</span> <span class="cm-variable">classes</span>[<span class="cm-builtin">int</span>(<span class="cm-variable">np</span>.<span class="cm-variable">squeeze</span>(<span class="cm-variable">my_predicted_image</span>)),].<span class="cm-variable">decode</span>(<span class="cm-string">"utf-8"</span>) <span class="cm-operator">+</span> <span class="cm-string">"\" picture."</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 232px;"></div><div class="CodeMirror-gutters" style="display: none; height: 245px;"></div></div></div></div></div></div><div class="output_wrapper"><div class="out_prompt_overlay prompt" title="click to scroll output; double click to hide" style=""></div><div class="output" style=""></div><div class="btn btn-default output_collapsed" title="click to expand output" style="display: none;">. . .</div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 113px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code" style=""><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-tag cm-bracket"><</span><span class="cm-tag">font</span> <span class="cm-attribute">color</span>=<span class="cm-string">'blue'</span><span class="cm-tag cm-bracket">></span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">**What to remember from this assignment:**</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">1. Preprocessing the dataset is important.</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">2. You implemented each function separately: initialize(), propagate(), optimize(). Then you built a model().</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">3. Tuning the learning rate (which is an example of a "hyperparameter") can make a big difference to the algorithm. You will see more examples of this later in this course!</span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 113px;"></div><div class="CodeMirror-gutters" style="display: none; height: 126px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p><font color="blue">
- <strong>What to remember from this assignment:</strong>
- </font></p><ol><font color="blue">
- <li>Preprocessing the dataset is important.</li>
- <li>You implemented each function separately: initialize(), propagate(), optimize(). Then you built a model().</li>
- <li>Tuning the learning rate (which is an example of a "hyperparameter") can make a big difference to the algorithm. You will see more examples of this later in this course!</li>
- </font></ol><font color="blue">
- </font><p></p></div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 96px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 16.5px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Finally, if you'd like, we invite you to try different things on this Notebook. Make sure you submit before trying anything. Once you submit, things you can play with include:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- Play with the learning rate and the number of iterations</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- Try different initialization methods and compare the results</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"> <span class="cm-comment">- Test other preprocessings (center the data, or divide each row by its standard deviation)</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 96px;"></div><div class="CodeMirror-gutters" style="display: none; height: 109px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p>Finally, if you'd like, we invite you to try different things on this Notebook. Make sure you submit before trying anything. Once you submit, things you can play with include:</p>
- <pre><code>- Play with the learning rate and the number of iterations
- - Try different initialization methods and compare the results
- - Test other preprocessings (center the data, or divide each row by its standard deviation)
- </code></pre></div></div></div><div class="cell text_cell unselected rendered" tabindex="2"><div class="prompt input_prompt"></div><div class="inner_cell"><div class="ctb_hideshow"><div class="celltoolbar"></div></div><div class="input_area"><div class="CodeMirror cm-s-default CodeMirror-wrap"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 5.59375px; left: 5.59375px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-vscrollbar" cm-not-content="true" style="bottom: 0px;"><div style="min-width: 1px; height: 0px;"></div></div><div class="CodeMirror-hscrollbar" cm-not-content="true"><div style="height: 100%; min-height: 1px; width: 0px;"></div></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; padding-right: 0px; padding-bottom: 0px; margin-bottom: -17px; border-right-width: 13px; min-height: 62px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines"><div style="position: relative; outline: none;"><div class="CodeMirror-measure"></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-cursors"><div class="CodeMirror-cursor" style="left: 0px; top: 0px; height: 17px;"> </div></div><div class="CodeMirror-code"><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;">Bibliography:</span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- </span><span class="cm-variable-2 cm-link">http://www.wildml.com/2015/09/implementing-a-neural-network-from-scratch/</span></span></pre><pre class=" CodeMirror-line "><span style="padding-right: 0.1px;"><span class="cm-variable-2">- </span><span class="cm-variable-2 cm-link">https://stats.stackexchange.com/questions/211436/why-do-we-normalize-images-by-subtracting-the-datasets-image-mean-and-not-the-c</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 13px; width: 1px; top: 62px;"></div><div class="CodeMirror-gutters" style="display: none; height: 75px;"></div></div></div></div><div class="text_cell_render rendered_html" tabindex="-1"><p>Bibliography:</p>
- <ul>
- <li><a href="http://www.wildml.com/2015/09/implementing-a-neural-network-from-scratch/" target="_blank">http://www.wildml.com/2015/09/implementing-a-neural-network-from-scratch/</a></li>
- <li><a href="https://stats.stackexchange.com/questions/211436/why-do-we-normalize-images-by-subtracting-the-datasets-image-mean-and-not-the-c" target="_blank">https://stats.stackexchange.com/questions/211436/why-do-we-normalize-images-by-subtracting-the-datasets-image-mean-and-not-the-c</a></li>
- </ul>
- </div></div></div></div><div class="end_space"></div></div>
- <div id="tooltip" class="ipython_tooltip" style="display:none"><div class="tooltipbuttons"><a href="#" role="button" class="ui-button"><span class="ui-icon ui-icon-close">Close</span></a><a href="#" class="ui-corner-all" role="button" id="expanbutton" title="Grow the tooltip vertically (press shift-tab twice)"><span class="ui-icon ui-icon-plus">Expand</span></a><a href="#" role="button" class="ui-button" title="show the current docstring in pager (press shift-tab 4 times)"><span class="ui-icon ui-icon-arrowstop-l-n">Open in Pager</span></a><a href="#" role="button" class="ui-button" title="Tooltip will linger for 10 seconds while you type" style="display: none;"><span class="ui-icon ui-icon-clock">Close</span></a></div><div class="pretooltiparrow"></div><div class="tooltiptext smalltooltip"></div></div>
- </div>
- </div>
-
-
-
- </div>
-
-
-
- <div id="pager" class="ui-resizable">
- <div id="pager-contents">
- <div id="pager-container" class="container"></div>
- </div>
- <div id="pager-button-area"><a role="button" title="Open the pager in an external window" class="ui-button"><span class="ui-icon ui-icon-extlink"></span></a><a role="button" title="Close the pager" class="ui-button"><span class="ui-icon ui-icon-close"></span></a></div>
- <div class="ui-resizable-handle ui-resizable-n" style="z-index: 90;"></div></div>
-
-
-
-
-
-
- <script type="text/javascript">
- sys_info = {"notebook_path": "/opt/conda/lib/python3.5/site-packages/notebook", "default_encoding": "UTF-8", "platform": "Linux-4.4.0-45-generic-x86_64-with-debian-8.6", "sys_platform": "linux", "sys_executable": "/opt/conda/bin/python", "notebook_version": "4.2.3", "sys_version": "3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]", "commit_source": "", "os_name": "posix", "commit_hash": ""};
- </script>
-
- <script src="/user/bxycpfikvlfnfowcbjepjo/static/components/text-encoding/lib/encoding.js?v=d5bb0fc9ffeff7d98a69bb83daa51052" charset="utf-8"></script>
-
-
- <script src="/user/bxycpfikvlfnfowcbjepjo/static/notebook/js/main.min.js?v=e80a0ad9bee7ad0d5865624902d5405d" type="text/javascript" charset="utf-8"></script>
-
-
-
-
-
-
- <div style="position: absolute; width: 0px; height: 0px; overflow: hidden; padding: 0px; border: 0px; margin: 0px;"><div id="MathJax_Font_Test" style="position: absolute; visibility: hidden; top: 0px; left: 0px; width: auto; padding: 0px; border: 0px; margin: 0px; white-space: nowrap; text-align: left; text-indent: 0px; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; font-size: 40px; font-weight: normal; font-style: normal;"></div></div><st-div id="__selection-translator__"> <st-div class="__st-box__" style="display: none; position: fixed; z-index: 99999; left: 0px; top: 0px; transform: translateX(875px) translateY(369px);"> <st-header> <st-span class="st-icon-pin" title="固定"></st-span> <st-span class="st-icon-down-open" title="展开"></st-span> <st-span class="st-icon-cog" title="设置"></st-span> </st-header> <st-div class="__query-form__" style="display: none;"> <st-div> <textarea placeholder="输入要翻译的句子或单词"></textarea> </st-div> <st-div> <select> <option value="">自动判断</option> <option value="zh">中文</option><option value="zh-CN">中文(简体)</option><option value="zh-HK">中文(香港)</option><option value="zh-TW">中文(繁体)</option><option value="en">英语</option><option value="ja">日语</option><option value="ko">朝鲜语</option><option value="de">德语</option><option value="fr">法语</option><option value="ru">俄语</option><option value="th">泰语</option><option value="af">南非语</option><option value="ar">阿拉伯语</option><option value="az">阿塞拜疆语</option><option value="be">比利时语</option><option value="bg">保加利亚语</option><option value="ca">加泰隆语</option><option value="cs">捷克语</option><option value="cy">威尔士语</option><option value="da">丹麦语</option><option value="dv">第维埃语</option><option value="el">希腊语</option><option value="eo">世界语</option><option value="es">西班牙语</option><option value="et">爱沙尼亚语</option><option value="eu">巴士克语</option><option value="fa">法斯语</option><option value="fi">芬兰语</option><option value="fo">法罗语</option><option value="gl">加里西亚语</option><option value="gu">古吉拉特语</option><option value="he">希伯来语</option><option value="hi">印地语</option><option value="hr">克罗地亚语</option><option value="hu">匈牙利语</option><option value="hy">亚美尼亚语</option><option value="id">印度尼西亚语</option><option value="is">冰岛语</option><option value="it">意大利语</option><option value="ka">格鲁吉亚语</option><option value="kk">哈萨克语</option><option value="kn">卡纳拉语</option><option value="kok">孔卡尼语</option><option value="ky">吉尔吉斯语</option><option value="lt">立陶宛语</option><option value="lv">拉脱维亚语</option><option value="mi">毛利语</option><option value="mk">马其顿语</option><option value="mn">蒙古语</option><option value="mr">马拉地语</option><option value="ms">马来语</option><option value="mt">马耳他语</option><option value="nb">挪威语(伯克梅尔)</option><option value="nl">荷兰语</option><option value="ns">北梭托语</option><option value="pa">旁遮普语</option><option value="pl">波兰语</option><option value="pt">葡萄牙语</option><option value="qu">克丘亚语</option><option value="ro">罗马尼亚语</option><option value="sa">梵文</option><option value="se">北萨摩斯语</option><option value="sk">斯洛伐克语</option><option value="sl">斯洛文尼亚语</option><option value="sq">阿尔巴尼亚语</option><option value="sv">瑞典语</option><option value="sw">斯瓦希里语</option><option value="syr">叙利亚语</option><option value="ta">泰米尔语</option><option value="te">泰卢固语</option><option value="tl">塔加路语</option><option value="tn">茨瓦纳语</option><option value="tr">土耳其语</option><option value="ts">宗加语</option><option value="tt">鞑靼语</option><option value="uk">乌克兰语</option><option value="ur">乌都语</option><option value="uz">乌兹别克语</option><option value="vi">越南语</option><option value="xh">班图语</option><option value="zu">祖鲁语</option> </select> <st-div class="__exchange__"> <st-span class="st-icon-exchange"></st-span> </st-div> <select> <option value="">自动选择</option> <option value="zh">中文</option><option value="zh-CN">中文(简体)</option><option value="zh-HK">中文(香港)</option><option value="zh-TW">中文(繁体)</option><option value="en">英语</option><option value="ja">日语</option><option value="ko">朝鲜语</option><option value="de">德语</option><option value="fr">法语</option><option value="ru">俄语</option><option value="th">泰语</option><option value="af">南非语</option><option value="ar">阿拉伯语</option><option value="az">阿塞拜疆语</option><option value="be">比利时语</option><option value="bg">保加利亚语</option><option value="ca">加泰隆语</option><option value="cs">捷克语</option><option value="cy">威尔士语</option><option value="da">丹麦语</option><option value="dv">第维埃语</option><option value="el">希腊语</option><option value="eo">世界语</option><option value="es">西班牙语</option><option value="et">爱沙尼亚语</option><option value="eu">巴士克语</option><option value="fa">法斯语</option><option value="fi">芬兰语</option><option value="fo">法罗语</option><option value="gl">加里西亚语</option><option value="gu">古吉拉特语</option><option value="he">希伯来语</option><option value="hi">印地语</option><option value="hr">克罗地亚语</option><option value="hu">匈牙利语</option><option value="hy">亚美尼亚语</option><option value="id">印度尼西亚语</option><option value="is">冰岛语</option><option value="it">意大利语</option><option value="ka">格鲁吉亚语</option><option value="kk">哈萨克语</option><option value="kn">卡纳拉语</option><option value="kok">孔卡尼语</option><option value="ky">吉尔吉斯语</option><option value="lt">立陶宛语</option><option value="lv">拉脱维亚语</option><option value="mi">毛利语</option><option value="mk">马其顿语</option><option value="mn">蒙古语</option><option value="mr">马拉地语</option><option value="ms">马来语</option><option value="mt">马耳他语</option><option value="nb">挪威语(伯克梅尔)</option><option value="nl">荷兰语</option><option value="ns">北梭托语</option><option value="pa">旁遮普语</option><option value="pl">波兰语</option><option value="pt">葡萄牙语</option><option value="qu">克丘亚语</option><option value="ro">罗马尼亚语</option><option value="sa">梵文</option><option value="se">北萨摩斯语</option><option value="sk">斯洛伐克语</option><option value="sl">斯洛文尼亚语</option><option value="sq">阿尔巴尼亚语</option><option value="sv">瑞典语</option><option value="sw">斯瓦希里语</option><option value="syr">叙利亚语</option><option value="ta">泰米尔语</option><option value="te">泰卢固语</option><option value="tl">塔加路语</option><option value="tn">茨瓦纳语</option><option value="tr">土耳其语</option><option value="ts">宗加语</option><option value="tt">鞑靼语</option><option value="uk">乌克兰语</option><option value="ur">乌都语</option><option value="uz">乌兹别克语</option><option value="vi">越南语</option><option value="xh">班图语</option><option value="zu">祖鲁语</option> </select> </st-div> <st-div> <select> <option value="YouDao">有道翻译</option> <option value="BaiDu">百度翻译</option> <option value="Google">谷歌翻译</option> <option value="GoogleCN">谷歌翻译(国内)</option> </select> <st-div class="__action-list__"> <st-div class="__button__ __btn-translate__">翻译 <st-span class="st-icon-down-dir"></st-span> </st-div> <st-div class="__expand__"> <st-div class="__button__">朗读</st-div> <st-div class="__button__">复制</st-div> </st-div> </st-div> </st-div> </st-div> <st-div class="__translate-result__" style="display: none;">正在查询,请稍候……</st-div> <st-div class="__translate-result__"> <st-div style="display: none;"> <st-span></st-span> <st-span class="__retry__">重试</st-span> </st-div> <st-div> <st-div class="__phonetic__"> <st-span style="display: none;"></st-span> <st-span class="__copy-and-read__"> <st-span>朗读</st-span> <st-span style="display: none;">复制</st-span> </st-span> </st-div> <st-div style="display: none;"> <st-ul> </st-ul> <st-div class="__copy-and-read__"> <st-span class="__copy-and-read__">复制</st-span> </st-div> </st-div> <st-div style="display: none;"> <st-div class="__copy-and-read__"> <st-span class="__copy-and-read__">朗读</st-span> <st-span class="__copy-and-read__">复制</st-span> </st-div> </st-div> </st-div> </st-div> <st-footer> <st-span style="">via <a target="_blank" href="">谷歌翻译(国内)</a></st-span> </st-footer> </st-div> <st-div class="__st-btn__" style="display: none; position: fixed; z-index: 99999; left: 0px; top: 0px; transform: translateX(875px) translateY(369px);">译</st-div> </st-div></body>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。