chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:45:52 +08:00
commit aa6c9c0720
43 changed files with 5178 additions and 0 deletions
+115
View File
@@ -0,0 +1,115 @@
{
"cells": [
{
"cell_type": "code",
"metadata": {},
"source": [
"# code by Tae Hwan Jung @graykode\n",
"import numpy as np\n",
"import torch\n",
"import torch.nn as nn\n",
"import torch.optim as optim\n",
"\n",
"def make_batch():\n",
" input_batch = []\n",
" target_batch = []\n",
"\n",
" for sen in sentences:\n",
" word = sen.split() # space tokenizer\n",
" input = [word_dict[n] for n in word[:-1]] # create (1~n-1) as input\n",
" target = word_dict[word[-1]] # create (n) as target, We usually call this 'casual language model'\n",
"\n",
" input_batch.append(np.eye(n_class)[input])\n",
" target_batch.append(target)\n",
"\n",
" return input_batch, target_batch\n",
"\n",
"class TextRNN(nn.Module):\n",
" def __init__(self):\n",
" super(TextRNN, self).__init__()\n",
" self.rnn = nn.RNN(input_size=n_class, hidden_size=n_hidden)\n",
" self.W = nn.Linear(n_hidden, n_class, bias=False)\n",
" self.b = nn.Parameter(torch.ones([n_class]))\n",
"\n",
" def forward(self, hidden, X):\n",
" X = X.transpose(0, 1) # X : [n_step, batch_size, n_class]\n",
" outputs, hidden = self.rnn(X, hidden)\n",
" # outputs : [n_step, batch_size, num_directions(=1) * n_hidden]\n",
" # hidden : [num_layers(=1) * num_directions(=1), batch_size, n_hidden]\n",
" outputs = outputs[-1] # [batch_size, num_directions(=1) * n_hidden]\n",
" model = self.W(outputs) + self.b # model : [batch_size, n_class]\n",
" return model\n",
"\n",
"if __name__ == '__main__':\n",
" n_step = 2 # number of cells(= number of Step)\n",
" n_hidden = 5 # number of hidden units in one cell\n",
"\n",
" sentences = [\"i like dog\", \"i love coffee\", \"i hate milk\"]\n",
"\n",
" word_list = \" \".join(sentences).split()\n",
" word_list = list(set(word_list))\n",
" word_dict = {w: i for i, w in enumerate(word_list)}\n",
" number_dict = {i: w for i, w in enumerate(word_list)}\n",
" n_class = len(word_dict)\n",
" batch_size = len(sentences)\n",
"\n",
" model = TextRNN()\n",
"\n",
" criterion = nn.CrossEntropyLoss()\n",
" optimizer = optim.Adam(model.parameters(), lr=0.001)\n",
"\n",
" input_batch, target_batch = make_batch()\n",
" input_batch = torch.FloatTensor(input_batch)\n",
" target_batch = torch.LongTensor(target_batch)\n",
"\n",
" # Training\n",
" for epoch in range(5000):\n",
" optimizer.zero_grad()\n",
"\n",
" # hidden : [num_layers * num_directions, batch, hidden_size]\n",
" hidden = torch.zeros(1, batch_size, n_hidden)\n",
" # input_batch : [batch_size, n_step, n_class]\n",
" output = model(hidden, input_batch)\n",
"\n",
" # output : [batch_size, n_class], target_batch : [batch_size] (LongTensor, not one-hot)\n",
" loss = criterion(output, target_batch)\n",
" if (epoch + 1) % 1000 == 0:\n",
" print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.6f}'.format(loss))\n",
"\n",
" loss.backward()\n",
" optimizer.step()\n",
"\n",
" input = [sen.split()[:2] for sen in sentences]\n",
"\n",
" # Predict\n",
" hidden = torch.zeros(1, batch_size, n_hidden)\n",
" predict = model(hidden, input_batch).data.max(1, keepdim=True)[1]\n",
" print([sen.split()[:2] for sen in sentences], '->', [number_dict[n.item()] for n in predict.squeeze()])"
],
"outputs": [],
"execution_count": null
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
+82
View File
@@ -0,0 +1,82 @@
# %%
# code by Tae Hwan Jung @graykode
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
def make_batch():
input_batch = []
target_batch = []
for sen in sentences:
word = sen.split() # space tokenizer
input = [word_dict[n] for n in word[:-1]] # create (1~n-1) as input
target = word_dict[word[-1]] # create (n) as target, We usually call this 'casual language model'
input_batch.append(np.eye(n_class)[input])
target_batch.append(target)
return input_batch, target_batch
class TextRNN(nn.Module):
def __init__(self):
super(TextRNN, self).__init__()
self.rnn = nn.RNN(input_size=n_class, hidden_size=n_hidden)
self.W = nn.Linear(n_hidden, n_class, bias=False)
self.b = nn.Parameter(torch.ones([n_class]))
def forward(self, hidden, X):
X = X.transpose(0, 1) # X : [n_step, batch_size, n_class]
outputs, hidden = self.rnn(X, hidden)
# outputs : [n_step, batch_size, num_directions(=1) * n_hidden]
# hidden : [num_layers(=1) * num_directions(=1), batch_size, n_hidden]
outputs = outputs[-1] # [batch_size, num_directions(=1) * n_hidden]
model = self.W(outputs) + self.b # model : [batch_size, n_class]
return model
if __name__ == '__main__':
n_step = 2 # number of cells(= number of Step)
n_hidden = 5 # number of hidden units in one cell
sentences = ["i like dog", "i love coffee", "i hate milk"]
word_list = " ".join(sentences).split()
word_list = list(set(word_list))
word_dict = {w: i for i, w in enumerate(word_list)}
number_dict = {i: w for i, w in enumerate(word_list)}
n_class = len(word_dict)
batch_size = len(sentences)
model = TextRNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
input_batch, target_batch = make_batch()
input_batch = torch.FloatTensor(input_batch)
target_batch = torch.LongTensor(target_batch)
# Training
for epoch in range(5000):
optimizer.zero_grad()
# hidden : [num_layers * num_directions, batch, hidden_size]
hidden = torch.zeros(1, batch_size, n_hidden)
# input_batch : [batch_size, n_step, n_class]
output = model(hidden, input_batch)
# output : [batch_size, n_class], target_batch : [batch_size] (LongTensor, not one-hot)
loss = criterion(output, target_batch)
if (epoch + 1) % 1000 == 0:
print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.6f}'.format(loss))
loss.backward()
optimizer.step()
input = [sen.split()[:2] for sen in sentences]
# Predict
hidden = torch.zeros(1, batch_size, n_hidden)
predict = model(hidden, input_batch).data.max(1, keepdim=True)[1]
print([sen.split()[:2] for sen in sentences], '->', [number_dict[n.item()] for n in predict.squeeze()])