Reputation: 1
I was trying to get the saliency map of NLP dataset. My X_test[11] = [ 6 24 7 22 19 7 41 62 55 45 31 5 7 22 23 8 29 36 31 12 14 2 4 3 1 5 26 25 25 43 18 2 2 2 4 15 14 2 2 0] and the class level Y_test[11] = 1 I am getting the outputs of tape.watch() and tape.gradient() are none. How can I write the code for saliency map.
def Create_Model(embed_len, input_dim):
model = Sequential()
model.add(Embedding(input_dim=input_dim, output_dim=embed_len, input_length=40))
model.add(Conv1D(32,3, padding = 'same' , activation = 'relu',strides = 1))
model.add(MaxPooling1D(pool_size = 2))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
output = model.add(Dense(units=1, activation='sigmoid'))
cnn1 = model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy')
return model
model = Create_Model(embed_len, input_dim)
history = model.fit(X_sm, Y_sm, batch_size=3000, epochs=5, validation_data=(X_val, Y_val), callbacks=[early],verbose=1)
Saliency Map
import tensorflow as tf
def loss_fn(y_true, y_pred):
return tf.reduce_sum(tf.square(y_true-y_pred))
with tf.GradientTape(persistent =True) as tape:
tensor_input = tf.convert_to_tensor(np.expand_dims(X_test[11], axis=0))
tensor_input = tf.cast(tensor_input, tf.float32)
x = tape.watch(tensor_input)
preds = model(tensor_input)
loss = loss_fn(1, preds)
Upvotes: 0
Views: 40