簡體   English   中英

將 nn.Softmax 轉換為 torch.tensor

[英]convert nn.Softmax to torch.tensor

我的神經網絡末端有一個 softmax function。 我希望將概率設為 torch.tensor。為此,我正在使用torch.tensor(nn.softmax(x))並出現錯誤RuntimeError: Could not infer dtype of Softmax

請問我在這里做錯了什么,或者有沒有其他方法可以做到這一點。

nn.Softmax是 class。 你可以像這樣使用它:

import torch

x = torch.tensor([10., 3., 8.])
softmax = torch.nn.Softmax(dim=0)

probs = softmax(x)

或者,您可以使用功能 API torch.nn.functional.softmax

import torch

x = torch.tensor([10., 3., 8.])

probs = torch.nn.functional.softmax(x, dim=0)

它們是等價的。 在這兩種情況下,您都可以檢查type(probs)是否為<class 'torch.Tensor'>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM