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

本站现有博文337篇,共被浏览961343次

本站已经建立2673天!

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