IniziaInizia gratis

Part 1: Text reversing model - Encoder

Creating a simple text reversing model is a great method to understand the mechanics of encoder decoder models and how they connect. You will now implement the encoder part of a text reversing model.

The implementation of the encoder has been split over two exercises. In this exercise, you will be defining the words2onehot() helper function. The words2onehot() function should take in a list of words and a dictionary word2index and convert the list of words to an array of one-hot vectors. The word2index dictionary is available in the workspace.

Questo esercizio fa parte del corso

Machine Translation with Keras

Visualizza il corso

Istruzioni dell'esercizio

  • Convert words to IDs using the word2index dictionary in the words2onehot() function
  • Convert word IDs to onehot vectors having length 3 (using the num_classes argument) and return the resulting array.
  • Call the words2onehot() function with the words I, like and cats and assign the result to onehot.
  • Print the words and their corresponding onehot vectors using print() and zip() functions. The zip() function allows you to iterate multiple lists at the same time.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

import numpy as np

def words2onehot(word_list, word2index):
  # Convert words to word IDs
  word_ids = [____[w] for w in ____]
  # Convert word IDs to onehot vectors and return the onehot array
  onehot = ____(____, num_classes=3)
  return ____

words = ["I", "like", "cats"]
# Convert words to onehot vectors using words2onehot
onehot = ____(____, ____)
# Print the result as (, ) tuples
print([(w,ohe.tolist()) for ____,____ in zip(words, ____)])
Modifica ed esegui il codice