最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pytorch中如何調(diào)用forward()函數(shù)

 更新時(shí)間:2023年02月17日 16:29:32   作者:good good study  
這篇文章主要介紹了Pytorch中如何調(diào)用forward()函數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Pytorch調(diào)用forward()函數(shù)

Module類是nn模塊里提供的一個模型構(gòu)造類,是所有神經(jīng)網(wǎng)絡(luò)模塊的基類,我們可以繼承它來定義我們想要的模型。

下面繼承Module類構(gòu)造本節(jié)開頭提到的多層感知機(jī)。

這里定義的MLP類重載了Module類的__init__函數(shù)和forward函數(shù)。

它們分別用于創(chuàng)建模型參數(shù)和定義前向計(jì)算。

前向計(jì)算也即正向傳播。

import torch
from torch import nn
?
class MLP(nn.Module):
? ? # 聲明帶有模型參數(shù)的層,這里聲明了兩個全連接層
? ? def __init__(self, **kwargs):
? ? ? ? # 調(diào)用MLP父類Module的構(gòu)造函數(shù)來進(jìn)行必要的初始化。這樣在構(gòu)造實(shí)例時(shí)還可以指定其他函數(shù)
? ? ? ? # 參數(shù),如“模型參數(shù)的訪問、初始化和共享”一節(jié)將介紹的模型參數(shù)params
? ? ? ? super(MLP, self).__init__(**kwargs)
? ? ? ? self.hidden = nn.Linear(784, 256) # 隱藏層
? ? ? ? self.act = nn.ReLU()
? ? ? ? self.output = nn.Linear(256, 10) ?# 輸出層
?
?
? ? # 定義模型的前向計(jì)算,即如何根據(jù)輸入x計(jì)算返回所需要的模型輸出
? ? def forward(self, x):
? ? ? ? a = self.act(self.hidden(x))
? ? ? ? return self.output(a)
??
X = torch.rand(2, 784)
net = MLP()
print(net)
net(X)

輸出:

MLP( (hidden): Linear(in_features=784, out_features=256, bias=True) (act): ReLU() (output): Linear(in_features=256, out_features=10, bias=True) ) tensor([[-0.1798, -0.2253, 0.0206, -0.1067, -0.0889, 0.1818, -0.1474, 0.1845, -0.1870, 0.1970], [-0.1843, -0.1562, -0.0090, 0.0351, -0.1538, 0.0992, -0.0883, 0.0911, -0.2293, 0.2360]], grad_fn=<ThAddmmBackward>)

為什么會調(diào)用forward()呢,是因?yàn)镸odule中定義了__call__()函數(shù),該函數(shù)調(diào)用了forward()函數(shù),當(dāng)執(zhí)行net(x)的時(shí)候,會自動調(diào)用__call__()函數(shù)

Pytorch函數(shù)調(diào)用的問題和源碼解讀

最近用到 softmax 函數(shù),但是發(fā)現(xiàn) softmax 的寫法五花八門,記錄如下

# torch._C._VariableFunctions
torch.softmax(x, dim=-1)
# class
softmax = torch.nn.Softmax(dim=-1)
x=softmax(x)
# function
x = torch.nn.functional.softmax(x, dim=-1)

簡單測試了一下,用 torch.nn.Softmax 類是最慢的,另外兩個差不多

torch.nn.Softmax 源碼如下,可以看到這是個類,而他這里的 return F.softmax(input, self.dim, _stacklevel=5) 調(diào)用的是 torch.nn.functional.softmax

class Softmax(Module):
? ? r"""Applies the Softmax function to an n-dimensional input Tensor
? ? rescaling them so that the elements of the n-dimensional output Tensor
? ? lie in the range [0,1] and sum to 1.

? ? Softmax is defined as:

? ? .. math::
? ? ? ? \text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}

? ? When the input Tensor is a sparse tensor then the unspecifed
? ? values are treated as ``-inf``.

? ? Shape:
? ? ? ? - Input: :math:`(*)` where `*` means, any number of additional
? ? ? ? ? dimensions
? ? ? ? - Output: :math:`(*)`, same shape as the input

? ? Returns:
? ? ? ? a Tensor of the same dimension and shape as the input with
? ? ? ? values in the range [0, 1]

? ? Args:
? ? ? ? dim (int): A dimension along which Softmax will be computed (so every slice
? ? ? ? ? ? along dim will sum to 1).

? ? .. note::
? ? ? ? This module doesn't work directly with NLLLoss,
? ? ? ? which expects the Log to be computed between the Softmax and itself.
? ? ? ? Use `LogSoftmax` instead (it's faster and has better numerical properties).

? ? Examples::

? ? ? ? >>> m = nn.Softmax(dim=1)
? ? ? ? >>> input = torch.randn(2, 3)
? ? ? ? >>> output = m(input)

? ? """
? ? __constants__ = ['dim']
? ? dim: Optional[int]

? ? def __init__(self, dim: Optional[int] = None) -> None:
? ? ? ? super(Softmax, self).__init__()
? ? ? ? self.dim = dim

? ? def __setstate__(self, state):
? ? ? ? self.__dict__.update(state)
? ? ? ? if not hasattr(self, 'dim'):
? ? ? ? ? ? self.dim = None

? ? def forward(self, input: Tensor) -> Tensor:
? ? ? ? return F.softmax(input, self.dim, _stacklevel=5)

? ? def extra_repr(self) -> str:
? ? ? ? return 'dim={dim}'.format(dim=self.dim)

torch.nn.functional.softmax 函數(shù)源碼如下,可以看到 ret = input.softmax(dim) 實(shí)際上調(diào)用了 torch._C._VariableFunctions 中的 softmax 函數(shù)

def softmax(input: Tensor, dim: Optional[int] = None, _stacklevel: int = 3, dtype: Optional[DType] = None) -> Tensor:
? ? r"""Applies a softmax function.

? ? Softmax is defined as:

? ? :math:`\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}`

? ? It is applied to all slices along dim, and will re-scale them so that the elements
? ? lie in the range `[0, 1]` and sum to 1.

? ? See :class:`~torch.nn.Softmax` for more details.

? ? Args:
? ? ? ? input (Tensor): input
? ? ? ? dim (int): A dimension along which softmax will be computed.
? ? ? ? dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
? ? ? ? ? If specified, the input tensor is casted to :attr:`dtype` before the operation
? ? ? ? ? is performed. This is useful for preventing data type overflows. Default: None.

? ? .. note::
? ? ? ? This function doesn't work directly with NLLLoss,
? ? ? ? which expects the Log to be computed between the Softmax and itself.
? ? ? ? Use log_softmax instead (it's faster and has better numerical properties).

? ? """
? ? if has_torch_function_unary(input):
? ? ? ? return handle_torch_function(softmax, (input,), input, dim=dim, _stacklevel=_stacklevel, dtype=dtype)
? ? if dim is None:
? ? ? ? dim = _get_softmax_dim("softmax", input.dim(), _stacklevel)
? ? if dtype is None:
? ? ? ? ret = input.softmax(dim)
? ? else:
? ? ? ? ret = input.softmax(dim, dtype=dtype)
? ? return ret

那么不如直接調(diào)用 built-in C 的函數(shù)?

但是有個博客 A selective excursion into the internals of PyTorch 里說

Note: That bilinear is exported as torch.bilinear is somewhat accidental. Do use the documented interfaces, here torch.nn.functional.bilinear whenever you can!

意思是說 built-in C 能被 torch.xxx 直接調(diào)用是意外的,強(qiáng)烈建議使用 torch.nn.functional.xxx 這樣的接口

看到最新的 transformer 官方代碼里也用的是 torch.nn.functional.softmax,還是和他們一致更好(雖然他們之前用的是類。。。)

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Django 多對多字段的更新和插入數(shù)據(jù)實(shí)例

    Django 多對多字段的更新和插入數(shù)據(jù)實(shí)例

    這篇文章主要介紹了Django 多對多字段的更新和插入數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Django中auth模塊用戶認(rèn)證的使用

    Django中auth模塊用戶認(rèn)證的使用

    本文主要介紹了Django中auth模塊用戶認(rèn)證的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • OpenCV實(shí)現(xiàn)相機(jī)校正

    OpenCV實(shí)現(xiàn)相機(jī)校正

    這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)相機(jī)校正,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • 如何使用Python發(fā)送HTML格式的郵件

    如何使用Python發(fā)送HTML格式的郵件

    這篇文章主要介紹了如何使用Python發(fā)送HTML格式的郵件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • python人工智能算法之線性回歸實(shí)例

    python人工智能算法之線性回歸實(shí)例

    這篇文章主要為大家介紹了python人工智能算法之線性回歸實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Python中的logging模塊實(shí)現(xiàn)日志打印

    Python中的logging模塊實(shí)現(xiàn)日志打印

    這篇文章主要介紹了Python中的logging模塊實(shí)現(xiàn)日志打印,其實(shí)不止print打印日志方便排查問題,Python自帶的logging模塊,也可以很簡單就能實(shí)現(xiàn)日志的配置和打印,下面來看看具體的實(shí)現(xiàn)過程吧,需要的朋友可以參考一下
    2022-03-03
  • 如何使用Python Dotenv庫管理環(huán)境變量

    如何使用Python Dotenv庫管理環(huán)境變量

    使用python-dotenv庫可以方便地管理環(huán)境變量,避免將敏感信息硬編碼在代碼中,這篇文章主要介紹了如何使用Python Dotenv庫管理環(huán)境變量,需要的朋友可以參考下
    2025-02-02
  • Python實(shí)現(xiàn)圖片與視頻互轉(zhuǎn)代碼實(shí)戰(zhàn)(親測有效)

    Python實(shí)現(xiàn)圖片與視頻互轉(zhuǎn)代碼實(shí)戰(zhàn)(親測有效)

    圖片轉(zhuǎn)視頻,視頻轉(zhuǎn)圖片手機(jī)一操作,立馬轉(zhuǎn)換過來,那么基于代碼是如何操作的呢?下面小編給大家?guī)砹薖ython實(shí)現(xiàn)圖片與視頻互轉(zhuǎn)代碼實(shí)戰(zhàn),感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • Python制作Windows按鍵通知腳本

    Python制作Windows按鍵通知腳本

    對于鍵盤沒有背光燈的同學(xué)而言,切換大小寫或控制Num鍵開關(guān)的時(shí)候沒有提示,經(jīng)常需要試探性地輸入一些字符來判斷開關(guān)是否打開,體驗(yàn)非常糟糕。所以本文就來用Python做一個Windows按鍵通知腳本,需要的可以參考一下
    2022-11-11
  • pycharm設(shè)置當(dāng)前工作目錄的操作(working directory)

    pycharm設(shè)置當(dāng)前工作目錄的操作(working directory)

    今天小編就為大家分享一篇pycharm設(shè)置當(dāng)前工作目錄的操作(working directory),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02

最新評論

呼和浩特市| 湟源县| 武宁县| 油尖旺区| 西平县| 郓城县| 分宜县| 潼南县| 阿拉尔市| 维西| 安新县| 静乐县| 广元市| 孟州市| 岢岚县| 江川县| 云浮市| 昌江| 岑巩县| 泰和县| 金秀| 南漳县| 英超| 沈丘县| 温宿县| 睢宁县| 左权县| 丹江口市| 出国| 洞头县| 澄城县| 根河市| 福建省| 伊川县| 崇阳县| 邻水| 淳化县| 邯郸市| 家居| 开原市| 农安县|