在使用新版本pytorch 执行老版本代码时,或使用 torchkeras 时,有事会出现如下错误:
AttributeError: module 'torch.nn' has no attribute 'MultiheadAttention'

解决方案:
这是由于版本不匹配导致的,一个快速的解决方法是安装另一个包:

  • pip install torch_multi_head_attention
  • from torch_multi_head_attention import MultiHeadAttention
  • 使用 MultiHeadAttention 代替 torch.nn.MultiheadAttention
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐