<jupyter_start><jupyter_text>Master Pytorch 8 : Basic with Sequence- 앞에 진행했던 Basic을 Sequence로 입력해서 학습<jupyter_code>import torch

letters = ['h', 'i', 'e', 'l', 'o']

# hihell -> ihello

x_data = [0, 1, 0, 2, 3, 3]
y_data = [1, 0, 2, 3, 3, 4]

inputs = torch.zeros(1, len(x_data), len(letters))
for i, idx in enumerate(x_data):
    inputs[0][i][idx] = 1

labels = torch.LongTensor(y_data)

print(inputs)
print(labels)
import torch.nn as nn
import torch.optim as optim

class_n = 5
input_size = 5
hidden_size = 5
batch_size = 1
seq_n = 6
layer_n = 1

class RNN(nn.Module):
    
    def __init__(self, class_n, input_size, hidden_size, layer_n):
        super(RNN, self).__init__()
        
        self.class_n = class_n
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.layer_n = layer_n
        self.seq_n = seq_n
        
        self.rnn = nn.RNN(input_size = 5, hidden_size = 5,
                          batch_first = True)
        
    def forward(self, x):
        h0 = torch.zeros(self.layer_n, x.size(0), self.hidden_size) # (layer_nXdirection_n) X batch_size X hidden_size 
        x = x.view(self.seq_n, x.size(0), self.input_size) # input = (seq_n, batch_size, input_size)
        
        output, h0 = self.rnn(x, h0)
        
        return output.view(-1, class_n)               

model = RNN(class_n, input_size, hidden_size, layer_n)
loss_function = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr = 0.1)

print(model)
epoch_n = 100

for epoch in range(epoch_n):
    
    outputs = model(inputs)
    
    optimizer.zero_grad()
    loss = loss_function(outputs, labels)
    
    loss.backward()
    optimizer.step()
    
    val, idx = outputs.max(1)
    for i in idx.data:
        print(letters[i], end = '')
        
    print(' Epoch : %d/100, Loss : %1.3f' %(epoch + 1, loss))
for p in model.parameters():
    print(p.size())<jupyter_output>torch.Size([5, 5])
torch.Size([5, 5])
torch.Size([5])
torch.Size([5])<jupyter_text>스스로 해보기<jupyter_code># hihell -> ihello
import torch

letters = ['h', 'i', 'e', 'l', 'o']
x_data = [0, 1, 0, 2, 3, 3]
y_data = [1, 0, 2, 3, 3, 4]

inputs = torch.zeros(1, len(x_data), len(letters))
for i, idx in enumerate(x_data):
    inputs[0][i][idx] = 1
    
labels = torch.Tensor(y_data).long()

print(inputs)
print(labels)
import torch.nn as nn

class_n = 5
input_size = 5
hidden_size = 5
batch_size = 1
seq_n = 6
layer_n = 1

class RNN(nn.Module):
    
    def __init__(self, input_size, hidden_size, class_n, layer_n):
        super(RNN, self).__init__()
        
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.class_n = class_n
        self.layer_n = layer_n
        
        self.rnn = nn.RNN(input_size, hidden_size, batch_first = True)
        
    def forward(self, x):
        
        h0 = torch.zeros(layer_n, batch_size, self.hidden_size)
        x = x.view(batch_size, seq_n, self.input_size)
        
        output, _ = self.rnn(x, h0)
        
        return output.view(-1, self.class_n)
    
model = RNN(input_size, hidden_size, class_n, layer_n)
print(model)
import torch.optim as optim

optimizer = optim.SGD(model.parameters(), lr = 0.1)
loss_function = nn.CrossEntropyLoss()

epoch_n = 100
loss_list = []

for epoch in range(1, epoch_n):
    
    optimizer.zero_grad()
    loss = 0
    output = model(inputs)
    
    loss = loss_function(output, labels)
    loss.backward()
    optimizer.step()
    
    val, idx = output.max(1)
    for i in idx:
        print(letters[i], end = '')

    if epoch % 10 == 0:
        print(' Epoch : %d/100, Loss : %1.3f' %(epoch, loss))
    else:
        print('')
    loss_list.append(loss)
import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(loss_list)<jupyter_output><empty_output><jupyter_text>한글 버젼- 한글로 학습 시도- sequence의 길이를 늘려서 시도<jupyter_code>from konlpy.tag import Okt

okt = Okt()

my_string = '행복은 항상 미래에 있다'
letters = [val for val, pos in okt.pos(my_string)] + [' ']
print(letters, len(letters))

# 행복은 항상 미래에 -> 은 항상 미래에 있다
import torch

x_data = [0, 1, 6, 2, 6, 3, 4, 6] # 행복은 항상 미래에
y_data = [1, 6, 2, 6, 3, 4, 6, 5] # 은 항상 미래에 있다

inputs = torch.zeros(1, len(x_data), len(letters))
for i, idx in enumerate(x_data):
    inputs[0][i][idx] = 1

labels = torch.LongTensor(y_data)

print(inputs)
print(labels)
import torch.nn as nn
import torch.optim as optim

class_n = len(letters) # 7
input_size = len(letters) # 7
hidden_size = 7 # input_size와 같다
batch_size = 1
seq_n = 8
layer_n = 1

class RNN(nn.Module):
    
    def __init__(self, input_size, hidden_size, class_n, layer_n):
        super(RNN, self).__init__()
        
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.class_n = class_n
        self.layer_n = layer_n
        self.seq_n = seq_n
        
        self.rnn = nn.RNN(input_size = 7, hidden_size = 7, batch_first = True)
        
    def forward(self, x):
        h0 = torch.Tensor(self.layer_n, batch_size, hidden_size)
        x = x.view(batch_size, self.seq_n, -1)
        
        output, _ = self.rnn(x, h0)
        
        return output.view(-1, self.class_n)

model = RNN(input_size, hidden_size, class_n, layer_n)
print(model)
import torch.optim as optim

loss_function = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr = 0.1)

epoch_n = 100
loss_list = []

for epoch in range(1, epoch_n):
    optimizer.zero_grad()
    
    output = model(inputs)
    
    loss = loss_function(output, labels)
    loss.backward()
    optimizer.step()
    
    val, idx = output.max(1)
    for i in idx.data:
        print(letters[i], end = '')
    print(' Epoch : %d/100, Loss = %1.3f' %(epoch, loss))
    loss_list.append(loss)
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(loss_list)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13152472527472528, 'fraction_numerical': 0.022493131868131868, 'mean_word_length': 2.7751134154244976, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 15, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '626099', 'license': 'MIT', 'n_tokens_mistral': 2400, 'n_tokens_neox': 2354, 'n_words': 537, 'path': '1. Beginner/Pytorch8_RNN_Basic_Sequence.ipynb', 'repo_name': 'gjustin40/Pytorch', 'size': '5824'}