在torch中softmax的使用


## 在torch中softmax的使用在torch中softmax的使用

在哪一維度上進行softmax操作,哪一維度的值之和為1

class Fc(nn.Module):
    def __init__(self, in_channel, out_channel):
        super(Fc, self).__init__()
        self.in_channel = in_channel
        self.out_channel = out_channel
        self.fc = nn.Linear(self.in_channel, self.out_channel)
    def forward(self, x):
        # x shape: (T, N, C)
        y = self.fc(x)
        y = F.softmax(y, dim=2)
        return y
if __name__ == "__main__":
    T, N, C = 1, 4, 64
    x = torch.randn((T, N, C))
    model = Fc(C, 60)
    y = model(x)
    a = 0
    for i in y[0, 0, :]:
        a = a + i
    print(a)

輸出:

tensor(1.0000, grad_fn=<AddBackward0>)
torch.Size([1, 4, 60])


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM