<jupyter_start><jupyter_text>1、data loader Loading the MNIST data<jupyter_code>dataMNIST_train = dsets.MNIST(
    root = 'data',
    train = True,
    download = True,
    transform = transforms.ToTensor()
)
dataMNIST_test = dsets.MNIST(
    root = 'data',
    train = False,
    download = True,
    transform = transforms.ToTensor()
)
dataLoaderMNIST_train = torch.utils.data.DataLoader(
    dataset = dataMNIST_train,
    batch_size = 128,
    shuffle = True,
)
dataLoaderMNIST_test = torch.utils.data.DataLoader(
    dataset = dataMNIST_test,
    batch_size = 128,
    shuffle = True,
)
dataMNIST_train
dataMNIST_test
x,y = iter(dataLoaderMNIST_train).next()
# (batch_size, channel, height, width)
# RNN input data shape:(batch_size, seq_size, input_size)
x.shape,x.squeeze(1).shape
x = x.squeeze(1)
#(batch_size)
y.shape<jupyter_output><empty_output><jupyter_text>2、RNN model<jupyter_code>class modelLSTM(nn.Module):
    def __init__(self, input_size = 28, hidden_size = 32, num_layers = 1):
        super(modelLSTM, self).__init__()
        self.lstm = nn.LSTM(
            input_size = input_size,
            hidden_size = hidden_size,
            num_layers = num_layers,
            batch_first = True,
        )
        self.linear = nn.Linear(
            in_features = hidden_size*input_size, 
            out_features = 10
        )
    
    def forward(self, x, state = None):
        x = x.view(-1,x.shape[-2],x.shape[-1])
        y, next_state = self.lstm(x, state)
        y = y.contiguous().view(x.shape[0],-1) # contiguous operation
        y = self.linear(y)
        y = F.softmax(y,dim = 1)
        return y,next_state

class modelGRU(nn.Module):
    def __init__(self, input_size = 28, hidden_size = 32, num_layers = 1):
        super(modelGRU, self).__init__()
        self.gru = nn.GRU(
            input_size = input_size,
            hidden_size = hidden_size,
            num_layers = num_layers,
            batch_first = True,
        )
        self.linear = nn.Linear(
            in_features = hidden_size*input_size, 
            out_features = 10
        )
    
    def forward(self, x, state = None):
        x = x.view(-1,x.shape[-2],x.shape[-1])
        y, next_state = self.gru(x, state)
        y = y.contiguous().view(x.shape[0],-1) # contiguous operation
        y = self.linear(y)
        y = F.softmax(y,dim = 1)
        return y,next_state

mylstm = modelLSTM()
mylstm.cpu()
mygru = modelGRU()
mygru.cpu()
x = torch.randn(32,1,28,28)
y,state = mylstm(x)
y,state = mygru(x)<jupyter_output><empty_output><jupyter_text>3、train_data<jupyter_code>optimizerLSTM = optim.Adam(mylstm.parameters(),lr = 0.001,)
optimizerGRU = optim.Adam(mygru.parameters(),lr = 0.001,)
loss_func = nn.CrossEntropyLoss()
mylstm.to(device)
mygru.to(device)
%%time
mylstm.train()
for epoch in range(5):
    for step,(x,y) in enumerate(dataLoaderMNIST_train):
        x = x.cuda(device)
        y = y.cuda(device)
        y_,state = mylstm(x)
        loss = loss_func(y_,y)
        
        optimizerLSTM.zero_grad()
        loss.backward()
        optimizerLSTM.step()

        print('\repoch:{epoch:3}--step:{step:5}--loss:{loss:.4}'.format(epoch = epoch, step=step, loss=loss),end = '')
    acc = 0
    for _,(x,y) in enumerate(dataLoaderMNIST_test):
        x = x.cuda(device)
        y = y.cuda(device)
        y_,state = mylstm(x)
        acc += torch.sum(y_.argmax(1) == y)
    print('\repoch:{epoch:3}--step:{step:5}--loss:{loss:.4}--acc:{acc:.4}%-----'.format(epoch = epoch, step=step, loss=loss, acc = acc/10000*100))
    print()
%%time
mygru.train()
for epoch in range(5):
    for step,(x,y) in enumerate(dataLoaderMNIST_train):
        x = x.cuda(device)
        y = y.cuda(device)
        y_,state = mygru(x)
        loss = loss_func(y_,y)
        
        optimizerGRU.zero_grad()
        loss.backward()
        optimizerGRU.step()

        print('\repoch:{epoch:3}--step:{step:5}--loss:{loss:.4}'.format(epoch = epoch, step=step, loss=loss),end = '')
    acc = 0
    for _,(x,y) in enumerate(dataLoaderMNIST_test):
        x = x.cuda(device)
        y = y.cuda(device)
        y_,state = mygru(x)
        acc += torch.sum(y_.argmax(1) == y)
    print('\repoch:{epoch:3}--step:{step:5}--loss:{loss:.4}--acc:{acc:.4}%-----'.format(epoch = epoch, step=step, loss=loss, acc = acc/10000*100))
    print()
FPS_LSTM = 70000*5/55.7
FPS_GRU = 70000*5/52.9
FPS_LSTM,FPS_GRU
(FPS_GRU-FPS_LSTM)/FPS_LSTM*100<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15286195286195287, 'fraction_numerical': 0.0244668911335578, 'mean_word_length': 2.754001684919966, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '44900', 'license': 'MIT', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1669, 'n_words': 281, 'path': 'RNN learning/MNIST_RNN_verson.ipynb', 'repo_name': 'Gwanjyun/GwanjyunDeepLearning-2021Summer', 'size': '4455'}