繁体   English   中英

使用 Pytorch 的基础 class “nn.Linear” class 实现简单单层 RNN 的困难

[英]Difficulty in Implementing a simple single-layer RNN using Pytorch's base class “nn.Linear” class

在使用 Pytorch nn.linear function 制作简单的 RNN 时。 所以首先我将我的权重初始化为

self.W_x = nn.Linear(self.input_dim, self.hidden_dim, bias=True)
self.W_h = nn.Linear(self.hidden_dim, self.hidden_dim, bias=True)

现在在主要步骤中,我通过使用以前的 state 和使用此代码语句的权重值来获取当前 state 的结果

h_t = np.tanh((inp * self.W_x) + (prev_h * self.W_h))

所以在这里我收到 python 错误,如下所示

TypeError: mul(): argument 'other' (position 1) must be Tensor, not Linear

谁能帮我打个招呼...

您的W_xW_h不是权重,而是线性层,它使用权重和偏差(因为bias=True )。 它们需要被称为 function。

Furthermore, you cannot use NumPy operations with PyTorch tensors, but if you convert your tensors to NumPy arrays you can't back propagate through them, since only PyTorch operations are tracked in the computational graph. 无论如何都不需要np.tanh ,因为 PyTorch 也有torch.tanh

h_t = torch.tanh(self.W_x(inp) + self.W_h(prev_h))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM