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

本站现有博文335篇,共被浏览937453

本站已经建立2648天!

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