{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1e40a88b", "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from PIL import Image\n", "import torch\n", "from torch import nn\n", "from importlib.util import find_spec\n", "if find_spec(\"text_recognizer\") is None:\n", " import sys\n", " sys.path.append('..')" ] }, { "cell_type": "code", "execution_count": 2, "id": "2ab9ac7a-a288-45bc-bfb7-8579a3a38d93", "metadata": {}, "outputs": [], "source": [ "import torch.nn.functional as F" ] }, { "cell_type": "code", "execution_count": 4, "id": "ecab65ba-5aa0-45f0-99d7-e837464185ac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ " torch.Tensor>" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "F.softmax" ] }, { "cell_type": "code", "execution_count": 5, "id": "3e812a1e", "metadata": {}, "outputs": [], "source": [ "import attr" ] }, { "cell_type": "code", "execution_count": 10, "id": "a42a7988", "metadata": {}, "outputs": [], "source": [ "@attr.s\n", "class C(object):\n", " d = {2: \"hej\"}\n", " x: F.softmax = attr.ib(init=False, default=F.softmax)\n", " @x.validator\n", " def check(self, attribute, value):\n", " print(attribute)\n", " print(self.x)" ] }, { "cell_type": "code", "execution_count": 14, "id": "660a7b1f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Attribute(name='x', default=, validator=, repr=True, eq=True, eq_key=None, order=True, order_key=None, hash=None, init=False, metadata=mappingproxy({}), type=, converter=None, kw_only=False, inherited=False, on_setattr=None)\n", "\n" ] } ], "source": [ "c = C()" ] }, { "cell_type": "code", "execution_count": 12, "id": "9c3d1163", "metadata": {}, "outputs": [ { "data": { "text/plain": [ " torch.Tensor>" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.x" ] }, { "cell_type": "code", "execution_count": 26, "id": "b3c8879c", "metadata": {}, "outputs": [], "source": [ "from torch import nn" ] }, { "cell_type": "code", "execution_count": 33, "id": "2f5f6b75", "metadata": {}, "outputs": [], "source": [ "l = nn.ModuleList([])" ] }, { "cell_type": "code", "execution_count": 34, "id": "9938ec53", "metadata": {}, "outputs": [], "source": [ "f = nn.Linear(10, 10)" ] }, { "cell_type": "code", "execution_count": 35, "id": "fc49db78", "metadata": {}, "outputs": [], "source": [ "for _ in range(10):\n", " l.append(f)" ] }, { "cell_type": "code", "execution_count": 36, "id": "e799a9dc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ModuleList(\n", " (0): Linear(in_features=10, out_features=10, bias=True)\n", " (1): Linear(in_features=10, out_features=10, bias=True)\n", " (2): Linear(in_features=10, out_features=10, bias=True)\n", " (3): Linear(in_features=10, out_features=10, bias=True)\n", " (4): Linear(in_features=10, out_features=10, bias=True)\n", " (5): Linear(in_features=10, out_features=10, bias=True)\n", " (6): Linear(in_features=10, out_features=10, bias=True)\n", " (7): Linear(in_features=10, out_features=10, bias=True)\n", " (8): Linear(in_features=10, out_features=10, bias=True)\n", " (9): Linear(in_features=10, out_features=10, bias=True)\n", ")" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "l" ] }, { "cell_type": "code", "execution_count": 37, "id": "17213dfb", "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'Linear' object has no attribute 'copy'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_31696/2302067867.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mff\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m~/.cache/pypoetry/virtualenvs/text-recognizer-ejNaVa9M-py3.9/lib/python3.9/site-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m 1128\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mname\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmodules\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1129\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodules\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1130\u001b[0;31m raise AttributeError(\"'{}' object has no attribute '{}'\".format(\n\u001b[0m\u001b[1;32m 1131\u001b[0m type(self).__name__, name))\n\u001b[1;32m 1132\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: 'Linear' object has no attribute 'copy'" ] } ], "source": [ "ff = f.copy()" ] }, { "cell_type": "code", "execution_count": 38, "id": "60277c26", "metadata": {}, "outputs": [], "source": [ "from copy import deepcopy" ] }, { "cell_type": "code", "execution_count": 39, "id": "cf86534a", "metadata": {}, "outputs": [], "source": [ "ff = deepcopy(f)" ] }, { "cell_type": "code", "execution_count": 43, "id": "2a260dc8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140011688939472" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(ff)" ] }, { "cell_type": "code", "execution_count": 42, "id": "6dcf5f63", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140011688936544" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(f)" ] }, { "cell_type": "code", "execution_count": 44, "id": "74958f8d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140011688936544" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l[0])" ] }, { "cell_type": "code", "execution_count": 45, "id": "bcceabd5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140011688936544" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l[1])" ] }, { "cell_type": "code", "execution_count": 58, "id": "191a0b03", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'nn'" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\".\".join(\"nn.LayerNorm\".split(\".\")[:-1])" ] }, { "cell_type": "code", "execution_count": 60, "id": "4ff8ae08", "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'str' object has no attribute 'LayerNorm'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_31696/162121485.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"torch.nn\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"LayerNorm\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'str' object has no attribute 'LayerNorm'" ] } ], "source": [ "getattr(\"torch.nn\", \"LayerNorm\")" ] }, { "cell_type": "code", "execution_count": null, "id": "4d536bf2", "metadata": {}, "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.9.6" } }, "nbformat": 4, "nbformat_minor": 5 }