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

        [[15., 16., 17.],
         [17., 18., 19.]]])
In [11]:
t4.shape
Out[11]:
torch.Size([2, 2, 3])
In [13]:
t4.dtype
Out[13]:
torch.float32
In [14]:
x = torch.tensor(3.)
w = torch.tensor(4.,requires_grad = True)
b = torch.tensor(5.,requires_grad = True)
In [15]:
y = x * w +b
y
Out[15]:
tensor(17., grad_fn=<AddBackward0>)
In [17]:
y.backward()
In [18]:
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 [19]:
import numpy as np
In [21]:
x = np.array([[1,2],[3,4]])
x
Out[21]:
array([[1, 2],
       [3, 4]])
In [22]:
y = torch.from_numpy(x)
y
Out[22]:
tensor([[1, 2],
        [3, 4]])
In [23]:
x.dtype, y.dtype
Out[23]:
(dtype('int64'), torch.int64)
In [24]:
z = y.numpy()
z
Out[24]:
array([[1, 2],
       [3, 4]])
In [ ]:
import jovian
jovian.commit()
[jovian] Saving notebook..
In [ ]: