Last active
May 17, 2024 06:30
-
-
Save dsaint31x/f34fcb64f0955a764592edd51628953d to your computer and use it in GitHub Desktop.
dl_module_list_dict_sequential.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"gpuType": "L4", | |
"authorship_tag": "ABX9TyOj7EPHVqUR71iuT2XeOmIn", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/dsaint31x/f34fcb64f0955a764592edd51628953d/dl_module_list_dict_sequential.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"import torch.nn as nn\n", | |
"\n", | |
"model = nn.Sequential(\n", | |
" nn.Linear(10, 20),\n", | |
" nn.ReLU(),\n", | |
" nn.Linear(20, 10)\n", | |
")\n", | |
"\n", | |
"print(model)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "iKziq8IWP058", | |
"outputId": "8b9e9c5e-63d1-441a-8bea-8adbd3b4474b" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Sequential(\n", | |
" (0): Linear(in_features=10, out_features=20, bias=True)\n", | |
" (1): ReLU()\n", | |
" (2): Linear(in_features=20, out_features=10, bias=True)\n", | |
")\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"import torch.nn as nn\n", | |
"from collections import OrderedDict\n", | |
"\n", | |
"model = nn.Sequential(\n", | |
" OrderedDict({\n", | |
" 'ds_l0':nn.Linear(10, 20),\n", | |
" 'ds_act0':nn.ReLU(),\n", | |
" 'ds_l1':nn.Linear(20, 10),\n", | |
" })\n", | |
")\n", | |
"\n", | |
"print(model)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "kyFWDY8pOHq6", | |
"outputId": "6715b963-5f12-48df-d273-243a10f595c1" | |
}, | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Sequential(\n", | |
" (ds_l0): Linear(in_features=10, out_features=20, bias=True)\n", | |
" (ds_act0): ReLU()\n", | |
" (ds_l1): Linear(in_features=20, out_features=10, bias=True)\n", | |
")\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"import torch.nn as nn\n", | |
"from collections import OrderedDict\n", | |
"\n", | |
"model = nn.Sequential(\n", | |
" OrderedDict([\n", | |
" ('ds_l0', nn.Linear(10, 20)),\n", | |
" ('ds_act0',nn.ReLU() ),\n", | |
" ('ds_l1', nn.Linear(20, 10)),\n", | |
" ])\n", | |
")\n", | |
"\n", | |
"print(model)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "5L7_bxsFOzXF", | |
"outputId": "27c45ced-81b4-4756-bb2b-c6a4ba232fe5" | |
}, | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Sequential(\n", | |
" (ds_l0): Linear(in_features=10, out_features=20, bias=True)\n", | |
" (ds_act0): ReLU()\n", | |
" (ds_l1): Linear(in_features=20, out_features=10, bias=True)\n", | |
")\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.ds_l0" | |
], | |
"metadata": { | |
"id": "0UqgKy4jPph6", | |
"outputId": "a572c8ee-2d43-401f-daf2-8a566ed0faab", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"Linear(in_features=10, out_features=20, bias=True)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 4 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"import torch.nn as nn\n", | |
"\n", | |
"class MyModel(nn.Module):\n", | |
" def __init__(self):\n", | |
" super(MyModel, self).__init__()\n", | |
" self.layers = nn.ModuleList([\n", | |
" nn.Linear(10, 20),\n", | |
" nn.ReLU(),\n", | |
" nn.Linear(20, 10)\n", | |
" ])\n", | |
"\n", | |
" def forward(self, x):\n", | |
" for layer in self.layers:\n", | |
" x = layer(x)\n", | |
" return x\n", | |
"\n", | |
"model = MyModel()\n", | |
"print(model)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "VldjbvYVP1mS", | |
"outputId": "1223b8ba-9b6a-45ae-de1b-fd205fafb39d" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"MyModel(\n", | |
" (layers): ModuleList(\n", | |
" (0): Linear(in_features=10, out_features=20, bias=True)\n", | |
" (1): ReLU()\n", | |
" (2): Linear(in_features=20, out_features=10, bias=True)\n", | |
" )\n", | |
")\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"import torch.nn as nn\n", | |
"\n", | |
"class MyModel(nn.Module):\n", | |
" def __init__(self):\n", | |
" super(MyModel, self).__init__()\n", | |
" self.layers = nn.ModuleDict({\n", | |
" 'fc1': nn.Linear(10, 20),\n", | |
" 'relu': nn.ReLU(),\n", | |
" 'fc2': nn.Linear(20, 10)\n", | |
" })\n", | |
"\n", | |
" def forward(self, x):\n", | |
" x = self.layers['fc1'](x)\n", | |
" x = self.layers['relu'](x)\n", | |
" x = self.layers['fc2'](x)\n", | |
" return x\n", | |
"\n", | |
"model = MyModel()\n", | |
"print(model)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "bOj_QrlyP4aW", | |
"outputId": "835517ac-c71a-4d02-feab-40bc60b4741a" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"MyModel(\n", | |
" (layers): ModuleDict(\n", | |
" (fc1): Linear(in_features=10, out_features=20, bias=True)\n", | |
" (relu): ReLU()\n", | |
" (fc2): Linear(in_features=20, out_features=10, bias=True)\n", | |
" )\n", | |
")\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"import torch.nn as nn\n", | |
"\n", | |
"class MyModel(nn.Module):\n", | |
" def __init__(self):\n", | |
" super(MyModel, self).__init__()\n", | |
" self.layers = [\n", | |
" nn.Linear(10, 20),\n", | |
" nn.ReLU(),\n", | |
" nn.Linear(20, 10)\n", | |
" ]\n", | |
"\n", | |
" def forward(self, x):\n", | |
" for layer in self.layers:\n", | |
" x = layer(x)\n", | |
" return x\n", | |
"\n", | |
"model = MyModel()\n", | |
"print(model)\n", | |
"print(\"Model parameters:\", list(model.parameters())) # 파라미터가 추적되지 않음" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "pNpN8LSyP60U", | |
"outputId": "ad99da24-3716-4047-d070-2b034f06b421" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"MyModel()\n", | |
"Model parameters: []\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"class MyModel(nn.Module):\n", | |
" def __init__(self):\n", | |
" super(MyModel, self).__init__()\n", | |
" self.layers = [\n", | |
" nn.Linear(10, 20),\n", | |
" nn.ReLU(),\n", | |
" nn.Linear(20, 10)\n", | |
" ]\n", | |
"\n", | |
" def forward(self, x):\n", | |
" for layer in self.layers:\n", | |
" x = layer(x)\n", | |
" return x\n", | |
"\n", | |
"model = MyModel()\n", | |
"model_gpu = model.to('cuda') # 일반 list를 사용하면 GPU로 이동하지 않음" | |
], | |
"metadata": { | |
"id": "gyiwnzWZP9Z4" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model_gpu" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "cL0yrgqgP_08", | |
"outputId": "59bbedbb-8a33-4333-9589-2b995e8d9d23" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"MyModel()" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 7 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "uC-CGmrKQnya", | |
"outputId": "1bf68998-d7b7-4880-e6ce-b5423ae73115" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"MyModel()" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 8 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "C7hVEE3KQpK4" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment