EADST

Attention Net with Pytorch

Attention net may be put after the LSTM processing in the NLP task.

import torch
import torch.nn as nn
from torch.autograd import Variable

# attention layer
def attention_net(lstm_output):
    hidden_size = 300
    w_omega = Variable(torch.zeros(hidden_size, 2))
    u_omega = Variable(torch.zeros(2))
    output_reshape = torch.Tensor.reshape(lstm_output, [-1, hidden_size])
    u = torch.tanh(torch.mm(output_reshape, w_omega))
    attn_hidden_layer = torch.mm(u, torch.Tensor.reshape(u_omega, [-1, 1]))
    sequence_length = lstm_output.size()[1]
    alphas = nn.functional.softmax(attn_hidden_layer, dim=1)
    alphas_reshape = torch.Tensor.reshape(alphas, [-1, sequence_length, 1])
    state = lstm_output.permute(1, 0, 2)
    attn_output = torch.sum(state * alphas_reshape, 1)
    return attn_output


# add attention layer after lstm
if attetion_mode == True:
    lstm_out = lstm_out.permute(1, 0, 2)
    lstm_out = attention_net(lstm_out)

相关标签
About Me
XD
Goals determine what you are going to be.
Category
标签云
搞笑 v0.dev Plate 论文速读 净利润 GPT4 ms-swift NameSilo 图形思考法 YOLO IndexTTS2 Pytorch Cloudreve Datetime Website PDB Tiktoken Tensor SQL Card scipy Breakpoint Hotel Harness Qwen transformers CTC Baidu 继承 GPTQ WebCrawler Diagram icon PIP printf PDF Streamlit AI 第一性原理 云服务器 ResNet-50 Domain Michelin Git Math LoRA BF16 Claude mmap C++ Bipartite Hilton Rebuttal uwsgi Markdown RAR Review Freesound TTS Llama VSCode ONNX Translation SQLite WAN Proxy PyCharm DeepStream diffusers VPN LeetCode Mixtral Knowledge Paper 关于博主 Permission Password EXCEL 强化学习 Transformers Sklearn FP32 CLAP Gemma ModelScope 公式 Tracking Bitcoin XGBoost llama.cpp QWEN 多进程 顶会 API网关 VGG-16 JSON LLAMA BTC Use Nginx Disk Logo Animate Firewall GGML Statistics NLTK LaTeX Quantization Bert Input InvalidArgumentError News Numpy SVR Safetensors Magnet Color COCO PyTorch 报税 Land tqdm Quantize Interview Algorithm LLM OpenCV TSV Paddle FastAPI CSV MD5 FP64 logger Base64 DeepSeek Crawler 财报 算法题 GIT 版权 Anaconda Augmentation 递归学习法 HuggingFace FlashAttention Video Conda git-lfs FP8 Miniforge CEIR CC UNIX Search Qwen2 腾讯云 API Heatmap CUDA GoogLeNet FP16 Plotly Web Pillow git 多线程 Jetson Zip Linux XML Windows Github Jupyter 证件照 HaggingFace Data tar OpenAI Jev UI TensorFlow NLP 域名 Agent Hungarian Google Random v2ray Shortcut Distillation CAM Food Docker Dataset torchinfo Pickle 签证 SAM Ptyhon Bin 音频 CV Template Django BeautifulSoup 论文 ChatGPT Attention SPIE hf uWSGI Vim TensorRT Clash Pandas Excel Ubuntu Python Qwen2.5 RGB 图标 Image2Text RL OCR Vmess 飞书 阿里云
站点统计

本站现有博文338篇,共被浏览976509次

本站已经建立2689天!

热门文章
文章归档
回到顶部