From 63b46f7bd6d9cf5dde1d565899c2e287e6c18c08 Mon Sep 17 00:00:00 2001 From: Raj Sangani <57042606+rajlm10@users.noreply.github.com> Date: Wed, 9 Mar 2022 10:54:51 +0530 Subject: [PATCH] Created using Colaboratory --- D2L_Dropout.ipynb | 1915 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1915 insertions(+) create mode 100644 D2L_Dropout.ipynb diff --git a/D2L_Dropout.ipynb b/D2L_Dropout.ipynb new file mode 100644 index 0000000..68a51ad --- /dev/null +++ b/D2L_Dropout.ipynb @@ -0,0 +1,1915 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "D2L_Dropout.ipynb", + "provenance": [], + "authorship_tag": "ABX9TyMDojPVW8R32c6mVfSY9Urj", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "4ea99cafd00b44e8ad0ea0292b9c203f": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HBoxModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HBoxView", + "_dom_classes": [], + "_model_name": "HBoxModel", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.5.0", + "box_style": "", + "layout": "IPY_MODEL_e5453ef2b28b4dc9a510c3895ed5f647", + "_model_module": "@jupyter-widgets/controls", + "children": [ + "IPY_MODEL_519d5a81ef0c4cc7a5ecec6af82fb2b9", + "IPY_MODEL_c1925df169f142979a96d0415021665c", + "IPY_MODEL_dd6aad65db47467088b05d08f0d3bd68" + ] + } + }, + "e5453ef2b28b4dc9a510c3895ed5f647": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "519d5a81ef0c4cc7a5ecec6af82fb2b9": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HTMLModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HTMLView", + "style": "IPY_MODEL_b7e2fd4f418548889b0eaf0a45e8b6e9", + "_dom_classes": [], + "description": "", + "_model_name": "HTMLModel", + "placeholder": "​", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": "", + "_view_count": null, + "_view_module_version": "1.5.0", + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_3afbc7b48ca2421cbdc881413667fbe5" + } + }, + "c1925df169f142979a96d0415021665c": { + "model_module": "@jupyter-widgets/controls", + "model_name": "FloatProgressModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "ProgressView", + "style": "IPY_MODEL_b3b12bc938d345f7a083d85c6c37f93b", + "_dom_classes": [], + "description": "", + "_model_name": "FloatProgressModel", + "bar_style": "success", + "max": 26421880, + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": 26421880, + "_view_count": null, + "_view_module_version": "1.5.0", + "orientation": "horizontal", + "min": 0, + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_e10bad1120734ad7a8f862e2215b53b1" + } + }, + "dd6aad65db47467088b05d08f0d3bd68": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HTMLModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HTMLView", + "style": "IPY_MODEL_5a88f1d529204cd3a79a7464ed91a1da", + "_dom_classes": [], + "description": "", + "_model_name": "HTMLModel", + "placeholder": "​", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": " 26422272/? [00:01<00:00, 24935347.31it/s]", + "_view_count": null, + "_view_module_version": "1.5.0", + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_02be45c3039d48c6a0b72c69ae306e3a" + } + }, + "b7e2fd4f418548889b0eaf0a45e8b6e9": { + "model_module": "@jupyter-widgets/controls", + "model_name": "DescriptionStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "DescriptionStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "_model_module": "@jupyter-widgets/controls" + } + }, + "3afbc7b48ca2421cbdc881413667fbe5": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "b3b12bc938d345f7a083d85c6c37f93b": { + "model_module": "@jupyter-widgets/controls", + "model_name": "ProgressStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "ProgressStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "bar_color": null, + "_model_module": "@jupyter-widgets/controls" + } + }, + "e10bad1120734ad7a8f862e2215b53b1": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "5a88f1d529204cd3a79a7464ed91a1da": { + "model_module": "@jupyter-widgets/controls", + "model_name": "DescriptionStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "DescriptionStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "_model_module": "@jupyter-widgets/controls" + } + }, + "02be45c3039d48c6a0b72c69ae306e3a": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "58f8831bf86f497498ddae711118e816": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HBoxModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HBoxView", + "_dom_classes": [], + "_model_name": "HBoxModel", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.5.0", + "box_style": "", + "layout": "IPY_MODEL_77c16c90583a4697be9cf0e64910dce3", + "_model_module": "@jupyter-widgets/controls", + "children": [ + "IPY_MODEL_4c615b9980614992926840d2c02059d2", + "IPY_MODEL_2a5b8d4ea5904aa291a68938d93d26fd", + "IPY_MODEL_01c7c441eb8e414fb83cbf13e25dc87e" + ] + } + }, + "77c16c90583a4697be9cf0e64910dce3": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "4c615b9980614992926840d2c02059d2": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HTMLModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HTMLView", + "style": "IPY_MODEL_c5a5412b595a4e8fb29055922b53e1ef", + "_dom_classes": [], + "description": "", + "_model_name": "HTMLModel", + "placeholder": "​", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": "", + "_view_count": null, + "_view_module_version": "1.5.0", + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_658b1f00f4634b54bb68bb299f42e88b" + } + }, + "2a5b8d4ea5904aa291a68938d93d26fd": { + "model_module": "@jupyter-widgets/controls", + "model_name": "FloatProgressModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "ProgressView", + "style": "IPY_MODEL_6280808617b446e5aaed317e47e26c45", + "_dom_classes": [], + "description": "", + "_model_name": "FloatProgressModel", + "bar_style": "success", + "max": 29515, + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": 29515, + "_view_count": null, + "_view_module_version": "1.5.0", + "orientation": "horizontal", + "min": 0, + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_7d66ae4ae4b844a1bd85abdf708c8c66" + } + }, + "01c7c441eb8e414fb83cbf13e25dc87e": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HTMLModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HTMLView", + "style": "IPY_MODEL_7ee0ff5480344bcb9861f1b4b2dd3b06", + "_dom_classes": [], + "description": "", + "_model_name": "HTMLModel", + "placeholder": "​", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": " 29696/? [00:00<00:00, 302144.79it/s]", + "_view_count": null, + "_view_module_version": "1.5.0", + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_28d72edfe17f4f3b85bcb0b37b62d163" + } + }, + "c5a5412b595a4e8fb29055922b53e1ef": { + "model_module": "@jupyter-widgets/controls", + "model_name": "DescriptionStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "DescriptionStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "_model_module": "@jupyter-widgets/controls" + } + }, + "658b1f00f4634b54bb68bb299f42e88b": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "6280808617b446e5aaed317e47e26c45": { + "model_module": "@jupyter-widgets/controls", + "model_name": "ProgressStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "ProgressStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "bar_color": null, + "_model_module": "@jupyter-widgets/controls" + } + }, + "7d66ae4ae4b844a1bd85abdf708c8c66": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "7ee0ff5480344bcb9861f1b4b2dd3b06": { + "model_module": "@jupyter-widgets/controls", + "model_name": "DescriptionStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "DescriptionStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "_model_module": "@jupyter-widgets/controls" + } + }, + "28d72edfe17f4f3b85bcb0b37b62d163": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "5ae0b17d6f0e4223bff00dc65e3c6179": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HBoxModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HBoxView", + "_dom_classes": [], + "_model_name": "HBoxModel", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.5.0", + "box_style": "", + "layout": "IPY_MODEL_30b1906b0cc743418a070f1cdccae12b", + "_model_module": "@jupyter-widgets/controls", + "children": [ + "IPY_MODEL_efe112eddae345cfbaff047160a5d6d0", + "IPY_MODEL_ec7d1fc911d343aab515f05cbaece3c9", + "IPY_MODEL_738f4fcf8237471bbedc8c54f0b5ff27" + ] + } + }, + "30b1906b0cc743418a070f1cdccae12b": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "efe112eddae345cfbaff047160a5d6d0": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HTMLModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HTMLView", + "style": "IPY_MODEL_69fc1226fdc845b185b901bd6ed0e3b3", + "_dom_classes": [], + "description": "", + "_model_name": "HTMLModel", + "placeholder": "​", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": "", + "_view_count": null, + "_view_module_version": "1.5.0", + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_55f0ad7ad5684babaccc0529422f854f" + } + }, + "ec7d1fc911d343aab515f05cbaece3c9": { + "model_module": "@jupyter-widgets/controls", + "model_name": "FloatProgressModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "ProgressView", + "style": "IPY_MODEL_1bb1d742275a49d180acf857d87531a7", + "_dom_classes": [], + "description": "", + "_model_name": "FloatProgressModel", + "bar_style": "success", + "max": 4422102, + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": 4422102, + "_view_count": null, + "_view_module_version": "1.5.0", + "orientation": "horizontal", + "min": 0, + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_252ce3b22c3849fba657a90de9996efc" + } + }, + "738f4fcf8237471bbedc8c54f0b5ff27": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HTMLModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HTMLView", + "style": "IPY_MODEL_f7f21bed86fb4360adc2a6f318279875", + "_dom_classes": [], + "description": "", + "_model_name": "HTMLModel", + "placeholder": "​", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": " 4422656/? [00:00<00:00, 7817734.31it/s]", + "_view_count": null, + "_view_module_version": "1.5.0", + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_9812a167538f4e3bb9b729c170aebf67" + } + }, + "69fc1226fdc845b185b901bd6ed0e3b3": { + "model_module": "@jupyter-widgets/controls", + "model_name": "DescriptionStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "DescriptionStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "_model_module": "@jupyter-widgets/controls" + } + }, + "55f0ad7ad5684babaccc0529422f854f": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "1bb1d742275a49d180acf857d87531a7": { + "model_module": "@jupyter-widgets/controls", + "model_name": "ProgressStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "ProgressStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "bar_color": null, + "_model_module": "@jupyter-widgets/controls" + } + }, + "252ce3b22c3849fba657a90de9996efc": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "f7f21bed86fb4360adc2a6f318279875": { + "model_module": "@jupyter-widgets/controls", + "model_name": "DescriptionStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "DescriptionStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "_model_module": "@jupyter-widgets/controls" + } + }, + "9812a167538f4e3bb9b729c170aebf67": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "0341226bf791414888dab096d1287aa5": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HBoxModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HBoxView", + "_dom_classes": [], + "_model_name": "HBoxModel", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.5.0", + "box_style": "", + "layout": "IPY_MODEL_dc895ebb3319452a880d7bb38de1ea0c", + "_model_module": "@jupyter-widgets/controls", + "children": [ + "IPY_MODEL_3a5c51a7195943f2a995c62043541389", + "IPY_MODEL_75616dad75524319b7ec2b50f9d580ca", + "IPY_MODEL_0eb7304d71a8481795d7b5a1aa06d350" + ] + } + }, + "dc895ebb3319452a880d7bb38de1ea0c": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "3a5c51a7195943f2a995c62043541389": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HTMLModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HTMLView", + "style": "IPY_MODEL_e32d919447b14669ae2d37b13842bbe9", + "_dom_classes": [], + "description": "", + "_model_name": "HTMLModel", + "placeholder": "​", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": "", + "_view_count": null, + "_view_module_version": "1.5.0", + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_11632fd42f82438e8206b09425cdd8b1" + } + }, + "75616dad75524319b7ec2b50f9d580ca": { + "model_module": "@jupyter-widgets/controls", + "model_name": "FloatProgressModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "ProgressView", + "style": "IPY_MODEL_e4786b11b8e5453487d4be6a58d875aa", + "_dom_classes": [], + "description": "", + "_model_name": "FloatProgressModel", + "bar_style": "success", + "max": 5148, + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": 5148, + "_view_count": null, + "_view_module_version": "1.5.0", + "orientation": "horizontal", + "min": 0, + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_e26db3175ed84d76ada78b92f6ab907c" + } + }, + "0eb7304d71a8481795d7b5a1aa06d350": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HTMLModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "HTMLView", + "style": "IPY_MODEL_dbf1e2bc91c642fd82ac04576841e1b8", + "_dom_classes": [], + "description": "", + "_model_name": "HTMLModel", + "placeholder": "​", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": " 6144/? [00:00<00:00, 125003.29it/s]", + "_view_count": null, + "_view_module_version": "1.5.0", + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_5288ab80f6d9493f993c23d5029c0924" + } + }, + "e32d919447b14669ae2d37b13842bbe9": { + "model_module": "@jupyter-widgets/controls", + "model_name": "DescriptionStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "DescriptionStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "_model_module": "@jupyter-widgets/controls" + } + }, + "11632fd42f82438e8206b09425cdd8b1": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "e4786b11b8e5453487d4be6a58d875aa": { + "model_module": "@jupyter-widgets/controls", + "model_name": "ProgressStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "ProgressStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "bar_color": null, + "_model_module": "@jupyter-widgets/controls" + } + }, + "e26db3175ed84d76ada78b92f6ab907c": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "dbf1e2bc91c642fd82ac04576841e1b8": { + "model_module": "@jupyter-widgets/controls", + "model_name": "DescriptionStyleModel", + "model_module_version": "1.5.0", + "state": { + "_view_name": "StyleView", + "_model_name": "DescriptionStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "_model_module": "@jupyter-widgets/controls" + } + }, + "5288ab80f6d9493f993c23d5029c0924": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "model_module_version": "1.2.0", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + } + } + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "czdWbhbT-2CY" + }, + "outputs": [], + "source": [ + "import torch\n", + "import torchvision\n", + "from torch.utils import data\n", + "from torchvision import transforms\n", + "from torch import nn" + ] + }, + { + "cell_type": "code", + "source": [ + "def get_fashion_mnist_labels(labels): \n", + " \"\"\"Return text labels for the Fashion-MNIST dataset.\"\"\" \n", + " text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat','sandal', 'shirt', 'sneaker', 'bag', 'ankle boot'] \n", + " return [text_labels[int(i)] for i in labels]" + ], + "metadata": { + "id": "edxZSikQ-5U9" + }, + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def accuracy(y_hat,y):\n", + " \"\"\"Compute the number of correct predictions.\"\"\" \n", + " if y_hat.shape[0]>1 and y_hat.shape[1]>1:\n", + " y_hat=y_hat.argmax(axis=1)\n", + " cmp=y_hat.type(y.dtype)==y\n", + " return float(cmp.type(y.dtype).sum())\n" + ], + "metadata": { + "id": "wJ-xUh63-6vI" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "class Accumulator: \n", + " \"\"\"For accumulating sums over `n` variables.\"\"\" \n", + " def __init__(self, n):\n", + " self.data = [0.0] * n \n", + " \n", + " def add(self, *args):\n", + " self.data = [a + float(b) for a, b in zip(self.data, args)] \n", + " \n", + " def reset(self):\n", + " self.data = [0.0] * len(self.data)\n", + " \n", + " def __getitem__(self, idx): \n", + " return self.data[idx]" + ], + "metadata": { + "id": "2a9mrA-1AMkM" + }, + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def evaluate_accuracy(net,test_iter):\n", + " \"\"\"Compute the accuracy for a model on a dataset.\"\"\"\n", + " if isinstance(net,torch.nn.Module):\n", + " net.eval()\n", + " metric=Accumulator(2) #no of correct preds, no of predictions\n", + " with torch.no_grad():\n", + " for X,y in test_iter:\n", + " metric.add(accuracy(net(X),y),y.numel())\n", + " return metric[0]/metric[1]\n" + ], + "metadata": { + "id": "PG3cNg7l_rrr" + }, + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def get_workers():\n", + " return 2" + ], + "metadata": { + "id": "2VtR0CYPA6-D" + }, + "execution_count": 6, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def load_fashion_mnist(batch_size,resize=None):\n", + " trans=[transforms.ToTensor()] #PIL image to tensor (normalized between 0-1)\n", + " if resize:\n", + " trans.insert(0,transforms.Resize(resize))\n", + " trans=transforms.Compose(trans) #Chains together transforms\n", + " \n", + " mnist_train=torchvision.datasets.FashionMNIST(root=\"../data\", train=True, transform=trans, download=True)\n", + " mnist_test=torchvision.datasets.FashionMNIST(root=\"../data\", train=False, transform=trans, download=True)\n", + "\n", + " return data.DataLoader(mnist_train,batch_size,shuffle=True,num_workers=get_workers()),data.DataLoader(mnist_test,batch_size,shuffle=True,num_workers=get_workers())" + ], + "metadata": { + "id": "pL-LuC3eA8vz" + }, + "execution_count": 7, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "batch_size = 256\n", + "train_iter, test_iter = load_fashion_mnist(batch_size)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 423, + "referenced_widgets": [ + "4ea99cafd00b44e8ad0ea0292b9c203f", + "e5453ef2b28b4dc9a510c3895ed5f647", + "519d5a81ef0c4cc7a5ecec6af82fb2b9", + "c1925df169f142979a96d0415021665c", + "dd6aad65db47467088b05d08f0d3bd68", + "b7e2fd4f418548889b0eaf0a45e8b6e9", + "3afbc7b48ca2421cbdc881413667fbe5", + "b3b12bc938d345f7a083d85c6c37f93b", + "e10bad1120734ad7a8f862e2215b53b1", + "5a88f1d529204cd3a79a7464ed91a1da", + "02be45c3039d48c6a0b72c69ae306e3a", + "58f8831bf86f497498ddae711118e816", + "77c16c90583a4697be9cf0e64910dce3", + "4c615b9980614992926840d2c02059d2", + "2a5b8d4ea5904aa291a68938d93d26fd", + "01c7c441eb8e414fb83cbf13e25dc87e", + "c5a5412b595a4e8fb29055922b53e1ef", + "658b1f00f4634b54bb68bb299f42e88b", + "6280808617b446e5aaed317e47e26c45", + "7d66ae4ae4b844a1bd85abdf708c8c66", + "7ee0ff5480344bcb9861f1b4b2dd3b06", + "28d72edfe17f4f3b85bcb0b37b62d163", + "5ae0b17d6f0e4223bff00dc65e3c6179", + "30b1906b0cc743418a070f1cdccae12b", + "efe112eddae345cfbaff047160a5d6d0", + "ec7d1fc911d343aab515f05cbaece3c9", + "738f4fcf8237471bbedc8c54f0b5ff27", + "69fc1226fdc845b185b901bd6ed0e3b3", + "55f0ad7ad5684babaccc0529422f854f", + "1bb1d742275a49d180acf857d87531a7", + "252ce3b22c3849fba657a90de9996efc", + "f7f21bed86fb4360adc2a6f318279875", + "9812a167538f4e3bb9b729c170aebf67", + "0341226bf791414888dab096d1287aa5", + "dc895ebb3319452a880d7bb38de1ea0c", + "3a5c51a7195943f2a995c62043541389", + "75616dad75524319b7ec2b50f9d580ca", + "0eb7304d71a8481795d7b5a1aa06d350", + "e32d919447b14669ae2d37b13842bbe9", + "11632fd42f82438e8206b09425cdd8b1", + "e4786b11b8e5453487d4be6a58d875aa", + "e26db3175ed84d76ada78b92f6ab907c", + "dbf1e2bc91c642fd82ac04576841e1b8", + "5288ab80f6d9493f993c23d5029c0924" + ] + }, + "id": "uCC7hP3HBvwu", + "outputId": "b61848fe-59b0-4d9e-963b-9060888bdd18" + }, + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to ../data/FashionMNIST/raw/train-images-idx3-ubyte.gz\n" + ] + }, + { + "output_type": "display_data", + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "4ea99cafd00b44e8ad0ea0292b9c203f", + "version_minor": 0, + "version_major": 2 + }, + "text/plain": [ + " 0%| | 0/26421880 [00:00dropout).float() #Random uniform distribution [0,1]\n", + "\n", + " return mask*activations/(1.0-dropout)" + ], + "metadata": { + "id": "-UK22ZLbBxnL" + }, + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "num_inputs, num_outputs, num_hiddens1, num_hiddens2 = 784, 10, 256, 256\n", + "dropout1, dropout2 = 0.2, 0.5\n", + "\n", + "class Net(nn.Module):\n", + " def __init__(self,num_inputs,num_outputs,num_hiddens1,num_hiddens2,is_training=True):\n", + " super(Net,self).__init__()\n", + " self.num_inputs=num_inputs\n", + " self.is_training=is_training\n", + " self.lin1=nn.Linear(num_inputs,num_hiddens1)\n", + " self.lin2=nn.Linear(num_hiddens1,num_hiddens2)\n", + " self.lin3=nn.Linear(num_hiddens2,num_outputs)\n", + " self.relu=nn.ReLU()\n", + "\n", + " def forward(self,X):\n", + " H1=self.relu(self.lin1(X.reshape(-1,num_inputs)))\n", + " # Use dropout only when training the model \n", + " if self.is_training:\n", + " H1=dropout_layer(H1,dropout1) \n", + " H2=self.relu(self.lin2(H1))\n", + " if self.is_training:\n", + " H2=dropout_layer(H2,dropout2)\n", + "\n", + " out = self.lin3(H2)\n", + " return out\n", + "\n", + "net=Net(num_inputs, num_outputs, num_hiddens1, num_hiddens2)" + ], + "metadata": { + "id": "9jAmXHtjCWpB" + }, + "execution_count": 14, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def train_epoch(net,training_set,loss,optimizer):\n", + " if (isinstance(net,torch.nn.Module)):\n", + " net.train()\n", + "\n", + " metric=Accumulator(3) #stores sum of training loss, sum of training accuracy, no. of examples\n", + " for X,y in training_set:\n", + " y_hat=net(X)\n", + " l=loss(y_hat,y) # nX10, nX1 -> nX1\n", + "\n", + " if isinstance(optimizer,torch.optim.Optimizer):\n", + " optimizer.zero_grad()\n", + " l.mean().backward()\n", + " optimizer.step()\n", + "\n", + " metric.add(float(l.sum()),accuracy(y_hat,y),y.shape[0])\n", + " return metric[0]/metric[2], metric[1]/metric[2]\n", + "\n" + ], + "metadata": { + "id": "TMpcrG8rEWNx" + }, + "execution_count": 15, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def train(net,training_set,test_set,loss,optimizer,num_epochs):\n", + " for epoch in range(num_epochs):\n", + " train_loss,train_acc=train_epoch(net,training_set,loss,optimizer)\n", + " test_acc = evaluate_accuracy(net, test_set)\n", + "\n", + " print(f'''epoch {epoch+1}: Train Loss: {train_loss},Train Acc: {train_acc}, Test Acc: {test_acc}''')\n" + ], + "metadata": { + "id": "3VF6m50iFdqh" + }, + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "num_epochs, lr, batch_size = 10, 0.5, 256\n", + "loss = nn.CrossEntropyLoss(reduction='none')\n", + "trainer = torch.optim.SGD(net.parameters(), lr=lr)\n", + "\n", + "train(net,train_iter,test_iter,loss,trainer,num_epochs)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "XacJm6UxFvO5", + "outputId": "f7af8527-76b3-4e47-86f1-5ed6f0da7072" + }, + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "epoch 1: Train Loss: 0.912048797861735,Train Acc: 0.6603166666666667, Test Acc: 0.764\n", + "epoch 2: Train Loss: 0.5352540400187175,Train Acc: 0.8038333333333333, Test Acc: 0.7954\n", + "epoch 3: Train Loss: 0.46573794848124184,Train Acc: 0.8301666666666667, Test Acc: 0.8324\n", + "epoch 4: Train Loss: 0.43205339024861655,Train Acc: 0.84285, Test Acc: 0.8101\n", + "epoch 5: Train Loss: 0.4048792521794637,Train Acc: 0.8529833333333333, Test Acc: 0.8368\n", + "epoch 6: Train Loss: 0.38504457925160723,Train Acc: 0.8602, Test Acc: 0.8268\n", + "epoch 7: Train Loss: 0.3714546977996826,Train Acc: 0.8639333333333333, Test Acc: 0.8289\n", + "epoch 8: Train Loss: 0.36186040891011556,Train Acc: 0.8685166666666667, Test Acc: 0.8471\n", + "epoch 9: Train Loss: 0.3489550939242045,Train Acc: 0.8707, Test Acc: 0.8327\n", + "epoch 10: Train Loss: 0.3412602102279663,Train Acc: 0.8729666666666667, Test Acc: 0.849\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Inbuilt layer " + ], + "metadata": { + "id": "SoL94zrxGeJe" + } + }, + { + "cell_type": "code", + "source": [ + "net=nn.Sequential(\n", + " nn.Flatten(),\n", + " nn.Linear(784,256),\n", + " nn.ReLU(),\n", + " nn.Dropout(dropout1),\n", + " nn.Linear(256,256),\n", + " nn.ReLU(),\n", + " nn.Dropout(dropout2),\n", + " nn.Linear(256,10)\n", + ")\n", + "\n", + "def init_weights(layer):\n", + " if isinstance(layer,nn.Linear):\n", + " nn.init.normal_(layer.weight,std=0.01)\n", + "\n", + "net.apply(init_weights)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "1ECLWx8KGhqi", + "outputId": "be4ba4b1-9379-42cc-d812-0a0f2bebaf5c" + }, + "execution_count": 23, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "Sequential(\n", + " (0): Flatten(start_dim=1, end_dim=-1)\n", + " (1): Linear(in_features=784, out_features=256, bias=True)\n", + " (2): ReLU()\n", + " (3): Dropout(p=0.2, inplace=False)\n", + " (4): Linear(in_features=256, out_features=256, bias=True)\n", + " (5): ReLU()\n", + " (6): Dropout(p=0.5, inplace=False)\n", + " (7): Linear(in_features=256, out_features=10, bias=True)\n", + ")" + ] + }, + "metadata": {}, + "execution_count": 23 + } + ] + }, + { + "cell_type": "code", + "source": [ + "trainer = torch.optim.SGD(net.parameters(), lr=lr) #We initialize a new trainer\n", + "\n", + "train(net,train_iter,test_iter,loss,trainer,num_epochs)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "qj6PLvJiHOvG", + "outputId": "99ca500b-c6ef-4342-f3b1-361a85d6725e" + }, + "execution_count": 24, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "epoch 1: Train Loss: 1.1860956886291505,Train Acc: 0.5430333333333334, Test Acc: 0.7129\n", + "epoch 2: Train Loss: 0.5906720139821371,Train Acc: 0.77985, Test Acc: 0.8059\n", + "epoch 3: Train Loss: 0.49914352366129555,Train Acc: 0.81765, Test Acc: 0.8142\n", + "epoch 4: Train Loss: 0.44643895702362063,Train Acc: 0.8367333333333333, Test Acc: 0.8112\n", + "epoch 5: Train Loss: 0.4219759000142415,Train Acc: 0.8464666666666667, Test Acc: 0.8362\n", + "epoch 6: Train Loss: 0.39922035910288495,Train Acc: 0.8530333333333333, Test Acc: 0.8476\n", + "epoch 7: Train Loss: 0.3804882472038269,Train Acc: 0.8611166666666666, Test Acc: 0.8473\n", + "epoch 8: Train Loss: 0.3704915075937907,Train Acc: 0.86485, Test Acc: 0.8573\n", + "epoch 9: Train Loss: 0.3563785500526428,Train Acc: 0.86855, Test Acc: 0.8504\n", + "epoch 10: Train Loss: 0.3467910073598226,Train Acc: 0.8733666666666666, Test Acc: 0.8423\n" + ] + } + ] + } + ] +} \ No newline at end of file