roatienza / Deep-Learning-Experiments

Videos, notes and experiments to understand deep learning
MIT License
1.1k stars 760 forks source link

Corrected 'unhashable type: list' error #16

Closed shubajitsaha closed 6 years ago

shubajitsaha commented 6 years ago

While running the code with the input file having multiple lines, I encountered the following error: Traceback (most recent call last): File "rnn_words.py", line 56, in <module> dictionary, reverse_dictionary = build_dataset(training_data) File "rnn_words.py", line 49, in build_dataset count = collections.Counter(words).most_common() File "/home/shubajit/anaconda3/lib/python3.6/collections/__init__.py", line 535, in __init__ self.update(*args, **kwds) File "/home/shubajit/anaconda3/lib/python3.6/collections/__init__.py", line 622, in update _count_elements(self, iterable) TypeError: unhashable type: 'list' On debugging the stack trace, I found out that the issue was with np.array() function which failed to convert a 2D list to a numpy array if the sublists differ in size. Thus the return value is a numpy array of python lists. Consequentially, the function collections.Counter() throws an exception unhashable type: 'list' because python lists are unhashable and hence cannot be a dictionary key. My solution is to create a simple list of all the words and return after converting to numpy array.