pyroch rnn

# hello -> ohlol
import torch    # import 导入工具箱或py文件
# from torch.nn import Module
from torch.nn.modules import Module     # from 只能到py文件 import py文件内部的函数或类
class model_rnn(Module):
    def __init__(self, input_size, hidden_size, batch_size, num_layers):
        super(model_rnn,self).__init__()  # 多继承时,防止父类多次调用
        self.num_layers = num_layers
        self.batch_size = batch_size
        self.input_size = input_size
        self.hidden_size = hidden_size
        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)

input_size = 4
hidden_size = 4
batch_size = 1
num_layers = 1

idx2char = ['e', 'h', 'l', 'o']
x_data = [1, 0, 2, 2, 3]
y_data = [3, 1, 2, 3, 2]
onehot_lookup = [[1, 0, 0, 0],
                 [0, 1, 0, 0],
                 [0, 0, 1, 0],
                 [0, 0, 0, 1]]
x_onehot = [onehot_lookup[x] for x in x_data]
# np转tensor
inputs = torch.Tensor(x_onehot).view(-1, batch_size, input_size)
labels = torch.LongTensor(y_data).view(-1)

net = model_rnn(input_size,hidden_size,batch_size,num_layers)

criterion = torch.nn.CrossEntropyLoss() # 交叉熵损失
opt = torch.optim.Adam(net.parameters(),lr=0.01)
# 优化器

for epoch in range(100):
    opt.zero_grad()  # 梯度归零
    outputs = net.forward(inputs)
    loss = criterion(outputs, labels)   # outputs.shape ([5,4]), labels.shape ([5])
    loss.backward()
    opt.step()

    out = outputs.max(dim=1)  #  1:每行最大值的序号
    idx = out.indices.numpy()  # tensor装numpy

    print('epoch: %d , %s, loss: %f'% (epoch, ''.join([idx2char[x] for x in idx]), loss))




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值