2. Create Random Tensor
-
create random tensor with shape (7, 7);
import torch
tensor = torch.rand(size=(7, 7))
tensor,tensor.shape
(tensor([[0.8076, 0.0608, 0.7226, 0.3354, 0.5350, 0.7117, 0.7979],
[0.2785, 0.8947, 0.6694, 0.8950, 0.4479, 0.4788, 0.3541],
[0.0467, 0.7471, 0.7821, 0.4296, 0.1128, 0.8413, 0.4978],
[0.3593, 0.7470, 0.0869, 0.4811, 0.4923, 0.7392, 0.3945],
[0.0290, 0.7696, 0.6214, 0.0891, 0.4282, 0.0289, 0.7090],
[0.3982, 0.1633, 0.7222, 0.2019, 0.7982, 0.8446, 0.0180],
[0.1051, 0.7829, 0.1152, 0.6189, 0.7437, 0.5098, 0.2611]]),
torch.Size([7, 7]))
3. Matrix Multiplication
-
perform matrix multiplication from random tensor with
shape (1, 7);hint提示:we need transpose the second tensor;
import torch
ta = torch.rand(size=(7, 7))
tb = torch.rand(size=(1, 7))
# transpose tb to avoid shape issue error
tc = torch.matmul(ta,tb.T) # no error because of transpose
ta,tb,tb.T,tc,tc.shape
(tensor([[0.0505, 0.2425, 0.9124, 0.7002, 0.6695, 0.0461, 0.7386],
[0.7351, 0.6666, 0.4273, 0.8781, 0.3929, 0.7245, 0.2822],
[0.0415, 0.7760, 0.5181, 0.3222, 0.7059, 0.8465, 0.2380],
[0.0596, 0.1614, 0.5427, 0.3571, 0.9552, 0.1087, 0.6462],
[0.0545, 0.0906, 0.1459, 0.4479, 0.8151, 0.0839, 0.2796],
[0.2882, 0.2222, 0.9025, 0.6714, 0.3078, 0.0557, 0.7026],
[0.5068, 0.6830, 0.7884, 0.8747, 0.2235, 0.3706, 0.7427]]),
tensor([[0.8883, 0.6329, 0.6733, 0.8659, 0.8234, 0.1796, 0.8460]]),
tensor([[0.8883],
[0.6329],
[0.6733],
[0.8659],
[0.8234],
[0.1796],
[0.8460]]),
tensor([[2.6033],
[2.8152],
[2.0904],
[2.1823],
[1.5146],
[2.4437],
[3.0496]]),
torch.Size([7, 1]))
4. Random Seed
-
set random seed to 0,do step create random tensor
and Perform Matrix Multiplication over again;
import torch
# set manual seed
torch.manual_seed(0)
ta = torch.rand(size=(7, 7))
tb = torch.rand(size=(1, 7))
# matrix multiply tensor
tc = torch.matmul(ta,tb.T)
tc,tc.shape
(tensor([[1.8542],
[1.9611],
[2.2884],
[3.0481],
[1.7067],
[2.5290],
[1.7989]]),
torch.Size([7, 1]))
6. Send Tensor To GPU
-
创建两个形状为(2,3)的随机张量,并将它们发送给GPU(含随机种子);
import torch
# set random seed
torch.manual_seed(1234)
# check for access to GPU
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Device: {device}")
# create two random tensors on GPU
ta = torch.rand(size=(2,3)).to(device)
tb = torch.rand(size=(2,3)).to(device)
ta,tb
Device: cuda
(tensor([[0.0290, 0.4019, 0.2598],
[0.3666, 0.0583, 0.7006]], device='cuda:0'),
tensor([[0.0518, 0.4681, 0.6738],
[0.3315, 0.7837, 0.5631]], device='cuda:0'))
7. GPU Matrix Multiplication
import torch
# set random seed
torch.manual_seed(1234)
# check for access to GPU
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Device: {device}")
# create two random tensors on GPU
ta = torch.rand(size=(2,3)).to(device)
tb = torch.rand(size=(2,3)).to(device)
# perform matmul on ta and tb
tc = torch.matmul(ta,tb.T)
tc, tc.shape
Device: cuda
(tensor([[0.3647, 0.4709],
[0.5184, 0.5617]], device='cuda:0'),
torch.Size([2, 2]))
8. Maximum and Minimum
import torch
# set random seed
torch.manual_seed(1234)
# check for access to GPU
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Device: {device}")
# create two random tensors on GPU
ta = torch.rand(size=(2,3)).to(device)
tb = torch.rand(size=(2,3)).to(device)
# perform matmul on ta and tb
tc = torch.matmul(ta,tb.T)
max = torch.max(tc)
min = torch.min(tc)
arg_max = torch.argmax(tc)
arg_min = torch.argmin(tc)
print("Tensor:{}\nShape:{}\nMinimum:{}\nMaximum:{}\nArgMin:{}\nArgMax:{}"
.format(tc,tc.shape,min,max,arg_min,arg_max))
Device: cuda
Tensor:tensor([[0.3647, 0.4709],
[0.5184, 0.5617]], device='cuda:0')
Shape:torch.Size([2, 2])
Minimum:0.3647301495075226
Maximum:0.5617256760597229
ArgMin:0
ArgMax:3
9. Tensor Squeeze
-
使用种子为7创建形状为(1,1,1,10)的随机张量,然后创建新的张量,
去掉所有1维,留下shape (10)的张量,打印两个张量和它的形状;
import torch
torch.manual_seed(7)
ta = torch.rand(size=(1, 1, 1, 10))
# remove single dimension
tb = ta.squeeze()
# print out tensor
print("Tensor A:{}\n Tensor A Shape:{}\nTensor B:{}\nTensor B Shape:{}"
.format(ta,ta.shape,tb,tb.shape))
Tensor A:tensor([[[[0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297,
0.3653, 0.8513]]]])
Tensor A Shape:torch.Size([1, 1, 1, 10])
Tensor B:tensor([0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297, 0.3653,
0.8513])
Tensor B Shape:torch.Size([10])