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

本站现有博文341篇,共被浏览1050408次

本站已经建立2730天!

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