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

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

本站已经建立2669天!

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