https://colab.research.google.com/drive/16OdOBEbZGqIG6LOmLZgsQVP2M8-j39OL?usp=sharing
https://drive.google.com/file/d/1ttFltm-vHdE1yCDK3XqAnUCPvNR3pHxV/view?usp=sharing
Notebook Link :- https://drive.google.com/file/d/1y9MZUINtAG3a3SwcfnKrPHro18uUpvEe/view?usp=sharing
def inference_test():
print("--- Starting inference test ---")
# Create and evaluate the model
test_model = make_model(11, 11, 2)
test_model.eval()
print("Model created and set to evaluation mode.")
# Input sequence: numbers 1 to 10
src = torch.tensor([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], dtype=torch.long)
src_mask = torch.ones(1, 1, 10)
print(f"Input sequence (src): {src}")
# Encode the input sequence
memory = test_model.encode(src, src_mask)
print("Input sequence encoded into memory.")
# Initialize output sequence with a single zero
ys = torch.zeros(1, 1).type_as(src)
print(f"Initial output sequence (ys): {ys}")
# Generate predictions for the next 9 tokens
for i in range(9):
print(f"\n--- Decoding step {i+1} ---")
# Decode the current output sequence
out = test_model.decode(
memory,
src_mask,
ys,
subsequent_mask(ys.size(1)).type_as(src.data)
)
print("Decoded output.")
# Generate probabilities for the next token
prob = test_model.generator(out[:, -1])
print("Generated probabilities for the next token.")
# Select the token with the highest probability
_, next_word = torch.max(prob, dim=1)
next_word = next_word.data[0]
print(f"Selected next token: {next_word}")
# Append the selected token to the output sequence
ys = torch.cat(
[ys, torch.full((1, 1), next_word, dtype=ys.dtype)],
dim=1
)
print(f"Updated output sequence (ys): {ys}")
print("\n--- Final Prediction ---")
print("Example Untrained Model Prediction:", ys)
def run_tests():
print("\n--- Running 10 inference tests ---")
for _ in range(10):
inference_test()
show_example(run_tests)