You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

decoder.py 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. class DecoderLayer(nn.Module):
  5. def __init__(self, self_attention, cross_attention, d_model, d_ff=None,
  6. dropout=0.1, activation="relu"):
  7. super(DecoderLayer, self).__init__()
  8. d_ff = d_ff or 4*d_model
  9. self.self_attention = self_attention
  10. self.cross_attention = cross_attention
  11. self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1)
  12. self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1)
  13. self.norm1 = nn.LayerNorm(d_model)
  14. self.norm2 = nn.LayerNorm(d_model)
  15. self.norm3 = nn.LayerNorm(d_model)
  16. self.dropout = nn.Dropout(dropout)
  17. self.activation = F.relu if activation == "relu" else F.gelu
  18. def forward(self, x, cross, x_mask=None, cross_mask=None):
  19. x = x + self.dropout(self.self_attention(
  20. x, x, x,
  21. attn_mask=x_mask
  22. )[0])
  23. x = self.norm1(x)
  24. x = x + self.dropout(self.cross_attention(
  25. x, cross, cross,
  26. attn_mask=cross_mask
  27. )[0])
  28. y = x = self.norm2(x)
  29. y = self.dropout(self.activation(self.conv1(y.transpose(-1,1))))
  30. y = self.dropout(self.conv2(y).transpose(-1,1))
  31. return self.norm3(x+y)
  32. class Decoder(nn.Module):
  33. def __init__(self, layers, norm_layer=None):
  34. super(Decoder, self).__init__()
  35. self.layers = nn.ModuleList(layers)
  36. self.norm = norm_layer
  37. def forward(self, x, cross, x_mask=None, cross_mask=None):
  38. for layer in self.layers:
  39. x = layer(x, cross, x_mask=x_mask, cross_mask=cross_mask)
  40. if self.norm is not None:
  41. x = self.norm(x)
  42. return x

基于MindSpore的多模态股票价格预测系统研究 Informer,LSTM,RNN