From 0930ed759b5035b47171dbf288ec33b6653b9eb8 Mon Sep 17 00:00:00 2001 From: ritchie46 Date: Fri, 7 Dec 2018 12:55:42 +0100 Subject: [PATCH] bug fixes --- seq2seq/attention_decoder.ipynb | 390 ++++++++++++++++++++++++++------ 1 file changed, 327 insertions(+), 63 deletions(-) diff --git a/seq2seq/attention_decoder.ipynb b/seq2seq/attention_decoder.ipynb index bef0ead..66f7ec7 100644 --- a/seq2seq/attention_decoder.ipynb +++ b/seq2seq/attention_decoder.ipynb @@ -22,7 +22,7 @@ "import os\n", "from tensorboardX import SummaryWriter\n", "\n", - "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", + "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", "print(device)" ] }, @@ -30,39 +30,7 @@ "cell_type": "code", "execution_count": 2, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " % Total % Received % Xferd Average Speed Time Time Time Current\n", - " Dload Upload Total Spent Left Speed\n", - "100 2814k 100 2814k 0 0 507k 0 0:00:05 0:00:05 --:--:-- 631k\n", - "Archive: data.zip\n", - " creating: data/\n", - " inflating: data/eng-fra.txt \n", - " creating: data/names/\n", - " inflating: data/names/Arabic.txt \n", - " inflating: data/names/Chinese.txt \n", - " inflating: data/names/Czech.txt \n", - " inflating: data/names/Dutch.txt \n", - " inflating: data/names/English.txt \n", - " inflating: data/names/French.txt \n", - " inflating: data/names/German.txt \n", - " inflating: data/names/Greek.txt \n", - " inflating: data/names/Irish.txt \n", - " inflating: data/names/Italian.txt \n", - " inflating: data/names/Japanese.txt \n", - " inflating: data/names/Korean.txt \n", - " inflating: data/names/Polish.txt \n", - " inflating: data/names/Portuguese.txt \n", - " inflating: data/names/Russian.txt \n", - " inflating: data/names/Scottish.txt \n", - " inflating: data/names/Spanish.txt \n", - " inflating: data/names/Vietnamese.txt \n" - ] - } - ], + "outputs": [], "source": [ "# download the needed data\n", "if not os.path.isfile('data.zip'):\n", @@ -71,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -100,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -185,7 +153,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -216,7 +184,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -239,12 +207,13 @@ " np.random.shuffle(self.shuffle_idx)\n", " self.pairs = self.pairs[self.shuffle_idx]\n", " self.idx_pairs = self.idx_pairs[self.shuffle_idx] \n", - " " + " \n", + " " ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -266,7 +235,7 @@ "array(['we are even EOS', 'nous sommes a egalite EOS'], dtype=')" + "tensor(-23351.4941, grad_fn=)" ] }, - "execution_count": 24, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -444,16 +405,23 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 10, "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch.Size([3, 1, 256])\n" + ] + }, { "data": { "text/plain": [ "torch.Size([1, 1, 2])" ] }, - "execution_count": 120, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -534,7 +502,7 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -559,7 +527,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ @@ -571,14 +539,45 @@ "encoder = Encoder(eng.n_words, embedding_size, context_vector_size, bidirectional)\n", "context_vector_size = context_vector_size * 2 if bidirectional else context_vector_size \n", "decoder = AttentionDecoder(embedding_size, context_vector_size, fra.n_words)\n", - "# writer = SummaryWriter('tb/train')" + "writer = SummaryWriter('tb/train3')" ] }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'cuda'" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "decoder.device" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "epoch 0\n", + "epoch 1\n", + "epoch 2\n", + "epoch 3\n" + ] + } + ], "source": [ "\n", "def train(encoder, decoder):\n", @@ -627,11 +626,267 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "def translate(start, end):\n", + "m = AttentionDecoder" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "English scentence:\t we re all single\n", + "French scentence:\t nous sommes toutes celibataires\n", + "\n", + "Model translation:\t nous sommes toutes celibataire \n", + "\n", + "\n", + "English scentence:\t he is amusing himself by playing video games\n", + "French scentence:\t il s amuse en jouant aux jeux videos\n", + "\n", + "Model translation:\t il est plein a des anglais \n", + "\n", + "\n", + "English scentence:\t i m an artist\n", + "French scentence:\t je suis une artiste\n", + "\n", + "Model translation:\t je suis un artiste \n", + "\n", + "\n", + "English scentence:\t he s enjoying himself\n", + "French scentence:\t il s amuse\n", + "\n", + "Model translation:\t il est est a \n", + "\n", + "\n", + "English scentence:\t he is an american\n", + "French scentence:\t il est americain\n", + "\n", + "Model translation:\t il est un \n", + "\n", + "\n", + "English scentence:\t you re the oldest\n", + "French scentence:\t tu es le plus vieux\n", + "\n", + "Model translation:\t vous etes le plus \n", + "\n", + "\n", + "English scentence:\t he s about to leave\n", + "French scentence:\t il est sur le point de partir\n", + "\n", + "Model translation:\t il est sur le point \n", + "\n", + "\n", + "English scentence:\t i m really happy\n", + "French scentence:\t je suis vraiment heureuse\n", + "\n", + "Model translation:\t je suis vraiment contente \n", + "\n", + "\n", + "English scentence:\t we re all hungry\n", + "French scentence:\t nous avons tous faim\n", + "\n", + "Model translation:\t nous avons toutes faim \n", + "\n", + "\n", + "English scentence:\t i m not giving you any more money\n", + "French scentence:\t je ne te donnerai pas davantage d argent\n", + "\n", + "Model translation:\t je ne te donnerai d argent d argent \n", + "\n", + "\n", + "English scentence:\t he is independent of his parents\n", + "French scentence:\t il est independant de ses parents\n", + "\n", + "Model translation:\t il est plein de ses parents \n", + "\n", + "\n", + "English scentence:\t i am not a doctor but a teacher\n", + "French scentence:\t je ne suis pas medecin mais professeur\n", + "\n", + "Model translation:\t je ne suis pas medecin mais enseignant \n", + "\n", + "\n", + "English scentence:\t you are responsible for the result\n", + "French scentence:\t vous etes responsable des resultats\n", + "\n", + "Model translation:\t vous es responsable de l \n", + "\n", + "\n", + "English scentence:\t i m sympathetic\n", + "French scentence:\t j eprouve de la compassion\n", + "\n", + "Model translation:\t je ne suis \n", + "\n", + "\n", + "English scentence:\t you are drunk\n", + "French scentence:\t tu es saoul\n", + "\n", + "Model translation:\t vous etes impressionnee \n", + "\n", + "\n", + "English scentence:\t you aren t as short as i am\n", + "French scentence:\t tu n es pas aussi petite que moi\n", + "\n", + "Model translation:\t vous n etes pas aussi petit que moi \n", + "\n", + "\n", + "English scentence:\t i m resilient\n", + "French scentence:\t je suis endurante\n", + "\n", + "Model translation:\t je suis endurant \n", + "\n", + "\n", + "English scentence:\t you re wrong again\n", + "French scentence:\t vous avez a nouveau tort\n", + "\n", + "Model translation:\t tu as tort \n", + "\n", + "\n", + "English scentence:\t i am thinking of my vacation\n", + "French scentence:\t je pense a mes vacances\n", + "\n", + "Model translation:\t je songe de mes enfants \n", + "\n", + "\n", + "English scentence:\t she is able to sing very well\n", + "French scentence:\t elle sait tres bien chanter\n", + "\n", + "Model translation:\t elle va bien bien bien bien \n", + "\n", + "\n", + "English scentence:\t i m lazy\n", + "French scentence:\t je suis faineant\n", + "\n", + "Model translation:\t je suis paresseux \n", + "\n", + "\n", + "English scentence:\t he is a cruel person\n", + "French scentence:\t c est un homme cruel\n", + "\n", + "Model translation:\t il est une personne \n", + "\n", + "\n", + "English scentence:\t he is a poet\n", + "French scentence:\t c est un poete\n", + "\n", + "Model translation:\t il est un \n", + "\n", + "\n", + "English scentence:\t we re plastered\n", + "French scentence:\t nous sommes bourres\n", + "\n", + "Model translation:\t nous sommes bourrees \n", + "\n", + "\n", + "English scentence:\t she suddenly fell silent\n", + "French scentence:\t elle se tut soudain\n", + "\n", + "Model translation:\t elle est deux deux \n", + "\n", + "\n", + "English scentence:\t you re not making this easy\n", + "French scentence:\t vous ne rendez pas ca facile\n", + "\n", + "Model translation:\t vous ne fais pas ca \n", + "\n", + "\n", + "English scentence:\t she s very afraid of dogs\n", + "French scentence:\t elle a une peur bleue des chiens\n", + "\n", + "Model translation:\t elle a tres peur des chiens \n", + "\n", + "\n", + "English scentence:\t she is living abroad\n", + "French scentence:\t elle vit actuellement a l etranger\n", + "\n", + "Model translation:\t elle est l a \n", + "\n", + "\n", + "English scentence:\t i m going to my grandmother s\n", + "French scentence:\t je vais chez ma grand mere\n", + "\n", + "Model translation:\t je vais me mon de la \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 elles n ont pas toujours \n", + "\n", + "\n", + "English scentence:\t you are very attractive in blue\n", + "French scentence:\t le bleu vous va tres bien\n", + "\n", + "Model translation:\t vous avez vraiment beaucoup a \n", + "\n", + "\n", + "English scentence:\t you re winning aren t you\n", + "French scentence:\t vous gagnez n est ce pas\n", + "\n", + "Model translation:\t vous etes est n est ce \n", + "\n", + "\n", + "English scentence:\t i m ticklish\n", + "French scentence:\t je suis chatouilleuse\n", + "\n", + "Model translation:\t je suis mal \n", + "\n", + "\n", + "English scentence:\t i m not going to name names\n", + "French scentence:\t je ne vais pas citer de noms\n", + "\n", + "Model translation:\t je ne vais pas m a \n", + "\n", + "\n", + "English scentence:\t they are at broadway avenue\n", + "French scentence:\t ils sont au broadway avenue\n", + "\n", + "Model translation:\t ils sont a l des \n", + "\n", + "\n", + "English scentence:\t i m sure\n", + "French scentence:\t j en suis sure\n", + "\n", + "Model translation:\t je suis certain \n", + "\n", + "\n", + "English scentence:\t she said that she was happy\n", + "French scentence:\t elle a dit qu elle etait heureuse\n", + "\n", + "Model translation:\t elle dit qu a heureuse \n", + "\n", + "\n", + "English scentence:\t i m all done\n", + "French scentence:\t j ai tout fini\n", + "\n", + "Model translation:\t je ai tout termine \n", + "\n", + "\n", + "English scentence:\t we re the problem\n", + "French scentence:\t nous sommes le probleme\n", + "\n", + "Model translation:\t nous sommes le probleme \n", + "\n", + "\n", + "English scentence:\t i m being serious\n", + "French scentence:\t je suis serieux\n", + "\n", + "Model translation:\t je suis serieux \n", + "\n", + "\n" + ] + } + ], + "source": [ + "def translate( start, end):\n", " \n", " for i in range(start, end):\n", " \n", @@ -662,6 +917,15 @@ " \n", "translate(20, 60)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "decoder" + ] } ], "metadata": {