'Can't see why this variable is not defined [closed]

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 1:[1]

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)

Solution 2:[2]

You're trying to print out an out of scope variable. Look at the indentation for your print statement, it is not part of the function get_weight_matrix(). Also, you're gonna want to print out the contents of the variable before returning from the function.

Solution 3:[3]

You currently define a function that returns weight_matrix. This means you can call your function, assign the output to a variable, and print the value of this variable outside your function.

def get_weight_matrix(embedding, vocab):
    vocab_size = len(vocab) + 1
    weight_matrix = np.zeros((vocab_size, 100))
    for word, i in vocab.items():
        weight_matrix[i] = embedding.get(word)
    return weight_matrix

weight_matrix = get_weight_matrix(embedding, vocab)
print(weight_matrix)

Variables defined inside your function can only be accessed within that function. The code below should let you print the value of vocab_size when you call the function

def get_weight_matrix(embedding, vocab):
    vocab_size = len(vocab) + 1
    print(vocab_size)
    weight_matrix = np.zeros((vocab_size, 100))
    for word, i in vocab.items():
        weight_matrix[i] = embedding.get(word)
    return weight_matrix

get_weight_matrix()

Alternatively, you can return a tuple, which contains multiple values. Because you return the value you can assign the value outside your function

def get_weight_matrix(embedding, vocab):
    vocab_size = len(vocab) + 1
    print(vocab_size)
    weight_matrix = np.zeros((vocab_size, 100))
    for word, i in vocab.items():
        weight_matrix[i] = embedding.get(word)
    return (weight_matrix, vocab_size)

weight_matrix, vocab_size = get_weight_matrix(embedding, vocab)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 mlb6300
Solution 3 psychonomics