1
23456789101112lstm=nn.LSTM(input_size, hidden_size, num_layers)x seq_len, batch, input_sizeh0 num_layers× \times×num_directions, batch, hidden_sizec0 num_layers× \times×num_directions, batch, hidden_sizeoutput seq_len, batch, num_directions× \times×hidden_size
hn num_layers× \times×num_directions, batch, hidden_sizecn num_layers× \times×num_directions, batch, hidden_size举个例子:
对句子进行LSTM操作假设有100个句子(sequence),每个句子里有7个词,batch_size=64,embedding_size=300
此时,各个参数为:
input_size=embedding_size=300batch=batch_size=64seq_len=7另外设置hidden_size=100, num_layers=1
import torch
import torch.nn as nnlstm = nn.LSTM(300, 100, 1)x = torch.randn(7, 64, 300)h0 = torch.randn(1, 64, 100)c0 = torch.randn(1, 64, 100)output, (hn, cn)=lstm(x, (h0, c0))>>
output.shape torch.Size([7, 64, 100])hn.shape torch.Size([1, 64, 100])cn.shape torch.Size([1, 64, 100])--------------------- 作者:huxuedan01 来源:CSDN 原文:https://blog.csdn.net/m0_37586991/article/details/88561746 版权声明:本文为博主原创文章,转载请附上博文链接!