From c10e439409835d0e543fbedab3cadec92dc5d795 Mon Sep 17 00:00:00 2001 From: "Sebastian M. Ernst" Date: Sun, 7 Apr 2019 21:44:29 +0200 Subject: [PATCH 1/2] referencing length of a dict inside a for loop, which is changing the dict itself, is error-prone --- Experiments/Tensorflow/RNN/rnn_words.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Experiments/Tensorflow/RNN/rnn_words.py b/Experiments/Tensorflow/RNN/rnn_words.py index 2c7eb3d..3d42772 100644 --- a/Experiments/Tensorflow/RNN/rnn_words.py +++ b/Experiments/Tensorflow/RNN/rnn_words.py @@ -46,9 +46,7 @@ def read_data(fname): def build_dataset(words): count = collections.Counter(words).most_common() - dictionary = dict() - for word, _ in count: - dictionary[word] = len(dictionary) + dictionary = {word: rank for rank, (word, _) in enumerate(count)} reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys())) return dictionary, reverse_dictionary @@ -177,4 +175,3 @@ def RNN(x, weights, biases): print(sentence) except: print("Word not in dictionary") - From 34982e151f4426d70f6a0dcb0c9b2154ccc0f554 Mon Sep 17 00:00:00 2001 From: "Sebastian M. Ernst" Date: Sun, 7 Apr 2019 21:47:04 +0200 Subject: [PATCH 2/2] accessing keys and values of a dict separately can fail - order is not guaranteed to be stable between calls --- Experiments/Tensorflow/RNN/rnn_words.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Experiments/Tensorflow/RNN/rnn_words.py b/Experiments/Tensorflow/RNN/rnn_words.py index 3d42772..4c55d58 100644 --- a/Experiments/Tensorflow/RNN/rnn_words.py +++ b/Experiments/Tensorflow/RNN/rnn_words.py @@ -47,7 +47,7 @@ def read_data(fname): def build_dataset(words): count = collections.Counter(words).most_common() dictionary = {word: rank for rank, (word, _) in enumerate(count)} - reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys())) + reverse_dictionary = {v: k for k, v in dictionary.items()} return dictionary, reverse_dictionary dictionary, reverse_dictionary = build_dataset(training_data)