{ "cells": [ { "cell_type": "markdown", "id": "6de11dee", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "# File I/O\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "3dff9f92", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T20:14:02.401475Z", "iopub.status.busy": "2023-08-18T20:14:02.401201Z", "iopub.status.idle": "2023-08-18T20:14:04.212167Z", "shell.execute_reply": "2023-08-18T20:14:04.211057Z" }, "origin_pos": 3, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "import torch\n", "from torch import nn\n", "from torch.nn import functional as F" ] }, { "cell_type": "markdown", "id": "ff5d77b5", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Loading and Saving Tensors" ] }, { "cell_type": "code", "execution_count": 3, "id": "5cb11ed0", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T20:14:04.253634Z", "iopub.status.busy": "2023-08-18T20:14:04.253018Z", "iopub.status.idle": "2023-08-18T20:14:04.262485Z", "shell.execute_reply": "2023-08-18T20:14:04.261617Z" }, "origin_pos": 13, "tab": [ "pytorch" ] }, "outputs": [ { "data": { "text/plain": [ "tensor([0, 1, 2, 3])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = torch.arange(4)\n", "torch.save(x, 'x-file')\n", "\n", "x2 = torch.load('x-file')\n", "x2" ] }, { "cell_type": "markdown", "id": "3fd9fd6c", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Store a list of tensors and read them back into memory" ] }, { "cell_type": "code", "execution_count": 4, "id": "86dba6ad", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T20:14:04.267689Z", "iopub.status.busy": "2023-08-18T20:14:04.267013Z", "iopub.status.idle": "2023-08-18T20:14:04.275474Z", "shell.execute_reply": "2023-08-18T20:14:04.274471Z" }, "origin_pos": 18, "tab": [ "pytorch" ] }, "outputs": [ { "data": { "text/plain": [ "(tensor([0, 1, 2, 3]), tensor([0., 0., 0., 0.]))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = torch.zeros(4)\n", "torch.save([x, y],'x-files')\n", "x2, y2 = torch.load('x-files')\n", "(x2, y2)" ] }, { "cell_type": "markdown", "id": "c8fe2e9f", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Write and read a dictionary that maps\n", "from strings to tensors" ] }, { "cell_type": "code", "execution_count": 5, "id": "1b8f14c8", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T20:14:04.279967Z", "iopub.status.busy": "2023-08-18T20:14:04.279484Z", "iopub.status.idle": "2023-08-18T20:14:04.286799Z", "shell.execute_reply": "2023-08-18T20:14:04.286045Z" }, "origin_pos": 23, "tab": [ "pytorch" ] }, "outputs": [ { "data": { "text/plain": [ "{'x': tensor([0, 1, 2, 3]), 'y': tensor([0., 0., 0., 0.])}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mydict = {'x': x, 'y': y}\n", "torch.save(mydict, 'mydict')\n", "mydict2 = torch.load('mydict')\n", "mydict2" ] }, { "cell_type": "markdown", "id": "8b5afad8", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Loading and Saving Model Parameters\n", "Let's start with our familiar MLP" ] }, { "cell_type": "code", "execution_count": 6, "id": "6917c6ad", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T20:14:04.290900Z", "iopub.status.busy": "2023-08-18T20:14:04.290420Z", "iopub.status.idle": "2023-08-18T20:14:04.301461Z", "shell.execute_reply": "2023-08-18T20:14:04.300490Z" }, "origin_pos": 28, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "class MLP(nn.Module):\n", " def __init__(self):\n", " super().__init__()\n", " self.hidden = nn.LazyLinear(256)\n", " self.output = nn.LazyLinear(10)\n", "\n", " def forward(self, x):\n", " return self.output(F.relu(self.hidden(x)))\n", "\n", "net = MLP()\n", "X = torch.randn(size=(2, 20))\n", "Y = net(X)" ] }, { "cell_type": "markdown", "id": "fd562080", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Store the parameters of the model as a file" ] }, { "cell_type": "code", "execution_count": 7, "id": "88dfe184", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T20:14:04.304908Z", "iopub.status.busy": "2023-08-18T20:14:04.304617Z", "iopub.status.idle": "2023-08-18T20:14:04.309701Z", "shell.execute_reply": "2023-08-18T20:14:04.308927Z" }, "origin_pos": 33, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "torch.save(net.state_dict(), 'mlp.params')" ] }, { "cell_type": "markdown", "id": "afffd310", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Read the parameters stored in the file directly" ] }, { "cell_type": "code", "execution_count": 8, "id": "0a8e9c03", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T20:14:04.312721Z", "iopub.status.busy": "2023-08-18T20:14:04.312444Z", "iopub.status.idle": "2023-08-18T20:14:04.320044Z", "shell.execute_reply": "2023-08-18T20:14:04.319202Z" }, "origin_pos": 38, "tab": [ "pytorch" ] }, "outputs": [ { "data": { "text/plain": [ "MLP(\n", " (hidden): LazyLinear(in_features=0, out_features=256, bias=True)\n", " (output): LazyLinear(in_features=0, out_features=10, bias=True)\n", ")" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clone = MLP()\n", "clone.load_state_dict(torch.load('mlp.params'))\n", "clone.eval()" ] }, { "cell_type": "code", "execution_count": 9, "id": "d65ae251", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T20:14:04.323094Z", "iopub.status.busy": "2023-08-18T20:14:04.322816Z", "iopub.status.idle": "2023-08-18T20:14:04.330451Z", "shell.execute_reply": "2023-08-18T20:14:04.329304Z" }, "origin_pos": 42, "tab": [ "pytorch" ] }, "outputs": [ { "data": { "text/plain": [ "tensor([[True, True, True, True, True, True, True, True, True, True],\n", " [True, True, True, True, True, True, True, True, True, True]])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y_clone = clone(X)\n", "Y_clone == Y" ] } ], "metadata": { "celltoolbar": "Slideshow", "language_info": { "name": "python" }, "required_libs": [], "rise": { "autolaunch": true, "enable_chalkboard": true, "overlay": "
", "scroll": true } }, "nbformat": 4, "nbformat_minor": 5 }