{ "cells": [ { "cell_type": "markdown", "id": "94874952", "metadata": { "id": "94874952" }, "source": [ "# **自定義類別物件**\n", "此份程式碼為 Custom_building 的 PyTorch 參考寫法。\n", "## 本章節內容大綱\n", "* ### [Custom Loss](#Loss)\n", "* ### [Custom Layer](#Layer)\n", "* ### [Custom Model](#Model)\n", "---" ] }, { "cell_type": "markdown", "id": "48c7f0ca", "metadata": { "id": "48c7f0ca" }, "source": [ "## 匯入套件" ] }, { "cell_type": "code", "execution_count": null, "id": "b55f3395", "metadata": { "id": "b55f3395" }, "outputs": [], "source": [ "!pip install torchsummary" ] }, { "cell_type": "code", "execution_count": null, "id": "aed34e7d", "metadata": { "id": "aed34e7d" }, "outputs": [], "source": [ "import numpy as np\n", "\n", "import torch\n", "from torchsummary import summary" ] }, { "cell_type": "code", "execution_count": null, "id": "5514391e", "metadata": { "id": "5514391e" }, "outputs": [], "source": [ "y_true = torch.randn((10, 4))\n", "y_pred = torch.randn((10, 4))" ] }, { "cell_type": "markdown", "id": "a4a38805", "metadata": { "id": "a4a38805" }, "source": [ "## Loss" ] }, { "cell_type": "code", "execution_count": null, "id": "2e2339c6", "metadata": { "id": "2e2339c6" }, "outputs": [], "source": [ "# build loss by torch.nn module\n", "mse_loss = torch.nn.MSELoss()\n", "mse_loss(y_pred, y_true)" ] }, { "cell_type": "code", "execution_count": null, "id": "55ce5237", "metadata": { "id": "55ce5237" }, "outputs": [], "source": [ "class my_mse(torch.nn.Module): # build loss object by custom loss\n", " def __init__(self):\n", " super(my_mse, self).__init__()\n", "\n", " def forward(self, outputs, targets):\n", " return torch.mean((outputs - targets)**2)" ] }, { "cell_type": "code", "execution_count": null, "id": "04013ceb", "metadata": { "id": "04013ceb" }, "outputs": [], "source": [ "my_mse_loss = my_mse()\n", "my_mse_loss(y_true, y_pred)" ] }, { "cell_type": "markdown", "id": "11cc631c", "metadata": { "id": "11cc631c" }, "source": [ "## Layer" ] }, { "cell_type": "code", "execution_count": null, "id": "d5468b23", "metadata": { "id": "d5468b23" }, "outputs": [], "source": [ "x = torch.randn((10, 5))" ] }, { "cell_type": "code", "execution_count": null, "id": "8ae95b79", "metadata": { "id": "8ae95b79" }, "outputs": [], "source": [ "# build layer by torch.nn module\n", "linear_layer = torch.nn.Linear(5, 4)\n", "linear_layer(x)" ] }, { "cell_type": "code", "execution_count": null, "id": "b24106d8", "metadata": { "id": "b24106d8" }, "outputs": [], "source": [ "# use same initial parameters\n", "w = linear_layer.weight\n", "b = linear_layer.bias" ] }, { "cell_type": "code", "execution_count": null, "id": "66fbba4c", "metadata": { "id": "66fbba4c" }, "outputs": [], "source": [ "class my_linear(torch.nn.Module): # build layer object by custom class\n", " def __init__(self, w, b):\n", " super().__init__()\n", " self.w = w\n", " self.b = b\n", "\n", " def forward(self, x):\n", " return torch.matmul(x, self.w.transpose(0, 1)) + self.b" ] }, { "cell_type": "code", "execution_count": null, "id": "b54ac45c", "metadata": { "id": "b54ac45c" }, "outputs": [], "source": [ "my_linear_layer = my_linear(w, b)\n", "my_linear_layer(x)" ] }, { "cell_type": "markdown", "id": "101c3754", "metadata": { "id": "101c3754" }, "source": [ "## Model" ] }, { "cell_type": "code", "execution_count": null, "id": "ee9ecf1d", "metadata": { "id": "ee9ecf1d" }, "outputs": [], "source": [ "num_classes = 4" ] }, { "cell_type": "code", "execution_count": null, "id": "6cef4001", "metadata": { "id": "6cef4001" }, "outputs": [], "source": [ "def build_model(input_shape, output_shape): # build model by torch.nn module\n", " model = torch.nn.Sequential(\n", " torch.nn.Linear(input_shape, 16),\n", " torch.nn.Linear(16, 32),\n", " torch.nn.Linear(32, output_shape))\n", " return model" ] }, { "cell_type": "code", "execution_count": null, "id": "ca1c3829", "metadata": { "id": "ca1c3829" }, "outputs": [], "source": [ "model = build_model(5, num_classes)\n", "summary(model, x[0].shape, device='cpu')" ] }, { "cell_type": "code", "execution_count": null, "id": "b0c18bad", "metadata": { "id": "b0c18bad" }, "outputs": [], "source": [ "class my_net(torch.nn.Module): # build model object by custom class\n", " def __init__(self, input_shape, output_shape):\n", " super(my_net, self).__init__()\n", " self.hidden_layer_1 = torch.nn.Linear(input_shape, 16)\n", " self.hidden_layer_2 = torch.nn.Linear(16, 32)\n", " self.output_layer = torch.nn.Linear(32, output_shape)\n", "\n", " def forward(self, x):\n", " x = self.hidden_layer_1(x)\n", " x = self.hidden_layer_2(x)\n", " outputs = self.output_layer(x)\n", " return outputs" ] }, { "cell_type": "code", "execution_count": null, "id": "c203e0e9", "metadata": { "id": "c203e0e9" }, "outputs": [], "source": [ "my_model = my_net(5, 4)\n", "summary(my_model, x[0].shape, device='cpu')" ] }, { "cell_type": "code", "execution_count": null, "id": "2570399d", "metadata": { "id": "2570399d" }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.12" }, "colab": { "provenance": [] }, "accelerator": "GPU", "gpuClass": "standard" }, "nbformat": 4, "nbformat_minor": 5 }