前言:
本文是邱锡鹏老师《神经网络与深度学习》实践题的实现。原题为tensorflow版本填空。这里改写为pytorch版本。
原题:GitHub - nndl/exercise: exercise for nndl chapter7
题面:
使用sequence to sequence 模型将一个字符串序列逆置。
例如 OIMESIQFIQ
逆置成 QIFQISEMIO
(下图来自网络,是一个sequence to sequence 模型示意图 )
实现:
注:此简洁实现数据处理较为简单,适合新手复现seq2seq模型。
import time, random, math, string
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import random
import string
def randomString(stringLength):
"""Generate a random string with the combination of lowercase and uppercase letters """
letters = string.ascii_uppercase
return ''.join(random.choice(letters) for i in range(stringLength))
def get_batch(batch_size, length):
batched_examples = [randomString(length) for i in range(batch_size)]
enc_x = [[ord(ch)-ord('A')+1 for ch in list(exp)] for exp in batched_examples]
y = [[o for o in reversed(e_idx)] for e_idx in enc_x]
dec_x = [[0]+e_idx[:-1] for