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
# # 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).view(-1, 1)
print(labels.shape)# torch.Size([5, 1]) [seqlen,1]
# 2.搭建神经网络
class Model(torch.nn.Module):
def __init__(self, input_size, hidden_size, batch_size):
super(Model, self).__init__()
self.batch_size = batch_size
self.input_size = input_size
self.hidden_size = hidden_size
self.rnncell = torch.nn.RNNCell(input_size=self.input_size, hidden_size=self.hidden_size)
def forward(self, input, hidden):
hidden = self.rnncell(input, hidden)
return hidden
def init_hidden(self):
return torch.zeros(self.batch_size, self.hidden_size)
net = Model(input_size, hidden_size, batch_size)
#3.定义优化器和损失函数
criterion = torch.nn.CrossEntropyLoss()
optimzer = torch.optim.Adam(net.parameters(), lr=0.1)
#4.模型训练
for epoch in range(15):
loss = 0
optimzer.zero_grad()
hidden = net.init_hidden()
print('Predicted string :', end='') #labels(seqlen,1) label(1)
for input, label in zip(inputs, labels): #inputs(seqlen,batch_size,input_size)input(batch_size,input_size)
hidden = net(input, hidden) #按序列拿出所有的x1,算出loss后,在拿出所有的x2算loss
loss += criterion(hidden, label)#与之前不同的是,这里的损失是所有序列损失的和,来构建计算图
_, idx = hidden.max(dim=1) #hidden(batchsize,hiddensize) 在hiddensize的维度上找最大值的下标
print(idx2char[idx.item()], end='')#用字典读出概率最大下标对应的字符
loss.backward()
optimzer.step()
print(',Epoch [%d/15] loss =%.4f' % (epoch + 1, loss.item()))
循环神经网络:(RNNCell)对字符构建one-hot向量(hello->ohlol)
猜你喜欢
转载自blog.csdn.net/qq_21686871/article/details/115418171
今日推荐
周排行