dnn网络改为rnn

本文介绍了如何将原本适用于DNN的图像数据(channel, height, width)转换为适合RNN的输入(batch_size, time_steps, width),其中time_steps等于原始图像的高度h。通过将高度视为时间步,宽度作为每个时间步的输入,可以调整数据并使用RNN进行处理。代码示例展示了在DNN和RNN中输入数据的差异。" 116776878,10534961,Linux下线程内存消耗揭秘:glibc的malloc策略,"['Linux内存管理', '多线程编程', 'glibc', '内存优化']
摘要由CSDN通过智能技术生成

对于图像数据而言,数据结构为(channel, height, width), 每个通道上数据为(height, width)。对于
dnn网络而言,我们把h*w作为input_size。后面height简写为h,width简写为w

如何改造为rnn输入

将h看成time_step, 每个time_step大小为w, 这样rnn网络输入可以处理成(batch_size, h,w),所以通常数据变换方法为data = data.to(device).squeeze(1), 1代表把channel去掉

来看下代码比较

#1. Import 
#2. Create network
class NN(nn.Module): #difference
    def __init__(self, input_size, num_classes):
        super(NN, self).__init__()
        self.fc1 = nn.Linear(input_size, 50)
        self.fc2 = nn.Linear(50, num_classes)
    
    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x
#3. Set device
print(device)
#4. Hyperparameters
input_size = 784 #difference
num_classes = 10
#5. Load data
#6. Initialize network
model = NN(input_size, num_classes).to(device)
#7. Loss and Optimizer
#8. Train network
for epoch in range(num_epoch):
    for batch_ix, (data, targets) in enumerate(train_loader):
        #Get data to cuda if possible
        data = data.to(device)
        targets = targets.to(device)
        #Get to correct shape
        data = data.reshape(data.shape[0], -1) #difference
        #print(data.shape)
        #forward
        scores = model(data)
        loss = criterion(scores, targets)
        #print(f'batch_ix: {batch_ix}, scores: {scores}, loss: {loss}')
        #backward
        optimzer.zero_grad()
        loss.backward()
        #gradient descent or adam step 
        optimzer.step()
    print(f'epoch:{epoch}, loss:{loss}')

#9. check accuracy on training and testing to see how good our model
def check_accuracy(loader, model):
    with torch.no_grad():
        for x, y in loader:
            x = x.to(device)
            y = y.to(device)
            x = x.reshape(x.shape[0], -1)  #difference
            scores = model(x) 

rnn代码

#1. Import 
import torch
import torch.nn as nn
import torch.nn.functional as F 
import torch.optim as optim
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

#2. Create network
class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, num_classes):
        super(RNN, self).__init__()
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.rnn 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值