溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

torch.nn.Parameter方法如何使用

發(fā)布時間:2021-08-05 17:46:50 來源:億速云 閱讀:221 作者:Leah 欄目:大數(shù)據(jù)

本篇文章給大家分享的是有關(guān)torch.nn.Parameter方法如何使用,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

torch.nn.Parameter方法如何使用
解釋:

它是Tensor的一個子類,它可以被作為module的參數(shù),把它賦值給module的屬性,
那么會自動被添加到module的參數(shù),也就是出現(xiàn)在parameters()迭代器中.
如果不使用Parameter類型,僅僅使用Tensor,
把Tensor類賦值給module的屬性,不會有這種效果,
不會出現(xiàn)在parameter()迭代器中.

實驗:

import torchimport torch.nn as nnclass Model4CXQ(nn.Module):def __init__(self):super(Model4CXQ, self).__init__()# super().__init__()self.attribute4cxq = nn.Parameter(torch.tensor(20200910.0))self.attribute4lzq = nn.Parameter(torch.tensor(20200.0))# self.attribute4scc = nn.Parameter(torch.Tensor(2.0))  # TypeError: new(): data must be a sequence (got float)# self.attribute4pq = nn.Parameter(torch.tensor(2))  # RuntimeError: Only Tensors of floating point dtype can require gradientsself.attribute4zh = nn.Parameter(torch.Tensor(2))# self.attribute4yzb = nn.Parameter(torch.tensor(912.0))self.attribute4yzb = (torch.tensor(912.0))self.attribute4gcx = (torch.tensor(3))self.attribute4ymw = (torch.Tensor(3))def forward(self, x):passif __name__ == "__main__":model = Model4CXQ()print()print("打印參數(shù)".center(50,'-'))for param in model.parameters():print(param)print()print("打印字典".center(50,'-'))for k, v in model.state_dict().items():print(k, v)

控制臺輸出:

Windows PowerShell
版權(quán)所有 (C) Microsoft Corporation。保留所有權(quán)利。

嘗試新的跨平臺 PowerShell https://aka.ms/pscore6

加載個人及系統(tǒng)配置文件用了 861 毫秒。(base) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> conda activate ssd4pytorch2_2_0(ssd4pytorch2_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>  & 'D:\Anaconda3\envs\ssd4pytorch2_2_0\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy\launcher' '56980' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\test4cxq\test8.py'-----------------------打印參數(shù)-----------------------Parameter containing:
tensor(20200910., requires_grad=True)Parameter containing:
tensor(20200., requires_grad=True)Parameter containing:
tensor([1.1673e-42, 0.0000e+00], requires_grad=True)-----------------------打印字典-----------------------attribute4cxq tensor(20200910.)attribute4lzq tensor(20200.)attribute4zh tensor([1.1673e-42, 0.0000e+00])(ssd4pytorch2_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>

以上就是torch.nn.Parameter方法如何使用,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI