From 8a5452be066bd93b8e906f820cfb2bb7274753a8 Mon Sep 17 00:00:00 2001 From: CharlieCornelius <147247234+CharlieCornelius@users.noreply.github.com> Date: Wed, 20 Mar 2024 20:39:20 +0800 Subject: [PATCH] Add files via upload --- "\345\261\210\351\223\204/1.ipynb" | 121 +++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 "\345\261\210\351\223\204/1.ipynb" diff --git "a/\345\261\210\351\223\204/1.ipynb" "b/\345\261\210\351\223\204/1.ipynb" new file mode 100644 index 0000000..8a0ef07 --- /dev/null +++ "b/\345\261\210\351\223\204/1.ipynb" @@ -0,0 +1,121 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'd2l'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[3], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m nn\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01md2l\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m torch \u001b[38;5;28;01mas\u001b[39;00m d2l\n\u001b[0;32m 5\u001b[0m batch_size \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m256\u001b[39m\n\u001b[0;32m 6\u001b[0m train_iter, test_iter \u001b[38;5;241m=\u001b[39m d2l\u001b[38;5;241m.\u001b[39mload_data_fashion_mnist(batch_size)\n", + "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'd2l'" + ] + } + ], + "source": [ + "import torch\n", + "from torch import nn\n", + "from d2l import torch as d2l\n", + "\n", + "batch_size = 256\n", + "train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num_inputs, num_outputs, num_hiddens = 784, 10, 256\n", + "\n", + "W1 = nn.Parameter(torch.randn(\n", + " num_inputs, num_hiddens, requires_grad=True) * 0.01)\n", + "b1 = nn.Parameter(torch.zeros(num_hiddens, requires_grad=True))\n", + "W2 = nn.Parameter(torch.randn(\n", + " num_hiddens, num_outputs, requires_grad=True) * 0.01)\n", + "b2 = nn.Parameter(torch.zeros(num_outputs, requires_grad=True))\n", + "\n", + "params = [W1, b1, W2, b2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def relu(X):\n", + " a = torch.zeros_like(X)\n", + " return torch.max(X, a)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def net(X):\n", + " X = X.reshape((-1, num_inputs))\n", + " H = relu(X@W1 + b1) # @ 矩阵乘法\n", + " return (H@W2 + b2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "loss = nn.CrossEntropyLoss(reduction='none')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num_epochs, lr = 10, 0.1\n", + "updater = torch.optim.SGD(params, lr=lr)\n", + "d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "d2l.predict_ch3(net, test_iter)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "pytorch", + "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.11.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}