def beam_search(image1,image2, beam_index): hidden_state = tf.zeros((1, enc_units)) image_features=image_feature_extraction(image1,image2) encoder_out = model.layers[0](image_features) start_token = [token.word_index["<sos>"]] dec_word = [[start_token, 0.0]] while len(dec_word[0][0]) < max_len: temp = [] for word in dec_word: predict, hidden_state,alpha = model.layers[1].onestep(tf.expand_dims([word[0][-1]],1), encoder_out, hidden_state) word_predict = np.argsort(predict[0])[-beam_index:] for i in word_predict: next_word, probab = word[0][:], word[1] next_word.append(i) probab += predict[0][i] temp.append([next_word, probab.numpy()]) dec_word = temp dec_word = sorted(dec_word, key=take_second) dec_word = dec_word[-beam_index:] final = dec_word[-1] report =final[0] score = final[1] temp = [] for word in report: if word!=0: if word != token.word_index['<eos>']: temp.append(token.index_word[word]) else: break rep = ' '.join(e for e in temp) return rep, score