-
大小: 6KB文件类型: .zip金币: 2下载: 1 次发布日期: 2021-06-17
- 语言: Python
- 标签:
资源简介
在PyTorch中关注神经机器翻译的最小Seq2Seq模型
代码片段和文件信息
import math
import torch
import random
from torch import nn
from torch.autograd import Variable
import torch.nn.functional as F
class Encoder(nn.Module):
def __init__(self input_size embed_size hidden_size
n_layers=1 dropout=0.5):
super(Encoder self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.embed_size = embed_size
self.embed = nn.embedding(input_size embed_size)
self.gru = nn.GRU(embed_size hidden_size n_layers
dropout=dropout bidirectional=True)
def forward(self src hidden=None):
embedded = self.embed(src)
outputs hidden = self.gru(embedded hidden)
# sum bidirectional outputs
outputs = (outputs[: : :self.hidden_size] +
outputs[: : self.hidden_size:])
return outputs hidden
class Attention(nn.Module):
def __init__(self hidden_size):
super(Attention self).__init__()
self.hidden_size = hidden_size
self.attn = nn.Linear(self.hidden_size * 2 hidden_size)
self.v = nn.Parameter(torch.rand(hidden_size))
stdv = 1. / math.sqrt(self.v.size(0))
self.v.data.uniform_(-stdv stdv)
def forward(self hidden encoder_outputs):
timestep = encoder_outputs.size(0)
h = hidden.repeat(timestep 1 1).transpose(0 1)
encoder_outputs = encoder_outputs.transpose(0 1) # [B*T*H]
attn_energies = self.score(h encoder_outputs)
return F.relu(attn_energies dim=1).unsqueeze(1)
def score(self hidden encoder_outputs):
# [B*T*2H]->[B*T*H]
energy = F.softmax(self.attn(torch.cat([hidden encoder_outputs] 2)))
energy = energy.transpose(1 2) # [B*H*T]
v = self.v.repeat(encoder_outputs.size(0) 1).unsqueeze(1) # [B*1*H]
energy = torch.bmm(v energy) # [B*1*T]
return energy.squeeze(1) # [B*T]
class Decoder(nn.Module):
def __init__(self embed_size hidden_size output_size
n_layers=1 dropout=0.2):
super(Decoder self).__init__()
self.embed_size = embed_size
self.hidden_size = hidden_size
self.output_size = output_size
self.n_layers = n_layers
self.embed = nn.embedding(output_size embed_size)
self.dropout = nn.Dropout(dropout inplace=True)
self.attention = Attention(hidden_size)
self.gru = nn.GRU(hidden_size + embed_size hidden_size
n_layers dropout=dropout)
self.out = nn.Linear(hidden_size * 2 output_size)
def forward(self input last_hidden encoder_outputs):
# Get the embedding of the current input word (last output word)
embedded = self.embed(input).unsqueeze(0) # (1BN)
embedded = self.dropout(embedded)
# Calculate attention weights and apply to encoder outputs
attn_weights = self.attention(last_hidden[-1] encoder_outputs)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-10-18 14:14 seq2seq-master\
文件 1204 2018-10-18 14:14 seq2seq-master\.gitignore
文件 1061 2018-10-18 14:14 seq2seq-master\LICENSE
文件 1247 2018-10-18 14:14 seq2seq-master\README.md
文件 4509 2018-10-18 14:14 seq2seq-master\model.py
文件 4155 2018-10-18 14:14 seq2seq-master\train.py
文件 1037 2018-10-18 14:14 seq2seq-master\utils.py
相关资源
- Python-DeepMoji模型的pyTorch实现
- Python-使用DeepFakes实现YouTube视频自动换
- Python-一系列高品质的动漫人脸数据集
- Python-Insightface人脸检测识别的最小化
- Python-自然场景文本检测PSENet的一个
- Python-在特征金字塔网络FPN的Pytorch实现
- Python-PyTorch实时多人姿态估计项目的实
- Python-用PyTorch10实现FasterRCNN和MaskRCNN比
- Python-心脏核磁共振MRI图像分割
- Python-基于YOLOv3的行人检测
- Python-RLSeq2Seq用于SequencetoSequence模型的
- Python-PyTorch对卷积CRF的参考实现
- Python-高效准确的EAST文本检测器的一个
- Python-pytorch实现的人脸检测和人脸识别
- Python-UNet用于医学图像分割的嵌套UN
- Python-TensorFlow弱监督图像分割
- Python-基于tensorflow实现的用textcnn方法
- Python-Keras实现Inceptionv4InceptionResnetv1和
- Python-pytorch中文手册
- Python-FastSCNN的PyTorch实现快速语义分割
- Python-滑动窗口高分辨率显微镜图像分
- Python-使用MovieLens数据集训练的电影推
- Python-机器学习驱动的Web应用程序防火
- Python-subpixel利用Tensorflow的一个子像素
-
Python-汉字的神经风格转移Neuralst
y - Python-神经网络模型能够从音频演讲中
- Python-深度增强学习算法的PyTorch实现策
- Python-基于深度学习的语音增强使用
- Python-基于知识图谱的红楼梦人物关系
- Python-STGAN用于图像合成的空间变换生
评论
共有 条评论