Comments (6)
You should change:
loss += criterion(decoder_output[0], target_variable[di])
to
loss += criterion(decoder_output, target_variable[di])
from practical-pytorch.
you need to modify score function in Attention Model.
maybe you can use code follow:
def score(self, hidden, encoder_output):
if self.method == 'dot':
energy =torch.dot(hidden.view(-1), encoder_output.view(-1))
elif self.method == 'general':
energy = self.attn(encoder_output)
energy = torch.dot(hidden.view(-1), energy.view(-1))
elif self.method == 'concat':
energy = self.attn(torch.cat((hidden, encoder_output), 1))
energy = torch.dot(self.v.view(-1), energy.view(-1))
return energy
but this implementation will very slower in GPU. #56
from practical-pytorch.
In pytorch v0.2, it removed implicit flattening for dot. pytorch/pytorch#2313
You can use the following writing
torch.dot(hidden.view(-1), energy.view(-1))
from practical-pytorch.
@czs0x55aa Thank you very much! It seems work. However, it raised another error.
ValueError Traceback (most recent call last)
in ()
8
9 # Run the train function
---> 10 loss = train(input_variable, target_variable, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion)
11
12 # Keep track of loss
in train(input_variable, target_variable, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, max_length)
32 for di in range(target_length):
33 decoder_output, decoder_context, decoder_hidden, decoder_attention = decoder(decoder_input, decoder_context, decoder_hidden, encoder_outputs)
---> 34 loss += criterion(decoder_output[0], target_variable[di])
35 decoder_input = target_variable[di] # Next target is next input
36
~/anaconda/envs/pytorch_nmt3.5/lib/python3.5/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
222 for hook in self._forward_pre_hooks.values():
223 hook(self, input)
--> 224 result = self.forward(*input, **kwargs)
225 for hook in self._forward_hooks.values():
226 hook_result = hook(self, input, result)
~/anaconda/envs/pytorch_nmt3.5/lib/python3.5/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
130 _assert_no_grad(target)
131 return F.nll_loss(input, target, self.weight, self.size_average,
--> 132 self.ignore_index)
133
134
~/anaconda/envs/pytorch_nmt3.5/lib/python3.5/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index)
674 return _functions.thnn.NLLLoss2d.apply(input, target, weight, size_average, ignore_index)
675 else:
--> 676 raise ValueError('Expected 2 or 4 dimensions (got {})'.format(dim))
677
678
ValueError: Expected 2 or 4 dimensions (got 1)
Btw, I am a beginner of coding. How can I deal with this kind of error raised from source code? Thank you for help!
from practical-pytorch.
@czs0x55aa Thank you very much for answering. But where should I revise in this case--seq2seq translation.
from practical-pytorch.
sorry, i'm not facing this issue.
maybe you should to check the tensor size of decoder_output
and target_variable
from practical-pytorch.
Related Issues (20)
- Issue on Windows
- Seq-seq not working for creating chatbot
- How to save and load train model and use it for evaluation HOT 2
- Link for Series 2 - RNNs for time-series data
- Question about Luong Attention Implementation HOT 7
- can't import torch HOT 2
- The link for Teacher Forcing in "Translation with a Sequence to Sequence Network and Attention" is broken
- Error in BahdanauAttnDecoderRNN HOT 1
- Issues in your tutorial on Classifying Names with a Character-Level RNN
- I can't calculate the score of attention in Seq2Seq Translation. HOT 2
- Error in practical-pytorch/seq2seq-translation/seq2seq-translation-batched.ipynb
- Question from character level RNN classifier, why not use the hidden state across epochs? HOT 1
- RuntimeError: 1D tensors expected, got 2D, 2D tensors HOT 1
- May I know how to support a new sentence translation?
- seq2seq: Replace the embeddings with pre-trained word embeddings such as word2vec
- About seq2seq-translation-batched.py RuntimeError HOT 1
- Wrong implementation of attention mechanism in pytorch tutorials
- FileNotFoundError: [Errno 2] No such file or directory: 'char-rnn-classification.pt'
- small format issue
- Implicit dimension choice for log_softmax has been deprecated while running 'python train.py'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from practical-pytorch.