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
num_class=4
input_size = 4
hidden_size = 8
embedding_size=10
batch_size = 1
num_layers = 2
seq_len = 5
#注意:这里数据准备这样写会报错,要改为input=torch.LongTensor(x_data).view(batch_size,seq_len)
#这一步执行完后hidden的第1维的长度就会变成batchsize*seqlen
#但是hidden是不需要知道seqlen即序列长的,因为RNN每一个cell的权重都是一样,不需要seqlen来更新全部cell的权重
# 1.构建数据集
idx2char = ['e', 'h', 'l', 'o']
x_data = [1, 0, 2, 2, 3]
y_data = [3, 1, 2, 3, 2]
inputs = torch.LongTensor(x_data).view(batch_size,seq_len)
labels = torch.LongTensor(y_data)
print(inputs.size())#torch.Size([1, 5])
print(labels.size())#torch.Size([5])
# 2.搭建神经网络
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.emb=torch.nn.Embedding(input_size,embedding_size)
self.rnn = torch.nn.RNN(input_size=embedding_size,
hidden_size=hidden_size,
num_layers=num_layers,
batch_first=True)
self.fc=torch.nn.Linear(hidden_size,num_class) #(batchsize,seqlen,hiddensize)->(batchsize,seqlen,numclass)
def forward(self, x): #input为长整型(batchsize,seqlen)
hidden = torch.zeros(num_layers,x.size(0),hidden_size)
x=self.emb(x) #output(batchsize,seqlen,embeddingsize) notice: batch_first
x, _ = self.rnn(x, hidden)
x=self.fc(x)
return x.view(-1, num_class) #(seqlen*batchsize,num_class)
net = Model()
# 3.定义优化器和损失函数
criterion = torch.nn.CrossEntropyLoss()
optimzer = torch.optim.Adam(net.parameters(), lr=0.05)
# 4.模型训练
for epoch in range(15):
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()))
循环神经网络:embedding(嵌入层)处理(hello->ohlol)
猜你喜欢
转载自blog.csdn.net/qq_21686871/article/details/115422696
今日推荐
周排行