pytorch RNN(构建多个相似结构的模型)

11 篇文章 0 订阅

加载数据(简单看)

from __future__ import unicode_literals, print_function, division
from io import open
import glob
import os
import unicodedata
import string

all_letters = string.ascii_letters + " .,;'-"
n_letters = len(all_letters) + 1 # Plus EOS marker

def findFiles(path): return glob.glob(path)

# Turn a Unicode string to plain ASCII, thanks to https://stackoverflow.com/a/518232/2809427
def unicodeToAscii(s):
    return ''.join(
        c for c in unicodedata.normalize('NFD', s)
        if unicodedata.category(c) != 'Mn'
        and c in all_letters
    )

# Read a file and split into lines
def readLines(filename):
    lines = open(filename, encoding='utf-8').read().strip().split('\n')
    return [unicodeToAscii(line) for line in lines]

# Build the category_lines dictionary, a list of lines per category
category_lines = {}
all_categories = []
for filename in findFiles('data/names/*.txt'):
    category = os.path.splitext(os.path.basename(filename))[0]
    all_categories.append(category)
    lines = readLines(filename)
    category_lines[category] = lines

n_categories = len(all_categories)

if n_categories == 0:
    raise RuntimeError('Data not found. Make sure that you downloaded data '
        'from https://download.pytorch.org/tutorial/data.zip and extract it to '
        'the current directory.')

print('# categories:', n_categories, all_categories)
print(unicodeToAscii("O'Néàl"))

数据准备

import random

#  随机获取到一个分类
def randomChoice(l):
    print('len',len(l))
    return l[random.randint(0, len(l) - 1)]

# Get a random category and random line from that category
#在这个分类的数据中随机抽取一些样本
def randomTrainingPair():
    category = randomChoice(all_categories)
    line = randomChoice(category_lines[category])
    return category, line


# One-hot vector for category
#这个分类的数据的one-hot矩阵
def categoryTensor(category):
    li = all_categories.index(category)
    tensor = torch.zeros(1, n_categories)
    tensor[0][li] = 1
    return tensor

# One-hot matrix of first to last letters (not including EOS) for input
#根据采样得到的数据构建onehot矩阵
def inputTensor(line):
    tensor = torch.zeros(len(line), 1, n_letters)
    for li in range(len(line)):
        letter = line[li]
        tensor[li][0][all_letters.find(letter)] = 1
    return tensor

# LongTensor of second letter to end (EOS) for target
def targetTensor(line):
    letter_indexes = [all_letters.find(line[li]) for li in range(1, len(line))]
    letter_indexes.append(n_letters - 1) # EOS
    return torch.LongTensor(letter_indexes)

# Make category, input, and target tensors from a random category, line pair
def randomTrainingExample():
    category, line = randomTrainingPair()
    category_tensor = categoryTensor(category)
    input_line_tensor = inputTensor(line)
    target_line_tensor = targetTensor(line)
    return category_tensor, input_line_tensor, target_line_tensor
randomTrainingExample()

模型构建

import torch
import torch.nn as nn

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        #因为这里我们构建的网络的每一层的网络节点数不同,所以传入一些参数来计算每一个层的网络节点数目
        super(RNN, self).__init__()
        self.hidden_size = hidden_size

        self.i2h = nn.Linear(n_categories + input_size + hidden_size, hidden_size)
        self.i2o = nn.Linear(n_categories + input_size + hidden_size, output_size)
        self.o2o = nn.Linear(hidden_size + output_size, output_size)
        self.dropout = nn.Dropout(0.1)
        self.softmax = nn.LogSoftmax(dim=1)

    def forward(self, category, input, hidden):
        input_combined = torch.cat((category, input, hidden), 1)#按列拼接张量
        hidden = self.i2h(input_combined)
        output = self.i2o(input_combined)
        output_combined = torch.cat((hidden, output), 1)
        output = self.o2o(output_combined)
        output = self.dropout(output)
        output = self.softmax(output)
        return output, hidden

    def initHidden(self):
        return torch.zeros(1, self.hidden_size)

模型训练

criterion = nn.NLLLoss()

learning_rate = 0.0005

def train(category_tensor, input_line_tensor, target_line_tensor):

    target_line_tensor.unsqueeze_(-1)
    hidden = rnn.initHidden()

    rnn.zero_grad()

    loss = 0

    for i in range(input_line_tensor.size(0)):
        
        output, hidden = rnn(category_tensor, input_line_tensor[i], hidden)
        l = criterion(output, target_line_tensor[i])
        loss += l

    loss.backward()#反向传播

    for p in rnn.parameters():#更新参数
        p.data.add_(-learning_rate, p.grad.data)

    return output, loss.item() / input_line_tensor.size(0)
    rnn = RNN(n_letters, 128, n_letters)

n_iters = 100000
total_loss = 0 # Reset every plot_every iters

for iter in range(1, n_iters + 1):
    output, loss = train(*randomTrainingExample())
    total_loss += loss
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyTorch是一个用于深度学习的开源框架,其中的RNN(循环神经网络)模块可以用于多输入变量回归任务。多输入变量回归是一种通过多个输入变量来预测一个连续值的任务。 在PyTorch中,可以使用torch.nn.RNN类来构建RNN模型。要实现多输入变量回归,首先需要将输入的多个变量进行堆叠或拼接,形成一个输入序列来喂给模型。可以使用torch.cat()函数将输入变量按列进行拼接。 然后,可以定义RNN模型的参数,如隐藏层的大小、RNN层的类型(如GRU或LSTM)等。可以使用torch.nn.GRU或torch.nn.LSTM类来定义RNN层。 在前向传播过程中,可以通过调用RNN模型的forward()方法来计算输出。输出结果可以通过添加全连接层,将RNN的输出转换为所需的预测结果。需要注意的是,在RNN模型的前向传播过程中,需要将输入序列作为参数传递给模型。 在训练过程中,可以定义损失函数和优化器,通过最小化损失函数来更新模型的参数。常用的损失函数包括均方误差(MSE)和平均绝对误差(MAE)等。 最后,在训练和测试阶段,可以循环遍历数据集,并将输入序列和对应的目标值喂给模型,然后计算损失并进行反向传播。可以通过调用optimizer.step()方法来更新模型的参数。 综上所述,PyTorch中的RNN模块可以用于多输入变量回归任务。通过适当的数据处理、设置模型参数、定义损失函数和优化器,可以实现对多输入变量的连续值预测。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值