1
+ {
2
+ "cells" : [
3
+ {
4
+ "cell_type" : " code" ,
5
+ "metadata" : {},
6
+ "source" : [
7
+ " # code by Tae Hwan Jung @graykode\n " ,
8
+ " import numpy as np\n " ,
9
+ " import torch\n " ,
10
+ " import torch.nn as nn\n " ,
11
+ " import torch.optim as optim\n " ,
12
+ " import torch.nn.functional as F\n " ,
13
+ " \n " ,
14
+ " class TextCNN(nn.Module):\n " ,
15
+ " def __init__(self):\n " ,
16
+ " super(TextCNN, self).__init__()\n " ,
17
+ " self.num_filters_total = num_filters * len(filter_sizes)\n " ,
18
+ " self.W = nn.Embedding(vocab_size, embedding_size)\n " ,
19
+ " self.Weight = nn.Linear(self.num_filters_total, num_classes, bias=False)\n " ,
20
+ " self.Bias = nn.Parameter(torch.ones([num_classes]))\n " ,
21
+ " self.filter_list = nn.ModuleList([nn.Conv2d(1, num_filters, (size, embedding_size)) for size in filter_sizes])\n " ,
22
+ " \n " ,
23
+ " def forward(self, X):\n " ,
24
+ " embedded_chars = self.W(X) # [batch_size, sequence_length, sequence_length]\n " ,
25
+ " embedded_chars = embedded_chars.unsqueeze(1) # add channel(=1) [batch, channel(=1), sequence_length, embedding_size]\n " ,
26
+ " \n " ,
27
+ " pooled_outputs = []\n " ,
28
+ " for i, conv in enumerate(self.filter_list):\n " ,
29
+ " # conv : [input_channel(=1), output_channel(=3), (filter_height, filter_width), bias_option]\n " ,
30
+ " h = F.relu(conv(embedded_chars))\n " ,
31
+ " # mp : ((filter_height, filter_width))\n " ,
32
+ " mp = nn.MaxPool2d((sequence_length - filter_sizes[i] + 1, 1))\n " ,
33
+ " # pooled : [batch_size(=6), output_height(=1), output_width(=1), output_channel(=3)]\n " ,
34
+ " pooled = mp(h).permute(0, 3, 2, 1)\n " ,
35
+ " pooled_outputs.append(pooled)\n " ,
36
+ " \n " ,
37
+ " h_pool = torch.cat(pooled_outputs, len(filter_sizes)) # [batch_size(=6), output_height(=1), output_width(=1), output_channel(=3) * 3]\n " ,
38
+ " h_pool_flat = torch.reshape(h_pool, [-1, self.num_filters_total]) # [batch_size(=6), output_height * output_width * (output_channel * 3)]\n " ,
39
+ " model = self.Weight(h_pool_flat) + self.Bias # [batch_size, num_classes]\n " ,
40
+ " return model\n " ,
41
+ " \n " ,
42
+ " if __name__ == '__main__':\n " ,
43
+ " embedding_size = 2 # embedding size\n " ,
44
+ " sequence_length = 3 # sequence length\n " ,
45
+ " num_classes = 2 # number of classes\n " ,
46
+ " filter_sizes = [2, 2, 2] # n-gram windows\n " ,
47
+ " num_filters = 3 # number of filters\n " ,
48
+ " \n " ,
49
+ " # 3 words sentences (=sequence_length is 3)\n " ,
50
+ " sentences = [\" i love you\" , \" he loves me\" , \" she likes baseball\" , \" i hate you\" , \" sorry for that\" , \" this is awful\" ]\n " ,
51
+ " labels = [1, 1, 1, 0, 0, 0] # 1 is good, 0 is not good.\n " ,
52
+ " \n " ,
53
+ " word_list = \" \" .join(sentences).split()\n " ,
54
+ " word_list = list(set(word_list))\n " ,
55
+ " word_dict = {w: i for i, w in enumerate(word_list)}\n " ,
56
+ " vocab_size = len(word_dict)\n " ,
57
+ " \n " ,
58
+ " model = TextCNN()\n " ,
59
+ " \n " ,
60
+ " criterion = nn.CrossEntropyLoss()\n " ,
61
+ " optimizer = optim.Adam(model.parameters(), lr=0.001)\n " ,
62
+ " \n " ,
63
+ " inputs = torch.LongTensor([np.asarray([word_dict[n] for n in sen.split()]) for sen in sentences])\n " ,
64
+ " targets = torch.LongTensor([out for out in labels]) # To using Torch Softmax Loss function\n " ,
65
+ " \n " ,
66
+ " # Training\n " ,
67
+ " for epoch in range(5000):\n " ,
68
+ " optimizer.zero_grad()\n " ,
69
+ " output = model(inputs)\n " ,
70
+ " \n " ,
71
+ " # output : [batch_size, num_classes], target_batch : [batch_size] (LongTensor, not one-hot)\n " ,
72
+ " loss = criterion(output, targets)\n " ,
73
+ " if (epoch + 1) % 1000 == 0:\n " ,
74
+ " print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.6f}'.format(loss))\n " ,
75
+ " \n " ,
76
+ " loss.backward()\n " ,
77
+ " optimizer.step()\n " ,
78
+ " \n " ,
79
+ " # Test\n " ,
80
+ " test_text = 'sorry hate you'\n " ,
81
+ " tests = [np.asarray([word_dict[n] for n in test_text.split()])]\n " ,
82
+ " test_batch = torch.LongTensor(tests)\n " ,
83
+ " \n " ,
84
+ " # Predict\n " ,
85
+ " predict = model(test_batch).data.max(1, keepdim=True)[1]\n " ,
86
+ " if predict[0][0] == 0:\n " ,
87
+ " print(test_text,\" is Bad Mean...\" )\n " ,
88
+ " else:\n " ,
89
+ " print(test_text,\" is Good Mean!!\" )"
90
+ ],
91
+ "outputs" : [],
92
+ "execution_count" : null
93
+ }
94
+ ],
95
+ "metadata" : {
96
+ "anaconda-cloud" : {},
97
+ "kernelspec" : {
98
+ "display_name" : " Python 3" ,
99
+ "language" : " python" ,
100
+ "name" : " python3"
101
+ },
102
+ "language_info" : {
103
+ "codemirror_mode" : {
104
+ "name" : " ipython" ,
105
+ "version" : 3
106
+ },
107
+ "file_extension" : " .py" ,
108
+ "mimetype" : " text/x-python" ,
109
+ "name" : " python" ,
110
+ "nbconvert_exporter" : " python" ,
111
+ "pygments_lexer" : " ipython3" ,
112
+ "version" : " 3.6.1"
113
+ }
114
+ },
115
+ "nbformat" : 4 ,
116
+ "nbformat_minor" : 4
117
+ }
0 commit comments