RNN的前向传播函数

 

程序:

import torch as t
from torch import nn
from torch.autograd import Variable as V

t.manual_seed(1000)

# 输入 batch_size=3,序列长度都为2,序列中每个元素占4维

input=V(t.randn(2,3,4))
print(input)

# lstm 输入向量4维,3个隐藏元,1层

lstm=nn.LSTM(4,3,1)

# 初始状态:1层,batch_size=3,3个隐层元

h0=V(t.randn(1,3,3))
c0=V(t.randn(1,3,3))

out,(hn,cn)=lstm((input,h0),c0)
out,(hn,cn)=lstm(input,(h0,c0))
print(out)
print("hn=\n",hn)
print("cn=\n",cn)

out,hn=lstm(input,(h0,c0))
print(out)
print("hn=\n",hn)

 

这样写,out,(hn,cn)=lstm((input,h0),c0) 会报错:

/home/wangbin/anaconda3/envs/deep_learning/bin/python3.7 /home/wangbin/anaconda3/envs/deep_learning/project/main.py
Traceback (most recent call last):
  File "/home/wangbin/anaconda3/envs/deep_learning/project/main.py", line 21, in <module>
    out,(hn,cn)=lstm((input,h0),c0)
  File "/home/wangbin/anaconda3/envs/deep_learning/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/wangbin/anaconda3/envs/deep_learning/lib/python3.7/site-packages/torch/nn/modules/rnn.py", line 564, in forward
    max_batch_size = input.size(0) if self.batch_first else input.size(1)
AttributeError: 'tuple' object has no attribute 'size'
tensor([[[-0.5306, -1.1300, -0.6734, -0.7669],
         [-0.7029,  0.9896, -0.4482,  0.8927],
         [-0.6043,  1.0726,  1.0481,  1.0527]],

        [[-0.6424, -1.2234, -1.0794, -0.6037],
         [-0.7926, -0.1414, -1.0225, -0.0482],
         [ 0.6610, -0.8908,  1.4793, -0.3934]]])

Process finished with exit code 1

 

根据报错信息查看:

可以发现lstm的forward具有三个参数,self、input、hx,但是hx可以再分。

    def forward(self, input, hx=None):  # noqa: F811
        orig_input = input
        # xxx: isinstance check needs to be in conditional for TorchScript to compile
        if isinstance(orig_input, PackedSequence):
            input, batch_sizes, sorted_indices, unsorted_indices = input
            max_batch_size = batch_sizes[0]
            max_batch_size = int(max_batch_size)
        else:
            batch_sizes = None
            max_batch_size = input.size(0) if self.batch_first else input.size(1)
            sorted_indices = None
            unsorted_indices = None

        if hx is None:
            num_directions = 2 if self.bidirectional else 1
            zeros = torch.zeros(self.num_layers * num_directions,
                                max_batch_size, self.hidden_size,
                                dtype=input.dtype, device=input.device)
            hx = (zeros, zeros)
        else:
            # Each batch of the hidden state should match the input sequence that
            # the user believes he/she is passing in.
            hx = self.permute_hidden(hx, sorted_indices)

        self.check_forward_args(input, hx, batch_sizes)
        if batch_sizes is None:
            result = _VF.lstm(input, hx, self._flat_weights, self.bias, self.num_layers,
                              self.dropout, self.training, self.bidirectional, self.batch_first)
        else:
            result = _VF.lstm(input, batch_sizes, hx, self._flat_weights, self.bias,
                              self.num_layers, self.dropout, self.training, self.bidirectional)
        output = result[0]
        hidden = result[1:]
        # xxx: isinstance check needs to be in conditional for TorchScript to compile
        if isinstance(orig_input, PackedSequence):
            output_packed = PackedSequence(output, batch_sizes, sorted_indices, unsorted_indices)
            return output_packed, self.permute_hidden(hidden, unsorted_indices)
        else:
            return output, self.permute_hidden(hidden, unsorted_indices)

 

 

如果这样写out,(hn,cn)=lstm(input,h0,c0) ,仍然报错:

/home/wangbin/anaconda3/envs/deep_learning/bin/python3.7 /home/wangbin/anaconda3/envs/deep_learning/project/main.py
tensor([[[-0.5306, -1.1300, -0.6734, -0.7669],
         [-0.7029,  0.9896, -0.4482,  0.8927],
         [-0.6043,  1.0726,  1.0481,  1.0527]],

        [[-0.6424, -1.2234, -1.0794, -0.6037],
         [-0.7926, -0.1414, -1.0225, -0.0482],
         [ 0.6610, -0.8908,  1.4793, -0.3934]]])
Traceback (most recent call last):
  File "/home/wangbin/anaconda3/envs/deep_learning/project/main.py", line 21, in <module>
    out,(hn,cn)=lstm(input,h0,c0)
  File "/home/wangbin/anaconda3/envs/deep_learning/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
TypeError: forward() takes from 2 to 3 positional arguments but 4 were given

Process finished with exit code 1

 

错误原因是self是人家的默认参数,虽然实参里面没写,但是形参里面已经存在,是默认传递的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
可以使用PyTorch中的RNN模型来拟合sin函数。以下是一个简单的例子: ```python import torch import torch.nn as nn import matplotlib.pyplot as plt import numpy as np # 定义模型 class RNN(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(RNN, self).__init__() self.hidden_size = hidden_size self.rnn = nn.RNN(input_size, hidden_size, batch_first=True) self.fc = nn.Linear(hidden_size, output_size) def forward(self, x, hidden): out, hidden = self.rnn(x, hidden) out = self.fc(out) return out, hidden # 训练模型 input_size = 1 hidden_size = 32 output_size = 1 sequence_length = 20 num_epochs = 1000 learning_rate = 0.01 rnn = RNN(input_size, hidden_size, output_size) criterion = nn.MSELoss() optimizer = torch.optim.Adam(rnn.parameters(), lr=learning_rate) hidden = None for epoch in range(num_epochs): # 生成随机序列 inputs = torch.from_numpy(np.random.uniform(-np.pi, np.pi, size=(1, sequence_length, input_size))).float() outputs = torch.sin(inputs) # 传播 rnn.zero_grad() hidden = hidden.detach() if hidden is not None else None outputs, hidden = rnn(inputs, hidden) loss = criterion(outputs, outputs) # 反向传播 optimizer.zero_grad() loss.backward() optimizer.step() if (epoch+1) % 100 == 0: print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item())) # 测试模型 with torch.no_grad(): inputs = torch.from_numpy(np.linspace(-np.pi, np.pi, num=100).reshape(1, -1, 1)).float() outputs = torch.sin(inputs) hidden = None preds, hidden = rnn(inputs, hidden) plt.plot(inputs.squeeze().numpy(), outputs.squeeze().numpy(), label='target') plt.plot(inputs.squeeze().numpy(), preds.squeeze().numpy(), label='prediction') plt.legend() plt.show() ``` 这个例子中,我们使用一个有一个隐藏层的RNN模型来拟合sin函数。我们生成随机序列作为输入,并将其作为sin函数的输出进行训练。在1000个epoch之后,我们使用训练好的模型来预测sin函数,并将其可视化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿的探索之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值