x = torch.arange(4)
print(x) # tensor([0, 1, 2, 3])
"""
我們可以使用下標引用矢量的任何元素。 例如,我們可以引用 x x 的第 i / th 元素 x x x x x x x x i xi。
請注意,元素 x i xi 是一個標量,因此我們在引用它時不使用粗體
"""
print(x[3]) # tensor(3)
# 3、長度、維度和形狀
"""
矢量只是一個數字數組。 就像每個數組都有長度一樣,每個向量也有長度。 在數學表示法中,如果我們說向量 x 由 n 個實數標量組成,
我們可以表示爲 x ∈ Rn x ∈ Rn。 矢量的長度通常稱爲矢量的維數。
"""
# 與普通的 Python 數組一樣,我們可以通過調用 Python 的內置 len ()函數來訪問張量的長度。
print(len(x)) # 4
# 查看形狀
print(x.shape) # torch.Size([4])
# 4、矩陣
A = torch.arange(20).reshape(5,4) # 將20以內的數字轉換成5行4列的矩陣
print(A)
"""
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
"""
# 打印矩陣的轉置
print(A.T)
"""
tensor([[ 0, 4, 8, 12, 16],
[ 1, 5, 9, 13, 17],
[ 2, 6, 10, 14, 18],
[ 3, 7, 11, 15, 19]])
"""
# 作爲方陣的特殊類型,對稱矩陣的轉置等於本身
B = torch.tensor([[1,2,3],[2,0,4],[3,4,5]])
print(B)
"""
tensor([[1, 2, 3],
[2, 0, 4],
[3, 4, 5]])
"""
# 5、張量
"""
張量(本小節中的“張量”指代數對象)爲我們提供了一種描述任意數目的 n 維數組的通用方法。
向量是一階張量,矩陣是二階張量。 張量用特殊字體的大寫字母表示
"""
X = torch.arange(24).reshape(2,3,4) # 表示兩個3x4的張量
print()
"""
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]]])
"""
# 6、張量算術的基本性質
A = torch.arange(20,dtype=torch.float32).reshape(5,4)
B = A.clone() # 通過分配新內存,將“ a”的副本分配給“ b”
print(A)
"""
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.],
[16., 17., 18., 19.]])
"""
print(A + B)
"""
tensor([[ 0., 2., 4., 6.],
[ 8., 10., 12., 14.],
[16., 18., 20., 22.],
[24., 26., 28., 30.],
[32., 34., 36., 38.]])
"""
print(A * B)
"""
tensor([[ 0., 1., 4., 9.],
[ 16., 25., 36., 49.],
[ 64., 81., 100., 121.],
[144., 169., 196., 225.],
[256., 289., 324., 361.]])
"""
# 將張量與標量相乘或相加也不會改變張量的形狀,其中操作數張量的每個元素都將相加或相乘於標量。
a = 2
X = torch.arange(24).reshape(2,3,4)
print(a + X)
"""
tensor([[[ 2, 3, 4, 5],
[ 6, 7, 8, 9],
[10, 11, 12, 13]],
[[14, 15, 16, 17],
[18, 19, 20, 21],
[22, 23, 24, 25]]])
"""
print((a * X).shape) # torch.Size([2, 3, 4])
# 7、變形
x = torch.arange(4,dtype=torch.float32)
print(x)
print(x.sum())
"""
tensor([0., 1., 2., 3.])
tensor(6.)
"""
print(A.shape) # torch.Size([5, 4])
print(A.sum()) # tensor(190.)
# 指定求和的軸
A_sum_axis1 = A.sum(axis = 1) # 0表示行,1表示列
print(A_sum_axis1) # tensor([ 6., 22., 38., 54., 70.])
print(A_sum_axis1.shape) # torch.Size([5])
print(A.sum(axis=[0,1])) # 和A.sum()一樣 ,tensor(190.)
# 求平均值
print(A.mean())
print(A.sum() / A.numel())
"""
tensor(9.5000)
tensor(9.5000)
"""
# 求各個軸的平均值
print(A.mean(axis=0))
print(A.sum(axis=0)/ A.shape[0])
"""
tensor([ 8., 9., 10., 11.])
tensor([ 8., 9., 10., 11.])
"""
# 8、Non-Reduction Sum
sum_A = A.sum(axis=1,keepdims = True) # 按行相加並保持矩陣的二維特性
print(sum_A)
"""
tensor([[ 6.],
[22.],
[38.],
[54.],
[70.]])
"""
print(A / sum_A)
"""
tensor([[0.0000, 0.1667, 0.3333, 0.5000],
[0.1818, 0.2273, 0.2727, 0.3182],
[0.2105, 0.2368, 0.2632, 0.2895],
[0.2222, 0.2407, 0.2593, 0.2778],
[0.2286, 0.2429, 0.2571, 0.2714]])
"""
# 如果我們想要計算 a 元素沿某個軸的累積和,比如說軸0(一行一行) ,我們可以調用累積函數。 這個函數不會沿任何軸降低輸入張量。
print(A.cumsum(axis=0))
"""
tensor([[ 0., 1., 2., 3.],
[ 4., 6., 8., 10.],
[12., 15., 18., 21.],
[24., 28., 32., 36.],
[40., 45., 50., 55.]])
"""
# 9、點乘
y = torch.ones(4,dtype=torch.float32)
print(x,y,torch.dot(x,y)) # tensor([0., 1., 2., 3.]) tensor([1., 1., 1., 1.]) tensor(6.)
# 注意,我們可以通過執行一個元素乘法和一個和來等價表示兩個向量的點積:
print(torch.sum(x * y)) # tensor(6.)
# 矩陣和向量相乘
"""
用張量表示矩陣向量積,我們使用與點積相同的點函數。 當我們用一個矩陣 a 和一個向量 x 調用 np.dot (a,x)時,將執行矩陣向量乘積。
請注意,a 的列維度(沿軸1的長度)必須與 x 的維度(其長度)相同。
"""
print(A.shape,x.shape,torch.mv(A,x)) # torch.Size([5, 4]) torch.Size([4]) tensor([ 14., 38., 62., 86., 110.])
# 矩陣乘法
B = torch.ones(4,3)
print(torch.mm(A,B))
"""
tensor([[ 6., 6., 6.],
[22., 22., 22.],
[38., 38., 38.],
[54., 54., 54.],
[70., 70., 70.]])
"""
# 10、範數
u = torch.tensor([3.0,-4.0])
print(torch.norm(u)) # tensor(5.)
動手學習深度學習最新版(pytorch)-線性代數知識
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.