bug fixes

This commit is contained in:
ritchie46
2018-12-07 12:37:33 +01:00
parent 04a3f555de
commit 71199dc24b

View File

@@ -601,21 +601,21 @@
" \n",
" pair = data.idx_pairs[i]\n",
"\n",
" eng_scentence = torch.tensor(pair[0], device=device)\n",
" fra_scentence = torch.tensor(pair[1], device=device)\n",
" eng_sentence = torch.tensor(pair[0], device=device)\n",
" fra_sentence = torch.tensor(pair[1], device=device)\n",
"\n",
" # Encode the input language\n",
" out, h = encoder(eng_scentence) \n",
" out, h = encoder(eng_sentence) \n",
" encoder_outputs = torch.zeros(max_length, out.shape[-1], device=device)\n",
" \n",
" if decoder.decoder == 'attention':\n",
" encoder_outputs[:out.shape[0], :out.shape[-1]] = out.view(out.shape[0], -1)\n",
"\n",
" teacher_forcing = np.random.rand() < teacher_forcing_ratio\n",
" loss = run_decoder(decoder, criterion, fra_scentence, h, teacher_forcing, encoder_outputs)\n",
" loss = run_decoder(decoder, criterion, fra_sentence, h, teacher_forcing, encoder_outputs)\n",
"\n",
" loss.backward()\n",
" writer.add_scalar('loss', loss.cpu().item() / (i + 1))\n",
" writer.add_scalar('loss', loss.cpu().item() / (len(fra_sentence)))\n",
"\n",
" optim_decoder.step()\n",
" optim_encoder.step()\n",
@@ -631,7 +631,7 @@
"metadata": {},
"outputs": [],
"source": [
"def translate( start, end):\n",
"def translate(start, end):\n",
" \n",
" for i in range(start, end):\n",
" \n",
@@ -658,299 +658,7 @@
"\n",
" if word.item() == 1: # <EOS>\n",
" break\n",
" print('\\nModel translation:\\t', ' '.join([eng.index2word[i] for i in translation][:-1]), '\\n\\n')\n",
" \n",
"translate(20, 60)"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"English scentence:\t i m pretty busy\n",
"French scentence:\t je suis plutot occupe\n",
"\n",
"Model translation:\t i m pretty busy \n",
"\n",
"\n",
"English scentence:\t she sang better than him\n",
"French scentence:\t elle chanta mieux que lui\n",
"\n",
"Model translation:\t she sang better than him \n",
"\n",
"\n",
"English scentence:\t i m all for that\n",
"French scentence:\t je suis tout a fait pour\n",
"\n",
"Model translation:\t i m all for that \n",
"\n",
"\n",
"English scentence:\t they re right behind me\n",
"French scentence:\t ils se trouvent juste derriere moi\n",
"\n",
"Model translation:\t they re right behind me \n",
"\n",
"\n",
"English scentence:\t you re very funny\n",
"French scentence:\t vous etes fort droles\n",
"\n",
"Model translation:\t you re very funny \n",
"\n",
"\n",
"English scentence:\t you re very rude\n",
"French scentence:\t vous etes fort grossier\n",
"\n",
"Model translation:\t you re very rude \n",
"\n",
"\n",
"English scentence:\t he s the class clown\n",
"French scentence:\t c est le pitre de la classe\n",
"\n",
"Model translation:\t he s the class \n",
"\n",
"\n",
"English scentence:\t they re not always right\n",
"French scentence:\t elles n ont pas toujours raison\n",
"\n",
"Model translation:\t they re not always right \n",
"\n",
"\n",
"English scentence:\t we re all safe\n",
"French scentence:\t nous sommes toutes en securite\n",
"\n",
"Model translation:\t we re all safe \n",
"\n",
"\n",
"English scentence:\t i m smarter than you\n",
"French scentence:\t je suis plus astucieux que vous\n",
"\n",
"Model translation:\t i m smarter than you \n",
"\n",
"\n",
"English scentence:\t i m still your friend\n",
"French scentence:\t je suis toujours votre amie\n",
"\n",
"Model translation:\t i m still your friend \n",
"\n",
"\n",
"English scentence:\t i m asking you for your help\n",
"French scentence:\t je requiers ton aide\n",
"\n",
"Model translation:\t i m asking you \n",
"\n",
"\n",
"English scentence:\t i m used to staying up late\n",
"French scentence:\t je suis habitue a rester debout tard\n",
"\n",
"Model translation:\t i m used to staying up late \n",
"\n",
"\n",
"English scentence:\t you re very helpful\n",
"French scentence:\t vous etes fort serviables\n",
"\n",
"Model translation:\t you re very helpful \n",
"\n",
"\n",
"English scentence:\t you re the reason i came\n",
"French scentence:\t c est pour vous que je suis venu\n",
"\n",
"Model translation:\t you re the reason i came \n",
"\n",
"\n",
"English scentence:\t you re respected by everybody\n",
"French scentence:\t vous etes respecte de tous\n",
"\n",
"Model translation:\t you re respected by everybody \n",
"\n",
"\n",
"English scentence:\t i am singing with my children\n",
"French scentence:\t je chante avec mes enfants\n",
"\n",
"Model translation:\t i am singing with my \n",
"\n",
"\n",
"English scentence:\t i m sorry i don t buy it\n",
"French scentence:\t je suis desolee je ne gobe pas ca\n",
"\n",
"Model translation:\t i m sorry i don t buy it \n",
"\n",
"\n",
"English scentence:\t i m not tired yet\n",
"French scentence:\t je ne suis pas encore fatigue\n",
"\n",
"Model translation:\t i m still tired yet \n",
"\n",
"\n",
"English scentence:\t you re so pathetic\n",
"French scentence:\t tu es si pitoyable\n",
"\n",
"Model translation:\t you re so pathetic \n",
"\n",
"\n",
"English scentence:\t i m not a teacher\n",
"French scentence:\t je ne suis pas instituteur\n",
"\n",
"Model translation:\t i m not a teacher \n",
"\n",
"\n",
"English scentence:\t he is far from happy\n",
"French scentence:\t il n est vraiment pas heureux\n",
"\n",
"Model translation:\t he is far from happy \n",
"\n",
"\n",
"English scentence:\t i m worried about them\n",
"French scentence:\t je me fais du souci pour elles\n",
"\n",
"Model translation:\t i m worried about them \n",
"\n",
"\n",
"English scentence:\t she is not wrong\n",
"French scentence:\t elle n a pas tort\n",
"\n",
"Model translation:\t she s not wrong \n",
"\n",
"\n",
"English scentence:\t i am very dangerous\n",
"French scentence:\t je suis vraiment dangereux\n",
"\n",
"Model translation:\t i am very dangerous \n",
"\n",
"\n",
"English scentence:\t he is washing a car\n",
"French scentence:\t il nettoie une voiture\n",
"\n",
"Model translation:\t he is washing a \n",
"\n",
"\n",
"English scentence:\t we are concerned about our planet\n",
"French scentence:\t nous nous occupons de notre planete\n",
"\n",
"Model translation:\t we are concerned about our planet \n",
"\n",
"\n",
"English scentence:\t she isn t lonely now\n",
"French scentence:\t elle n est plus seule maintenant\n",
"\n",
"Model translation:\t she isn t lonely now \n",
"\n",
"\n",
"English scentence:\t they re all waiting\n",
"French scentence:\t elles attendent toutes\n",
"\n",
"Model translation:\t they re all \n",
"\n",
"\n",
"English scentence:\t she s been sick since last wednesday\n",
"French scentence:\t elle est malade depuis mercredi dernier\n",
"\n",
"Model translation:\t she s been sick since last \n",
"\n",
"\n",
"English scentence:\t we are listening to the radio\n",
"French scentence:\t nous sommes en train d ecouter la radio\n",
"\n",
"Model translation:\t we are listening to the radio \n",
"\n",
"\n",
"English scentence:\t i m sorry to have bothered you\n",
"French scentence:\t je suis desolee de t avoir derange\n",
"\n",
"Model translation:\t i m sorry to have bothered you \n",
"\n",
"\n",
"English scentence:\t i m not at all afraid\n",
"French scentence:\t je n ai pas peur du tout\n",
"\n",
"Model translation:\t i m not at at all \n",
"\n",
"\n",
"English scentence:\t he is pleased with his new shoes\n",
"French scentence:\t il est content de ses nouvelles chaussures\n",
"\n",
"Model translation:\t he is pleased with his new shoes \n",
"\n",
"\n",
"English scentence:\t she is a very intelligent young lady\n",
"French scentence:\t c est une jeune femme tres intelligente\n",
"\n",
"Model translation:\t she is a very intelligent young lady \n",
"\n",
"\n",
"English scentence:\t we are sorry for the inconvenience\n",
"French scentence:\t nous sommes desoles pour le derangement\n",
"\n",
"Model translation:\t we are sorry for the interruption \n",
"\n",
"\n",
"English scentence:\t i am not studying now\n",
"French scentence:\t en ce moment je n etudie pas\n",
"\n",
"Model translation:\t i am not studying now \n",
"\n",
"\n",
"English scentence:\t you re not alone\n",
"French scentence:\t vous n etes pas seule\n",
"\n",
"Model translation:\t you re not alone \n",
"\n",
"\n",
"English scentence:\t he is clumsy with his hands\n",
"French scentence:\t il est maladroit de ses mains\n",
"\n",
"Model translation:\t he is clumsy with his hands \n",
"\n",
"\n",
"English scentence:\t i am too tired to run\n",
"French scentence:\t je suis trop fatigue pour courir\n",
"\n",
"Model translation:\t i am too tired to run \n",
"\n",
"\n"
]
}
],
"source": [
"def translate( start, end):\n",
" \n",
" for i in range(start, end):\n",
" \n",
" pair = data.idx_pairs[i]\n",
" eng_scentence = torch.tensor(pair[0], device=device)\n",
" fra_scentence = torch.tensor(pair[1], device=device)\n",
"\n",
" print('English scentence:\\t', ' '.join([eng.index2word[i] for i in eng_scentence.cpu().data.numpy()][:-1]))\n",
" print('French scentence:\\t', ' '.join([fra.index2word[i] for i in fra_scentence.cpu().data.numpy()][:-1]))\n",
"\n",
" # Encode the input language\n",
" out, h = encoder(fra_scentence) \n",
" encoder_outputs = torch.zeros(max_length, out.shape[-1], device=device)\n",
" encoder_outputs[:out.shape[0], :out.shape[-1]] = out.view(out.shape[0], -1)\n",
" \n",
" word = torch.tensor([0], device=device) # <SOS>\n",
" \n",
" translation = []\n",
" for j in range(fra_scentence.shape[0]):\n",
" x, h = decoder(word, h, encoder_outputs=encoder_outputs)\n",
" \n",
" word = x.argmax().detach()\n",
" translation.append(word.cpu().data.tolist())\n",
"\n",
" if word.item() == 1: # <EOS>\n",
" break\n",
" print('\\nModel translation:\\t', ' '.join([eng.index2word[i] for i in translation][:-1]), '\\n\\n')\n",
" print('\\nModel translation:\\t', ' '.join([fra.index2word[i] for i in translation][:-1]), '\\n\\n')\n",
" \n",
"translate(20, 60)"
]