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

本站现有博文333篇,共被浏览922213

本站已经建立2628天!

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