# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# + [markdown] id="ms_0hSSaqUqd"
# # Pytorch
#
# Core use case -
#
# - replace numpy to use GPU for fast computing
# - automatic differention which can help in calculating gradients => you can implimenting ANNs

# + id="I8tP7SPqle2-"
import torch

import numpy as np

# + [markdown] id="RA8elXZ9ryWG"
# # Tensors
#
# A kind of data structure => multidimensional arrays or matrices
# With tensors you enocode all your parameters.
#

# + colab={"base_uri": "https://localhost:8080/"} id="XlHr-pBXrq7n" outputId="1dd50b59-cadc-4873-9abe-d66e3e3b017c"
data = [[9, 10],
        [4, 5], 
        [12, 13]]


tensor_data = torch.tensor(data)


print(tensor_data)

print(type(tensor_data))

# + colab={"base_uri": "https://localhost:8080/"} id="DSKyjbNUsx3Z" outputId="c79727a7-1f72-43ab-f8f5-b5d53bc275b9"
tensor_data[0, 0]

# + colab={"base_uri": "https://localhost:8080/"} id="m_EmYMI-s5lc" outputId="db1d4996-88ea-4e01-bdb9-f34f77e63381"
np_data = np.array([[9, 10],
        [4, 5], 
        [12, 13]])


tensor_data_np = torch.tensor(np_data)


print(tensor_data_np)

print(type(tensor_data_np))

# + colab={"base_uri": "https://localhost:8080/"} id="jIVDOPtwtLx2" outputId="deabd447-0133-49c3-c5a2-0860d8771855"
ones_like_data = torch.ones_like(tensor_data_np)
ones_like_data

# + colab={"base_uri": "https://localhost:8080/", "height": 187} id="kuF6Q4RatfoV" outputId="2bd00a7d-2753-4ab5-f289-48a8550e27ea"
ones_like_data = torch.ones_like(np_data)
ones_like_data

# + colab={"base_uri": "https://localhost:8080/", "height": 187} id="Kfo9JMXbtkJX" outputId="ea72446c-78b8-4515-93a7-5ac2ae7df1b1"
ones_like_data = torch.ones_like(data)
ones_like_data

# + colab={"base_uri": "https://localhost:8080/", "height": 187} id="rRVMvVybto9m" outputId="39275258-1641-457f-b265-d9cd150c4b14"
ones_like_data_random = torch.rand_like(tensor_data_np)
ones_like_data_random

# + colab={"base_uri": "https://localhost:8080/"} id="ZXjyIlE1t8Us" outputId="ac9b8f2c-f403-4cdf-f82d-c96a76adbaf5"
ones_like_data_random = torch.rand_like(tensor_data_np, dtype=torch.float)
ones_like_data_random

# + colab={"base_uri": "https://localhost:8080/"} id="j8pDzQQMuMFe" outputId="1df851a7-adbb-4213-87e2-d09c99d2c093"
shape = (4,3,2)

rand_tensor = torch.rand(shape)
rand_tensor

# + colab={"base_uri": "https://localhost:8080/"} id="XO7mZ5P6uexR" outputId="0520321b-3e1b-4c43-f7c6-ef62ab8e1aeb"
rand_tensor.shape

# + colab={"base_uri": "https://localhost:8080/"} id="exP8lvqgvknT" outputId="1ea07c2a-6690-4a48-c36c-a4ea7962bd6c"
rand_tensor.dtype

# + colab={"base_uri": "https://localhost:8080/"} id="6qDsgWTXv4hS" outputId="245c0688-0113-4b86-8940-f3bf7a303ac5"
rand_tensor.device

# + colab={"base_uri": "https://localhost:8080/"} id="PT_d0glpu9p0" outputId="6f7a7993-0c30-4aac-90bb-4f5b3405fe95"
ones = torch.ones(shape)
ones

# + colab={"base_uri": "https://localhost:8080/"} id="2Wy-zmSHvIER" outputId="edde748e-f96e-4113-db6f-ddeb980f9a52"
zeros = torch.zeros(shape)
zeros

# + colab={"base_uri": "https://localhost:8080/"} id="C1Mz41u6vY8L" outputId="b0cae908-cd86-4b87-b298-9e60637b16bf"
ones = torch.ones(shape)
ones*3

# + colab={"base_uri": "https://localhost:8080/"} id="9pAafDVyveyL" outputId="b474965f-1c20-4a24-c66d-03e16e501251"
torch.cuda.is_available()

# + id="8_BZ0dt1wIJm"
current_device = torch.cuda.current_device()

# + colab={"base_uri": "https://localhost:8080/", "height": 35} id="0C7zXCPXwd9h" outputId="d9f6e161-7a2a-4eda-db16-be78c085175d"
torch.cuda.get_device_name(current_device)

# + colab={"base_uri": "https://localhost:8080/"} id="kZHKFlM6xCQy" outputId="60d32c74-b777-4320-979b-b24e98c0de18"
# !nvidia-smi

# + colab={"base_uri": "https://localhost:8080/"} id="j9yrkT1FxFZY" outputId="95a47eb2-8ac1-4a10-cbdf-8e785ce6ebbe"
if torch.cuda.is_available():
  rand_tensor = rand_tensor.to("cuda") 
  print(rand_tensor.device)

# + [markdown] id="il2ZLUQyy78Q"
# # Concat operations -

# + colab={"base_uri": "https://localhost:8080/"} id="JDxsy4krxb-Q" outputId="eff1a15a-a2a5-4007-c60e-4d2ec58789e6"
ones = torch.ones(2,2)
ones

# + colab={"base_uri": "https://localhost:8080/"} id="Y8fWH3BOzZkS" outputId="233ac66e-b7eb-4c12-fa25-112c45301eb7"
torch.cat([ones, ones, ones])

# + colab={"base_uri": "https://localhost:8080/"} id="Y8RhAcPizhRE" outputId="092b6c28-2e4d-4735-bae6-c6312e3ffc77"
torch.cat([ones, ones, ones], dim=1)


# + [markdown] id="PzKwralhzwrY"
# # Arithmatic Ops

# + colab={"base_uri": "https://localhost:8080/"} id="5c56Nvzwzrqd" outputId="dba0523f-2dbe-4ecf-dda3-a8cc37fa1054"
y = ones @ ones.T
y

# + colab={"base_uri": "https://localhost:8080/"} id="HlDT28Anz67J" outputId="1375cb2d-76a8-4f02-b12d-8a6285b56ff2"
y = ones.matmul(ones.T)
y

# + colab={"base_uri": "https://localhost:8080/"} id="ZPstYPrW0CiK" outputId="5ec6a9a6-97a5-4069-95af-57fb641edc12"
y = ones * ones
y 

# + colab={"base_uri": "https://localhost:8080/"} id="PIKPz6wH0HyW" outputId="89974d2b-642c-41d8-ff53-0189ecbc5f26"
y = ones.mul(ones)
y 

# + colab={"base_uri": "https://localhost:8080/"} id="dRrDjjUh0NL8" outputId="e5bc1320-bdd4-4c29-9c41-1d0a0d608967"
fives = ones + 5
fives

# + colab={"base_uri": "https://localhost:8080/"} id="uk-ILuGO0XDm" outputId="7c9c8072-df7d-48fd-eaec-7651ee820921"
fives.add_(5)

# + colab={"base_uri": "https://localhost:8080/"} id="KzWjo0Rl0mbJ" outputId="f7e12c0f-962e-49bc-a186-fe00e792d671"
fives.item()

# + colab={"base_uri": "https://localhost:8080/"} id="XWLYlZvt0o_5" outputId="54b0f506-7699-4069-aad4-eb70341ecafb"
type(ones)

# + colab={"base_uri": "https://localhost:8080/"} id="kw4vCaZ_0xvd" outputId="97e3a400-bb83-41cd-d3b1-5933d31192fa"
ones_np = ones.numpy()
type(ones_np)

# + colab={"base_uri": "https://localhost:8080/"} id="TyUStgaF05ZL" outputId="9811d04d-f0b8-460e-cfdd-c468792184df"
ones_tensor = torch.from_numpy(ones_np)
type(ones_tensor)

# + id="Hxk1cyUd1Ft3"
zeros=torch.zeros(shape)
zeros
# -



StarcoderdataJupyterScriptsDedupFiltered