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

本站现有博文339篇,共被浏览998412次

本站已经建立2709天!

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