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

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

本站已经建立2597天!

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