import torch
import numpy as np
x = torch.Tensor(2, 3) # Create an un-initialized Tensor of size 2x3
print(x) # Print out the Tensor
tensor([[2.5226e-18, 2.5930e-09, 1.0186e-11], [3.0883e-09, 2.6707e-06, 4.0744e-11]])
type(x)
torch.Tensor
torch.is_tensor(x)
True
# Initialize
x = torch.Tensor(2, 3) # An un-initialized Tensor object. x holds garbage data.
y = torch.rand(2, 3) # Initialize with random values
# Operations
z1 = x + y
z2 = torch.add(x, y) # Same as above
print(z2) # [torch.FloatTensor of size 2x3]
tensor([[0.3785, 0.9980, 0.9008], [0.4766, 0.1663, 0.8045]])
x.add_(y) # Same as x = x + y
tensor([[0.3785, 0.9980, 0.9008], [0.4766, 0.1663, 0.8045]])
x = torch.Tensor([[1,2,3],[4,5,6]])
x
tensor([[1., 2., 3.], [4., 5., 6.]])
y = torch.ones(2,3)
y
tensor([[1., 1., 1.], [1., 1., 1.]])
r1 = torch.add(x, y)
r1
tensor([[2., 3., 4.], [5., 6., 7.]])
x+y
tensor([[2., 3., 4.], [5., 6., 7.]])
x[:, 1] # Can use numpy type indexing
tensor([2., 5.])
x[:, 0] = 0 # For assignment
x
tensor([[0., 2., 3.], [0., 5., 6.]])
# Conversion
a = np.array([1, 2, 3])
v = torch.from_numpy(a) # Convert a numpy array to a Tensor
v
tensor([1, 2, 3])
b = v.numpy() # Tensor to numpy
b
array([1, 2, 3])
b[1] = -1 # Numpy and Tensor share the same memory
assert(a[1] == b[1]) # Change Numpy will also change the Tensor
b
array([ 1, -1, 3])
v
tensor([ 1, -1, 3])
a
array([ 1, -1, 3])
x = torch.Tensor(2,3)
x.size() # torch.Size([2, 3])
torch.Size([2, 3])
x.shape
torch.Size([2, 3])
torch.numel(x) # 6: number of elements in x
6
x = torch.randn(2, 3) # Size 2x3
x
tensor([[-0.1663, 0.0727, 1.3484], [-0.8737, -0.2693, -0.5124]])
y = x.view(6) # Resize x to size 6
y
tensor([-0.1663, 0.0727, 1.3484, -0.8737, -0.2693, -0.5124])
z = x.view(-1, 2) # Size 3x2
z
tensor([[-0.1663, 0.0727], [ 1.3484, -0.8737], [-0.2693, -0.5124]])
v = torch.Tensor(2, 3) # An un-initialized torch.FloatTensor of size 2x3
v = torch.Tensor([[1,2],[4,5]]) # A Tensor initialized with a specific array
v = torch.LongTensor([1,2,3]) # A Tensor of type Long
v = torch.FloatTensor([1,2,3]) # A Tensor of type Float
v
tensor([1., 2., 3.])
torch.manual_seed(1)
<torch._C.Generator at 0x7f1ae00bdc30>
v = torch.randperm(4) # Size 4. Random permutation of integers from 0 to 3
v = torch.rand(2, 3) # Initialize with random number (uniform distribution)
v = torch.randn(2, 3) # With normal distribution (SD=1, mean=0)
v
tensor([[-0.6617, -0.0426, -1.3328], [ 0.5161, 0.7455, -0.0751]])
x = torch.randn(5, 3)
x
tensor([[-0.6919, -0.4043, 0.2222], [ 0.5773, -1.7637, 0.2264], [-0.2355, 0.3019, -0.2770], [ 0.4771, -0.1103, 0.2913], [ 0.5848, 0.2149, -0.4090]])
x.type(torch.LongTensor)
tensor([[ 0, 0, 0], [ 0, -1, 0], [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]])
eye = torch.eye(3) # Create an identity 3x3 tensor
v = torch.ones(10) # A tensor of size 10 containing all ones
v = torch.ones(2, 1, 2, 1) # Size 2x1x2x1
v = torch.ones_like(eye) # A tensor with same shape as eye. Fill it with 1.
v = torch.zeros(10) # A tensor of size 10 containing all zeros
# 1 1 1
# 2 2 2
# 3 3 3
v = torch.ones(3, 3)
v[1].fill_(2)
v[2].fill_(3)
tensor([3., 3., 3.])
v = torch.ones(3, 3)
v
tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]])
v[1].fill_(2)
tensor([2., 2., 2.])
v
tensor([[1., 1., 1.], [2., 2., 2.], [1., 1., 1.]])
v = torch.arange(5) # similar to range(5) but creating a Tensor
v = torch.arange(0, 5, step=1) # Size 5. Similar to range(0, 5, 1)
# 0 1 2
# 3 4 5
# 6 7 8
v = torch.arange(9)
v = v.view(3, 3)
v
tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
v = torch.linspace(1, 10, steps=10) # Create a Tensor with 10 linear points for (1, 10) inclusively
v = torch.logspace(start=-10, end=10, steps=5) # Size 5: 1.0e-10 1.0e-05 1.0e+00, 1.0e+05, 1.0e+10
v
tensor([1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])
# 0 1 2
# 3 4 5
# 6 7 8
x = torch.arange(9)
x = x.view(3, 3)
x
tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
# Concatenation
r = torch.cat((x, x, x), 0) # Concatenate in the 0 dimension
r
tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 1, 2], [3, 4, 5], [6, 7, 8]])
# Concatenation
r = torch.cat((x, x, x), 1) # Concatenate in the 1 dimension
r, r.shape
(tensor([[0, 1, 2, 0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5, 3, 4, 5], [6, 7, 8, 6, 7, 8, 6, 7, 8]]), torch.Size([3, 9]))
# Stack
r = torch.stack((v, v))
r, r.shape
(tensor([[1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10], [1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10]]), torch.Size([2, 5]))
unsqueeze()
"adds" superficial 1 dimension to tensor (at specified dimension), while squeeze()
removes all superficial 1 dimensions from tensor.
t = torch.ones(2,1,2,1) # Size 2x1x2x1
r = torch.squeeze(t) # Size 2x2
r.size()
torch.Size([2, 2])
r = torch.squeeze(t, 1) # Squeeze dimension 1: Size 2x2x1
r.size()
torch.Size([2, 2, 1])
# Un-squeeze a dimension
x = torch.Tensor([1, 2, 3])
r = torch.unsqueeze(x, 0) # Size: 1x3
r = torch.unsqueeze(x, 1) # Size: 3x1
r
tensor([[1.], [2.], [3.]])
v = torch.arange(9)
# Accumulate sum
# 0 1 2
# 3 5 7
# 9 12 15
r = torch.cumsum(v, dim=0)
r
tensor([ 0, 1, 3, 6, 10, 15, 21, 28, 36])
# Mean
# 1 4 7
w = v.type(torch.FloatTensor)
w = w.view(3,3)
r = torch.mean(w, 1) # Size 3: Mean in dim 1
w,r
(tensor([[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]]), tensor([1., 4., 7.]))
# Sum
# 3 12 21
w = v.view(3,3)
r = torch.sum(w, 1) # Sum over dim 1
r
tensor([ 3, 12, 21])
# 36
r = torch.sum(w)
r.item()
36