import os

import tensorflow as tf

from tensor2tensor import problems
from tensor2tensor.bin import t2t_decoder  # To register the hparams set
from tensor2tensor.utils import registry
from tensor2tensor.utils import trainer_lib
from tensor2tensor.visualization import attention
from tensor2tensor.visualization import visualization

%%javascript
require.config({
  paths: {
      d3: '//cdnjs.cloudflare.com/ajax/libs/d3/3.4.8/d3.min'
  }
});

"""
Explanation: Create Your Own Visualizations!
Instructions:
1. Install tensor2tensor and train up a Transformer model following the instruction in the repository https://github.com/tensorflow/tensor2tensor.
2. Update cell 3 to point to your checkpoint, it is currently set up to read from the default checkpoint location that would be created from following the instructions above.
3. If you used custom hyper parameters then update cell 4.
4. Run the notebook!
End of explanation
"""


# PUT THE MODEL YOU WANT TO LOAD HERE!
CHECKPOINT = os.path.expanduser('~/t2t_train/translate_ende_wmt32k/transformer-transformer_base_single_gpu')

# HParams
problem_name = 'translate_ende_wmt32k'
data_dir = os.path.expanduser('~/t2t_data/')
model_name = "transformer"
hparams_set = "transformer_base_single_gpu"

"""
Explanation: HParams
End of explanation
"""


visualizer = visualization.AttentionVisualizer(hparams_set, model_name, data_dir, problem_name, beam_size=1)

tf.Variable(0, dtype=tf.int64, trainable=False, name='global_step')

sess = tf.train.MonitoredTrainingSession(
    checkpoint_dir=CHECKPOINT,
    save_summaries_secs=0,
)

input_sentence = "I have two dogs."
output_string, inp_text, out_text, att_mats = visualizer.get_vis_data_from_string(sess, input_sentence)
print(output_string)

"""
Explanation: Visualization
End of explanation
"""


attention.show(inp_text, out_text, *att_mats)

"""
Explanation: Interpreting the Visualizations

The layers drop down allow you to view the different Transformer layers, 0-indexed of course.
Tip: The first layer, last layer and 2nd to last layer are usually the most interpretable.
The attention dropdown allows you to select different pairs of encoder-decoder attentions:
All: Shows all types of attentions together. NOTE: There is no relation between heads of the same color - between the decoder self attention and decoder-encoder attention since they do not share parameters.
Input - Input: Shows only the encoder self-attention.
Input - Output: Shows the decoder’s attention on the encoder. NOTE: Every decoder layer attends to the final layer of encoder so the visualization will show the attention on the final encoder layer regardless of what layer is selected in the drop down.
Output - Output: Shows only the decoder self-attention. NOTE: The visualization might be slightly misleading in the first layer since the text shown is the target of the decoder, the input to the decoder at layer 0 is this text with a GO symbol prepreded.
The colored squares represent the different attention heads.
You can hide or show a given head by clicking on it’s color.
Double clicking a color will hide all other colors, double clicking on a color when it’s the only head showing will show all the heads again.
You can hover over a word to see the individual attention weights for just that position.
Hovering over the words on the left will show what that position attended to.
Hovering over the words on the right will show what positions attended to it.
End of explanation
"""


