{ "cells": [ { "cell_type": "markdown", "id": "000259b0-c4c0-49e1-982c-51b9c58da63a", "metadata": {}, "source": [ "# 1. 认识张量" ] }, { "cell_type": "markdown", "id": "fe1497a8-7903-41ab-b5cd-5ae04054a699", "metadata": {}, "source": [ "- 标量\n", "- 向量(矢量)\n", "- 张量(变化关系)" ] }, { "cell_type": "code", "execution_count": null, "id": "acf46b28-8e90-4414-bd1b-c2b243afea68", "metadata": {}, "outputs": [], "source": [ "!pip install torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --index-url https://download.pytorch.org/whl/cpu" ] }, { "cell_type": "markdown", "id": "f55cf0df-851f-4e1b-ab36-9710fe2dd0bc", "metadata": {}, "source": [ "- 张量的使用与numpy的ndarray基本上差不多!(与线性代数相关的部分)" ] }, { "cell_type": "markdown", "id": "52b332d5-c081-4cfe-867b-de30dea8ae33", "metadata": {}, "source": [ "## 1.1. 创建张量" ] }, { "cell_type": "code", "execution_count": 10, "id": "41c77bcd-8730-459f-9040-150ae67a55f5", "metadata": {}, "outputs": [], "source": [ "from torch import LongTensor, FloatTensor, Tensor" ] }, { "cell_type": "code", "execution_count": 11, "id": "0a45c3e5-725d-4271-916f-808891a08f73", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([1., 2., 3., 4.])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = Tensor((1, 2, 3, 4))\n", "t" ] }, { "cell_type": "code", "execution_count": 12, "id": "c7d21c70-aa9a-42e8-804b-f82ca38a9639", "metadata": {}, "outputs": [], "source": [ "import numpy \n", "\n", "m = numpy.array([3,4,5,6])\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "828cf0d2-2474-402d-a460-855a04f29aef", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([3., 4., 5., 6.])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = Tensor(m)\n", "t" ] }, { "cell_type": "markdown", "id": "bcd56473-184d-478c-9d3d-f4d5a4ae2f40", "metadata": {}, "source": [ "> 1. 构造器(Storage:tuple,list, numpy.ndarray, Tensor, 整数:维度,)\n", "> 2. 工厂模式:函数生成对象" ] }, { "cell_type": "code", "execution_count": 16, "id": "b70bb183-c993-491d-aa00-041ea0e75217", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[5.2857e-18, 1.0243e-42, 0.0000e+00, 0.0000e+00],\n", " [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],\n", " [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = Tensor(3, 4) # Tensor == FloatTensor # 不稳定的\n", "t" ] }, { "cell_type": "code", "execution_count": 24, "id": "7f1745e4-38f4-4034-8aef-a11ac45fe92a", "metadata": {}, "outputs": [], "source": [ "from torch import tensor # 通过类的构造器创建\n", "t = tensor(data=[1,2,3], dtype=float, device=\"cuda:0\", requires_grad=True) # 参数是稳定 : cpu, cuda\n", "# t.backward()" ] }, { "cell_type": "code", "execution_count": 26, "id": "a8842c37-3ed4-45c9-a940-ca1c5126c680", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(1)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import torch\n", "t = torch.tensor(1) # 0维张量\n", "t" ] }, { "cell_type": "code", "execution_count": 30, "id": "fc31fbcf-31d3-4e9f-ad42-5647c4b0f5bc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array(1, dtype=int64), 1)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = t.detach().numpy() # numpy向量 t.cpu().detach().numpy()\n", "b = t.detach().item() # 标量\n", "a, b" ] }, { "cell_type": "markdown", "id": "82295a52-583d-4c67-9e61-56f099671490", "metadata": {}, "source": [ "## 1.2. 张量的操作" ] }, { "cell_type": "code", "execution_count": 41, "id": "0f5e12ee-b0ed-4d3f-b29a-130679a000af", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "torch.Size([3, 3, 4])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = torch.tensor([\n", " [[1, 2, 3, 4],\n", " [5, 6, 7, 8],\n", " [9, 0, 1, 2]],\n", " [[1, 2, 3, 4],\n", " [5, 6, 7, 8],\n", " [9, 0, 1, 2]],\n", " [[1, 2, 3, 4],\n", " [5, 6, 7, 8],\n", " [9, 0, 1, 2]]\n", "], dtype=float)\n", "# 与numpy完全一样\n", "t.dtype\n", "t.shape" ] }, { "cell_type": "code", "execution_count": 47, "id": "6b0df8a4-6a35-4fff-9cb1-2082e9c6c360", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([2., 6., 0.], dtype=torch.float64)" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[0, ..., 1]" ] }, { "cell_type": "code", "execution_count": 44, "id": "13152bee-ab2b-4251-ba6a-ad3862c95f5f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[1., 2., 3., 4.],\n", " [5., 6., 7., 8.]], dtype=torch.float64)" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[0, 0:2, ...]" ] }, { "cell_type": "markdown", "id": "e606f7f2-f90a-4d23-8e52-a4f666256a79", "metadata": {}, "source": [ "## 1.3. 张量的运算" ] }, { "cell_type": "code", "execution_count": 48, "id": "be38db75-1940-4421-ae48-22f3dde14949", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1, 4, 9, 16],\n", " [25, 36, 49, 64]])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = tensor([\n", " [1, 2, 3, 4],\n", " [5, 6, 7, 8] \n", "])\n", "\n", "b = tensor([\n", " [1, 2, 3, 4],\n", " [5, 6, 7, 8] \n", "])\n", "\n", "a * b" ] }, { "cell_type": "code", "execution_count": 49, "id": "98062436-e5cf-4db5-8b73-da3e4dc7cb4c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 5, 10, 15, 20],\n", " [25, 30, 35, 40]])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * 5" ] }, { "cell_type": "code", "execution_count": 50, "id": "79661746-de23-47e2-9290-de5be3097335", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1, 4, 9, 16],\n", " [25, 36, 49, 64]])" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a ** 2" ] }, { "cell_type": "code", "execution_count": 59, "id": "d4115674-f5b8-47a7-a332-9e86b6648e28", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 30, 70],\n", " [ 70, 174]])" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.matmul(b.T) # mul(元素乘) matmul(内积)" ] }, { "cell_type": "code", "execution_count": 57, "id": "da49a3a7-4676-4a12-9de0-3537bbc30f95", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[1;31mSignature:\u001b[0m \u001b[0mabs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m/\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mDocstring:\u001b[0m Return the absolute value of the argument.\n", "\u001b[1;31mType:\u001b[0m builtin_function_or_method" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "abs?" ] }, { "cell_type": "markdown", "id": "a2cd297e-9138-4c57-87d2-2d0dbc8d4372", "metadata": {}, "source": [ "## 1.4. 张量的求导数" ] }, { "cell_type": "code", "execution_count": 60, "id": "95aa038a-2f04-40cd-aede-1b71ee8d5454", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "None\n", "None\n", "False\n" ] } ], "source": [ "x = torch.tensor([1.0, 2.0, 3.3])\n", "print(x.is_leaf)\n", "print(x.grad)\n", "print(x.grad_fn)\n", "print(x.requires_grad)" ] }, { "cell_type": "code", "execution_count": 63, "id": "05f96a6a-7539-4ad2-bef6-2ad59add1bf1", "metadata": {}, "outputs": [], "source": [ "x.requires_grad=True\n", "y = 3* (x ** 2) + 4" ] }, { "cell_type": "code", "execution_count": 64, "id": "d9d7cf2d-6773-44c5-a651-65ad4dfa561c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "None\n", "None\n", "True\n" ] } ], "source": [ "print(x.is_leaf)\n", "print(x.grad)\n", "print(x.grad_fn)\n", "print(x.requires_grad)" ] }, { "cell_type": "code", "execution_count": 66, "id": "382613b1-a129-4a19-85a3-776a6e1fa2e9", "metadata": {}, "outputs": [], "source": [ "y.sum().backward()" ] }, { "cell_type": "code", "execution_count": 67, "id": "638c2090-0bad-4255-8e04-fc758c79de2f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "tensor([ 6.0000, 12.0000, 19.8000])\n", "None\n", "True\n" ] } ], "source": [ "print(x.is_leaf)\n", "print(x.grad)\n", "print(x.grad_fn)\n", "print(x.requires_grad)" ] }, { "cell_type": "markdown", "id": "6bc1b84b-15fb-459f-acef-49d260f36138", "metadata": {}, "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.10.7" } }, "nbformat": 4, "nbformat_minor": 5 }