Skip to content

Commit

Permalink
Automatic convert from py to ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
graykode committed Jul 25, 2021
1 parent 93dde25 commit d05e31e
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions 4-1.Seq2Seq/Seq2Seq.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"metadata": {},
"source": [
"# code by Tae Hwan Jung @graykode\n",
"import argparse\n",
"import numpy as np\n",
"import torch\n",
"import torch.nn as nn\n",
Expand All @@ -32,6 +31,19 @@
" # make tensor\n",
" return torch.FloatTensor(input_batch), torch.FloatTensor(output_batch), torch.LongTensor(target_batch)\n",
"\n",
"# make test batch\n",
"def make_testbatch(input_word):\n",
" input_batch, output_batch = [], []\n",
"\n",
" input_w = input_word + 'P' * (n_step - len(input_word))\n",
" input = [num_dic[n] for n in input_w]\n",
" output = [num_dic[n] for n in 'S' + 'P' * n_step]\n",
"\n",
" input_batch = np.eye(n_class)[input]\n",
" output_batch = np.eye(n_class)[output]\n",
"\n",
" return torch.FloatTensor(input_batch).unsqueeze(0), torch.FloatTensor(output_batch).unsqueeze(0)\n",
"\n",
"# Model\n",
"class Seq2Seq(nn.Module):\n",
" def __init__(self):\n",
Expand Down Expand Up @@ -92,11 +104,11 @@
" optimizer.step()\n",
"\n",
" # Test\n",
" def translate(word, args):\n",
" input_batch, output_batch, _ = make_batch([[word, 'P' * len(word)]], args)\n",
" def translate(word):\n",
" input_batch, output_batch = make_testbatch(word)\n",
"\n",
" # make hidden shape [num_layers * num_directions, batch_size, n_hidden]\n",
" hidden = torch.zeros(1, 1, args.n_hidden)\n",
" hidden = torch.zeros(1, 1, n_hidden)\n",
" output = model(input_batch, hidden, output_batch)\n",
" # output : [max_len+1(=6), batch_size(=1), n_class]\n",
"\n",
Expand Down

0 comments on commit d05e31e

Please sign in to comment.