You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromtensorflow.python.keras.utilsimportto_categorical# Create a list of words and convert them to indiceswords= ["I", "like", "cats"]
word_ids= [word2index[w] forwinwords]
print(word_ids)
fromtensorflow.python.keras.utilsimportto_categorical# Create a list of words and convert them to indiceswords= ["I", "like", "cats"]
word_ids= [word2index[w] forwinwords]
print(word_ids)
# Create onehot vectors using to_categorical functiononehot_1=to_categorical(word_ids)
fromtensorflow.python.keras.utilsimportto_categorical# Create a list of words and convert them to indiceswords= ["I", "like", "cats"]
word_ids= [word2index[w] forwinwords]
print(word_ids)
# Create onehot vectors using to_categorical functiononehot_1=to_categorical(word_ids)
# Print words and their corresponding onehot vectorsprint([(w,ohe.tolist()) forw,oheinzip(words, onehot_1)])
# Create onehot vectors with a fixed number of classesonehot_2=to_categorical(word_ids, num_classes=5)
print([(w,ohe.tolist()) forw,oheinzip(words, onehot_2)])
Part 1: Exploring the to_categorical() function
defcompute_onehot_length(words, word2index):
# Create word IDs for wordsword_ids= [word2index[w] forwinwords]
# Convert word IDs to onehot vectorsonehot=to_categorical(word_ids)
# Return the length of a single one-hot vectorreturnonehot.shape[1]
word2index= {"He":0, "drank": 1, "milk": 2}
# Compute and print onehot length of a list of wordsprint(compute_onehot_length(["He", "drank", "milk"], word2index))
importnumpyasnpdefwords2onehot(word_list, word2index):
# Convert words to word IDsword_ids= [word2index[w] forwinword_list]
# Convert word IDs to onehot vectors and return the onehot arrayonehot=to_categorical(word_ids, num_classes=3)
returnonehotwords= ["I", "like", "cats"]
# Convert words to onehot vectors using words2onehotonehot=words2onehot(words, word2index)
# Print the result as (<word>, <onehot>) tuplesprint([(w,ohe.tolist()) forw,oheinzip(words, onehot)])
Part 2: Text reversing model - Encoder
defencoder(onehot):
# Get word IDs from onehot vectors and return the IDsword_ids=np.argmax(onehot, axis=1)
returnword_ids# Define "we like dogs" as wordswords= ["We", "like", "dogs"]
# Convert words to onehot vectors using words2onehotonehot=words2onehot(words, word2index)
# Get the context vector by using the encoder functioncontext=encoder(onehot)
print(context)
Complete text reversing model
# Define the onehot2words function that returns words for a set of onehot vectorsdefonehot2words(onehot, index2word):
ids=np.argmax(onehot, axis=1)
res= [index2word[id] foridinids]
returnres# Define the decoder function that returns reversed onehot vectorsdefdecoder(context_vector):
word_ids_rev=context_vector[::-1]
onehot_rev=to_categorical(word_ids_rev, num_classes=3)
returnonehot_rev# Define the onehot2words function that returns words for a set of onehot vectorsdefonehot2words(onehot, index2word):
ids=np.argmax(onehot, axis=1)
res= [index2word[id] foridinids]
returnres# Define the decoder function that returns reversed onehot vectorsdefdecoder(context_vector):
word_ids_rev=context_vector[::-1]
onehot_rev=to_categorical(word_ids_rev, num_classes=3)
returnonehot_rev# Convert context to reversed onehot vectors using decoderonehot_rev=decoder(context)
# Get the reversed words using the onehot2words functionreversed_words=onehot2words(onehot_rev, index2word)
Part 1: Understanding GRU models
importtensorflow.kerasaskerasimportnumpyasnp# Define an input layerinp=keras.layers.Input(batch_shape=(2,3,4))
importtensorflow.kerasaskerasimportnumpyasnp# Define an input layerinp=keras.layers.Input(batch_shape=(2,3,4))
# Define a GRU layer that takes in the inputgru_out=keras.layers.GRU(10)(inp)
importtensorflow.kerasaskerasimportnumpyasnp# Define an input layerinp=keras.layers.Input(batch_shape=(2,3,4))
# Define a GRU layer that takes in the inputgru_out=keras.layers.GRU(10)(inp)
# Define a model that outputs the GRU outputmodel=keras.models.Model(inputs=inp, outputs=gru_out)
x=np.random.normal(size=(2,3,4))
# Get the output of the model and print the resulty=model.predict(x)
print("shape (y) =", y.shape, "\ny = \n", y)
Part 2: Understanding GRU models
# Define an input layerinp=keras.layers.Input(shape=(3,4))
# Define a GRU layer that takes in the inputgru_out=keras.layers.GRU(10)(inp)
# Define a model that outputs the GRU outputmodel=keras.models.Model(inputs=inp, outputs=gru_out)
x1=np.random.normal(size=(2,3,4))
x2=np.random.normal(size=(5,3,4))
# Get the output of the model and print the resulty1=model.predict(x1)
y2=model.predict(x2)
print("shape (y1) = ", y1.shape, " shape (y2) = ", y2.shape)
Understanding sequential model output
# Define the Input layerinp=keras.layers.Input(batch_shape=(3,20,5))
# Define a GRU layer that takes in inp as the inputgru_out1=keras.layers.GRU(10)(inp)
print("gru_out1.shape = ", gru_out1.shape)
# Define the Input layerinp=keras.layers.Input(batch_shape=(3,20,5))
# Define a GRU layer that takes in inp as the inputgru_out1=keras.layers.GRU(10)(inp)
print("gru_out1.shape = ", gru_out1.shape)
# Define the second GRU and print the shape of the outputsgru_out2, gru_state=keras.layers.GRU(10, return_state=True)(inp)
print("gru_out2.shape = ", gru_out2.shape)
print("gru_state.shape = ", gru_state.shape)
# Define the Input layerinp=keras.layers.Input(batch_shape=(3,20,5))
# Define a GRU layer that takes in inp as the inputgru_out1=keras.layers.GRU(10)(inp)
print("gru_out1.shape = ", gru_out1.shape)
# Define the second GRU and print the shape of the outputsgru_out2, gru_state=keras.layers.GRU(10, return_state=True)(inp)
print("gru_out2.shape = ", gru_out2.shape)
print("gru_state.shape = ", gru_state.shape)
# Define the third GRU layer which will return all the outputsgru_out3=keras.layers.GRU(10, return_sequences=True)(inp)
print("gru_out3.shape = ", gru_out3.shape)