diff --git a/My_Experiments/PyTorch_Course/00_pytorch_fundamentals.ipynb b/My_Experiments/PyTorch_Course/00_pytorch_fundamentals.ipynb index 3268457..81f77f7 100644 --- a/My_Experiments/PyTorch_Course/00_pytorch_fundamentals.ipynb +++ b/My_Experiments/PyTorch_Course/00_pytorch_fundamentals.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 2, + "execution_count": 63, "id": "4138ee2e", "metadata": {}, "outputs": [ @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 64, "id": "fa46b5f3", "metadata": {}, "outputs": [ @@ -43,7 +43,7 @@ "tensor(7)" ] }, - "execution_count": 3, + "execution_count": 64, "metadata": {}, "output_type": "execute_result" } @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 65, "id": "80146c19", "metadata": {}, "outputs": [ @@ -66,7 +66,7 @@ "0" ] }, - "execution_count": 4, + "execution_count": 65, "metadata": {}, "output_type": "execute_result" } @@ -77,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 66, "id": "8ebd6387", "metadata": {}, "outputs": [ @@ -87,7 +87,7 @@ "7" ] }, - "execution_count": 5, + "execution_count": 66, "metadata": {}, "output_type": "execute_result" } @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 67, "id": "975ba0ba", "metadata": {}, "outputs": [ @@ -108,7 +108,7 @@ "tensor([7, 7])" ] }, - "execution_count": 6, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } @@ -120,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 68, "id": "adc95f24", "metadata": {}, "outputs": [ @@ -130,7 +130,7 @@ "1" ] }, - "execution_count": 7, + "execution_count": 68, "metadata": {}, "output_type": "execute_result" } @@ -141,7 +141,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 69, "id": "1364d5be", "metadata": {}, "outputs": [ @@ -151,7 +151,7 @@ "torch.Size([2])" ] }, - "execution_count": 8, + "execution_count": 69, "metadata": {}, "output_type": "execute_result" } @@ -162,7 +162,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 70, "id": "3d811521", "metadata": {}, "outputs": [ @@ -173,7 +173,7 @@ " [ 9, 10]])" ] }, - "execution_count": 9, + "execution_count": 70, "metadata": {}, "output_type": "execute_result" } @@ -186,7 +186,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 71, "id": "e9acf917", "metadata": {}, "outputs": [ @@ -196,7 +196,7 @@ "2" ] }, - "execution_count": 10, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -207,7 +207,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 72, "id": "40bd74ac", "metadata": {}, "outputs": [ @@ -217,7 +217,7 @@ "tensor([7, 8])" ] }, - "execution_count": 11, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } @@ -228,7 +228,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 73, "id": "8093f799", "metadata": {}, "outputs": [ @@ -238,7 +238,7 @@ "tensor([ 9, 10])" ] }, - "execution_count": 12, + "execution_count": 73, "metadata": {}, "output_type": "execute_result" } @@ -249,7 +249,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 74, "id": "85b1d000", "metadata": {}, "outputs": [ @@ -259,7 +259,7 @@ "torch.Size([2, 2])" ] }, - "execution_count": 13, + "execution_count": 74, "metadata": {}, "output_type": "execute_result" } @@ -270,7 +270,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 75, "id": "2ff8b7c4", "metadata": {}, "outputs": [], @@ -290,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 76, "id": "1e12a399", "metadata": {}, "outputs": [ @@ -300,7 +300,7 @@ "3" ] }, - "execution_count": 15, + "execution_count": 76, "metadata": {}, "output_type": "execute_result" } @@ -311,7 +311,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 77, "id": "32e5193a", "metadata": {}, "outputs": [ @@ -321,7 +321,7 @@ "torch.Size([3, 3, 3])" ] }, - "execution_count": 16, + "execution_count": 77, "metadata": {}, "output_type": "execute_result" } @@ -332,7 +332,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 78, "id": "3a25ca11", "metadata": {}, "outputs": [ @@ -344,7 +344,7 @@ " [7, 8, 9]])" ] }, - "execution_count": 17, + "execution_count": 78, "metadata": {}, "output_type": "execute_result" } @@ -363,19 +363,19 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 79, "id": "7526c1ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "tensor([[0.8956, 0.3751, 0.1271, 0.0786],\n", - " [0.6605, 0.9228, 0.6594, 0.6637],\n", - " [0.8143, 0.3942, 0.9663, 0.9637]])" + "tensor([[0.1109, 0.4425, 0.1133, 0.9341],\n", + " [0.7576, 0.9063, 0.7243, 0.5338],\n", + " [0.3031, 0.8889, 0.0658, 0.3763]])" ] }, - "execution_count": 18, + "execution_count": 79, "metadata": {}, "output_type": "execute_result" } @@ -388,7 +388,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 80, "id": "26cc4026", "metadata": {}, "outputs": [ @@ -398,7 +398,7 @@ "2" ] }, - "execution_count": 19, + "execution_count": 80, "metadata": {}, "output_type": "execute_result" } @@ -409,7 +409,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 81, "id": "bda29746", "metadata": {}, "outputs": [ @@ -419,7 +419,7 @@ "(torch.Size([3, 224, 224]), 3)" ] }, - "execution_count": 20, + "execution_count": 81, "metadata": {}, "output_type": "execute_result" } @@ -440,7 +440,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 82, "id": "9583ab83", "metadata": {}, "outputs": [ @@ -452,7 +452,7 @@ " [0., 0., 0., 0.]])" ] }, - "execution_count": 21, + "execution_count": 82, "metadata": {}, "output_type": "execute_result" } @@ -464,7 +464,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 83, "id": "18cc934f", "metadata": {}, "outputs": [ @@ -476,7 +476,7 @@ " [1., 1., 1., 1.]])" ] }, - "execution_count": 22, + "execution_count": 83, "metadata": {}, "output_type": "execute_result" } @@ -488,7 +488,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 84, "id": "fc494597", "metadata": {}, "outputs": [ @@ -498,7 +498,7 @@ "torch.float32" ] }, - "execution_count": 23, + "execution_count": 84, "metadata": {}, "output_type": "execute_result" } @@ -517,7 +517,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 85, "id": "01ff10a6", "metadata": {}, "outputs": [ @@ -529,7 +529,7 @@ " 9.0000, 9.5000, 10.0000, 10.5000])" ] }, - "execution_count": 24, + "execution_count": 85, "metadata": {}, "output_type": "execute_result" } @@ -542,7 +542,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 86, "id": "00fcd1b0", "metadata": {}, "outputs": [ @@ -552,7 +552,7 @@ "tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" ] }, - "execution_count": 25, + "execution_count": 86, "metadata": {}, "output_type": "execute_result" } @@ -573,7 +573,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 87, "id": "f9fbb1f3", "metadata": {}, "outputs": [ @@ -583,7 +583,7 @@ "tensor([3., 6., 9.])" ] }, - "execution_count": 26, + "execution_count": 87, "metadata": {}, "output_type": "execute_result" } @@ -599,7 +599,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 88, "id": "661203f0", "metadata": {}, "outputs": [ @@ -609,7 +609,7 @@ "torch.float32" ] }, - "execution_count": 27, + "execution_count": 88, "metadata": {}, "output_type": "execute_result" } @@ -620,7 +620,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 89, "id": "931e8a6b", "metadata": {}, "outputs": [ @@ -630,7 +630,7 @@ "tensor([3., 6., 9.], dtype=torch.float16)" ] }, - "execution_count": 28, + "execution_count": 89, "metadata": {}, "output_type": "execute_result" } @@ -642,7 +642,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 90, "id": "79935a92", "metadata": {}, "outputs": [ @@ -652,7 +652,7 @@ "torch.float32" ] }, - "execution_count": 29, + "execution_count": 90, "metadata": {}, "output_type": "execute_result" } @@ -663,19 +663,19 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 91, "id": "b80e8014", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "tensor([[0.0459, 0.5510, 0.7350, 0.0445],\n", - " [0.9295, 0.0692, 0.2381, 0.5553],\n", - " [0.2460, 0.5533, 0.6115, 0.1076]])" + "tensor([[0.6552, 0.8326, 0.2693, 0.4535],\n", + " [0.4237, 0.0072, 0.9796, 0.4627],\n", + " [0.7492, 0.4209, 0.5884, 0.7448]])" ] }, - "execution_count": 30, + "execution_count": 91, "metadata": {}, "output_type": "execute_result" } @@ -689,7 +689,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 92, "id": "81a5af8e", "metadata": {}, "outputs": [ @@ -697,9 +697,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "tensor([[0.0459, 0.5510, 0.7350, 0.0445],\n", - " [0.9295, 0.0692, 0.2381, 0.5553],\n", - " [0.2460, 0.5533, 0.6115, 0.1076]])\n", + "tensor([[0.6552, 0.8326, 0.2693, 0.4535],\n", + " [0.4237, 0.0072, 0.9796, 0.4627],\n", + " [0.7492, 0.4209, 0.5884, 0.7448]])\n", "Datatype of tensor: torch.float32\n", "Shape of tensor: torch.Size([3, 4])\n", "Device of tensor: cpu\n" @@ -725,7 +725,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 93, "id": "05fadd99", "metadata": {}, "outputs": [ @@ -735,7 +735,7 @@ "tensor([101, 102, 103])" ] }, - "execution_count": 32, + "execution_count": 93, "metadata": {}, "output_type": "execute_result" } @@ -748,7 +748,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 94, "id": "cced037e", "metadata": {}, "outputs": [ @@ -758,7 +758,7 @@ "tensor([202, 204, 206])" ] }, - "execution_count": 33, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } @@ -778,7 +778,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 95, "id": "dae43f89", "metadata": {}, "outputs": [ @@ -795,7 +795,7 @@ "tensor([1, 4, 9])" ] }, - "execution_count": 34, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } @@ -809,7 +809,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 96, "id": "5335eb6a", "metadata": {}, "outputs": [ @@ -819,7 +819,7 @@ "tensor(14)" ] }, - "execution_count": 35, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } @@ -831,7 +831,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 97, "id": "96214e1c", "metadata": {}, "outputs": [ @@ -841,7 +841,7 @@ "14" ] }, - "execution_count": 36, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } @@ -853,7 +853,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 98, "id": "839afbd2", "metadata": {}, "outputs": [ @@ -861,8 +861,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 0 ns, sys: 2.13 ms, total: 2.13 ms\n", - "Wall time: 1.14 ms\n" + "CPU times: user 1.87 ms, sys: 993 μs, total: 2.86 ms\n", + "Wall time: 1.7 ms\n" ] }, { @@ -871,7 +871,7 @@ "tensor(14)" ] }, - "execution_count": 37, + "execution_count": 98, "metadata": {}, "output_type": "execute_result" } @@ -886,7 +886,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 99, "id": "1c1c93ce", "metadata": {}, "outputs": [ @@ -894,8 +894,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 501 μs, sys: 0 ns, total: 501 μs\n", - "Wall time: 616 μs\n" + "CPU times: user 718 μs, sys: 0 ns, total: 718 μs\n", + "Wall time: 541 μs\n" ] }, { @@ -904,7 +904,7 @@ "tensor(14)" ] }, - "execution_count": 38, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } @@ -924,7 +924,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 100, "id": "837697b0", "metadata": {}, "outputs": [ @@ -932,8 +932,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 355 μs, sys: 0 ns, total: 355 μs\n", - "Wall time: 323 μs\n" + "CPU times: user 733 μs, sys: 0 ns, total: 733 μs\n", + "Wall time: 608 μs\n" ] }, { @@ -942,7 +942,7 @@ "tensor(14)" ] }, - "execution_count": 39, + "execution_count": 100, "metadata": {}, "output_type": "execute_result" } @@ -955,19 +955,19 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 101, "id": "12f61493", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "(tensor([[0.4556, 0.5742],\n", - " [1.0881, 0.6826]]),\n", + "(tensor([[0.9213, 0.9373],\n", + " [0.9613, 1.0427]]),\n", " torch.Size([2, 2]))" ] }, - "execution_count": 40, + "execution_count": 101, "metadata": {}, "output_type": "execute_result" } @@ -979,7 +979,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 102, "id": "bef98c6b", "metadata": {}, "outputs": [ @@ -1024,7 +1024,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 103, "id": "4b0c4fe3", "metadata": {}, "outputs": [ @@ -1034,7 +1034,7 @@ "tensor([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90.])" ] }, - "execution_count": 42, + "execution_count": 103, "metadata": {}, "output_type": "execute_result" } @@ -1046,7 +1046,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 104, "id": "013597a9", "metadata": {}, "outputs": [ @@ -1056,7 +1056,7 @@ "(tensor(0.), tensor(0.))" ] }, - "execution_count": 43, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } @@ -1067,7 +1067,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 105, "id": "c071479e", "metadata": {}, "outputs": [ @@ -1077,7 +1077,7 @@ "(tensor(90.), tensor(90.))" ] }, - "execution_count": 44, + "execution_count": 105, "metadata": {}, "output_type": "execute_result" } @@ -1088,7 +1088,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 106, "id": "9bb59339", "metadata": {}, "outputs": [ @@ -1098,7 +1098,7 @@ "(tensor(45.), tensor(45.))" ] }, - "execution_count": 45, + "execution_count": 106, "metadata": {}, "output_type": "execute_result" } @@ -1109,7 +1109,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 107, "id": "a4896145", "metadata": {}, "outputs": [ @@ -1119,7 +1119,7 @@ "(tensor(450.), tensor(450.))" ] }, - "execution_count": 46, + "execution_count": 107, "metadata": {}, "output_type": "execute_result" } @@ -1138,7 +1138,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 108, "id": "abe4c14d", "metadata": {}, "outputs": [ @@ -1148,40 +1148,40 @@ "tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])" ] }, - "execution_count": 49, + "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "x = torch.tensor([ 122, 10, 20, 30, 40, 50, 60, 70, 80, 90])\n", + "x = torch.tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])\n", "x" ] }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 109, "id": "7ef986e5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "tensor(0)" + "(tensor(0), tensor(9))" ] }, - "execution_count": 53, + "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "x.argmin()" + "x.argmin(), x.argmax()" ] }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 110, "id": "7ad63bda", "metadata": {}, "outputs": [ @@ -1191,7 +1191,7 @@ "tensor(0)" ] }, - "execution_count": 54, + "execution_count": 110, "metadata": {}, "output_type": "execute_result" } @@ -1201,17 +1201,378 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "id": "6b49deec", "metadata": {}, + "source": [ + "## Reshaping, stacking, sqeezing and unsqeezing tensors" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "id": "f0a1909b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([1., 2., 3., 4., 5., 6., 7., 8., 9.]), torch.Size([9]))" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import torch\n", + "x = torch.arange(1., 10.)\n", + "x, x.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "id": "ba064450", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([[1.],\n", + " [2.],\n", + " [3.],\n", + " [4.],\n", + " [5.],\n", + " [6.],\n", + " [7.],\n", + " [8.],\n", + " [9.]]),\n", + " torch.Size([9, 1]))" + ] + }, + "execution_count": 112, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Add an extra dimension\n", + "x_reshaped = x.reshape(9, 1)\n", + "x_reshaped, x_reshaped.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "id": "ed78b429", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([[1., 2., 3., 4., 5., 6., 7., 8., 9.]]), torch.Size([1, 9]))" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Change the view\n", + "z = x.view(1, 9)\n", + "z, z.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "id": "cf403167", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([[5., 2., 3., 4., 5., 6., 7., 8., 9.]]),\n", + " tensor([5., 2., 3., 4., 5., 6., 7., 8., 9.]))" + ] + }, + "execution_count": 114, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Changing z chages x (they share memory)\n", + "z[:, 0] = 5\n", + "z, x" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "id": "ca8cb352", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[5., 5., 5., 5.],\n", + " [2., 2., 2., 2.],\n", + " [3., 3., 3., 3.],\n", + " [4., 4., 4., 4.],\n", + " [5., 5., 5., 5.],\n", + " [6., 6., 6., 6.],\n", + " [7., 7., 7., 7.],\n", + " [8., 8., 8., 8.],\n", + " [9., 9., 9., 9.]])" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Stack tensors on top of each other\n", + "x_stack = torch.stack([x,x,x,x],dim=1)\n", + "x_stack" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "cdb0b28c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch.Size([9, 1])\n", + "tensor([5., 2., 3., 4., 5., 6., 7., 8., 9.])\n", + "torch.Size([9])\n" + ] + } + ], + "source": [ + "# Sqeeze\n", + "print(x_reshaped.shape)\n", + "print(x_reshaped.squeeze())\n", + "print(x_reshaped.squeeze().shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "id": "5f036e71", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch.Size([9, 1])\n", + "tensor([[[5.]],\n", + "\n", + " [[2.]],\n", + "\n", + " [[3.]],\n", + "\n", + " [[4.]],\n", + "\n", + " [[5.]],\n", + "\n", + " [[6.]],\n", + "\n", + " [[7.]],\n", + "\n", + " [[8.]],\n", + "\n", + " [[9.]]])\n", + "torch.Size([9, 1, 1])\n" + ] + } + ], + "source": [ + "# Sqeeze\n", + "print(x_reshaped.shape)\n", + "print(x_reshaped.unsqueeze(dim=2))\n", + "print(x_reshaped.unsqueeze(dim=2).shape)" + ] + }, + { + "cell_type": "markdown", + "id": "57a63474", + "metadata": {}, + "source": [ + "## Inxdexing" + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "id": "bb3e71e7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([[[1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]]]),\n", + " torch.Size([1, 3, 3]))" + ] + }, + "execution_count": 121, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = torch.arange(1, 10).reshape(1, 3, 3)\n", + "x, x.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "id": "8c42f28e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([[[1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]]])\n", + "tensor([[1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]])\n", + "tensor([1, 2, 3])\n", + "tensor(1)\n" + ] + } + ], + "source": [ + "print(x)\n", + "print(x[0])\n", + "print(x[0][0])\n", + "print(x[0][0][0])" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "id": "ab0376d9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[1, 2, 3]])" + ] + }, + "execution_count": 126, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x[:, 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "id": "c7ecdc4d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[2, 5, 8]])" + ] + }, + "execution_count": 127, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x[:, :, 1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cc2c0da2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([5])" + ] + }, + "execution_count": 133, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x[:, 1, 1]" + ] + }, + { + "cell_type": "code", + "execution_count": 134, + "id": "20b11409", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([1, 2, 3])" + ] + }, + "execution_count": 134, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x[0, 0, :]" + ] + }, + { + "cell_type": "markdown", + "id": "9f3ba24e", + "metadata": {}, + "source": [ + "## PyTorch tensors & NumPy" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e7bb6813", + "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import torch\n", + "import numpy as np\n", + "array = np.arange(1.0, 8.0)\n", + "tensor = torch.tensor" + ] } ], "metadata": { "kernelspec": { - "display_name": ".venv (3.11.15.final.0)", + "display_name": ".venv (3.13.7)", "language": "python", "name": "python3" }, @@ -1225,7 +1586,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.15" + "version": "3.13.7" } }, "nbformat": 4,