[英]Difficulty in Implementing a simple single-layer RNN using Pytorch's base class “nn.Linear” class
While working on making a simple RNN using Pytorch nn.linear function.在使用 Pytorch nn.linear function 制作简单的 RNN 时。 So firstly I initialized my weights as
所以首先我将我的权重初始化为
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)
Now in the main step where I am getting the result of the current state by using the previous state and the values of the weights using this code statement现在在主要步骤中,我通过使用以前的 state 和使用此代码语句的权重值来获取当前 state 的结果
h_t = np.tanh((inp * self.W_x) + (prev_h * self.W_h))
So here I am getting the python error as shown below所以在这里我收到 python 错误,如下所示
TypeError: mul(): argument 'other' (position 1) must be Tensor, not Linear
Can anyone help me with his regards...谁能帮我打个招呼...
Your W_x
and W_h
are not weights, but linear layers, which use a weight and bias (since bias=True
).您的
W_x
和W_h
不是权重,而是线性层,它使用权重和偏差(因为bias=True
)。 They need to be called as a function.它们需要被称为 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. 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. There is no need for
np.tanh
anyway, as PyTorch has torch.tanh
as well.无论如何都不需要
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.