对于图像数据而言,数据结构为(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