1. 程式人生 > >Pytorch第二課:package-torch(2) 之數學操作

Pytorch第二課:package-torch(2) 之數學操作

微博:https://weibo.com/wangxiaocaoai/profile?rightmod=1&wvr=6&mod=personinfo
微信公眾號:搜尋"AI躁動街"


本節要點:

1 逐點計算操作

2 縮減操作

3 比較操作

4 其他操作

5 線性代數操作

1 逐點計算操作

# 匯入包
import torch
# 1.計算絕對值
a = torch.FloatTensor([-1, -10])
a_abs = torch.abs(a)
print(a_abs)
tensor([  1.,  10.])
# 2.計算餘弦
a = torch.randn(4) a_cos = torch.cos(a) print(a_cos)
tensor([ 0.3623,  0.0784,  0.9808,  0.8221])
# 3.計算雙曲餘弦
a = torch.randn(4)
a_cosh = torch.cosh(a)
print(a_cosh)
tensor([ 2.2139,  1.0241,  1.0118,  1.2354])
# 4.計算反餘弦
a = torch.randn(4)
a_acos = torch.acos(a)
print(a_acos)
tensor([ 2.1772,  0.9836,  2.2749,  0.5994])
# 5.計算正弦
a = torch.randn(4)
a_sinh = torch.sinh(a)
print(a_sinh)
tensor([-0.9122,  0.2392,  1.2656,  0.4663])
# 6.計算雙曲正弦
a = torch.randn(4)
a_sin = torch.sin(a)
print(a_sin)
# 7.計算反正弦
a = torch.randn(4)
a_asin = torch.asin(a)
print(a_asin)
tensor([-0.9362,     nan, -1.1308,  0.0407])
# 8.計算正切
a = torch.randn(4) tan = torch.tan(a) print(tan)
tensor([  0.0061,  -0.9945, -17.3586,  -0.3077])
# 9.計算雙曲正切
a = torch.randn(4)
tanh = torch.tanh(a)
print(tanh)
tensor([ 0.9856, -0.8689, -0.6669,  0.4711])
# 10.計算一個張量的反正切
a = torch.randn(4)
atan = torch.atan(a)
print(atan)
tensor([ 0.3666,  1.0404, -0.5340,  0.0825])
# 11.計算兩個張量的反正切
a = torch.randn(4)
b = torch.randn(4)
atan2 = torch.atan2(a, b)
print(atan2)
tensor([ 0.7203, -0.7093, -2.4139, -0.9148])
# 12.加法
# 直接加法 a+b
a = torch.randn(4)
b = 20
add = torch.add(a, b)
print(add)

# a + c * b
c = torch.rand(4)
add2 = torch.add(a, b, c)
print(add2)
tensor([ 20.6347,  19.7540,  21.7134,  20.2289])
tensor([ 15.7686,   8.5032,   4.6279,   5.1963])
# 13.先除後加:t + 0.5*(t1 / t2)
t = torch.randn(2, 3)
t1 = torch.randn(2, 3)
t2 = torch.randn(2, 3)
addcdiv = torch.addcdiv(t, 0.5, t1, t2)
print(addcdiv)  
tensor([[-0.9947, -1.5404, -0.5799],
        [-1.5395,  3.4531,  1.6741]])
# 14.先乘後加:t + 0.5*(t1 * t2)
addcmul = torch.addcmul(t, 0.5, t1, t2)
print(addcmul)
tensor([[-0.9904, -1.3817, -0.3505],
        [-1.2655,  3.0514,  1.3952]])
# 15.乘法計算
# 張量與標量相乘
a = torch.randn(2, 3)
a_mul = torch.mul(a, 5)
print('a:', a)
print('a_mul:', a_mul)

# 張量與張量相乘,對應位置的元素相乘
b = torch.randn(2, 3)
a_b_mul = torch.mul(a, b)
print('b:', b)
print('a_b_mul:', a_b_mul)
a: tensor([[ 0.0863,  1.7408,  0.8538],
        [ 0.8702,  0.1472,  0.2192]])
a_mul: tensor([[ 0.4315,  8.7038,  4.2688],
        [ 4.3508,  0.7359,  1.0962]])
b: tensor([[ 0.5903, -0.6919, -0.4070],
        [-0.3127,  0.1756,  0.9016]])
a_b_mul: tensor([[ 0.0510, -1.2044, -0.3474],
        [-0.2721,  0.0258,  0.1977]])
# 16.除法計算
# 張量除標量 
a = torch.randn(1,4)
a_div = torch.div(a, 2)  # a / 2
print(a_div)
tensor([[ 0.4224,  0.7171, -0.4719, -0.1562]])
# 17.張量除張量
a = torch.randn(1, 4)
b = torch.randn(1, 4)
div = torch.div(a, b)
print(div)
tensor([[ 2.2698,  0.7206,  1.0432,  0.2880]])
# 18.計算除法餘數
a = torch.tensor([-1, 2, 3, 4])
a_fmod = torch.fmod(a, 2) # a%2
print(a_fmod)
tensor([-1,  0,  1,  0])
# 19.計算除法餘數,餘數與除數有相同的符號。
a = torch.tensor([-1, 2, 3, 4])
a_re = torch.remainder(a, 2) # a%2
print(a_re)
tensor([ 1,  0,  1,  0])
# 20.指數計算
a = torch.randn(1, 4)
a_exp = torch.exp(a)
print(a_exp)
tensor([[ 0.1983,  3.7585,  1.6955,  3.2236]])
# 21.自然對數計算
a = torch.randn(1, 4)
a_log = torch.log(a)
print(a_log)
tensor([[-0.7030,     nan,     nan, -5.1565]])
# 22.冪值計算
# 冪值為標量時
a = torch.Tensor([1, 2, 3, 4])
a_pow = torch.pow(a, 3)
print(a)
print(a_pow)

# 冪值為張量時
exp = torch.Tensor([1, 2, 3, 4])
a_pow = torch.pow(a, exp)
print(a_pow)
tensor([ 1.,  2.,  3.,  4.])
tensor([  1.,   8.,  27.,  64.])
tensor([   1.,    4.,   27.,  256.])
# 23.計算平方根的
a = torch.Tensor([1, 2, 3, 4])
a_sqrt = torch.sqrt(a)
print(a_sqrt)
tensor([ 1.0000,  1.4142,  1.7321,  2.0000])
# 24.計算平方根的倒數
a = torch.Tensor([1, 2, 3, 4])
a_rsqrt = torch.rsqrt(a)
print(a_rsqrt)
tensor([ 1.0000,  0.7071,  0.5774,  0.5000])
# 25.計算sigmoid
a = torch.randn(2, 2)
a_sig = torch.sigmoid(a)
print(a_sig)
tensor([[ 0.1865,  0.6050],
        [ 0.4330,  0.6606]])
# 26.向上取整
a = torch.randn(2, 2)
a_ceil = torch.ceil(a)
print(a_ceil)
tensor([[-1., -0.],
        [-0.,  1.]])
# 27.向下取整
a = torch.randn(2, 2)
a_floor = torch.floor(a)
print(a_floor)
tensor([[ 1., -1.],
        [ 0., -2.]])
# 28.四捨五入到整數
a = torch.tensor([-1.5, 2.1, 3.9, 4.5])
a_round = torch.round(a) # a%2
print(a_round)
tensor([-2.,  2.,  4.,  4.])
# 29.計算截斷值,即去掉小數部分的整數
a = torch.tensor([-1.5, 2.1, 3.9, 4.5])
a_trunc = torch.trunc(a) # a%2
print(a_trunc)
tensor([-1.,  2.,  3.,  4.])
# 30.返回分數
a = torch.Tensor([1, 1.5, 1.6, 2.9])
a_frac = torch.frac(a)
print(a_frac)
tensor([ 0.0000,  0.5000,  0.6000,  0.9000])
# 31.取負
a = torch.randn(1, 4)
a_neg = torch.neg(a)
print(a)
print(a_neg)
tensor([[-0.0075, -1.8761,  0.6364,  0.8317]])
tensor([[ 0.0075,  1.8761, -0.6364, -0.8317]])
# 32.取倒數
a = torch.Tensor([1, 2, 3, 4])
a_r  =torch.reciprocal(a)
print(a_r)
tensor([ 1.0000,  0.5000,  0.3333,  0.2500])
# 33.取正負號
a = torch.randn(4)
a_sign = torch.sign(a)
print(a)
print(a_sign)
tensor([ 1.1125, -1.9569,  1.8638,  0.3364])
tensor([ 1., -1.,  1.,  1.])
# 34.夾緊到某個區間
#        min, if x_i < min
# y_i =  x_i, if min <= x_i <= max
#        max, if x_i > max
    
a = torch.randn(1, 4)
print(a)
a_clamp = torch.clamp(a, min=-0.7, max = 0.7)
print(a_clamp)
tensor([[-1.6682, -0.3986, -0.6452, -0.4119]])
tensor([[-0.7000, -0.3986, -0.6452, -0.4119]])
# 也可以只限定min或者max
a_clamp = torch.clamp(a, max = 0.7)
print(a_clamp)

a_clamp = torch.clamp(a, min=-0.7)
print(a_clamp)
tensor([[-1.6682, -0.3986, -0.6452, -0.4119]])
tensor([[-0.7000, -0.3986, -0.6452, -0.4119]])
# 35.對張量做線性插值
start = torch.arange(1, 5)
end = torch.Tensor(4).fill_(2)
a = torch.lerp(start, end, 0.5)
print(start)
print(end)
print(a)
tensor([ 1.,  2.,  3.,  4.])
tensor([ 2.,  2.,  2.,  2.])
tensor([ 1.5000,  2.0000,  2.5000,  3.0000])

2 縮減操作

import torch 
# 1.沿指定維度的累積積
a = torch.randn(10)
print('a:', a)

a_cumprod = torch.cumprod(a, 0)
print(a_cumprod)  # 注意維度沒有變,只是累加了
a: tensor([ 2.4314, -0.3712, -1.3956, -0.7340,  0.6754,  0.9834, -0.3069,
         0.8109, -0.4161, -1.4032])
tensor([ 2.4314, -0.9026,  1.2596, -0.9246, -0.6244, -0.6141,  0.1885,
         0.1528, -0.0636,  0.0892])
# 2.沿指定維度的累積積
a = torch.randn(10)
print('a:', a)

a_cumsum = torch.cumsum(a, 0)
print(a_cumsum)  # 注意維度沒有變,只是累加了
a: tensor([-0.0111,  0.3256, -0.4843, -0.5490,  0.7626,  0.6011, -0.8144,
        -0.4629,  0.4934, -0.6671])
tensor([-0.0111,  0.3145, -0.1698, -0.7188,  0.0438,  0.6449, -0.1694,
        -0.6323, -0.1389, -0.8060])
# 3.返回 (input - other) 的 p範數 。
a = torch.randn(3)
b = torch.randn(3)
a_dist = torch.dist(a, b, 3)
print(a_dist)
tensor(3.2886)
# 4.返回輸入張量input 的p 範數。
a = torch.randn(4, 5)
a_n = torch.norm(a, 2)
print(a_n)

# 可指定維度
a_n = torch.norm(a, 2, 1)
print(a_n)
tensor(4.2416)
tensor([ 2.5676,  1.8141,  1.5184,  2.4087])
# 5.返回輸入張量所有元素的均值。
a = torch.randn(3)
a_m = torch.mean(a)
print(a_m)

# 多維
a = torch.randn(3,3)
a_m = torch.mean(a)
print(a_m)

# 指定維度
a = torch.randn(2,3)
a_m = torch.mean(a, 1)
print(a_m)
tensor(-0.1776)
tensor(1.00000e-02 *
       4.2739)
tensor([-0.9272,  0.3165])
# 6.返回輸入張量給定維度每行的中位數,同時返回一個包含中位數的索引的LongTensor。
a = torch.randn(3)
a_m = torch.median(a)
print(a_m)

# 多維
a = torch.randn(2, 3)
a_m = torch.median(a)
print(a_m)

# 指定維度
a = torch.randn(2, 3)
a_m = torch.median(a, 1)
print(a_m)
tensor(1.00000e-02 *
       -4.8281)
tensor(0.1583)
(tensor([ 0.4999, -0.3078]), tensor([ 2,  0]))
# 7.返回輸入張量input 所有元素的積。
a = torch.randn(3)
a_p = torch.prod(a)
print(a_p)

# 多維
a = torch.randn(2, 3)
a_p = torch.prod(a)
print(a_p)

# 指定維度
a = torch.randn(2, 3)
a_m = torch.prod(a, 1)
print(a_p)
tensor(0.5736)
tensor(1.00000e-03 *
       3.2077)
tensor(1.00000e-03 *
       3.2077)
# 8.返回輸入張量input 所有元素的標準差,套路和上面都一樣,可一維,可多維,可指定維度
a = torch.randn(2, 3)
a_s = torch.std(a)
print(a_s)

tensor(0.7310)
# 9.返回輸入張量input 所有元素的方差,套路和上面都一樣,可一維,可多維,可指定維度
a = torch.randn(2, 3)
a_var = torch.var(a)
print(a_var)
tensor(1.7634)
# 10.返回輸入張量input 所有元素的和,套路和上面都一樣,可一維,可多維,可指定維度
a = torch.randn(2, 3)
a_s = torch.sum(a)
print(a_s)
tensor(-1.3865)
# 11.返回給定維dim上,每行的眾數值。 同時返回一個LongTensor,包含眾數職的索引。dim值預設為輸入張量的最後一維。
a = torch.randn(4, 5)
a_m = torch.mode(a)
print(a_m)

a = torch.randn(4, 5)
a_m = torch.mode(a, 0)
print(a_m)
(tensor([ 0.0055,  0.1009, -0.7734, -1.3452]), tensor([ 2,  2,  2,  0]))
(tensor([-0.0192, -0.9864, -1.0947, -0.8963, -0.4842]), tensor([ 2,  0,  3,  2,  3]))

3 比較操作

import torch
# 比較兩個張量是否相等
a = torch.Tensor([[1,2],[3,4]])
b = torch.Tensor([[1,2], [5,6]])
eq = torch.eq(a, b)
print(eq) # 若相同則對應位置輸出1
tensor([[ 1,  1],
        [ 0,  0]], dtype=torch.uint8)
# 比較前者張量是否大於等於後者
a = torch.Tensor([[1,2],[3,4]])
b = torch.Tensor([[1,2], [5,6]])
ge = torch.ge(a, b)
print(ge)
tensor([[ 1,  1],
        [ 0,  0]], dtype=torch.uint8)
# 比較前者張量是否大於後者
a = torch.Tensor([[1,2],[3,4]])
b = torch.Tensor([[1,2], [5,6]])
gt = torch.gt(a, b)
print(gt)
tensor([[ 0,  0],
        [ 0,  0]], dtype=torch.uint8)
# 比較前者張量是否小於等於後者
a = torch.Tensor([[1,2],[3,4]])
b = torch.Tensor([[1,2], [5,6]])
le = torch.le(a, b)
print(le)
tensor([[ 1,  1],
        [ 1,  1]], dtype=torch.uint8)
# 比較前者張量是否小於等於後者
a = torch.Tensor([[1,2],[3,4]])
b = torch.Tensor([[1,2], [5,6]])
lt = torch.lt(a, b)
print(lt)
tensor([[ 0,  0],
        [ 1,  1]], dtype=torch.uint8)
# 取輸入張量input指定維上第k 個最小值。如果不指定dim,則預設為input的最後一維。
a = torch.arange(1, 6)
a_kth = torch.kthvalue(a, 3)
print(a_kth)
(tensor(3.), tensor(2))
# 沿給定dim維度返回輸入張量input中 k 個最大值。 如果不指定dim,
# 則預設為input的最後一維
# 如果為largest為 False ,則返回最小的 k 個值。
a = torch.randn(6, 6)
a_kth = torch.topk(a, 3)
print(a_kth)

a_kth = torch.topk(a, k=3, dim=0, largest=False)