Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
351 changes: 351 additions & 0 deletions Part_1_Deep_Learning_with_Pytorch/exercise_submissions/week1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,351 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNmgmQPfwDlOMc4/UDTVN3a",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/maryamalka85/ArewaDS-Deep-Learning/blob/main/Part_1_Deep_Learning_with_Pytorch/exercise_submissions/week1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "V5ABxNIOnyAr"
},
"outputs": [],
"source": [
"import torch"
]
},
{
"cell_type": "code",
"source": [
"# create random tensor\n",
"X = torch.rand(size=(7,7))\n",
"X,X.shape"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4sPUYe-AR22P",
"outputId": "ff2f59aa-db29-467f-a2ab-a196be08ee1a"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(tensor([[0.3142, 0.4865, 0.7705, 0.3263, 0.7264, 0.6868, 0.0522],\n",
" [0.5438, 0.9234, 0.6737, 0.3396, 0.4684, 0.9051, 0.9022],\n",
" [0.5038, 0.2225, 0.9657, 0.4975, 0.0989, 0.6913, 0.5937],\n",
" [0.7634, 0.8749, 0.1176, 0.0565, 0.5362, 0.1045, 0.6571],\n",
" [0.2290, 0.1231, 0.1060, 0.2285, 0.8602, 0.9666, 0.6981],\n",
" [0.5703, 0.9126, 0.0165, 0.9157, 0.9846, 0.1595, 0.7734],\n",
" [0.0339, 0.1776, 0.6518, 0.5989, 0.2043, 0.2849, 0.2325]]),\n",
" torch.Size([7, 7]))"
]
},
"metadata": {},
"execution_count": 3
}
]
},
{
"cell_type": "code",
"source": [
"# Create another random tensor\n",
"Y = torch.rand(size=(1, 7))\n",
"# Perform matrix multiplication\n",
"Z = torch.matmul(X, Y.T)\n",
"Z, Z.shape"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "RduF2fcySFm8",
"outputId": "c85f229b-27d6-4215-8292-adff242d0708"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(tensor([[1.4617],\n",
" [1.7799],\n",
" [1.3014],\n",
" [0.9893],\n",
" [1.7503],\n",
" [1.7336],\n",
" [0.8774]]),\n",
" torch.Size([7, 1]))"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"source": [
"# Set manual seed\n",
"torch.manual_seed(0)\n",
"\n",
"# Create two random tensors\n",
"X = torch.rand(size=(7, 7))\n",
"Y = torch.rand(size=(1, 7))\n",
"\n",
"# Matrix multiply tensors\n",
"Z = torch.matmul(X, Y.T)\n",
"Z, Z.shape"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XJT1P2NBSfMu",
"outputId": "cf85f2aa-6bf2-4cf9-855d-0e451701d738"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(tensor([[1.8542],\n",
" [1.9611],\n",
" [2.2884],\n",
" [3.0481],\n",
" [1.7067],\n",
" [2.5290],\n",
" [1.7989]]),\n",
" torch.Size([7, 1]))"
]
},
"metadata": {},
"execution_count": 5
}
]
},
{
"cell_type": "code",
"source": [
"# Set random seed on the GPU\n",
"torch.cuda.manual_seed(1234)"
],
"metadata": {
"id": "rcgfaPeMWPg2"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Set random seed\n",
"torch.manual_seed(1234)\n",
"\n",
"# Check for access to GPU\n",
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"print(f\"Device: {device}\")\n",
"\n",
"# Create two random tensors on GPU\n",
"tensor_A = torch.rand(size=(2,3)).to(device)\n",
"tensor_B = torch.rand(size=(2,3)).to(device)\n",
"tensor_A, tensor_B"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kXGUX9tLWtQR",
"outputId": "e715efb1-6115-4218-9c82-df5f494945be"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Device: cpu\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(tensor([[0.0290, 0.4019, 0.2598],\n",
" [0.3666, 0.0583, 0.7006]]),\n",
" tensor([[0.0518, 0.4681, 0.6738],\n",
" [0.3315, 0.7837, 0.5631]]))"
]
},
"metadata": {},
"execution_count": 7
}
]
},
{
"cell_type": "code",
"source": [
"# Perform matmul on tensor_A and tensor_B\n",
"tensor_C = torch.matmul(tensor_A, tensor_B.T)\n",
"tensor_C, tensor_C.shape"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "EzvxTswjW2GP",
"outputId": "1cf4adf2-6618-4b23-b23c-f451f482741d"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(tensor([[0.3647, 0.4709],\n",
" [0.5184, 0.5617]]),\n",
" torch.Size([2, 2]))"
]
},
"metadata": {},
"execution_count": 9
}
]
},
{
"cell_type": "code",
"source": [
"# Find max\n",
"max = torch.max(tensor_C)\n",
"\n",
"# Find min\n",
"min = torch.min(tensor_C)\n",
"max, min"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lHPg2D23XDo6",
"outputId": "140287af-d71f-4e8d-b7c1-03ddb285f902"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(tensor(0.5617), tensor(0.3647))"
]
},
"metadata": {},
"execution_count": 10
}
]
},
{
"cell_type": "code",
"source": [
"# Find arg max\n",
"arg_max = torch.argmax(tensor_C)\n",
"\n",
"# Find arg min\n",
"arg_min = torch.argmin(tensor_C)\n",
"arg_max, arg_min"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6Fcr_F6QXK_r",
"outputId": "b6045ba0-92a8-472f-de2f-104187b1b546"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(tensor(3), tensor(0))"
]
},
"metadata": {},
"execution_count": 11
}
]
},
{
"cell_type": "code",
"source": [
"# Set seed\n",
"torch.manual_seed(7)\n",
"\n",
"# Create random tensor\n",
"tensor_D = torch.rand(size=(1, 1, 1, 10))\n",
"\n",
"# Remove single dimensions\n",
"tensor_E = tensor_D.squeeze()\n",
"\n",
"# Print out tensors\n",
"print(tensor_D, tensor_D.shape)\n",
"print(tensor_E, tensor_E.shape)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "f9OBG49yXRVN",
"outputId": "e96e91a7-a407-4660-bba7-9867b3f6bb7f"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"tensor([[[[0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297,\n",
" 0.3653, 0.8513]]]]) torch.Size([1, 1, 1, 10])\n",
"tensor([0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297, 0.3653,\n",
" 0.8513]) torch.Size([10])\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "tz9_OxvrXXyr"
},
"execution_count": null,
"outputs": []
}
]
}
Loading