如何找到torch.nn.Module.parameters()的方法定义



我正在关注这个笔记本:

其中一种方法:

def init_hidden(self, batch_size):
''' Initializes hidden state '''
# Create two new tensors with sizes n_layers x batch_size x n_hidden,
# initialized to zero, for hidden state and cell state of LSTM
weight = next(self.parameters()).data

if (train_on_gpu):
hidden = (weight.new(self.n_layers, batch_size, self.n_hidden).zero_().cuda(),
weight.new(self.n_layers, batch_size, self.n_hidden).zero_().cuda())
else:
hidden = (weight.new(self.n_layers, batch_size, self.n_hidden).zero_(),
weight.new(self.n_layers, batch_size, self.n_hidden).zero_())

return hidden

我想看看weight是什么类型,以及如何使用new()方法,所以我试图找出parameters()方法,因为数据属性来自paramerters()方法。

令人惊讶的是,在阅读了PyTorch中nn模块的源代码后,我找不到它的来源。

你是如何弄清楚在哪里可以看到PyTorch中方法的定义的?

所以我试图找出parameters((方法作为数据属性来自parameters((方法。令人惊讶的是,在阅读了PyTorch中nn模块的源代码。

您可以在此处的第178行看到torch/nn/modules/module.py下的模块定义。
然后您可以在这里轻松找到parameters()方法。

你们怎么知道在哪里可以看到方法的定义从PyTorch看到的?

我自己一直使用的最简单的方法是使用VSCode的Go to Definition或其Peek -> Peek definition功能。我相信Pycharm也有类似的功能。

您也可以直接从PyTorch文档中检查源代码。

请参阅此处了解torch.nn.Module.parameters函数(只需单击橙色的"Source",即可到达此处(。

源代码是链接的,如果它不是用C/C++/低级别编写的,在这种情况下,你必须通过GitHub绕过这个项目。

最新更新