-
Notifications
You must be signed in to change notification settings - Fork 370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
class EncoderDecoder last element of output of decoder does not match #51
Comments
Hi @jdgh000 , I believe the reproducibility issue boils down to the Once we set the probability to # create encoder
torch.manual_seed(23)
encoder = Encoder(n_features=2, hidden_dim=2)
# creaet decoder
torch.manual_seed(23)
decoder = Decoder(n_features=2, hidden_dim=2)
encdec = EncoderDecoder(encoder, decoder, input_len=2, target_len=2, teacher_forcing_prob=1.0)
encdec.train() # teacher forcing can only happen in training mode
encdec(full_seq) The output:
Now, if we do it manually: torch.manual_seed(23) # matching seed
encoder = Encoder(n_features=2, hidden_dim=2)
hidden_seq = encoder(source_seq) # output is N, L, F
hidden_final = hidden_seq[:, -1:] # takes last hidden state
print(hidden_seq)
torch.manual_seed(23) # matching seed
decoder = Decoder(n_features=2, hidden_dim=2)
decoder.init_hidden(hidden_seq)
# Generation loop
inputs = source_seq[:, -1:]
# HERE: so it's always teacher forcing and we guarantee reproducibility
teacher_forcing_prob = 1.0
target_len = 2
for i in range(target_len):
print(f'Hidden: {decoder.hidden}')
out = decoder(inputs)
print(f'Output: {out}\n')
# If it is teacher forcing
if torch.rand(1) <= teacher_forcing_prob:
# Takes the actual element
inputs = target_seq[:, i:i+1]
else:
# Otherwise uses the last predicted output
inputs = out The output is:
Notice that the two printed outputs ( I hope it helps. Best, |
ok so that teacher forcing prob is set to 1.0? I was not sure about it as i thought that happened before introducing teacher forcing. I may be wrong on this i will try that and see if it works. thx |
Here is the partial output of my code:
code sources is at:
https://gitlab.com/codelabs8265339/codelab-gpu/-/blob/dev-gpu-sbs/ml/tf/tf-from-scratch/3/code-exercises/ch9/ch9-p247-encdec.py?ref_type=heads
Basically I copied line by line from around p247.
The previous example using separate encoder and decoder class directly (w/o using encoderDecoder) class, output of the decoder was not matching (although encoder's does) which I fixed (matched output) by setting seed reset at 21 before initialization of both encoder and decoder instantiation:
I tried to do same trick before instantiation of encoderDecoder class but this time luck did not hit me:
The text was updated successfully, but these errors were encountered: