初始化隐藏状态【h0】和细胞状态【c0】到RNN,LSTM,GRU --OCR(光学字符识别)

在PyTorch中,我们可以通过直接创建具有适当形状和类型的张量来自定义初始化LSTM的隐藏状态和细胞状态。这些张量的形状应该是(num_layers * num_directions, batch_size, hidden_size),其中num_layers是LSTM层数,num_directions是LSTM的方向数(对于双向LSTM为2,对于单向LSTM为1),batch_size是批量中的样本数,hidden_size是隐藏层的大小。
以下展示了一张图片
如何自定义初始化隐藏状态和细胞状态 到RNN,LSTM,GRU 网络:
…导入必备库…《
import torch.nn as nn
import torch
from PIL import Image
from torchvision import transforms
…RNN网络…
img = Image.open(‘00000.jpg’).convert(‘L’) #本地图片一张
tt = transforms.ToTensor()
pic = tt(img)
print(pic.size())
rnn = nn.RNN(input_size=100, hidden_size=256, num_layers=1,batch_first=True)
h0 = torch.randn(1, 1, 256) #[num_layers * num_directions, batch_size, hidden_size]。
out, hn= rnn(pic,h0)
print(out)
print(out.shape)
print(hn)
print(hn.shape)
…LSTM网络…

img = Image.open(‘00000.jpg’).convert(‘L’)
tt = transforms.ToTensor()
pic = tt(img)
print(pic.size())

ls = nn.LSTM(input_size=100, hidden_size=256, num_layers=2)

h0 = torch.zeros(2, pic.size(1), 256)
c0 = torch.zeros(2, pic.size(1), 256)
output, (hn, cn) = ls(pic, (h0, c0))
print(f’output:‘, output)
print(f’outputshape:’, output.shape)
print(f’hn:‘, hn)
print(f’hnshape:’, hn.shape)
print(f’cn:‘, cn)
print(f’cnshape:’, cn.shape)
…GRU网络…
img = Image.open(‘00000.jpg’).convert(‘L’) #本地图片一张
tt = transforms.ToTensor()
pic = tt(img)
print(pic.size())
rnn = nn.GRU(input_size=100, hidden_size=256, num_layers=1,batch_first=True)
h0 = torch.randn(1, 1, 256) #[num_layers * num_directions, batch_size, hidden_size]。
out, hn= rnn(pic,h0)
print(out)
print(out.shape)
print(hn)
print(hn.shape)

h0和c0被初始化,可以根据需要使用其他值或策略来初始化它们。重要的是要确保这些张量的形状与RNN,LSTM,GRU层期望的形状相匹配。

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值