diff --git a/My_Experiments/PyTorch_Course/00_pytorch_fundamentals.ipynb b/My_Experiments/PyTorch_Course/00_pytorch_fundamentals.ipynb new file mode 100644 index 0000000..3268457 --- /dev/null +++ b/My_Experiments/PyTorch_Course/00_pytorch_fundamentals.ipynb @@ -0,0 +1,1233 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "4138ee2e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.12.1+cu130\n" + ] + } + ], + "source": [ + "import torch\n", + "import pandas as pd\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "print(torch.__version__)" + ] + }, + { + "cell_type": "markdown", + "id": "b5b731d2", + "metadata": {}, + "source": [ + "## Inrtoduction to tensors\n", + "### Creating tensors" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "fa46b5f3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor(7)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# scalar\n", + "scalar = torch.tensor(7)\n", + "scalar" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "80146c19", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scalar.ndim" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "8ebd6387", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scalar.item()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "975ba0ba", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([7, 7])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vector = torch.tensor([7, 7])\n", + "vector" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "adc95f24", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vector.ndim" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "1364d5be", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "torch.Size([2])" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vector.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "3d811521", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[ 7, 8],\n", + " [ 9, 10]])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "MATRIX = torch.tensor([[7, 8],\n", + " [9, 10]])\n", + "MATRIX" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "e9acf917", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "MATRIX.ndim" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "40bd74ac", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([7, 8])" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "MATRIX[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "8093f799", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([ 9, 10])" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "MATRIX[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "85b1d000", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "torch.Size([2, 2])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "MATRIX.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "2ff8b7c4", + "metadata": {}, + "outputs": [], + "source": [ + "TENSOR = torch.tensor([[[1,2,3],\n", + " [4,5,6],\n", + " [7,8,9]],\n", + " \n", + " [[1,2,3],\n", + " [4,5,6],\n", + " [7,8,9]],\n", + "\n", + " [[1,2,3],\n", + " [4,5,6],\n", + " [7,8,9]]])" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "1e12a399", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "TENSOR.ndim" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "32e5193a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "torch.Size([3, 3, 3])" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "TENSOR.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "3a25ca11", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]])" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "TENSOR[0]" + ] + }, + { + "cell_type": "markdown", + "id": "21b5f275", + "metadata": {}, + "source": [ + "### Random tensors" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "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]])" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a random tensor of size (3, 4)\n", + "random_tensor = torch.rand(3, 4)\n", + "random_tensor" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "26cc4026", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random_tensor.ndim" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "bda29746", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(torch.Size([3, 224, 224]), 3)" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a random tensor with similar shape to an image tensor\n", + "random_image_size_tensor = torch.rand(size=(3, 224, 224))\n", + "random_image_size_tensor.shape, random_image_size_tensor.ndim" + ] + }, + { + "cell_type": "markdown", + "id": "23e76ee6", + "metadata": {}, + "source": [ + "## Zeros and ones" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "9583ab83", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[0., 0., 0., 0.],\n", + " [0., 0., 0., 0.],\n", + " [0., 0., 0., 0.]])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "zeros = torch.zeros((3, 4))\n", + "zeros*random_tensor" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "18cc934f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[1., 1., 1., 1.],\n", + " [1., 1., 1., 1.],\n", + " [1., 1., 1., 1.]])" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ones = torch.ones(3, 4)\n", + "ones" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "fc494597", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "torch.float32" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ones.dtype\n" + ] + }, + { + "cell_type": "markdown", + "id": "0e3f3b39", + "metadata": {}, + "source": [ + "## Creating a range of tensors and tensors-like" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "01ff10a6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([ 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000,\n", + " 5.0000, 5.5000, 6.0000, 6.5000, 7.0000, 7.5000, 8.0000, 8.5000,\n", + " 9.0000, 9.5000, 10.0000, 10.5000])" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Use torch.arange()\n", + "one_to_ten = torch.arange(1, 11, 0.5)\n", + "one_to_ten" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "00fcd1b0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Creating tensors like\n", + "ten_zeros = torch.zeros_like(input=one_to_ten)\n", + "ten_zeros" + ] + }, + { + "cell_type": "markdown", + "id": "4235cba4", + "metadata": {}, + "source": [ + "## Tensor datatypes" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "f9fbb1f3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([3., 6., 9.])" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Float 32 tensor\n", + "float_32_tensor = torch.tensor([3.0, 6.0, 9.0], \n", + " dtype=None,\n", + " device=None,\n", + " requires_grad=False)\n", + "float_32_tensor" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "661203f0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "torch.float32" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "float_32_tensor.dtype" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "931e8a6b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([3., 6., 9.], dtype=torch.float16)" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "float_16_tensor = float_32_tensor.type(torch.float16)\n", + "float_16_tensor" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "79935a92", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "torch.float32" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(float_16_tensor * float_32_tensor).dtype" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "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]])" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "### Getting information from tensors\n", + "\n", + "some_tensor = torch.rand(3,4)\n", + "some_tensor" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "81a5af8e", + "metadata": {}, + "outputs": [ + { + "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", + "Datatype of tensor: torch.float32\n", + "Shape of tensor: torch.Size([3, 4])\n", + "Device of tensor: cpu\n" + ] + } + ], + "source": [ + "\n", + "print(some_tensor)\n", + "print(f\"Datatype of tensor: {some_tensor.dtype}\")\n", + "print(f\"Shape of tensor: {some_tensor.shape}\")\n", + "print(f\"Device of tensor: {some_tensor.device}\")" + ] + }, + { + "cell_type": "markdown", + "id": "e8901479", + "metadata": {}, + "source": [ + "### Manipulating Tensors (tensor operations)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "05fadd99", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([101, 102, 103])" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tensor = torch.tensor([1, 2, 3])\n", + "tensor += 100\n", + "tensor" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "cced037e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([202, 204, 206])" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tensor = torch.mul(tensor, 2)\n", + "tensor\n" + ] + }, + { + "cell_type": "markdown", + "id": "1b6b226b", + "metadata": {}, + "source": [ + "### Matrix multiplication" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "dae43f89", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([1, 2, 3]) * tensor([1, 2, 3])\n" + ] + }, + { + "data": { + "text/plain": [ + "tensor([1, 4, 9])" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Element wise multiplication\n", + "tensor = torch.tensor([1, 2, 3])\n", + "print(f\"{tensor} * {tensor}\")\n", + "tensor * tensor" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "5335eb6a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor(14)" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Matrix multiplication\n", + "torch.matmul(tensor, tensor)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "96214e1c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Matrix multiplication by hand\n", + "1*1 + 2*2 + 3*3" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "839afbd2", + "metadata": {}, + "outputs": [ + { + "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" + ] + }, + { + "data": { + "text/plain": [ + "tensor(14)" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%%time\n", + "value = 0\n", + "for i in range(len(tensor)):\n", + " value += tensor[i] * tensor[i]\n", + "value" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "1c1c93ce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 501 μs, sys: 0 ns, total: 501 μs\n", + "Wall time: 616 μs\n" + ] + }, + { + "data": { + "text/plain": [ + "tensor(14)" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%%time\n", + "torch.matmul(tensor, tensor)" + ] + }, + { + "cell_type": "markdown", + "id": "b0e54302", + "metadata": {}, + "source": [ + "### One of the most common errors in deep learning:shape errors" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "837697b0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 355 μs, sys: 0 ns, total: 355 μs\n", + "Wall time: 323 μs\n" + ] + }, + { + "data": { + "text/plain": [ + "tensor(14)" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%%time\n", + "tensor = torch.tensor([1, 2, 3])\n", + "tensor @ tensor" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "12f61493", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([[0.4556, 0.5742],\n", + " [1.0881, 0.6826]]),\n", + " torch.Size([2, 2]))" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch_matmul = torch.matmul(torch.rand(2, 3), torch.rand(3, 2))\n", + "torch_matmul, torch_matmul.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "bef98c6b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor_A:\n", + "tensor([[1, 2],\n", + " [3, 4],\n", + " [5, 6]])\n", + " * \n", + "tensor_B.T:\n", + "tensor([[ 7, 9, 11],\n", + " [ 8, 10, 12]])\n", + "\n", + "tensor([[ 23, 29, 35],\n", + " [ 53, 67, 81],\n", + " [ 83, 105, 127]])\n" + ] + } + ], + "source": [ + "# Shapes for matrix multiplication\n", + "tensor_A = torch.tensor([[1, 2],\n", + " [3, 4],\n", + " [5, 6]])\n", + "tensor_B = torch.tensor([[7, 8],\n", + " [9, 10],\n", + " [11, 12]])\n", + "print(f\"tensor_A:\\n{tensor_A}\\n * \\ntensor_B.T:\\n{tensor_B.T}\\n\")\n", + "print(torch.mm(tensor_A, tensor_B.T)) ## torch.mm() is an alias torch.matmul()" + ] + }, + { + "cell_type": "markdown", + "id": "000567a4", + "metadata": {}, + "source": [ + "## Finding the min, max, mean, sum, etc (tensor aggregation)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "4b0c4fe3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90.])" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = torch.arange(0., 100., 10.)\n", + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "013597a9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor(0.), tensor(0.))" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.min(x), x.min()" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "c071479e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor(90.), tensor(90.))" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.max(x), x.max()" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "9bb59339", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor(45.), tensor(45.))" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.mean(x), x.mean()" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "a4896145", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor(450.), tensor(450.))" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "torch.sum(x), x.sum()" + ] + }, + { + "cell_type": "markdown", + "id": "fc5e3a0b", + "metadata": {}, + "source": [ + "# Finding the positional min and max" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "abe4c14d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = torch.tensor([ 122, 10, 20, 30, 40, 50, 60, 70, 80, 90])\n", + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "7ef986e5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor(0)" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x.argmin()" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "7ad63bda", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor(0)" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6b49deec", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv (3.11.15.final.0)", + "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.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/My_Experiments/data/FashionMNIST/raw/t10k-images-idx3-ubyte b/My_Experiments/data/FashionMNIST/raw/t10k-images-idx3-ubyte new file mode 100644 index 0000000..37bac79 Binary files /dev/null and b/My_Experiments/data/FashionMNIST/raw/t10k-images-idx3-ubyte differ diff --git a/My_Experiments/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz b/My_Experiments/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz new file mode 100644 index 0000000..667844f Binary files /dev/null and b/My_Experiments/data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz differ diff --git a/My_Experiments/data/FashionMNIST/raw/t10k-labels-idx1-ubyte b/My_Experiments/data/FashionMNIST/raw/t10k-labels-idx1-ubyte new file mode 100644 index 0000000..2195a4d Binary files /dev/null and b/My_Experiments/data/FashionMNIST/raw/t10k-labels-idx1-ubyte differ diff --git a/My_Experiments/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz b/My_Experiments/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz new file mode 100644 index 0000000..abdddb8 Binary files /dev/null and b/My_Experiments/data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz differ diff --git a/My_Experiments/data/FashionMNIST/raw/train-images-idx3-ubyte b/My_Experiments/data/FashionMNIST/raw/train-images-idx3-ubyte new file mode 100644 index 0000000..ff2f5a9 Binary files /dev/null and b/My_Experiments/data/FashionMNIST/raw/train-images-idx3-ubyte differ diff --git a/My_Experiments/data/FashionMNIST/raw/train-images-idx3-ubyte.gz b/My_Experiments/data/FashionMNIST/raw/train-images-idx3-ubyte.gz new file mode 100644 index 0000000..e6ee0e3 Binary files /dev/null and b/My_Experiments/data/FashionMNIST/raw/train-images-idx3-ubyte.gz differ diff --git a/My_Experiments/data/FashionMNIST/raw/train-labels-idx1-ubyte b/My_Experiments/data/FashionMNIST/raw/train-labels-idx1-ubyte new file mode 100644 index 0000000..30424ca Binary files /dev/null and b/My_Experiments/data/FashionMNIST/raw/train-labels-idx1-ubyte differ diff --git a/My_Experiments/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz b/My_Experiments/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz new file mode 100644 index 0000000..9c4aae2 Binary files /dev/null and b/My_Experiments/data/FashionMNIST/raw/train-labels-idx1-ubyte.gz differ diff --git a/My_Experiments/pytorch.ipynb b/My_Experiments/pytorch.ipynb new file mode 100644 index 0000000..dba0e77 --- /dev/null +++ b/My_Experiments/pytorch.ipynb @@ -0,0 +1,155 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "ed9e0a13", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "from torch import nn\n", + "from torch.utils.data import DataLoader\n", + "from torchvision import datasets\n", + "from torchvision.transforms import v2" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "c838fd53", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "100.0%\n", + "100.0%\n", + "100.0%\n", + "100.0%\n" + ] + } + ], + "source": [ + "training_data = datasets.FashionMNIST(\n", + " root=\"data\",\n", + " train=True,\n", + " download=True,\n", + " transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]),\n", + ")\n", + "\n", + "test_data = datasets.FashionMNIST(\n", + " root=\"data\",\n", + " train=False,\n", + " download=True,\n", + " transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]),\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "744a8ecb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Shape of X [N, C, H, W]: torch.Size([64, 1, 28, 28])\n", + "Shape of y: torch.Size([64]) torch.int64\n" + ] + } + ], + "source": [ + "batch_size = 64\n", + "\n", + "train_dataloader = DataLoader(training_data, batch_size=batch_size)\n", + "test_dataloader = DataLoader(test_data, batch_size=batch_size)\n", + "\n", + "for X, y in test_dataloader:\n", + " print(f\"Shape of X [N, C, H, W]: {X.shape}\")\n", + " print(f\"Shape of y: {y.shape} {y.dtype}\")\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "25639067", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Using cuda device\n", + "NeuralNetwork(\n", + " (flatten): Flatten(start_dim=1, end_dim=-1)\n", + " (linear_relu_stack): Sequential(\n", + " (0): Linear(in_features=784, out_features=512, bias=True)\n", + " (1): ReLU()\n", + " (2): Linear(in_features=512, out_features=512, bias=True)\n", + " (3): ReLU()\n", + " (4): Linear(in_features=512, out_features=10, bias=True)\n", + " )\n", + ")\n" + ] + } + ], + "source": [ + "device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else \"cpu\"\n", + "print(f\"Using {device} device\")\n", + "\n", + "class NeuralNetwork(nn.Module):\n", + " def __init__(self):\n", + " super().__init__()\n", + " self.flatten = nn.Flatten()\n", + " self.linear_relu_stack = nn.Sequential(\n", + " nn.Linear(28*28, 512),\n", + " nn.ReLU(),\n", + " nn.Linear(512, 512),\n", + " nn.ReLU(),\n", + " nn.Linear(512, 10)\n", + " )\n", + " def forward(self, x):\n", + " x = self.flatten(x)\n", + " logits = self.linear_relu_stack(x)\n", + " return logits\n", + " \n", + "model = NeuralNetwork().to(device)\n", + "print(model)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4d995f06", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv (3.11.15)", + "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.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/My_Experiments/pytorch2.ipynb b/My_Experiments/pytorch2.ipynb new file mode 100644 index 0000000..6f90ea4 --- /dev/null +++ b/My_Experiments/pytorch2.ipynb @@ -0,0 +1,101 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "7af2ca72", + "metadata": {}, + "outputs": [], + "source": [ + "import torch" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "0684c0ff", + "metadata": {}, + "outputs": [], + "source": [ + "X = torch.tensor([[1.0],[2.0],[3.0]])\n", + "y = torch.tensor([[4.0],[5.0],[6.0]])" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "1078a0d7", + "metadata": {}, + "outputs": [], + "source": [ + "w = torch.randn(1, requires_grad=True)\n", + "b = torch.randn(1, requires_grad=True)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "6d7c95c5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.4485014081001282 3.9534966945648193\n" + ] + } + ], + "source": [ + "for _ in range(100):\n", + " y_pred = X @ w + b\n", + " loss = ((y_pred - y) ** 2).mean()\n", + " loss.backward()\n", + " with torch.no_grad():\n", + " w -= 0.05 * w.grad\n", + " b -= 0.05 * w.grad\n", + " w.grad.zero_()\n", + " b.grad.zero_()\n", + "\n", + "print(w.item(),b.item())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b23bda3c", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1b6fcc44", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv (3.11.15)", + "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.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/My_Experiments/pytorch3.ipynb b/My_Experiments/pytorch3.ipynb new file mode 100644 index 0000000..8e953e0 --- /dev/null +++ b/My_Experiments/pytorch3.ipynb @@ -0,0 +1,284 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "a11ebdff", + "metadata": {}, + "source": [ + "## Manual approach" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "3163fb75", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "X = torch.tensor([[0.0],[10.0], [20.0], [30.0], [40.0]])\n", + "Y = torch.tensor([[32.0], [50.0], [68.0], [86.0], [104.0]])" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "64a02514", + "metadata": {}, + "outputs": [], + "source": [ + "W = torch.randn((1, 1), requires_grad=True)\n", + "B = torch.randn((1, 1), requires_grad=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "35200e59", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 0: Loss = 42.0600\n", + "Epoch 400: Loss = 24.6847\n", + "Epoch 800: Loss = 14.4873\n", + "Epoch 1200: Loss = 8.5024\n", + "Epoch 1600: Loss = 4.9900\n", + "Epoch 2000: Loss = 2.9286\n", + "Epoch 2400: Loss = 1.7188\n", + "Epoch 2800: Loss = 1.0087\n", + "Epoch 3200: Loss = 0.5920\n", + "Epoch 3600: Loss = 0.3474\n", + "Epoch 4000: Loss = 0.2039\n", + "Epoch 4400: Loss = 0.1197\n", + "Epoch 4800: Loss = 0.0702\n", + "Epoch 5200: Loss = 0.0412\n", + "Epoch 5600: Loss = 0.0242\n", + "Epoch 6000: Loss = 0.0142\n", + "Epoch 6400: Loss = 0.0083\n", + "Epoch 6800: Loss = 0.0049\n", + "Epoch 7200: Loss = 0.0029\n", + "Epoch 7600: Loss = 0.0017\n", + "Epoch 8000: Loss = 0.0010\n", + "Epoch 8400: Loss = 0.0006\n", + "Epoch 8800: Loss = 0.0003\n", + "Epoch 9200: Loss = 0.0002\n", + "Epoch 9600: Loss = 0.0001\n", + "Epoch 10000: Loss = 0.0001\n", + "Epoch 10400: Loss = 0.0000\n", + "Epoch 10800: Loss = 0.0000\n", + "Epoch 11200: Loss = 0.0000\n", + "Epoch 11600: Loss = 0.0000\n", + "Epoch 12000: Loss = 0.0000\n", + "Epoch 12400: Loss = 0.0000\n", + "Epoch 12800: Loss = 0.0000\n", + "Epoch 13200: Loss = 0.0000\n", + "Epoch 13600: Loss = 0.0000\n", + "Epoch 14000: Loss = 0.0000\n", + "Epoch 14400: Loss = 0.0000\n", + "Epoch 14800: Loss = 0.0000\n", + "Epoch 15200: Loss = 0.0000\n", + "Epoch 15600: Loss = 0.0000\n", + "Epoch 16000: Loss = 0.0000\n", + "Epoch 16400: Loss = 0.0000\n", + "Epoch 16800: Loss = 0.0000\n", + "Epoch 17200: Loss = 0.0000\n", + "Epoch 17600: Loss = 0.0000\n", + "Epoch 18000: Loss = 0.0000\n", + "Epoch 18400: Loss = 0.0000\n", + "Epoch 18800: Loss = 0.0000\n", + "Epoch 19200: Loss = 0.0000\n", + "Epoch 19600: Loss = 0.0000\n" + ] + } + ], + "source": [ + "learning_rate = 0.001\n", + "\n", + "for epoch in range(20000):\n", + " Y_pred = X.matmul(W) + B\n", + "\n", + " loss = ((Y_pred - Y)**2).mean()\n", + "\n", + " loss.backward()\n", + " with torch.no_grad():\n", + " W -= learning_rate * W.grad\n", + " B -= learning_rate * B.grad\n", + "\n", + " W.grad.zero_()\n", + " B.grad.zero_()\n", + "\n", + " if epoch % 400 == 0:\n", + " print(f\"Epoch {epoch}: Loss = {loss.item():.4f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "be1eb1bd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Результаты обучения ---\n", + "Предсказание для 100°C: 211.99°F (Ожидалось: 212.00)\n", + "Итоговый вес W: 1.7999 (Ожидалось: 1.8)\n", + "Итоговое смещение B: 32.0029 (Ожидалось: 32.0)\n" + ] + } + ], + "source": [ + "with torch.no_grad():\n", + " X_test = torch.tensor([[100.0]])\n", + " Y_test_pred = X_test.matmul(W) + B\n", + " print(\"\\n--- Результаты обучения ---\")\n", + " print(f\"Предсказание для 100°C: {Y_test_pred.item():.2f}°F (Ожидалось: 212.00)\")\n", + " print(f\"Итоговый вес W: {W.item():.4f} (Ожидалось: 1.8)\")\n", + " print(f\"Итоговое смещение B: {B.item():.4f} (Ожидалось: 32.0)\")" + ] + }, + { + "cell_type": "markdown", + "id": "1a066894", + "metadata": {}, + "source": [ + "## Professional approach" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "fa3c1e81", + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "import torch.nn as nn\n", + "import torch.optim as optim\n", + "\n", + "X = torch.tensor([[0.0],[10.0], [20.0], [30.0], [40.0]])\n", + "Y = torch.tensor([[32.0], [50.0], [68.0], [86.0], [104.0]])" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "8860a027", + "metadata": {}, + "outputs": [], + "source": [ + "model = nn.Linear(in_features=1, out_features=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "b1213a8a", + "metadata": {}, + "outputs": [], + "source": [ + "criterion = nn.MSELoss()\n", + "optimizer = optim.SGD(model.parameters(), lr=0.001)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "27d203c4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 0: loss = 6039.51318359375\n", + "Epoch 400: loss = 199.59164428710938\n", + "Epoch 800: loss = 117.13835144042969\n", + "Epoch 1200: loss = 68.74734497070312\n", + "Epoch 1600: loss = 40.34719467163086\n" + ] + } + ], + "source": [ + "for epoch in range(2000):\n", + " Y_pred = model(X)\n", + "\n", + " loss = criterion(Y_pred, Y)\n", + "\n", + " optimizer.zero_grad()\n", + "\n", + " loss.backward()\n", + "\n", + " optimizer.step()\n", + "\n", + " if epoch % 400 == 0:\n", + " print(f\"Epoch {epoch}: loss = {loss.item()}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "024b4276", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Результаты обучения ---\n", + "Предсказание для 100°C: 231.68°F\n", + "Итоговый вес W: 2.0811\n", + "Итоговое смещение B: 23.5716\n" + ] + } + ], + "source": [ + "with torch.no_grad():\n", + " X_test = torch.tensor([[100.0]])\n", + " Y_test_pred = model(X_test)\n", + " print(\"\\n--- Результаты обучения ---\")\n", + " print(f\"Предсказание для 100°C: {Y_test_pred.item():.2f}°F\")\n", + " \n", + " # Доступ к обученным весам внутри слоя\n", + " print(f\"Итоговый вес W: {model.weight.item():.4f}\")\n", + " print(f\"Итоговое смещение B: {model.bias.item():.4f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "df8942cd", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv (3.11.15)", + "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.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/micrograd_from_scratch.ipynb b/micrograd_from_scratch.ipynb index db143f4..d44fda1 100644 --- a/micrograd_from_scratch.ipynb +++ b/micrograd_from_scratch.ipynb @@ -776,7 +776,7 @@ ], "metadata": { "kernelspec": { - "display_name": ".venv (3.11.15.final.0)", + "display_name": ".venv (3.11.15)", "language": "python", "name": "python3" },