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

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

本站已经建立2621天!

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