|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "6e4d2e04", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import torch" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 11, |
| 16 | + "id": "ec74c8a7", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "class MyModule(torch.nn.Module):\n", |
| 21 | + " def __init__(self, N, M):\n", |
| 22 | + " super(MyModule, self).__init__()\n", |
| 23 | + " self.linear = torch.nn.Linear(N, M)\n", |
| 24 | + "\n", |
| 25 | + " def forward(self, input):\n", |
| 26 | + " return self.linear(input)\n" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": null, |
| 32 | + "id": "180e54ac", |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "my_module = MyModule(10,20)\n", |
| 37 | + "# sm = torch.jit.script(my_module)\n", |
| 38 | + "sm = torch.jit.script(my_module)\n", |
| 39 | + "sm.save(\"models/my_module.pt\")" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": null, |
| 45 | + "id": "d5e377e0", |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": null, |
| 53 | + "id": "89e90304", |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [ |
| 56 | + { |
| 57 | + "ename": "RuntimeError", |
| 58 | + "evalue": "Parent directory models does not exist.", |
| 59 | + "output_type": "error", |
| 60 | + "traceback": [ |
| 61 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 62 | + "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", |
| 63 | + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43msm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msave\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmodels/my_module.pt\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", |
| 64 | + "File \u001b[0;32m~/.venv/lib/python3.12/site-packages/torch/jit/_script.py:754\u001b[0m, in \u001b[0;36mRecursiveScriptModule.save\u001b[0;34m(self, f, **kwargs)\u001b[0m\n\u001b[1;32m 745\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21msave\u001b[39m(\u001b[38;5;28mself\u001b[39m, f, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 746\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"Save with a file-like object.\u001b[39;00m\n\u001b[1;32m 747\u001b[0m \n\u001b[1;32m 748\u001b[0m \u001b[38;5;124;03m save(f, _extra_files={})\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 752\u001b[0m \u001b[38;5;124;03m DO NOT confuse these two functions when it comes to the 'f' parameter functionality.\u001b[39;00m\n\u001b[1;32m 753\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 754\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_c\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msave\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mf\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", |
| 65 | + "\u001b[0;31mRuntimeError\u001b[0m: Parent directory models does not exist." |
| 66 | + ] |
| 67 | + } |
| 68 | + ], |
| 69 | + "source": [ |
| 70 | + "# sm.save(\"models/my_module.pt\")" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": null, |
| 76 | + "id": "d85b6e83", |
| 77 | + "metadata": {}, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "m = torch.jit.load(\"models/my_module.pt\")" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 17, |
| 86 | + "id": "7d6255fd", |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [ |
| 89 | + { |
| 90 | + "data": { |
| 91 | + "text/plain": [ |
| 92 | + "RecursiveScriptModule(\n", |
| 93 | + " original_name=MyModule\n", |
| 94 | + " (linear): RecursiveScriptModule(original_name=Linear)\n", |
| 95 | + ")" |
| 96 | + ] |
| 97 | + }, |
| 98 | + "execution_count": 17, |
| 99 | + "metadata": {}, |
| 100 | + "output_type": "execute_result" |
| 101 | + } |
| 102 | + ], |
| 103 | + "source": [ |
| 104 | + "m" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": 20, |
| 110 | + "id": "0d8ff397", |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "x = torch.randn(10)" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": 21, |
| 120 | + "id": "ffe62563", |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [ |
| 123 | + { |
| 124 | + "data": { |
| 125 | + "text/plain": [ |
| 126 | + "tensor([-0.5386, 0.6545, 0.4650, -0.3320, 0.2735, 0.2796, -0.4549, 0.2646,\n", |
| 127 | + " -0.9322, -0.3031, -0.3441, -0.3761, 0.6457, 0.6456, -0.2478, -0.2270,\n", |
| 128 | + " 0.8485, 0.9710, -0.0596, 0.6110], grad_fn=<ViewBackward0>)" |
| 129 | + ] |
| 130 | + }, |
| 131 | + "execution_count": 21, |
| 132 | + "metadata": {}, |
| 133 | + "output_type": "execute_result" |
| 134 | + } |
| 135 | + ], |
| 136 | + "source": [ |
| 137 | + "m(x)" |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "code", |
| 142 | + "execution_count": null, |
| 143 | + "id": "51739d61", |
| 144 | + "metadata": {}, |
| 145 | + "outputs": [], |
| 146 | + "source": [] |
| 147 | + } |
| 148 | + ], |
| 149 | + "metadata": { |
| 150 | + "kernelspec": { |
| 151 | + "display_name": ".venv", |
| 152 | + "language": "python", |
| 153 | + "name": "python3" |
| 154 | + }, |
| 155 | + "language_info": { |
| 156 | + "codemirror_mode": { |
| 157 | + "name": "ipython", |
| 158 | + "version": 3 |
| 159 | + }, |
| 160 | + "file_extension": ".py", |
| 161 | + "mimetype": "text/x-python", |
| 162 | + "name": "python", |
| 163 | + "nbconvert_exporter": "python", |
| 164 | + "pygments_lexer": "ipython3", |
| 165 | + "version": "3.12.9" |
| 166 | + } |
| 167 | + }, |
| 168 | + "nbformat": 4, |
| 169 | + "nbformat_minor": 5 |
| 170 | +} |
0 commit comments