簡體   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