Problem with cell UNQ_C5 of final programming assignment

Hi there,
I’m really stuck on one part of the assignment, specifically cell UNQ_C5.

My problem is with the very last part, which runs the encoder multiple times using a for loop.

There are a couple of things I don’t understand about how the code is written:

  1. self.enc_layers, defined in the init section as

self.enc_layers = [EncoderLayer(embedding_dim=self.embedding_dim,
num_heads=num_heads,
fully_connected_dim=fully_connected_dim,
dropout_rate=dropout_rate,
layernorm_eps=layernorm_eps)
for _ in range(self.num_layers)]

appears to generate a list by iterating the encoder (self.num_layers) times - so why do you need the for loop towards the bottom of the function body?

  1. In the comments, it says that the function returns:
    Returns:
    out2 – Tensor of shape (batch_size, input_seq_len, embedding_dim)
    but the function code as written returns x (which has a different size).

Thanks for your help!
Joe

1 Like

The self.init_layers() creates a list of EncoderLayer objects. But that just creates the layers, it doesn’t run them.

The code in the “call” method uses a for loop to runs the layers and pass the data through them.

Regarding the doctext for the return value, it’s incorrect. I’ll submit a ticket to fix it.

2 Likes

Thanks very much - that makes much more sense now. All sorted!

1 Like