torch的LSTM输出问题

class Net(torch.nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.bilstm = torch.nn.LSTM(4,10,batch_first=False,bidirectional=True)
    def forward(self,input):
        rnn_out,_ = self.bilstm(input)  #输出形状 [length:3,batch:2,hidden:20]
        encoding = torch.cat([rnn_out[0], rnn_out[-1]], dim=1)  ## 选择双向hidden的最后一个向量,并把他们拼接成一个向量
        #cat  拼接向量  dim=0是按第一个维度拼接,dim=1是按第二个维度拼接,dim=2 以此类推
        return encoding  

net = Net()
input = torch.rand(6,2,4)
input
out = net(input)
out
输出:
tensor([[[0.8778, 0.0818, 0.6875, 0.8859],
         [0.5734, 0.2775, 0.3590, 0.2174]],

        [[0.9982, 0.5425, 0.3168, 0.1968],
         [0.3750, 0.4696, 0.5498, 0.0981]],

        [[0.1177, 0.0353, 0.0145, 0.5931],
         [0.9969, 0.8779, 0.0992, 0.0971]],

        [[0.2751, 0.5499, 0.0971, 0.6711],
         [0.1580, 0.5890, 0.0929, 0.2064]],

        [[0.5682, 0.0360, 0.0856, 0.8951],
         [0.1791, 0.3881, 0.4138, 0.0593]],

        [[0.8850, 0.2704, 0.5389, 0.3144],
         [0.8112, 0.8874, 0.0174, 0.1409]]])

tensor([[-0.0343,  0.1127,  0.0279,  0.0927,  0.0789,  0.0817, -0.0883,  0.1394,
         -0.0650,  0.0198, -0.2320, -0.1571, -0.2744,  0.0421, -0.0620, -0.1478,
         -0.0336, -0.1328,  0.1629, -0.4289, -0.1029,  0.1805,  0.0289,  0.1386,
          0.1701,  0.1697, -0.1425,  0.1867, -0.0695, -0.0141, -0.1513, -0.0761,
         -0.1537,  0.0194, -0.0216, -0.0107,  0.0298, -0.1285,  0.0615, -0.1595],
        [-0.0676,  0.0644, -0.0016,  0.0671,  0.0838,  0.0723, -0.0314,  0.0668,
         -0.0258, -0.0100, -0.2025, -0.1248, -0.2316, -0.0192, -0.1111, -0.0949,
          0.0649, -0.1628,  0.0912, -0.3886, -0.1119,  0.0934,  0.0270,  0.0647,
          0.1424,  0.1345, -0.0460,  0.1188, -0.0534, -0.0672, -0.1266, -0.0497,
         -0.1637, -0.0041, -0.0519,  0.0089,  0.0486, -0.1394, -0.0221, -0.1710]],
       grad_fn=<CatBackward>)

转载于:https://www.cnblogs.com/rise0111/p/11533661.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
torch.lstm 是 PyTorch 深度学习框架中的一个模块,用于构建长短期记忆(Long Short-Term Memory,LSTM)网络。LSTM 是一种循环神经网络(Recurrent Neural Network,RNN)的变种,它通过引入记忆单元来解决传统 RNN 中的梯度消失问题。 在 PyTorch 中,可以通过使用 torch.nn.LSTM 类来创建 LSTM 模型。该类接受输入维度、隐藏层维度等参数,并提供了 forward 方法用于定义模型的前向传播过程。可以通过调用该类的实例来构建 LSTM 模型,并在训练或推理过程中使用。 下面是一个使用 torch.nn.LSTM 创建一个简单的 LSTM 模型的示例代码: ```python import torch import torch.nn as nn # 定义 LSTM 模型 input_size = 10 hidden_size = 20 num_layers = 2 batch_size = 1 sequence_length = 5 lstm = nn.LSTM(input_size, hidden_size, num_layers) # 构造输入数据 input_data = torch.randn(sequence_length, batch_size, input_size) # 初始化隐藏状态和记忆状态 h0 = torch.randn(num_layers, batch_size, hidden_size) c0 = torch.randn(num_layers, batch_size, hidden_size) # 前向传播 output, (hn, cn) = lstm(input_data, (h0, c0)) # 输出结果 print(output) print(hn) print(cn) ``` 以上示例代码中,我们创建了一个输入维度为10,隐藏层维度为20,层数为2的 LSTM 模型。然后构造了一个尺寸为(5, 1, 10)的输入数据,分别表示序列长度为5,批次大小为1,特征维度为10。最后通过调用 lstm.forward() 方法进行前向传播,并输出模型的输出结果和最后一步的隐藏状态和记忆状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值