天天看點

torch.div()的使用舉例

參考連結: torch.div()

torch.div()的使用舉例

說明:

張量和标量做逐元素除法
或者兩個可廣播的張量之間做逐元素除法
           

代碼實驗:

(base) PS C:\Users\chenxuqi> python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000002277D112FD0>
>>> a = torch.randn(5)
>>> a
tensor([ 0.2824, -0.3715,  0.9088, -1.7601, -0.1806])
>>> torch.div(a, 0.5)
tensor([ 0.5648, -0.7430,  1.8176, -3.5202, -0.3612])
>>> torch.div(a, 2)
tensor([ 0.1412, -0.1857,  0.4544, -0.8801, -0.0903])
>>>
>>> a.div(0.5)
tensor([ 0.5648, -0.7430,  1.8176, -3.5202, -0.3612])
>>> a.div(2)
tensor([ 0.1412, -0.1857,  0.4544, -0.8801, -0.0903])
>>>
>>>
>>>
>>>
>>>
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000002277D112FD0>
>>> a = torch.randn(4, 4)
>>> b = torch.randn(4)
>>> a
tensor([[ 0.5816,  2.0060,  1.6013, -0.6379],
        [-1.1943,  0.1426,  1.3612, -1.4171],
        [-2.7679,  2.1961,  1.3462, -0.7650],
        [ 1.5528,  0.7210, -0.1314,  0.1448]])
>>> b
tensor([1.1216, 0.8440, 0.1783, 0.6859])
>>> torch.div(a, b)
tensor([[ 0.5185,  2.3769,  8.9794, -0.9301],
        [-1.0648,  0.1690,  7.6328, -2.0661],
        [-2.4677,  2.6022,  7.5490, -1.1153],
        [ 1.3844,  0.8543, -0.7369,  0.2112]])
>>> a.div(b)
tensor([[ 0.5185,  2.3769,  8.9794, -0.9301],
        [-1.0648,  0.1690,  7.6328, -2.0661],
        [-2.4677,  2.6022,  7.5490, -1.1153],
        [ 1.3844,  0.8543, -0.7369,  0.2112]])
>>> torch.div(b, a)
tensor([[ 1.9286,  0.4207,  0.1114, -1.0752],
        [-0.9391,  5.9189,  0.1310, -0.4840],
        [-0.4052,  0.3843,  0.1325, -0.8966],
        [ 0.7223,  1.1705, -1.3571,  4.7354]])
>>> b.div(a)
tensor([[ 1.9286,  0.4207,  0.1114, -1.0752],
        [-0.9391,  5.9189,  0.1310, -0.4840],
        [-0.4052,  0.3843,  0.1325, -0.8966],
        [ 0.7223,  1.1705, -1.3571,  4.7354]])
>>>
>>> 
           

繼續閱讀