This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Extract only the values for title (input) and class (output) | |
data = total[['title','class']] | |
data.head() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
real = pd.read_csv("/content/drive/My Drive/News/True.csv") | |
fake = pd.read_csv("/content/drive/My Drive/News/Fake.csv") | |
#Shape of real news dataset | |
print("Real news: ",real.shape) | |
#Shape of fake news dataset | |
print("Fake News: ", fake.shape) | |
# Assigning a value of 0 for all real news data and placing it in the dataframe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def plotAttributions(baseline,attributions, | |
image, | |
cmap=None, | |
overlay_alpha=0.4): | |
#Sum of the abs value of the attriutions determining feature importance | |
mask = tf.reduce_sum(tf.math.abs(attributions), axis=-1) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def integralEstimates(gradients): | |
# riemann_trapezoidal | |
grads = (gradients[:-1] + gradients[1:]) / tf.constant(2.0) | |
integrated_gradients = tf.math.reduce_mean(grads, axis=0) | |
return integrated_gradients | |
integ = integralEstimates(gradients=computedGradients) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gradient(images, targetClassindex): | |
#Computes gradient of probabilities with respect to images | |
with tf.GradientTape() as gradRecord: | |
gradRecord.watch(images) | |
logits = vgg(images) | |
probs = tf.nn.softmax(logits, axis=-1)[:, targetClassindex] | |
return gradRecord.gradient(probs, images) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interpolatedImages = interpolation(baseline=baseline,image=image,alpha=alpha) | |
fig = plt.figure(figsize=(20, 20)) | |
i = 0 | |
for alphas, image in zip(alpha[0::10], interpolatedImages[0::10]): | |
i += 1 | |
plt.subplot(1, len(alpha[0::10]), i) | |
plt.title(f'alpha: {alphas:.1f}') | |
plt.imshow(image) | |
plt.axis('off') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def interpolation(baseline, image, alpha): | |
#Interpolates images by computing the difference between its pixel values | |
#and the baseline (all zeroes) | |
#From there, construct a new tensor containing the baseline and interpolated | |
#images, which are the picture values multiplied by the linear relationship. | |
alphaTensor = alpha[:, tf.newaxis, tf.newaxis, tf.newaxis] | |
baselineTensor = tf.expand_dims(baseline, axis=0) | |
inputTensor = tf.expand_dims(image, axis=0) | |
diff = inputTensor - baselineTensor | |
images = baselineTensor + alphaTensor * diff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow import io | |
#Convert image to a tensor - 3 RGB Channels - resized to 224x224 | |
image = tf.io.read_file("drive/MyDrive/oboe.png") | |
image = tf.io.decode_jpeg(image, channels=3) | |
image = tf.image.convert_image_dtype(image, tf.float32) | |
image = tf.image.resize_with_pad(image, target_height=224, target_width=224) | |
#Generates 50 intervals for the interpolation of images |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow import io | |
#Convert image to a tensor - 3 RGB Channels - resized to 224x224 | |
image = tf.io.read_file("drive/MyDrive/oboe.png") | |
image = tf.io.decode_jpeg(image, channels=3) | |
image = tf.image.convert_image_dtype(image, tf.float32) | |
image = tf.image.resize_with_pad(image, target_height=224, target_width=224) | |
#Generates 50 intervals for the interpolation of images |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Implementing our Baseline | |
baseline = tf.zeros(shape=(224,224,3)) | |
#Displaying it with matplotlib | |
plt.imshow(baseline) | |
plt.title("Baseline") | |
plt.axis('off') | |
plt.show() |