tensor自身的softmax,log,log_softmax函数

tensor自身带有softmax,log,log_softmax函数,与torch.nn,torch.nn.functional 类似,具体的原理可以去看相关的源码,实际使用中知道这些会比较方便。

import torch

if __name__ == '__main__':

    a = torch.randn(2,4)
    print(f"a = {a}")
    print(f"a.softmax(dim=-1) = {a.softmax(dim=-1)}")
    print(f"a.softmax(dim=-1).log() = {a.softmax(dim=-1).log()}")
    print(f"a.log_softmax(dim=-1) = {a.log_softmax(dim=-1)}")

输出结果:

a = tensor([[ 0.8762, -1.5293, -0.0871,  2.5112],
        [-0.5123, -0.9871,  1.0301,  0.5816]])
a.softmax(dim=-1) = tensor([[0.1515, 0.0137, 0.0578, 0.7770],
        [0.1077, 0.0670, 0.5037, 0.3216]])
a.softmax(dim=-1).log() = tensor([[-1.8873, -4.2927, -2.8505, -0.2523],
        [-2.2283, -2.7031, -0.6858, -1.1344]])
a.log_softmax(dim=-1) = tensor([[-1.8873, -4.2927, -2.8505, -0.2523],
        [-2.2283, -2.7031, -0.6858, -1.1344]])

你可能感兴趣的:(pytorch,深度学习,python)