I am following the pytorch tutorial here and got to the word embeddings tutorial but there is some code I do not understand.
When constructing n-grams they use the following:
ngrams = [
(
[test_sentence[i - j - 1] for j in range(CONTEXT_SIZE)],
test_sentence[i]
)
for i in range(CONTEXT_SIZE, len(test_sentence))
]
This to me does not seem syntactically correct as i
is referenced before it is initialized, there is nothing inside the for
loop, and the for
loop is missing the :
at the end. What is going on with this block of code? It does not seem like it should work but it does.
CodePudding user response:
This is a syntax of python like you can define a list like this:
a_list = [i*2 for i in range(100)]
in your example it is defining a 2-D list, so there are two for loop like:
b_list = [(a_list) for j in range(2)]
if we combine, we can write them like the example you posted.