<jupyter_start><jupyter_text>Load pre-trained IMDB model and data<jupyter_code>imdb_model = DocumentModel.load_model(config.MODEL_DIR+ '/imdb/model_02.json')
imdb_model.load_model_weights(config.MODEL_DIR+ '/imdb/model_02.hdf5')

model = imdb_model.get_classification_model()
model.compile(loss="binary_crossentropy", optimizer='rmsprop', metrics=["accuracy"])
train_df = Loader.load_imdb_data(directory = 'train')
print(train_df.shape)

corpus = train_df['review'].tolist()
target = train_df['sentiment'].tolist()
corpus, target = remove_empty_docs(corpus, target)
print(len(corpus))<jupyter_output><empty_output><jupyter_text>Pre process input and compute document embeddings<jupyter_code>preprocessor = Preprocess(corpus=corpus)
corpus_to_seq = preprocessor.fit()

corpus = train_df['review'].tolist()
target = train_df['sentiment'].tolist()
corpus_to_seq = preprocessor.transform(corpus)

x_train = np.array(corpus_to_seq)
y_train = np.array(target)

print(x_train.shape, y_train.shape)
print('Evaluating Model ...')
print(model.evaluate(x_train, y_train))

preds = model.predict(x_train)

#invert predicted label
pseudo_label = np.subtract(1,preds)<jupyter_output><empty_output><jupyter_text>Gradient Calculation of inverted output w.r.t sentence embeddings<jupyter_code>#Get the learned sentence embeddings
sentence_ebd = imdb_model.get_sentence_model().predict(x_train)

input_tensors = [model.inputs[0], # input data
                 model.sample_weights[0], # how much to weight each sample by
                 model.targets[0], # labels                 
]
#variable tensor at the sentence embeding layer
weights = imdb_model.get_sentence_model().outputs

#calculate gradient of the total model loss w.r.t 
#the variables at sentence embd layer                                     
gradients = model.optimizer.get_gradients(model.total_loss, weights) 
get_gradients = K.function(inputs=input_tensors, outputs=gradients)
document_number = 2
K.set_learning_phase(0)
inputs = [[x_train[document_number]], # X
          [1], # sample weights
          [[pseudo_label[document_number][0]]], # y
]
grad = get_gradients(inputs)

sent_score = []
for i in range(Preprocess.NUM_SENTENCES):
    #sent_score.append((i, -np.abs(np.dot(grad[0][0][i],sentence_ebd[document_number][i])))) #DECREASING
    sent_score.append((i, -np.linalg.norm(grad[0][0][i])))

sent_score.sort(key=lambda tup: tup[1])
summary_sentences = [ i for i, s in sent_score[:4]]

doc = corpus[document_number]
label = y_train[document_number]
prediction = preds[document_number]
print(doc, label , prediction)

sentences = sent_tokenize(doc)
for i in summary_sentences:
    print(i, sentences[i])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12439658373561084, 'fraction_numerical': 0.007055328629780914, 'mean_word_length': 5.735, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '523680', 'license': 'Apache-2.0', 'n_tokens_mistral': 941, 'n_tokens_neox': 920, 'n_words': 166, 'path': 'notebooks/Ch07 - Text Document Categorization/Text_Summarization_IMDB.ipynb', 'repo_name': 'mustafabozkaya/hands-on-transfer-learning-with-python', 'size': '2693'}