{ "cells": [ { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:34.735042Z", "start_time": "2024-09-30T06:24:33.640162Z" } }, "cell_type": "code", "source": "import torch", "id": "e385fc3e5349bef2", "outputs": [], "execution_count": 2 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:34.770875Z", "start_time": "2024-09-30T06:24:34.769086Z" } }, "cell_type": "code", "source": "import numpy as np", "id": "5e5756cd9697c301", "outputs": [], "execution_count": 3 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:34.832973Z", "start_time": "2024-09-30T06:24:34.828625Z" } }, "cell_type": "code", "source": [ "data = [[1,2], [3,4]]\n", "x_data = torch.tensor(data)\n", "print(x_data)\n", "np_array = np.array(data)\n", "print(np_array)\n", "x_data = torch.from_numpy(np_array)\n", "print(x_data)" ], "id": "c933f4f596ad8f11", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[1, 2],\n", " [3, 4]])\n", "[[1 2]\n", " [3 4]]\n", "tensor([[1, 2],\n", " [3, 4]])\n" ] } ], "execution_count": 4 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:34.884394Z", "start_time": "2024-09-30T06:24:34.881417Z" } }, "cell_type": "code", "source": [ "x_data = torch.tensor(data, dtype=torch.float)\n", "print(x_data)" ], "id": "f00ded5999db8fd8", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[1., 2.],\n", " [3., 4.]])\n" ] } ], "execution_count": 5 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:34.936237Z", "start_time": "2024-09-30T06:24:34.933906Z" } }, "cell_type": "code", "source": [ "x_data = torch.tensor(data, dtype=torch.bool)\n", "print(x_data)" ], "id": "e95516603c93e1dd", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[True, True],\n", " [True, True]])\n" ] } ], "execution_count": 6 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:34.989723Z", "start_time": "2024-09-30T06:24:34.985714Z" } }, "cell_type": "code", "source": [ "initial_tensor = torch.tensor([[4., 5.], [6., 7.]])\n", "print(initial_tensor)\n", "\n", "# Initialise a new tensor of 1s\n", "new_tensor_ones = torch.ones_like(initial_tensor)\n", "print(new_tensor_ones)\n", "\n", "# Initialise a new tensor of 0s\n", "new_tensor_zeros = torch.zeros_like(initial_tensor)\n", "print(new_tensor_zeros)\n", "\n", "# Initialise a new tensor with all elements sampled from a uniform distribution between 0 and 1\n", "new_tensor_rand = torch.rand_like(initial_tensor)\n", "print(new_tensor_rand)\n", "\n", "# Initialise a new tensor where all elements are sampled from a normal distribution\n", "new_tensor_randn = torch.randn_like(initial_tensor)\n", "print(new_tensor_randn)" ], "id": "772ca3015568d8e0", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[4., 5.],\n", " [6., 7.]])\n", "tensor([[1., 1.],\n", " [1., 1.]])\n", "tensor([[0., 0.],\n", " [0., 0.]])\n", "tensor([[0.0317, 0.8632],\n", " [0.0142, 0.9799]])\n", "tensor([[-0.0114, -1.1777],\n", " [-0.5821, -0.9272]])\n" ] } ], "execution_count": 7 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:35.042240Z", "start_time": "2024-09-30T06:24:35.037846Z" } }, "cell_type": "code", "source": [ "shape = (2, 3, 4)\n", "\n", "x_zeros = torch.zeros(shape)\n", "print(x_zeros)\n", "\n", "x_ones = torch.ones(shape)\n", "print(x_ones)\n", "\n", "torch.manual_seed(142)\n", "random = torch.rand(shape)\n", "print(random)" ], "id": "f863644d805df89d", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.]],\n", "\n", " [[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.]]])\n", "tensor([[[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]],\n", "\n", " [[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]]])\n", "tensor([[[0.9278, 0.3969, 0.7987, 0.3094],\n", " [0.9321, 0.6097, 0.1127, 0.6895],\n", " [0.1704, 0.6049, 0.5771, 0.4412]],\n", "\n", " [[0.4884, 0.0448, 0.6659, 0.1933],\n", " [0.6323, 0.0458, 0.6127, 0.1192],\n", " [0.0961, 0.9311, 0.6790, 0.5379]]])\n" ] } ], "execution_count": 8 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:35.091909Z", "start_time": "2024-09-30T06:24:35.089647Z" } }, "cell_type": "code", "source": [ "x = torch.arange(start=0, end=10, step=2)\n", "print(x)" ], "id": "6e9ca1f3bbfb2718", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([0, 2, 4, 6, 8])\n" ] } ], "execution_count": 9 }, { "metadata": {}, "cell_type": "markdown", "source": "^the above is only 1D, there may be an optional argument though...", "id": "a3141e2557699a4a" }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:35.143042Z", "start_time": "2024-09-30T06:24:35.140899Z" } }, "cell_type": "code", "source": [ "x = torch.rand(3,2)\n", "print(x.shape)\n", "print(x.dtype)\n", "print(x.size(0))" ], "id": "5b3e919a9cbb8f09", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "torch.Size([3, 2])\n", "torch.float32\n", "3\n" ] } ], "execution_count": 10 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:35.195706Z", "start_time": "2024-09-30T06:24:35.192294Z" } }, "cell_type": "code", "source": [ "x = torch.Tensor([[11, 12], [13, 14], [15, 16]])\n", "print(x)\n", "# we can change the shape from (2,3) to (3,2)\n", "x_view = x.view(2,3)\n", "print(x_view)\n", "\n", "# You can also use torch.reshape() for this\n", "x_reshaped = torch.reshape(x, (2,3))\n", "print(x_reshaped)" ], "id": "804762b8904cfed9", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[11., 12.],\n", " [13., 14.],\n", " [15., 16.]])\n", "tensor([[11., 12., 13.],\n", " [14., 15., 16.]])\n", "tensor([[11., 12., 13.],\n", " [14., 15., 16.]])\n" ] } ], "execution_count": 11 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:35.348807Z", "start_time": "2024-09-30T06:24:35.244104Z" } }, "cell_type": "code", "source": [ "print(x.device)\n", "print(torch.cuda.is_available())\n", "\n", "#to leverage the gpu we use\n", "if torch.cuda.is_available():\n", " x.to('cuda')" ], "id": "6135c1b0838167f7", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cpu\n", "True\n" ] } ], "execution_count": 12 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:35.360355Z", "start_time": "2024-09-30T06:24:35.356131Z" } }, "cell_type": "code", "source": [ "#element-wise ops\n", "x = torch.ones(2, 3, 4)\n", "print(x)\n", "\n", "print(x + 3)\n", "print(x - 0.5)\n", "print(x*2)\n", "print(x/2)\n" ], "id": "ff0fbbed6cbe3eba", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]],\n", "\n", " [[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]]])\n", "tensor([[[4., 4., 4., 4.],\n", " [4., 4., 4., 4.],\n", " [4., 4., 4., 4.]],\n", "\n", " [[4., 4., 4., 4.],\n", " [4., 4., 4., 4.],\n", " [4., 4., 4., 4.]]])\n", "tensor([[[0.5000, 0.5000, 0.5000, 0.5000],\n", " [0.5000, 0.5000, 0.5000, 0.5000],\n", " [0.5000, 0.5000, 0.5000, 0.5000]],\n", "\n", " [[0.5000, 0.5000, 0.5000, 0.5000],\n", " [0.5000, 0.5000, 0.5000, 0.5000],\n", " [0.5000, 0.5000, 0.5000, 0.5000]]])\n", "tensor([[[2., 2., 2., 2.],\n", " [2., 2., 2., 2.],\n", " [2., 2., 2., 2.]],\n", "\n", " [[2., 2., 2., 2.],\n", " [2., 2., 2., 2.],\n", " [2., 2., 2., 2.]]])\n", "tensor([[[0.5000, 0.5000, 0.5000, 0.5000],\n", " [0.5000, 0.5000, 0.5000, 0.5000],\n", " [0.5000, 0.5000, 0.5000, 0.5000]],\n", "\n", " [[0.5000, 0.5000, 0.5000, 0.5000],\n", " [0.5000, 0.5000, 0.5000, 0.5000],\n", " [0.5000, 0.5000, 0.5000, 0.5000]]])\n" ] } ], "execution_count": 13 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:35.412117Z", "start_time": "2024-09-30T06:24:35.406579Z" } }, "cell_type": "code", "source": [ "x = torch.ones(2, 2, 4)\n", "print(x)\n", "\n", "y = torch.ones(2, 2, 4)\n", "print(y)\n", "\n", "z_cat = torch.cat([x, y], dim=0)\n", "print(z_cat)\n", "z_cat = torch.cat([x, y], dim=1)\n", "print(z_cat)" ], "id": "223a8b26c3830428", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[[1., 1., 1., 1.],\n", " [1., 1., 1., 1.]],\n", "\n", " [[1., 1., 1., 1.],\n", " [1., 1., 1., 1.]]])\n", "tensor([[[1., 1., 1., 1.],\n", " [1., 1., 1., 1.]],\n", "\n", " [[1., 1., 1., 1.],\n", " [1., 1., 1., 1.]]])\n", "tensor([[[1., 1., 1., 1.],\n", " [1., 1., 1., 1.]],\n", "\n", " [[1., 1., 1., 1.],\n", " [1., 1., 1., 1.]],\n", "\n", " [[1., 1., 1., 1.],\n", " [1., 1., 1., 1.]],\n", "\n", " [[1., 1., 1., 1.],\n", " [1., 1., 1., 1.]]])\n", "tensor([[[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]],\n", "\n", " [[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]]])\n" ] } ], "execution_count": 14 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:25:20.953845Z", "start_time": "2024-09-30T06:25:20.936621Z" } }, "cell_type": "code", "source": [ "x = torch.tensor([1, 2, 3], dtype=torch.float)\n", "y = torch.tensor([4, 5, 6], dtype=torch.float)\n", "print(x+y)\n", "\n", "# you can also use torch.add()\n", "print(torch.add(x,y))\n", "print(torch.add(x,y).sum())\n", "print(x.dot(y))\n", "x = torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.float)\n", "print(x.shape)\n", "y = torch.tensor([[1, 2], [3, 4], [5, 6]], dtype=torch.float)\n", "print(y.shape)\n", "\n", "# Matrix Multiplication\n", "print(torch.matmul(x, y))\n", "\n", "# you can also use torch.mm() \n", "print(torch.mm(x, y))\n", "\n", "# or you can use x@y\n", "print(x@y)" ], "id": "ec744f2ae5a78d3f", "outputs": [ { "ename": "RuntimeError", "evalue": "The size of tensor a (30) must match the size of tensor b (3) at non-singleton dimension 0", "output_type": "error", "traceback": [ "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[0;31mRuntimeError\u001B[0m Traceback (most recent call last)", "Cell \u001B[0;32mIn[19], line 3\u001B[0m\n\u001B[1;32m 1\u001B[0m x \u001B[38;5;241m=\u001B[39m torch\u001B[38;5;241m.\u001B[39mtensor([\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m, \u001B[38;5;241m3\u001B[39m]\u001B[38;5;241m*\u001B[39m\u001B[38;5;241m10\u001B[39m, dtype\u001B[38;5;241m=\u001B[39mtorch\u001B[38;5;241m.\u001B[39mfloat)\n\u001B[1;32m 2\u001B[0m y \u001B[38;5;241m=\u001B[39m torch\u001B[38;5;241m.\u001B[39mtensor([\u001B[38;5;241m4\u001B[39m, \u001B[38;5;241m5\u001B[39m, \u001B[38;5;241m6\u001B[39m], dtype\u001B[38;5;241m=\u001B[39mtorch\u001B[38;5;241m.\u001B[39mfloat)\n\u001B[0;32m----> 3\u001B[0m \u001B[38;5;28mprint\u001B[39m(x\u001B[38;5;241m+\u001B[39my)\n\u001B[1;32m 5\u001B[0m \u001B[38;5;66;03m# you can also use torch.add()\u001B[39;00m\n\u001B[1;32m 6\u001B[0m \u001B[38;5;28mprint\u001B[39m(torch\u001B[38;5;241m.\u001B[39madd(x,y))\n", "\u001B[0;31mRuntimeError\u001B[0m: The size of tensor a (30) must match the size of tensor b (3) at non-singleton dimension 0" ] } ], "execution_count": 19 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:25:04.822706Z", "start_time": "2024-09-30T06:25:04.820059Z" } }, "cell_type": "code", "source": [ "print(x)\n", "z = x*10\n", "print(z)" ], "id": "8d0c58929ce3d86b", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[10., 20., 30.],\n", " [40., 50., 60.]])\n" ] } ], "execution_count": 18 }, { "metadata": { "ExecuteTime": { "end_time": "2024-09-30T06:24:39.089263Z", "start_time": "2024-09-30T06:24:39.086120Z" } }, "cell_type": "code", "source": "", "id": "39a77d2056fa55d", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[10., 20., 30.],\n", " [40., 50., 60.]])\n" ] } ], "execution_count": 17 } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }