<jupyter_start><jupyter_text>RNN with a single Neuron- 싱글 뉴런이란게 hidden이 1개라는건지 hidden의 size가 1이라는건지..<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F
import os
import numpy as np
# single RNN

class SingleRNN(nn.Module):
    def __init__(self, input_size, neurons_size):
        super(SingleRNN, self).__init__() # 상위 클래스로 설정 -> nn.Moudle안에 있는 부모 class중 가장 위로 올린다.
        
        self.Wx = torch.randn(input_size, neurons_size) # 4 x 1 -> weight값
        self.Wy = torch.randn(neurons_size, neurons_size) # 1 x 1 -> weight값
        
        self.b = torch.zeros(1,neurons_size) # 1 x 4 -> bias값
        
    def forward(self, x0, x1):
        self.Y0 = torch.tanh(torch.mm(x0, self.Wx) + self.b) # 4 x 1 -> x와 weight값 내적
        self.Y1 = torch.tanh(torch.mm(self.Y0, self.Wy) + 
                            torch.mm(x1, self.Wx) + self.b) # 4 x 1 -> x와 weight값 내적
        
        return self.Y0, self.Y1
# batch로 입력 -> 한 번에 4개씩
x0_batch = torch.tensor([[0,1,2,0],
                         [3,4,5,0],
                         [6,7,8,0],
                         [9,0,1,0]], dtype = torch.float) # 4 x 4

x1_batch = torch.tensor([[9,8,7,0], 
                         [0,0,0,0], 
                         [6,5,4,0], 
                         [3,2,1,0]], dtype = torch.float) # 4 x 4
input_size = 4
neurons_size = 1
model = SingleRNN(input_size, neurons_size)
model
Y0_val, Y1_val = model(x0_batch, x1_batch)
Y0_val
model.Wx.size()
x0_batch.size()<jupyter_output><empty_output><jupyter_text>RNN with Multi NeuronsBasic RNN<jupyter_code>class BasicRNN(nn.Module):
    def __init__(self, input_size, neurons_size):
        super(BasicRNN, self).__init__()
        
        self.Wx = torch.randn(input_size, neurons_size)
        self.Wy = torch.randn(neurons_size, neurons_size)
        
        self.b = torch.randn(1, neurons_size)
        
    def forward(self, x0, x1):
        self.Y0 = torch.tanh(torch.mm(x0, self.Wx) + self.b)
        self.Y1 = torch.tanh(torch.mm(self.Y0, self.Wy) + 
                            torch.mm(x1, self.Wx) + self.b)
        
        return self.Y0, self.Y1
input_size = 3
neurons_size = 5

X0_batch = torch.tensor([[0,1,2], 
                         [3,4,5], 
                         [6,7,8], 
                         [9,0,1]], dtype = torch.float) #t=0 => 4 X 3

X1_batch = torch.tensor([[9,8,7], 
                         [0,0,0], 
                         [6,5,4], 
                         [3,2,1]], dtype = torch.float) #t=1 => 4 X 3
model = BasicRNN(input_size, neurons_size)
model
y0, y1 = model(X0_batch, X1_batch)
y0<jupyter_output><empty_output><jupyter_text>PyTorch Built-in RNN Cell- 여기서 Built-in RNN Cell은 이미 만들어져 있는 RNN을 말하는 것 같다.- nn Class에서 바로 가져오자<jupyter_code>rnn = nn.RNNCell(3,5) # input_size : 3, neurons_size = 5

x_batch = torch.tensor([[[0,1,2], 
                         [3,4,5], 
                         [6,7,8], 
                         [9,0,1]],
                        [[9,8,7], 
                         [0,0,0], 
                         [6,5,4],
                         [3,2,1]]], dtype = torch.float) # X0 and X1

x_batch.size()
# hidden layer
hx = torch.randn(4,5) # m x neurons_size

output = []
for i in range(2):
    hx = rnn(x_batch[i], hx)
    output.append(hx)
    
print(output)<jupyter_output>[tensor([[-0.7622,  0.9226, -0.9347, -0.1788,  0.6269],
        [-0.9285,  0.9771, -0.9775, -0.9971,  0.9974],
        [-0.9764,  0.9987, -1.0000, -0.9998,  1.0000],
        [ 0.9588,  0.9851, -0.9974, -0.9984,  0.9964]],
       grad_fn=<TanhBackward>), tensor([[-0.6137,  0.9984, -1.0000, -1.0000,  1.0000],
        [ 0.2652,  0.3874, -0.1155, -0.3601, -0.1925],
        [-0.0387,  0.9792, -0.9996, -0.9986,  0.9999],
        [ 0.8634,  0.4560, -0.9214, -0.9613,  0.9836]],
       grad_fn=<TanhBackward>)]<jupyter_text>Notice- 초반에 진행했던 BasicRNN과 같은 모델이다.- 계산에 필요한 weight와 bias를 자동으로 생성해준다.- 히든 레이어까지 그대로 다음 step으로 넘길 수 있다.<jupyter_code>class CleanBasicRNN(nn.Module):
    def __init__(self, batch_size, input_size, hidden_size):
        super(CleanBasicRNN, self).__init__()
        
        rnn = nn.RNNCell(input_size, hidden_size)
        self.hx = torch.randn(batch_size, hidden_size) # hidden layer 초기화
        
    def forward(self, x):
        output = []
        
        for i in range(2):
            self.hx = rnn(x[i], self.hx)
            output.append(self.hx)
            
        return output, self.hx
batch_size = 4
input_size = 3
hidden_size = 5

x_batch = torch.tensor([[[0,1,2], 
                         [3,4,5], 
                         [6,7,8], 
                         [9,0,1]],
                        [[9,8,7], 
                         [0,0,0], 
                         [6,5,4], 
                         [3,2,1]]], dtype = torch.float) # X0 and X1
x_batch.size()
model = CleanBasicRNN(batch_size, input_size, hidden_size)
model
output, hidden_layer = model(x_batch)
print(output)<jupyter_output>[tensor([[-0.9496,  0.9757, -0.7815, -0.1414,  0.0891],
        [-0.9257,  0.9916, -0.9969, -0.9903,  0.9989],
        [-0.9917,  0.9998, -1.0000, -0.9996,  1.0000],
        [ 0.9205,  0.9911, -0.9987, -0.9975,  0.9939]],
       grad_fn=<TanhBackward>), tensor([[-0.6878,  0.9983, -1.0000, -1.0000,  1.0000],
        [ 0.2721,  0.3846, -0.1309, -0.3553, -0.1860],
        [-0.0449,  0.9794, -0.9996, -0.9986,  0.9999],
        [ 0.8597,  0.4649, -0.9227, -0.9607,  0.9832]],
       grad_fn=<TanhBackward>)]
{'fraction_non_alphanumeric': 0.15711135611907387, 'fraction_numerical': 0.10676221977214259, 'mean_word_length': 1.7812979049565663, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '242924', 'license': 'MIT', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2371, 'n_words': 426, 'path': '1. Beginner/Pytorch7_1_RNN_Basic.ipynb', 'repo_name': 'gjustin40/Pytorch', 'size': '5442'}