JimmyChin1998/Pytorch-Learning-File
0
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": 10,6 "id": "3de75f13-7140-4572-a254-dcd42018420c",7 "metadata": {},8 "outputs": [9 {10 "name": "stdout",11 "output_type": "stream",12 "text": [13 "PyTorch 版本: 2.5.0\n",14 "CUDA 是否可用: True\n",15 "NumPy 版本: 1.26.4\n",16 "pandas 版本: 2.2.2\n",17 "Matplotlib 版本: 3.9.2\n",18 "python版本: 3.12.7\n"19 ]20 }21 ],22 "source": [23 "# 測試 PyTorch\n",24 "import torch\n",25 "print(\"PyTorch 版本:\", torch.__version__)\n",26 "print(\"CUDA 是否可用:\", torch.cuda.is_available())\n",27 "\n",28 "# 測試 NumPy、pandas Matplotlib和 python\n",29 "import platform\n",30 "import numpy as np\n",31 "import pandas as pd\n",32 "import matplotlib.pyplot as plt\n",33 "print(\"NumPy 版本:\", np.__version__)\n",34 "print(\"pandas 版本:\", pd.__version__)\n",35 "print(\"Matplotlib 版本:\", plt.matplotlib.__version__)\n",36 "print(\"python版本:\", platform.python_version())"37 ]38 },39 {40 "cell_type": "code",41 "execution_count": 2,42 "id": "eebe468a-a38c-4ae2-a1f5-43aa57e8d47f",43 "metadata": {},44 "outputs": [45 {46 "name": "stdout",47 "output_type": "stream",48 "text": [49 "當前使用的 GPU: NVIDIA GeForce RTX 3080\n"50 ]51 }52 ],53 "source": [54 "if torch.cuda.is_available():\n",55 " device = torch.device(\"cuda\")\n",56 " print(\"當前使用的 GPU:\", torch.cuda.get_device_name(0))\n",57 "else:\n",58 " print(\"CUDA 不可用,只能使用 CPU\")"59 ]60 },61 {62 "cell_type": "code",63 "execution_count": 4,64 "id": "fcedf100-cfed-418b-ba50-7f4bebac5081",65 "metadata": {},66 "outputs": [67 {68 "name": "stdout",69 "output_type": "stream",70 "text": [71 "Sun Oct 27 19:54:58 2024 \n",72 "+-----------------------------------------------------------------------------------------+\n",73 "| NVIDIA-SMI 566.03 Driver Version: 566.03 CUDA Version: 12.7 |\n",74 "|-----------------------------------------+------------------------+----------------------+\n",75 "| GPU Name Driver-Model | Bus-Id Disp.A | Volatile Uncorr. ECC |\n",76 "| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |\n",77 "| | | MIG M. |\n",78 "|=========================================+========================+======================|\n",79 "| 0 NVIDIA GeForce RTX 3080 WDDM | 00000000:2D:00.0 On | N/A |\n",80 "| 0% 40C P8 33W / 350W | 1489MiB / 12288MiB | 18% Default |\n",81 "| | | N/A |\n",82 "+-----------------------------------------+------------------------+----------------------+\n",83 " \n",84 "+-----------------------------------------------------------------------------------------+\n",85 "| Processes: |\n",86 "| GPU GI CI PID Type Process name GPU Memory |\n",87 "| ID ID Usage |\n",88 "|=========================================================================================|\n",89 "| 0 N/A N/A 660 C+G ...1.0_x64__8wekyb3d8bbwe\\Video.UI.exe N/A |\n",90 "| 0 N/A N/A 5808 C+G ...les\\Microsoft OneDrive\\OneDrive.exe N/A |\n",91 "| 0 N/A N/A 6268 C+G ...__8wekyb3d8bbwe\\Notepad\\Notepad.exe N/A |\n",92 "| 0 N/A N/A 7864 C+G ...ata\\Local\\LINE\\bin\\current\\LINE.exe N/A |\n",93 "| 0 N/A N/A 8360 C+G C:\\Windows\\explorer.exe N/A |\n",94 "| 0 N/A N/A 10268 C+G ...nt.CBS_cw5n1h2txyewy\\SearchHost.exe N/A |\n",95 "| 0 N/A N/A 10292 C+G ...2txyewy\\StartMenuExperienceHost.exe N/A |\n",96 "| 0 N/A N/A 11612 C+G ...siveControlPanel\\SystemSettings.exe N/A |\n",97 "| 0 N/A N/A 12780 C+G ...CBS_cw5n1h2txyewy\\TextInputHost.exe N/A |\n",98 "| 0 N/A N/A 13556 C+G ...Brave-Browser\\Application\\brave.exe N/A |\n",99 "| 0 N/A N/A 14224 C+G ...5n1h2txyewy\\ShellExperienceHost.exe N/A |\n",100 "| 0 N/A N/A 15860 C+G ...nt.CBS_cw5n1h2txyewy\\SearchHost.exe N/A |\n",101 "| 0 N/A N/A 17692 C+G ...ekyb3d8bbwe\\PhoneExperienceHost.exe N/A |\n",102 "| 0 N/A N/A 18484 C+G ...crosoft\\Edge\\Application\\msedge.exe N/A |\n",103 "| 0 N/A N/A 19380 C+G ...Brave-Browser\\Application\\brave.exe N/A |\n",104 "+-----------------------------------------------------------------------------------------+\n"105 ]106 }107 ],108 "source": [109 "!nvidia-smi"110 ]111 },112 {113 "cell_type": "code",114 "execution_count": 1,115 "id": "48811db2-a0ff-463e-9a04-2a257b81626d",116 "metadata": {},117 "outputs": [118 {119 "name": "stdout",120 "output_type": "stream",121 "text": [122 "執行時間: 157.84243774414062 毫秒\n"123 ]124 }125 ],126 "source": [127 "import torch\n",128 "import time\n",129 "\n",130 "# 確認是否有 GPU 設備\n",131 "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",132 "\n",133 "# 設定開始和結束的事件\n",134 "start = torch.cuda.Event(enable_timing=True)\n",135 "end = torch.cuda.Event(enable_timing=True)\n",136 "\n",137 "# 開始計時\n",138 "start.record()\n",139 "\n",140 "# 執行矩陣相乘\n",141 "x = torch.rand(10000, 10000, device=device)\n",142 "y = torch.rand(10000, 10000, device=device)\n",143 "z = torch.matmul(x, y)\n",144 "\n",145 "# 結束計時\n",146 "end.record()\n",147 "\n",148 "# 等待所有 CUDA 操作完成\n",149 "torch.cuda.synchronize()\n",150 "\n",151 "# 計算經過時間(以毫秒為單位)\n",152 "elapsed_time = start.elapsed_time(end)\n",153 "print(f\"執行時間: {elapsed_time} 毫秒\")\n"154 ]155 },156 {157 "cell_type": "code",158 "execution_count": 3,159 "id": "fff14081-0aa0-4577-8dd1-70b0b40f57ed",160 "metadata": {},161 "outputs": [162 {163 "name": "stdout",164 "output_type": "stream",165 "text": [166 "執行時間: 98.69107055664062 毫秒\n"167 ]168 }169 ],170 "source": [171 "# 指定使用 GPU\n",172 "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",173 "\n",174 "# 設定 CUDA 的計時事件\n",175 "start = torch.cuda.Event(enable_timing=True)\n",176 "end = torch.cuda.Event(enable_timing=True)\n",177 "\n",178 "# 開始計時\n",179 "start.record()\n",180 "\n",181 "# 執行矩陣相乘\n",182 "x = torch.rand(10000, 10000, device=device)\n",183 "y = torch.rand(10000, 10000, device=device)\n",184 "z = torch.matmul(x, y)\n",185 "\n",186 "# 結束計時\n",187 "end.record()\n",188 "\n",189 "# 等待所有 CUDA 操作完成\n",190 "torch.cuda.synchronize()\n",191 "\n",192 "# 計算經過時間(以毫秒為單位)\n",193 "elapsed_time = start.elapsed_time(end)\n",194 "print(f\"執行時間: {elapsed_time} 毫秒\")"195 ]196 },197 {198 "cell_type": "code",199 "execution_count": 4,200 "id": "61846cfc-dcdf-4e93-8698-b361540ec84f",201 "metadata": {},202 "outputs": [203 {204 "data": {205 "text/plain": [206 "1"207 ]208 },209 "execution_count": 4,210 "metadata": {},211 "output_type": "execute_result"212 }213 ],214 "source": [215 "# Set device type\n",216 "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",217 "# Count number of devices\n",218 "torch.cuda.device_count()"219 ]220 },221 {222 "cell_type": "code",223 "execution_count": 5,224 "id": "f0ac6491-24fd-45d3-aa0f-9f40cef256ad",225 "metadata": {},226 "outputs": [227 {228 "name": "stdout",229 "output_type": "stream",230 "text": [231 "tensor([1, 2, 3]) cpu\n"232 ]233 },234 {235 "data": {236 "text/plain": [237 "tensor([1, 2, 3], device='cuda:0')"238 ]239 },240 "execution_count": 5,241 "metadata": {},242 "output_type": "execute_result"243 }244 ],245 "source": [246 "# Create tensor (default on CPU)\n",247 "tensor = torch.tensor([1, 2, 3])\n",248 "\n",249 "# Tensor not on GPU\n",250 "print(tensor, tensor.device)\n",251 "\n",252 "# Move tensor to GPU (if available)\n",253 "tensor_on_gpu = tensor.to(device)\n",254 "tensor_on_gpu"255 ]256 },257 {258 "cell_type": "code",259 "execution_count": 6,260 "id": "0df45458-e9e2-4213-a692-64a11166c6ce",261 "metadata": {},262 "outputs": [263 {264 "ename": "TypeError",265 "evalue": "can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.",266 "output_type": "error",267 "traceback": [268 "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",269 "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",270 "Cell \u001b[1;32mIn[6], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# If tensor is on GPU, can't transform it to NumPy (this will error)\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m tensor_on_gpu\u001b[38;5;241m.\u001b[39mnumpy()\n",271 "\u001b[1;31mTypeError\u001b[0m: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first."272 ]273 }274 ],275 "source": [276 "# If tensor is on GPU, can't transform it to NumPy (this will error)\n",277 "tensor_on_gpu.numpy()\n",278 "# NumPy does not leverage the GPU"279 ]280 },281 {282 "cell_type": "code",283 "execution_count": 7,284 "id": "cce66a6e-5bf9-45d6-bf82-32e42e1e689c",285 "metadata": {},286 "outputs": [287 {288 "data": {289 "text/plain": [290 "array([1, 2, 3], dtype=int64)"291 ]292 },293 "execution_count": 7,294 "metadata": {},295 "output_type": "execute_result"296 }297 ],298 "source": [299 "# Instead, copy the tensor back to cpu\n",300 "tensor_back_on_cpu = tensor_on_gpu.cpu().numpy()\n",301 "tensor_back_on_cpu"302 ]303 },304 {305 "cell_type": "code",306 "execution_count": 8,307 "id": "8833452a-9db2-4c7b-a739-9c3632d5fe69",308 "metadata": {},309 "outputs": [310 {311 "data": {312 "text/plain": [313 "tensor(7)"314 ]315 },316 "execution_count": 8,317 "metadata": {},318 "output_type": "execute_result"319 }320 ],321 "source": [322 "# Scalar\n",323 "scalar = torch.tensor(7)\n",324 "scalar"325 ]326 },327 {328 "cell_type": "code",329 "execution_count": 9,330 "id": "835a965b-6990-4dfb-b9a9-ea8012031ddd",331 "metadata": {},332 "outputs": [333 {334 "data": {335 "text/plain": [336 "0"337 ]338 },339 "execution_count": 9,340 "metadata": {},341 "output_type": "execute_result"342 }343 ],344 "source": [345 "scalar.ndim"346 ]347 },348 {349 "cell_type": "code",350 "execution_count": 10,351 "id": "6eba4e4e-d399-4b4f-b021-78897eb7656e",352 "metadata": {},353 "outputs": [354 {355 "data": {356 "text/plain": [357 "tensor([7, 7])"358 ]359 },360 "execution_count": 10,361 "metadata": {},362 "output_type": "execute_result"363 }364 ],365 "source": [366 "# Vector\n",367 "vector = torch.tensor([7, 7])\n",368 "vector"369 ]370 },371 {372 "cell_type": "code",373 "execution_count": 11,374 "id": "6d2cfdfe-150c-44c1-ad56-22d51f34ca4b",375 "metadata": {},376 "outputs": [377 {378 "data": {379 "text/plain": [380 "1"381 ]382 },383 "execution_count": 11,384 "metadata": {},385 "output_type": "execute_result"386 }387 ],388 "source": [389 "# Check the number of dimensions of vector\n",390 "vector.ndim"391 ]392 },393 {394 "cell_type": "code",395 "execution_count": 12,396 "id": "4234de6e-0dff-4ad6-9c51-f4808cd8b376",397 "metadata": {},398 "outputs": [399 {400 "data": {401 "text/plain": [402 "(torch.Size([2]), torch.Size([]))"403 ]404 },405 "execution_count": 12,406 "metadata": {},407 "output_type": "execute_result"408 }409 ],410 "source": [411 "# Check shape of vector\n",412 "vector.shape, scalar.shape"413 ]414 },415 {416 "cell_type": "code",417 "execution_count": 13,418 "id": "60f0cc6a-d04f-41af-ad19-763e9214485d",419 "metadata": {},420 "outputs": [421 {422 "data": {423 "text/plain": [424 "(2, torch.Size([2, 2]))"425 ]426 },427 "execution_count": 13,428 "metadata": {},429 "output_type": "execute_result"430 }431 ],432 "source": [433 "# Matrix\n",434 "MATRIX = torch.tensor([[7, 8], \n",435 " [9, 10]])\n",436 "MATRIX.ndim, MATRIX.shape"437 ]438 },439 {440 "cell_type": "code",441 "execution_count": 14,442 "id": "2b308352-f9d5-4d92-8f0f-5ac8d6fb84ee",443 "metadata": {},444 "outputs": [445 {446 "data": {447 "text/plain": [448 "(3, torch.Size([1, 3, 3]))"449 ]450 },451 "execution_count": 14,452 "metadata": {},453 "output_type": "execute_result"454 }455 ],456 "source": [457 "# Tensor\n",458 "TENSOR = torch.tensor([[[1, 2, 3],\n",459 " [3, 6, 9],\n",460 " [2, 4, 5]]])\n",461 "TENSOR.ndim, TENSOR.shape"462 ]463 },464 {465 "cell_type": "code",466 "execution_count": 15,467 "id": "393fdaea-fe4a-4e91-a76d-dde715fb0a04",468 "metadata": {},469 "outputs": [470 {471 "data": {472 "text/plain": [473 "(torch.Size([224, 224, 3]), 3)"474 ]475 },476 "execution_count": 15,477 "metadata": {},478 "output_type": "execute_result"479 }480 ],481 "source": [482 "# Create a random tensor of size (224, 224, 3)\n",483 "random_image_size_tensor = torch.rand(size=(224, 224, 3))\n",484 "random_image_size_tensor.shape, random_image_size_tensor.ndim"485 ]486 },487 {488 "cell_type": "code",489 "execution_count": 16,490 "id": "cbda99ba-03cc-4196-a9ca-113af3284809",491 "metadata": {},492 "outputs": [493 {494 "data": {495 "text/plain": [496 "(torch.Size([3]), torch.float32, device(type='cpu'))"497 ]498 },499 "execution_count": 16,500 "metadata": {},501 "output_type": "execute_result"502 }503 ],504 "source": [505 "# Default datatype for tensors is float32\n",506 "float_32_tensor = torch.tensor([3.0, 6.0, 9.0],\n",507 " dtype=None, # defaults to None, which is torch.float32 or whatever datatype is passed\n",508 " device=None, # defaults to None, which uses the default tensor type\n",509 " requires_grad=False) # if True, operations performed on the tensor are recorded \n",510 "\n",511 "float_32_tensor.shape, float_32_tensor.dtype, float_32_tensor.device"512 ]513 },514 {515 "cell_type": "code",516 "execution_count": 17,517 "id": "eddf8eee-e6e7-4b97-a71d-6c3afbb50328",518 "metadata": {},519 "outputs": [520 {521 "name": "stdout",522 "output_type": "stream",523 "text": [524 "tensor([[0.7984, 0.3269, 0.3039, 0.2807],\n",525 " [0.0025, 0.2863, 0.5769, 0.3109],\n",526 " [0.1705, 0.5987, 0.8661, 0.6635]])\n",527 "Shape of tensor: torch.Size([3, 4])\n",528 "Datatype of tensor: torch.float32\n",529 "Device tensor is stored on: cpu\n"530 ]531 }532 ],533 "source": [534 "# Create a tensor\n",535 "some_tensor = torch.rand(3, 4)\n",536 "\n",537 "# Find out details about it\n",538 "print(some_tensor)\n",539 "print(f\"Shape of tensor: {some_tensor.shape}\")\n",540 "print(f\"Datatype of tensor: {some_tensor.dtype}\")\n",541 "print(f\"Device tensor is stored on: {some_tensor.device}\") # will default to CPU"542 ]543 },544 {545 "cell_type": "code",546 "execution_count": 19,547 "id": "1294e9fd-e6f7-46f5-bd8f-091569c11f92",548 "metadata": {},549 "outputs": [],550 "source": [551 "# Shapes need to be in the right way \n",552 "tensor_A = torch.tensor([[1, 2],\n",553 " [3, 4],\n",554 " [5, 6]], dtype=torch.float32)\n",555 "\n",556 "tensor_B = torch.tensor([[7, 10],\n",557 " [8, 11], \n",558 " [9, 12]], dtype=torch.float32)"559 ]560 },561 {562 "cell_type": "code",563 "execution_count": 20,564 "id": "ca005cae-2ad5-4a99-9391-8c5a0c8d7714",565 "metadata": {},566 "outputs": [567 {568 "name": "stdout",569 "output_type": "stream",570 "text": [571 "tensor([[1., 2.],\n",572 " [3., 4.],\n",573 " [5., 6.]])\n",574 "tensor([[ 7., 10.],\n",575 " [ 8., 11.],\n",576 " [ 9., 12.]])\n"577 ]578 }579 ],580 "source": [581 "# View tensor_A and tensor_B\n",582 "print(tensor_A)\n",583 "print(tensor_B)"584 ]585 },586 {587 "cell_type": "code",588 "execution_count": 21,589 "id": "e43b936a-4baa-40c7-ad02-f2cb6158ea8c",590 "metadata": {},591 "outputs": [592 {593 "name": "stdout",594 "output_type": "stream",595 "text": [596 "tensor([[1., 2.],\n",597 " [3., 4.],\n",598 " [5., 6.]])\n",599 "tensor([[ 7., 8., 9.],\n",600 " [10., 11., 12.]])\n"601 ]602 }603 ],604 "source": [605 "# View tensor_A and tensor_B.T\n",606 "print(tensor_A)\n",607 "print(tensor_B.T)"608 ]609 },610 {611 "cell_type": "code",612 "execution_count": 22,613 "id": "d3a9dae7-c0a9-4967-9af4-ee77688810a9",614 "metadata": {},615 "outputs": [616 {617 "data": {618 "text/plain": [619 "tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])"620 ]621 },622 "execution_count": 22,623 "metadata": {},624 "output_type": "execute_result"625 }626 ],627 "source": [628 "# Create a tensor\n",629 "x = torch.arange(0, 100, 10)\n",630 "x"631 ]632 },633 {634 "cell_type": "code",635 "execution_count": 23,636 "id": "3e8a01e5-afa2-4307-99ff-a18f040b920d",637 "metadata": {},638 "outputs": [639 {640 "name": "stdout",641 "output_type": "stream",642 "text": [643 "Minimum: 0\n",644 "Maximum: 90\n",645 "Mean: 45.0\n",646 "Sum: 450\n"647 ]648 }649 ],650 "source": [651 "print(f\"Minimum: {x.min()}\")\n",652 "print(f\"Maximum: {x.max()}\")\n",653 "# print(f\"Mean: {x.mean()}\") # this will error\n",654 "print(f\"Mean: {x.type(torch.float32).mean()}\") # won't work without float datatype\n",655 "print(f\"Sum: {x.sum()}\")"656 ]657 },658 {659 "cell_type": "code",660 "execution_count": 24,661 "id": "5d079a9a-3f01-4299-ade6-8f21e9cb0ff5",662 "metadata": {},663 "outputs": [664 {665 "name": "stdout",666 "output_type": "stream",667 "text": [668 "Tensor: tensor([10, 20, 30, 40, 50, 60, 70, 80, 90])\n",669 "Index where max value occurs: 8\n",670 "Index where min value occurs: 0\n"671 ]672 }673 ],674 "source": [675 "# Create a tensor\n",676 "tensor = torch.arange(10, 100, 10)\n",677 "print(f\"Tensor: {tensor}\")\n",678 "\n",679 "# Returns index of max and min values\n",680 "print(f\"Index where max value occurs: {tensor.argmax()}\")\n",681 "print(f\"Index where min value occurs: {tensor.argmin()}\")"682 ]683 },684 {685 "cell_type": "code",686 "execution_count": 28,687 "id": "e4c4e47f-460f-4d84-a73f-3fc88915d7b3",688 "metadata": {},689 "outputs": [690 {691 "data": {692 "text/plain": [693 "(tensor([1., 2., 3., 4., 5., 6., 7.]), torch.Size([7]))"694 ]695 },696 "execution_count": 28,697 "metadata": {},698 "output_type": "execute_result"699 }700 ],701 "source": [702 "x = torch.arange(1., 8.)\n",703 "x, x.shape"704 ]705 },706 {707 "cell_type": "code",708 "execution_count": 29,709 "id": "89b7318b-4aa5-4218-b3ca-cce6eb85c882",710 "metadata": {},711 "outputs": [712 {713 "data": {714 "text/plain": [715 "(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))"716 ]717 },718 "execution_count": 29,719 "metadata": {},720 "output_type": "execute_result"721 }722 ],723 "source": [724 "# Add an extra dimension\n",725 "x_reshaped = x.reshape(1, 7)\n",726 "x_reshaped, x_reshaped.shape"727 ]728 },729 {730 "cell_type": "code",731 "execution_count": 31,732 "id": "0e7d8f0c-d1da-40ef-bee4-21531f1d2389",733 "metadata": {},734 "outputs": [735 {736 "data": {737 "text/plain": [738 "(tensor([[1., 2., 3., 4., 5., 6., 7.]]), torch.Size([1, 7]))"739 ]740 },741 "execution_count": 31,742 "metadata": {},743 "output_type": "execute_result"744 }745 ],746 "source": [747 "z = x.view(1, 7)\n",748 "z, z.shape"749 ]750 },751 {752 "cell_type": "code",753 "execution_count": 35,754 "id": "ae52e298-5c91-4f97-b75c-06d2a9c5d63f",755 "metadata": {},756 "outputs": [757 {758 "data": {759 "text/plain": [760 "(tensor([[5., 2., 3., 4., 5., 6., 7.]]), tensor([5., 2., 3., 4., 5., 6., 7.]))"761 ]762 },763 "execution_count": 35,764 "metadata": {},765 "output_type": "execute_result"766 }767 ],768 "source": [769 "# Changing z changes x\n",770 "z[:, 0] = 5\n",771 "z, x"772 ]773 },774 {775 "cell_type": "code",776 "execution_count": 34,777 "id": "8e3533cc-93b3-4d8d-b686-fe219be03ee0",778 "metadata": {},779 "outputs": [780 {781 "data": {782 "text/plain": [783 "tensor([[5., 2., 3., 4., 5., 6., 7.],\n",784 " [5., 2., 3., 4., 5., 6., 7.],\n",785 " [5., 2., 3., 4., 5., 6., 7.],\n",786 " [5., 2., 3., 4., 5., 6., 7.]])"787 ]788 },789 "execution_count": 34,790 "metadata": {},791 "output_type": "execute_result"792 }793 ],794 "source": [795 "# Stack tensors on top of each other\n",796 "x_stacked = torch.stack([x, x, x, x], dim=0) # try changing dim to dim=1 and see what happens\n",797 "x_stacked"798 ]799 },800 {801 "cell_type": "code",802 "execution_count": null,803 "id": "73b9a862-afaf-4e54-b21d-9a147547d930",804 "metadata": {},805 "outputs": [],806 "source": []807 }808 ],809 "metadata": {810 "kernelspec": {811 "display_name": "Python 3 (ipykernel)",812 "language": "python",813 "name": "python3"814 },815 "language_info": {816 "codemirror_mode": {817 "name": "ipython",818 "version": 3819 },820 "file_extension": ".py",821 "mimetype": "text/x-python",822 "name": "python",823 "nbconvert_exporter": "python",824 "pygments_lexer": "ipython3",825 "version": "3.12.7"826 }827 },828 "nbformat": 4,829 "nbformat_minor": 5830}831 