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

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

本站已经建立2598天!

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