Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

tensorflow - Keras Tokenizer: How to disable elements in dictionary?

t = Tokenizer(num_words=50000)
tokenizer.fit_on_texts(trainText)
my_items = list(tokenizer.word_index.items())  

I need to sequentially disable blocks of 100 elements from the dictionary and check how disabling a particular block affects the accuracy of the network. How can I disable, say, the first hundred dictionary elements? Size of my_items in my case is 140000. I need use just first 50000 elements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

below is a function that given an input word index, will return a new word index that is the same as the original word index but excludes entries from start_number through end_number.

my_dict={} #create a dictionary where the values are integers
for i in range (10):
    my_dict[i]=i

# function to return a dictionary identical to input dictionary but missing entries from start_number through end_number
def dump_words(word_index, start_number, end_number):
    temp_dict={}
    new_dict={}
    for key, value in word_index.items():
        if value<start_number or value>end_number:
            temp_dict[key]=value
    # now renumber the values in the dictionary 
    for key, value in temp_dict.items():
        if value<start_number:
            new_dict[key]=value
        else:
            new_dict[key]=value-end_number
    return new_dict

new_dict=dump_words(my_dict, 1, 5)
for key, value in new_dict.items():
    print (key,value)
    
# printout shows dictionay items 0 through 4 are eliminated

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...