Ok, it’s just a bit of my problems with Onnx-convertation of lstm beam-search, and converting a tensor to a Python number is the first of them. When I export this for Onnx, I got the constant output from the model with incorrect tracing. I found, that I should script it with torch.script, but those examples not easy to understand 
    def forward(self, img_features, beam_size):
        """
        Function to generate the caption for the corresponding encoded image using beam search to provide the most optimal caption 
        combination. 
        
        Arguments:
            img_features (tensor): Extracted features from the encoder module. (batch_size, feature_pixels = 7x7, encoder_dim = 2048)
            beam_size (tensor): Number of top candidates to consider for beam search. (int = 3 or 6 or 9)
            
        Output:
            sentence (tensor): ordered list of words of the final optimal caption (list)

        """
        beam_size = beam_size.item()#!Converting a tensor to a Python number
        
        prev_words = torch.zeros(beam_size, 1).long()
        
        sentences = prev_words
        
        top_preds = torch.zeros(beam_size, 1)
        
        completed_sentences = []
        completed_sentences_preds = []
        
        step = 1
        h, c = self.get_init_lstm_state(img_features)

        while True:
            embedding = self.embedding(prev_words).squeeze(1)
            context = self.attention(img_features, h)[0]
            gate = self.sigmoid(self.f_beta(h))
            gated_context = gate * context

            lstm_input = torch.cat((embedding, gated_context), dim=1)
            h, c = self.lstm(lstm_input, (h, c))
            output = self.deep_output(h)
            output = top_preds.expand_as(output) + output

            if step == 1:
                top_preds, top_words = output[0].topk(beam_size, 0, True, True)
            else:
                top_preds, top_words = output.view(-1).topk(beam_size, 0, True, True)
            prev_word_idxs = top_words / output.size(1)
            next_word_idxs = top_words % output.size(1)

            sentences = torch.cat((sentences[prev_word_idxs], next_word_idxs.unsqueeze(1)), dim=1)

            incomplete = [idx for idx, next_word in enumerate(next_word_idxs) if next_word != 1]#!Converting a tensor to a Python boolean
            complete = list(set(range(len(next_word_idxs))) - set(incomplete))#!Converting a tensor to a Python index

            if len(complete) > 0:
                completed_sentences.extend(sentences[complete].tolist())#!Converting a tensor to a Python list
                completed_sentences_preds.extend(top_preds[complete])#!Converting a tensor to a Python index
            beam_size -= len(complete)

            if beam_size == 0:
                break
            sentences = sentences[incomplete]
            h = h[prev_word_idxs[incomplete]]
            c = c[prev_word_idxs[incomplete]]
            img_features = img_features[prev_word_idxs[incomplete]]
            top_preds = top_preds[incomplete].unsqueeze(1)
            prev_words = next_word_idxs[incomplete].unsqueeze(1)

            if step > 50:
                break
            step += 1

        idx = completed_sentences_preds.index(max(completed_sentences_preds))#!Converting a tensor to a Python boolean
        sentence = completed_sentences[idx]

        sentence = torch.tensor(sentence)#!torch.tensor results are registered as constants
        return sentence