循环神经网络:(RNN)对字符构建one-hot向量(hello->ohlol)

import torch
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as  F
import torch.optim as optim
from matplotlib import pyplot as plt
import os
import sys

input_size = 4
hidden_size = 4
batch_size = 1
num_layers = 1
seq_len = 5
# 1.构建数据集
idx2char = ['e', 'h', 'l', 'o']
x_data = [1, 0, 2, 2, 3]
y_data = [3, 1, 2, 3, 2]
one_hot_lookup = [
    [1, 0, 0, 0],   #对应one_hot_lookup[0]
    [0, 1, 0, 0],   #对应one_hot_lookup[1]
    [0, 0, 1, 0],   #对应one_hot_lookup[2]
    [0, 0, 0, 1]    #对应one_hot_lookup[3]
]
#通过字典的查询组成x
x_one_hot = [one_hot_lookup[x] for x in x_data]
# [[0, 1, 0, 0],
# [1, 0, 0, 0],
# [0, 0, 1, 0],
# [0, 0, 1, 0],
# [0, 0, 0, 1]]
print(x_one_hot)
inputs = torch.Tensor(x_one_hot)
print(inputs.shape)# torch.Size([5, 4])
inputs = inputs.view(-1, batch_size, input_size)#[seqlen,batch_size,input_size]
print(inputs.shape)# torch.Size([5, 1, 4])

labels = torch.LongTensor(y_data)
print(labels.shape)# torch.Size([5, 1])  [seqlen,1]


# 2.搭建神经网络
class Model(torch.nn.Module):
    def __init__(self, input_size, hidden_size, batch_size, num_layers):
        super(Model, self).__init__()
        self.batch_size = batch_size
        self.input_size = input_size
        self.hidden_size = hidden_size #为了构建h0
        self.num_layers = num_layers
        self.rnn = torch.nn.RNN(input_size=self.input_size, hidden_size=self.hidden_size, num_layers=self.num_layers)

    def forward(self, input):
        hidden = torch.zeros(self.num_layers,
                             self.batch_size,
                             self.hidden_size)
        out, _ = self.rnn(input, hidden)
        return out.view(-1, self.hidden_size)  #转化为两维(seqlen*batchsize,hiddensize) 目的让labels(seq,b,1->(seq*b,1)


net = Model(input_size, hidden_size, batch_size, num_layers)
# 3.定义优化器和损失函数
criterion = torch.nn.CrossEntropyLoss()
optimzer = torch.optim.Adam(net.parameters(), lr=0.05)
# 4.模型训练
for epoch in range(20):
    loss = 0
    optimzer.zero_grad()
    outputs = net(inputs)   #inputs(seqlen,batchsize,inputsize) outputs(seqlen,batchsize,hiddensize)
    loss = criterion(outputs, labels)#labels(seqlen,bitchsize,1)
    loss.backward()
    optimzer.step()
    _, idx = outputs.max(dim=1)
    idx = idx.data.numpy()
    print('Predicted: ', ''.join([idx2char[x] for x in idx]), end='')#join将预测的字符拼接为一个字符串
    print(',Epoch [%d/15] loss =%.3f' % (epoch + 1, loss.item()))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShuaS2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值