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

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

本站已经建立2583天!

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