I can’t see why it won’t just print the variable vocab_size (for context I’m following along a tutorial for text classification with word2vec in tensorflow).
# create a weight matrix for the Embedding layer from a loaded embedding
def get_weight_matrix(embedding, vocab):
# total vocabulary size plus 0 for unknown words
vocab_size = 25768
#len(vocab) + 1
# define weight matrix dimensions with all 0
weight_matrix = np.zeros((vocab_size, 100))
# step vocab, store vectors using the Tokenizer's integer mapping
for word, i in vocab.items():
weight_matrix[i] = embedding.get(word)
return weight_matrix
print(vocab_size)
The error: NameError: name 'vocab_size' is not defined
Before, the variable was intialized with another function inside it so it thought that might have been the problem, so I just kept it simple and intialized with an integer but it’s still not defined?
>Solution :
Define the vocab_size outside of the function
# total vocabulary size plus 0 for unknown words
vocab_size = 25768
def get_weight_matrix(embedding, vocab):
#len(vocab) + 1
# define weight matrix dimensions with all 0
weight_matrix = np.zeros((vocab_size, 100))
# step vocab, store vectors using the Tokenizer's integer mapping
for word, i in vocab.items():
weight_matrix[i] = embedding.get(word)
return weight_matrix
print(vocab_size)