### decoder of the inference model (Recursive decoder generator)

import tensorflow.keras.layers as layers
from tensorflow.keras.models import Model

en_input_tensors = Input(shape=(en_len,en_vocab))
en_gru_layer = GRU(hsize, return_state=True)
en_output_tensors, en_state = en_gru_layer(en_input_tensors)
encoder = Model(inputs=en_input_tensors, outputs=en_state) # Our encoder model will generate  output state from input data 

de_input_tensors = Input(shape=(1, fr_vocab)) # An input layer that accepts a single onehot encoded word
de_state_in = Input(shape=(hsize,)) # Takes en_state as input (comes from en_gru_layer same tensor dimension)
de_gru_layer = GRU(hsize, return_state=True) # GRU layer that produces output and a new state
de_output_tensors, de_state_out = de_gru_layer(de_input_tensors, initial_state=de_state_in) # Recursion: state is another input
de_dense_layer = layers.Dense(fr_vocab, activation='softmax') # prediction of most probable word among unique words in fr_vocab
de_predictions = de_dense_layer(de_output_tensors)
decoder = Model(inputs=[de_input_tensors, de_state_in], outputs=[de_predictions, de_state_out])

### You need to copy the weights from the trained model to the inference model. 
### Always remember to perform this step if you have different training and inference models. 
### If you miss this step, the model will still run but the translations will be incorrect.

en_gru_w = tr_en_gru.get_weights() # Load the weights to the encoder GRU from the trained model
en_gru.set_weights(en_gru_w) # Set the weights of the encoder GRU of the inference model
de_gru.set_weights(tr_de_gru.get_weights()) # Load and set the weights to the decoder GRU
de_dense.set_weights(tr_de_dense.get_weights()) # Load and set the weights to the decoder Dense

en_sent = ['the united states is sometimes chilly during december , but it is sometimes freezing in june .']
en_data = sents2seqs('source', en_st, onehot=True, reverse=True) # preprocess the sentence (tokenized, reversed, one-hot encoded)
de_input_state = encoder.predict(en_data) # use encoder model to convert into decoder input state

de_data = word2onehot(fr_tokens, 'sos', fr_vocab) # Converting "sos" (initial word) to a sequence
fr_sent = ''
for _ in range(fr_len): # Keep doing this until the end of the sentence is reached 
    de_prob, de_input_state = decoder.predict([de_data,de_input_state]) # This is where de_input_state keeps changing for each word
    de_word = probs2word(de_prob, fr_tokens) # get the max probable word
    de_data = word2onehot(fr_tokens, de_w, fr_vocab)
    if (de_word == 'eos'): # Stop generating once end of sentence word "eos" is found
        break 
    else:
        fr_sent += de_word + ' ' # Keep adding words to make a sentence

def word2onehot(tokenizer, word, vocab_size):
    de_seq = tokenizer.texts_to_sequences([[word]])
    de_onehot = to_categorical(de_seq, num_classes=vocab_size)
    de_onehot = np.expand_dims(de_onehot, axis=1)    
    return de_onehot

def probs2word(probs, tok):
    wid = np.argmax(probs[0,:], axis=-1)
    w = tok.index_word[wid]
    return w

Python相关代码片段

how to use cmd class in python

sigma python

python convert time.time to datetime

flask visible across the network?

add function in python

Extract unique values and their counts

django forms make field not required

quartile cuts and new column

pyspark sort desc

python generator select top 10

sorting and extracting rows

django db backup

flask babel messages are not translated

import selenium WebDriverWait

pytorch neural network

text font customtkinter

tkinter grid

numpy vector magnitude

libvirt-python error

yolov8 real time object detection

milvus client python

ython isna() data frame

customtkinter gauge

python regex start of line

6.7 fl perfume

has attribute python

learning rate pytorch

arrays multidimensionales numpy

Checking if a list is empty in Python

Verify if a Python function returns a list

Syntax of the ternary operator in Python

How to check if an item exists in a Python list

Creating a list of dictionaries in Python

pytorch basics

creat key value jetstream python api

lambda python tkinter

add data into json python

create admin in Django

Convert a string to an int in Python

python named parameters command line

Return a list from a function in Python

keras decoder of the inference model

teacher forcing in keras

df.rename(columns= 'a' 'b' )

Python check whether object is picklable

tkinter custom icons

title of a webpage using selenium python

what does DataFrame.factorize do in pandas

tensorflow mse

encoder decoder architecture in keras

pathlib replace extension

pandas access datetime filed

python stopwatch decorator

There is more than one Python

tkinter combobox get value

beautifulsoup returns empty list

importerror: numba needs numpy 1.21 or less

how to install jdk in windows 10

combo box tkinte

run python script on raspberry pi command line

minus infinity in python

hallar angulo de referencia python

create filtered pivot tables in pandas

text in keras

how to mirror screen on tv with python

python read file to string

python import tuple

python sefine function type

meta llama 3 base huggingface

equal to or more than python

equal to or less than python

python pillow transparent background

CLEAR ALL MIGRATIONS DJANGO

decrement in python

hugging face to dataframe

Table Creation and Data Insertion in PySpark

Testing ETL Framework for Fixed-Length Files

Making Bulk SQL Queries from Notebooks

dictionary of tuple python

kml to csv with scraping