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

本站现有博文332篇,共被浏览875150

本站已经建立2583天!

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