Jovian
⭐️
Sign In
In [2]:
import torch
t1=torch.tensor(4.)
print(t1)
tensor(4.)
In [3]:
import numpy as np
print(np.zeros((4,4)))
[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]
In [4]:
t1.dtype

Out[4]:
torch.float32
In [6]:
t2=torch.tensor([1,2,3,4])
t2
Out[6]:
tensor([1, 2, 3, 4])
In [7]:
t3=torch.tensor([[5.,6],[7,8],[9,10]])
t3

Out[7]:
tensor([[ 5.,  6.],
        [ 7.,  8.],
        [ 9., 10.]])
In [8]:
t4=torch.tensor([[[11,12,13],[21,22,23]],[[15,16,17],[25,26,27]]])
t4
Out[8]:
tensor([[[11, 12, 13],
         [21, 22, 23]],

        [[15, 16, 17],
         [25, 26, 27]]])
In [10]:
print(t1.shape)
print(t2.shape)
print(t3.shape)
print(t4.shape)
torch.Size([]) torch.Size([4]) torch.Size([3, 2]) torch.Size([2, 2, 3])
In [11]:
import time 
print(time.time)
<built-in function time>
In [16]:
x=torch.tensor(3.)
w= torch.tensor(4.,requires_grad=True)
b=torch.tensor(5.,requires_grad=True)
y=w*x+b
print(y)
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 [22]:
A= torch.zeros([2,4],dtype=torch.int32)
print(A)

tensor([[0, 0, 0, 0], [0, 0, 0, 0]], dtype=torch.int32)

interoperability with numpy numpy. is a popular open source library used for mathematical and scitific computing in Python.

In [26]:
import numpy as np
x = np.array([[1,2],[3,4.]])
y = torch.tensor(x)
print(x,y,x.dtype, y.dtype)
[[1. 2.] [3. 4.]] tensor([[1., 2.], [3., 4.]], dtype=torch.float64) float64 torch.float64
In [25]:
z=y.numpy()
print(z.dtype)
float64
In [ ]:
import jovian
jovian.commit()
[jovian] Saving notebook..
In [ ]: