import torch
t1 = torch.tensor(4.)
t1
tensor(4.)
t1.dtype
torch.float32
t2 = torch.tensor([1., 2, 3,4])
t2
tensor([1., 2., 3., 4.])
t2.dtype
torch.float32
t3 = torch.tensor([[5.,6], [7,8], [9,10]])
t3
tensor([[ 5., 6.],
[ 7., 8.],
[ 9., 10.]])
t4 = torch.tensor([
[
[11,12,13],
[13,14,15]
],
[
[15,16,17],
[17,18,19.]
]
])
t4
tensor([[[11., 12., 13.],
[13., 14., 15.]],
[[15., 16., 17.],
[17., 18., 19.]]])
t1.shape
torch.Size([])
t2.shape
torch.Size([4])
t3.shape
torch.Size([3, 2])
t4.shape
torch.Size([2, 2, 3])
x = torch.tensor(3.)
w = torch.tensor(4., requires_grad = True)
b = torch.tensor(5., requires_grad = True)
y = w * x + b
y
tensor(17., grad_fn=<AddBackward0>)
y.backward()
# 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.)
import numpy as np
x = np.array([
[1,2],
[3.,4]
])
x
array([[1., 2.],
[3., 4.]])
type(x)
numpy.ndarray
y = torch.from_numpy(x) # use the same memory, does not create a copy.
y
tensor([[1., 2.],
[3., 4.]], dtype=torch.float64)
type(y)
torch.Tensor
z = torch.tensor(x) # Creates a copy of the oringinal data, not using the same space.
z
tensor([[1., 2.],
[3., 4.]], dtype=torch.float64)
x.dtype, y.dtype
(dtype('float64'), torch.float64)
a = y.numpy()
a
array([[1., 2.],
[3., 4.]])
import jovian
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