Skip to content

Commit a8eacb7

Browse files
committed
clear TextCNN
1 parent 26ae49e commit a8eacb7

File tree

4 files changed

+50
-28
lines changed

4 files changed

+50
-28
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

2-1.TextCNN/TextCNN-Tensor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
pooled_outputs.append(pooled) # dim of pooled : [batch_size(=6), output_height(=1), output_width(=1), channel(=3)]
5959

6060
num_filters_total = num_filters * len(filter_sizes)
61-
h_pool = tf.concat(pooled_outputs, num_filters) # h_pool : [batch_size(=6), output_height(=1), output_width(=1), output_channel(=3) * 3]
61+
h_pool = tf.concat(pooled_outputs, len(filter_sizes)) # h_pool : [batch_size(=6), output_height(=1), output_width(=1), output_channel(=3) * 3]
6262
h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) # [batch_size, output_height * output_width * (channel * 3)]
6363

6464
# Model-Training

2-1.TextCNN/TextCNN-Torch.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ def forward(self, X):
5353

5454
pooled_outputs = []
5555
for filter_size in filter_sizes:
56-
# conv : [input_channel(=1), output_channel(=3), (filter_hleight, filter_width), bias_option]
56+
# conv : [input_channel(=1), output_channel(=3), (filter_height, filter_width), bias_option]
5757
conv = nn.Conv2d(1, num_filters, (filter_size, embedding_size), bias=True)(embedded_chars)
5858
h = F.relu(conv)
59-
# mp : ((filter_hleight, filter_width))
59+
# mp : ((filter_height, filter_width))
6060
mp = nn.MaxPool2d((sequence_length - filter_size + 1, 1))
6161
# pooled : [batch_size(=6), output_height(=1), output_width(=1), output_channel(=3)]
6262
pooled = mp(h).permute(0, 3, 2, 1)
6363
pooled_outputs.append(pooled)
6464

65-
h_pool = torch.cat(pooled_outputs, num_filters) # [batch_size(=6), output_height(=1), output_width(=1), output_channel(=3) * 3]
65+
h_pool = torch.cat(pooled_outputs, len(filter_sizes)) # [batch_size(=6), output_height(=1), output_width(=1), output_channel(=3) * 3]
6666
h_pool_flat = torch.reshape(h_pool, [-1, self.num_filters_total]) # [batch_size(=6), output_height * output_width * (output_channel * 3)]
6767

6868
model = torch.mm(h_pool_flat, self.Weight) + self.Bias # [batch_size, num_classes]

4-2.Attention/Attention-Torch.py

+45-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'''
2-
code by Tae Hwan Jung(Jeff Jung) @graykode
3-
'''
1+
# code by Tae Hwan Jung(Jeff Jung) @graykode
42
import numpy as np
53
import torch
64
import torch.nn as nn
@@ -10,35 +8,58 @@
108
# S: Symbol that shows starting of decoding input
119
# E: Symbol that shows starting of decoding output
1210
# P: Symbol that will fill in blank sequence if current batch data size is short than time steps
11+
sentences = ['ich mochte ein bier P', 'S i want a beer', 'i want a beer E']
1312

14-
char_arr = [c for c in 'SEPabcdefghijklmnopqrstuvwxyz ']
15-
num_dic = {n: i for i, n in enumerate(char_arr)}
16-
dic_len = len(num_dic)
13+
word_list = " ".join(sentences).split()
14+
word_list = list(set(word_list))
15+
word_dict = {w: i for i, w in enumerate(word_list)}
16+
n_class = len(word_dict)
1717

1818
# Parameter
19-
max_len = 20
19+
max_len = 5 # 'S' or 'E' will be added (= n_step,seq_len)
2020
n_hidden = 128
21-
total_epoch = 10000
22-
n_class = dic_len
21+
batch_size = 1
2322

24-
seq_data = [['Ich mochte ein bier', 'I want a BEER']]
23+
def make_batch(sentences):
24+
input_batch = [np.eye(n_class)[[word_dict[n] for n in sentences[0].split()]]]
25+
output_batch = [np.eye(n_class)[[word_dict[n] for n in sentences[1].split()]]]
26+
target_batch = [[word_dict[n] for n in sentences[2].split()]]
2527

26-
def make_batch(seq_data):
27-
input_batch = []
28-
output_batch = []
29-
target_batch = []
28+
# make tensor
29+
return Variable(torch.Tensor(input_batch)), Variable(torch.Tensor(output_batch)), Variable(torch.LongTensor(target_batch))
30+
31+
class Attention(nn.Module):
32+
def __init__(self):
33+
super(Attention, self).__init__()
34+
self.enc_cell = nn.RNN(input_size=n_class, hidden_size=n_hidden, dropout=0.5)
35+
self.dec_cell = nn.RNN(input_size=n_class, hidden_size=n_hidden, dropout=0.5)
36+
37+
# Linear for attention
38+
self.attn = nn.Linear(n_hidden, n_hidden)
39+
40+
def forward(self, enc_input, hidden, dec_input):
41+
enc_input = enc_input.transpose(0, 1) # enc_input: [max_len(=n_step, time step), batch_size, n_hidden]
42+
dec_input = dec_input.transpose(0, 1) # dec_input: [max_len(=n_step, time step), batch_size, n_hidden]
3043

31-
for seq in seq_data:
32-
for i in range(2):
33-
seq[i] = seq[i] + 'P' * (max_len - len(seq[i]))
44+
# enc_outputs : [max_len, batch_size, num_directions(=1) * n_hidden(=1)]
45+
# enc_states : [num_layers(=1) * num_directions(=1), batch_size, n_hidden]
46+
enc_outputs, enc_states = self.enc_cell(enc_input, hidden)
47+
dec_outputs, _ = self.dec_cell(dec_input, enc_states)
3448

35-
input = [num_dic[n] for n in seq[0]]
36-
output = [num_dic[n] for n in ('S' + seq[1])]
37-
target = [num_dic[n] for n in (seq[1] + 'E')]
49+
50+
return dec_outputs
51+
52+
def get_att_weight(self, hidden, enc_outputs):
53+
attn_scores = Variable(torch.zeros(len(enc_outputs))) # attn_scores : [n_step]
3854

39-
input_batch.append(np.eye(dic_len)[input])
40-
output_batch.append(np.eye(dic_len)[output])
55+
def get_att_score(self, hidden, encoder_hidden):
56+
score = self.attn(encoder_hidden)
57+
return torch.dot(hidden.view(-1), score.view(-1))
4158

42-
target_batch.append(target)
59+
input_batch, output_batch, target_batch = make_batch(sentences)
4360

44-
return input_batch, output_batch, target_batch
61+
# hidden : [num_layers(=1) * num_directions(=1), batch_size, n_hidden]
62+
hidden = Variable(torch.zeros(1, 1, n_hidden))
63+
64+
model = Attention()
65+
output = model(input_batch, hidden, output_batch)

0 commit comments

Comments
 (0)