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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import mindspore.nn as nn
  2. import mindspore.ops.operations as ops
  3. class DecoderLayer(nn.Cell):
  4. def __init__(self, self_attention, cross_attention, d_model, d_ff=None,
  5. dropout=0.1, activation="relu"):
  6. super(DecoderLayer, self).__init__()
  7. d_ff = d_ff or 4 * d_model
  8. self.self_attention = self_attention
  9. self.cross_attention = cross_attention
  10. self.conv1 = nn.Conv1d(d_model, d_ff, kernel_size=1)
  11. self.conv2 = nn.Conv1d(d_ff, d_model, kernel_size=1)
  12. self.norm1 = nn.LayerNorm(d_model)
  13. self.norm2 = nn.LayerNorm(d_model)
  14. self.norm3 = nn.LayerNorm(d_model)
  15. self.dropout = nn.Dropout(dropout)
  16. self.activation = ops.ReLU() if activation == "relu" else ops.GELU()
  17. def construct(self, x, cross, x_mask=None, cross_mask=None):
  18. attn_output, _ = self.self_attention(x, x, x, x_mask)
  19. x = x + self.dropout(attn_output)
  20. x = self.norm1(x)
  21. attn_output, _ = self.cross_attention(x, cross, cross, cross_mask)
  22. x = x + self.dropout(attn_output)
  23. x = self.norm2(x)
  24. y = x
  25. y = y.transpose(1, -1)
  26. y = self.dropout(self.activation(self.conv1(y)))
  27. y = self.conv2(y).transpose(1, -1)
  28. y = self.dropout(y)
  29. x = x + y
  30. x = self.norm3(x)
  31. return x
  32. class Decoder(nn.Cell):
  33. def __init__(self, layers, norm_layer=None):
  34. super(Decoder, self).__init__()
  35. self.layers = nn.LayerList(layers)
  36. self.norm = norm_layer
  37. def construct(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