Jovian
⭐️
Sign In
In [1]:
import torch
In [2]:
t1 = torch.tensor(4.)
t1
Out[2]:
tensor(4.)
In [3]:
t1.dtype
Out[3]:
torch.float32
In [4]:
t2 = torch.tensor([1., 2, 3,4])
t2
Out[4]:
tensor([1., 2., 3., 4.])
In [5]:
t2.dtype
Out[5]:
torch.float32
In [6]:
t3 = torch.tensor([[5.,6], [7,8], [9,10]])
t3
Out[6]:
tensor([[ 5.,  6.],
        [ 7.,  8.],
        [ 9., 10.]])
In [7]:
t4 = torch.tensor([
    [
        [11,12,13],
        [13,14,15]
    ],
    [
        [15,16,17],
        [17,18,19.]
    ]
])
t4
Out[7]:
tensor([[[11., 12., 13.],
         [13., 14., 15.]],

        [[15., 16., 17.],
         [17., 18., 19.]]])
In [8]:
t1.shape
Out[8]:
torch.Size([])
In [9]:
t2.shape
Out[9]:
torch.Size([4])
In [10]:
t3.shape
Out[10]:
torch.Size([3, 2])
In [11]:
t4.shape
Out[11]:
torch.Size([2, 2, 3])
In [13]:
x = torch.tensor(3.)
w = torch.tensor(4., requires_grad = True)
b = torch.tensor(5., requires_grad = True)
In [14]:
y = w * x + b
y
Out[14]:
tensor(17., grad_fn=<AddBackward0>)
In [15]:
y.backward()
In [16]:
# Display gradiesnts.
print('dy/dx: ', x.grad)
print('dy/dw: ', w.grad)
print('dy/db: ', b.grad)
dy/dx: None dy/dw: tensor(3.) dy/db: tensor(1.)
In [17]:
import numpy as np
x = np.array([
    [1,2],
    [3.,4]
])
x
Out[17]:
array([[1., 2.],
       [3., 4.]])
In [18]:
type(x)
Out[18]:
numpy.ndarray
In [23]:
y = torch.from_numpy(x) # use the same memory, does not create a copy.
y
Out[23]:
tensor([[1., 2.],
        [3., 4.]], dtype=torch.float64)
In [20]:
type(y)
Out[20]:
torch.Tensor
In [22]:
z = torch.tensor(x) # Creates a copy of the oringinal data, not using the same space.
z
Out[22]:
tensor([[1., 2.],
        [3., 4.]], dtype=torch.float64)
In [24]:
x.dtype, y.dtype
Out[24]:
(dtype('float64'), torch.float64)
In [25]:
a = y.numpy()
a
Out[25]:
array([[1., 2.],
       [3., 4.]])
In [26]:
import jovian
In [27]:
jovian.commit()
[jovian] Saving notebook..
[jovian] Creating a new notebook on https://jovian.ml/ [jovian] Please enter your API key ( from https://jovian.ml/ ):
API Key: ···························································································································································································································································································································
[jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/walid-gomaa/my-pytorch-basics
In [ ]: