import torch
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
a = torch.tensor([1,2,3,4,5,6])
a.dtype
torch.int64
b = torch.FloatTensor([1,2,3,4,5,6])
b.dtype
torch.float32
b.size()
torch.Size([6])
c = torch.Tensor([1,2,3])
c.dtype
torch.float32
b.view(3,2)
tensor([[1., 2.], [3., 4.], [5., 6.]])
a = torch.arange(9)
a
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])
a.view(3,3)
tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
a = torch.arange(27)
a = a.view(3,3,3)
a
tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]])
a[1,1,1]
tensor(13)
a = torch.tensor([1,2,3])
b = torch.tensor([1,2,3]).view(3,1)
a
tensor([1, 2, 3])
b
tensor([[1], [2], [3]])
a*b
tensor([[1, 2, 3], [2, 4, 6], [3, 6, 9]])
a@b
tensor([14])
a = torch.tensor([1,2,3])
b = torch.tensor([1,2,3])
torch.dot(a,b)
tensor(14)
x = torch.tensor(2.0, requires_grad=True)
x
tensor(2., requires_grad=True)
y = 5*x**2 + 20*x
y.backward()
x.grad
tensor(40.)