<jupyter_start><jupyter_text>Saliency using Gradient BackpropagationImplement the saliency algorithm described in https://arxiv.org/pdf/1312.6034.pdf.<jupyter_code>from google.colab import drive
drive.mount('/content/drive')

! cp -r '/content/drive/MyDrive/Colab Notebooks/cs-no/6.grads/config' .
import tensorflow as tf
from config import config, data, model, utils

from config.utils import to_image, normalize

utils.setup_clean_image_plotting()
images = data.load_images()
x = model.preprocess(images)
nn = model.build()

logits = nn(x, training=False)
preds = tf.argmax(logits, axis=1)
probs = tf.nn.softmax(logits)
decoded = model.decode(probs.numpy(), top=2)
utils.plot(utils.to_image(images),
           titles=[f'{f}\n{p[0][1]} {p[0][2]:.0%}'
                  for f, p in zip(data.class_names, decoded)],
           rows=4)
def activation_loss(y, units):
    return tf.gather(y, units, axis=1, batch_dims=1)

@tf.function
def gradients(inputs, units):
    with tf.GradientTape() as tape:
        tape.watch(inputs)
        y = nn(inputs)
        loss = activation_loss(y, units)
    
    grads = tape.gradient(loss, inputs)

    return loss, grads
_, g = gradients(x, preds)

sa = normalize(tf.reduce_sum(tf.abs(g), axis=-1))
sp = normalize(tf.reduce_sum(tf.nn.relu(g), axis=-1))
sn = normalize(tf.reduce_sum(tf.nn.relu(-g), axis=-1))
columns = [
    to_image(images),
    sa.numpy(),
    sp.numpy(),
    sn.numpy(),
    to_image(images * sa[..., tf.newaxis])
]
titles = ['original',
          'absolute gradients',
          'positive gradients',
          'negative gradients',
          'original * abs(gradients)']

utils.plot(sum(zip(*columns), ()),
           titles=titles,
           rows=len(images))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15428571428571428, 'fraction_numerical': 0.012571428571428572, 'mean_word_length': 3.548051948051948, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '404621', 'license': 'Apache-2.0', 'n_tokens_mistral': 636, 'n_tokens_neox': 616, 'n_words': 123, 'path': 'notebooks/explaining/saliency-gradient-backpropagation.ipynb', 'repo_name': 'lucasdavid/algorithms-in-tensorflow', 'size': '1750'}