Skip to content

Commit f97cef9

Browse files
committed
Automatic convert from py to ipynb
1 parent 3bd37d8 commit f97cef9

File tree

12 files changed

+1908
-0
lines changed

12 files changed

+1908
-0
lines changed

Diff for: 1-1.NNLM/NNLM.ipynb

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"metadata": {},
6+
"source": [
7+
"# code by Tae Hwan Jung @graykode\n",
8+
"import torch\n",
9+
"import torch.nn as nn\n",
10+
"import torch.optim as optim\n",
11+
"\n",
12+
"def make_batch():\n",
13+
" input_batch = []\n",
14+
" target_batch = []\n",
15+
"\n",
16+
" for sen in sentences:\n",
17+
" word = sen.split() # space tokenizer\n",
18+
" input = [word_dict[n] for n in word[:-1]] # create (1~n-1) as input\n",
19+
" target = word_dict[word[-1]] # create (n) as target, We usually call this 'casual language model'\n",
20+
"\n",
21+
" input_batch.append(input)\n",
22+
" target_batch.append(target)\n",
23+
"\n",
24+
" return input_batch, target_batch\n",
25+
"\n",
26+
"# Model\n",
27+
"class NNLM(nn.Module):\n",
28+
" def __init__(self):\n",
29+
" super(NNLM, self).__init__()\n",
30+
" self.C = nn.Embedding(n_class, m)\n",
31+
" self.H = nn.Linear(n_step * m, n_hidden, bias=False)\n",
32+
" self.d = nn.Parameter(torch.ones(n_hidden))\n",
33+
" self.U = nn.Linear(n_hidden, n_class, bias=False)\n",
34+
" self.W = nn.Linear(n_step * m, n_class, bias=False)\n",
35+
" self.b = nn.Parameter(torch.ones(n_class))\n",
36+
"\n",
37+
" def forward(self, X):\n",
38+
" X = self.C(X) # X : [batch_size, n_step, n_class]\n",
39+
" X = X.view(-1, n_step * m) # [batch_size, n_step * n_class]\n",
40+
" tanh = torch.tanh(self.d + self.H(X)) # [batch_size, n_hidden]\n",
41+
" output = self.b + self.W(X) + self.U(tanh) # [batch_size, n_class]\n",
42+
" return output\n",
43+
"\n",
44+
"if __name__ == '__main__':\n",
45+
" n_step = 2 # number of steps, n-1 in paper\n",
46+
" n_hidden = 2 # number of hidden size, h in paper\n",
47+
" m = 2 # embedding size, m in paper\n",
48+
"\n",
49+
" sentences = [\"i like dog\", \"i love coffee\", \"i hate milk\"]\n",
50+
"\n",
51+
" word_list = \" \".join(sentences).split()\n",
52+
" word_list = list(set(word_list))\n",
53+
" word_dict = {w: i for i, w in enumerate(word_list)}\n",
54+
" number_dict = {i: w for i, w in enumerate(word_list)}\n",
55+
" n_class = len(word_dict) # number of Vocabulary\n",
56+
"\n",
57+
" model = NNLM()\n",
58+
"\n",
59+
" criterion = nn.CrossEntropyLoss()\n",
60+
" optimizer = optim.Adam(model.parameters(), lr=0.001)\n",
61+
"\n",
62+
" input_batch, target_batch = make_batch()\n",
63+
" input_batch = torch.LongTensor(input_batch)\n",
64+
" target_batch = torch.LongTensor(target_batch)\n",
65+
"\n",
66+
" # Training\n",
67+
" for epoch in range(5000):\n",
68+
" optimizer.zero_grad()\n",
69+
" output = model(input_batch)\n",
70+
"\n",
71+
" # output : [batch_size, n_class], target_batch : [batch_size]\n",
72+
" loss = criterion(output, target_batch)\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+
" # Predict\n",
80+
" predict = model(input_batch).data.max(1, keepdim=True)[1]\n",
81+
"\n",
82+
" # Test\n",
83+
" print([sen.split()[:2] for sen in sentences], '->', [number_dict[n.item()] for n in predict.squeeze()])"
84+
],
85+
"outputs": [],
86+
"execution_count": null
87+
}
88+
],
89+
"metadata": {
90+
"anaconda-cloud": {},
91+
"kernelspec": {
92+
"display_name": "Python 3",
93+
"language": "python",
94+
"name": "python3"
95+
},
96+
"language_info": {
97+
"codemirror_mode": {
98+
"name": "ipython",
99+
"version": 3
100+
},
101+
"file_extension": ".py",
102+
"mimetype": "text/x-python",
103+
"name": "python",
104+
"nbconvert_exporter": "python",
105+
"pygments_lexer": "ipython3",
106+
"version": "3.6.1"
107+
}
108+
},
109+
"nbformat": 4,
110+
"nbformat_minor": 4
111+
}

Diff for: 1-2.Word2Vec/Word2Vec-Skipgram(Softmax).ipynb

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 matplotlib.pyplot as plt\n",
13+
"\n",
14+
"def random_batch():\n",
15+
" random_inputs = []\n",
16+
" random_labels = []\n",
17+
" random_index = np.random.choice(range(len(skip_grams)), batch_size, replace=False)\n",
18+
"\n",
19+
" for i in random_index:\n",
20+
" random_inputs.append(np.eye(voc_size)[skip_grams[i][0]]) # target\n",
21+
" random_labels.append(skip_grams[i][1]) # context word\n",
22+
"\n",
23+
" return random_inputs, random_labels\n",
24+
"\n",
25+
"# Model\n",
26+
"class Word2Vec(nn.Module):\n",
27+
" def __init__(self):\n",
28+
" super(Word2Vec, self).__init__()\n",
29+
" # W and WT is not Traspose relationship\n",
30+
" self.W = nn.Linear(voc_size, embedding_size, bias=False) # voc_size > embedding_size Weight\n",
31+
" self.WT = nn.Linear(embedding_size, voc_size, bias=False) # embedding_size > voc_size Weight\n",
32+
"\n",
33+
" def forward(self, X):\n",
34+
" # X : [batch_size, voc_size]\n",
35+
" hidden_layer = self.W(X) # hidden_layer : [batch_size, embedding_size]\n",
36+
" output_layer = self.WT(hidden_layer) # output_layer : [batch_size, voc_size]\n",
37+
" return output_layer\n",
38+
"\n",
39+
"if __name__ == '__main__':\n",
40+
" batch_size = 2 # mini-batch size\n",
41+
" embedding_size = 2 # embedding size\n",
42+
"\n",
43+
" sentences = [\"apple banana fruit\", \"banana orange fruit\", \"orange banana fruit\",\n",
44+
" \"dog cat animal\", \"cat monkey animal\", \"monkey dog animal\"]\n",
45+
"\n",
46+
" word_sequence = \" \".join(sentences).split()\n",
47+
" word_list = \" \".join(sentences).split()\n",
48+
" word_list = list(set(word_list))\n",
49+
" word_dict = {w: i for i, w in enumerate(word_list)}\n",
50+
" voc_size = len(word_list)\n",
51+
"\n",
52+
" # Make skip gram of one size window\n",
53+
" skip_grams = []\n",
54+
" for i in range(1, len(word_sequence) - 1):\n",
55+
" target = word_dict[word_sequence[i]]\n",
56+
" context = [word_dict[word_sequence[i - 1]], word_dict[word_sequence[i + 1]]]\n",
57+
" for w in context:\n",
58+
" skip_grams.append([target, w])\n",
59+
"\n",
60+
" model = Word2Vec()\n",
61+
"\n",
62+
" criterion = nn.CrossEntropyLoss()\n",
63+
" optimizer = optim.Adam(model.parameters(), lr=0.001)\n",
64+
"\n",
65+
" # Training\n",
66+
" for epoch in range(5000):\n",
67+
" input_batch, target_batch = random_batch()\n",
68+
" input_batch = torch.Tensor(input_batch)\n",
69+
" target_batch = torch.LongTensor(target_batch)\n",
70+
"\n",
71+
" optimizer.zero_grad()\n",
72+
" output = model(input_batch)\n",
73+
"\n",
74+
" # output : [batch_size, voc_size], target_batch : [batch_size] (LongTensor, not one-hot)\n",
75+
" loss = criterion(output, target_batch)\n",
76+
" if (epoch + 1) % 1000 == 0:\n",
77+
" print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.6f}'.format(loss))\n",
78+
"\n",
79+
" loss.backward()\n",
80+
" optimizer.step()\n",
81+
"\n",
82+
" for i, label in enumerate(word_list):\n",
83+
" W, WT = model.parameters()\n",
84+
" x, y = W[0][i].item(), W[1][i].item()\n",
85+
" plt.scatter(x, y)\n",
86+
" plt.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points', ha='right', va='bottom')\n",
87+
" plt.show()\n"
88+
],
89+
"outputs": [],
90+
"execution_count": null
91+
}
92+
],
93+
"metadata": {
94+
"anaconda-cloud": {},
95+
"kernelspec": {
96+
"display_name": "Python 3",
97+
"language": "python",
98+
"name": "python3"
99+
},
100+
"language_info": {
101+
"codemirror_mode": {
102+
"name": "ipython",
103+
"version": 3
104+
},
105+
"file_extension": ".py",
106+
"mimetype": "text/x-python",
107+
"name": "python",
108+
"nbconvert_exporter": "python",
109+
"pygments_lexer": "ipython3",
110+
"version": "3.6.1"
111+
}
112+
},
113+
"nbformat": 4,
114+
"nbformat_minor": 4
115+
}

Diff for: 2-1.TextCNN/TextCNN.ipynb

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)