nicodjimenez / lstm

Minimal, clean example of lstm neural network training in python, for learning purposes.
1.72k stars 654 forks source link

line 85&86:if s_prev == None: s_prev = np.zeros_like(self.state.s) #28

Open dingqunfei opened 6 years ago

dingqunfei commented 6 years ago

when run test.py, there is a error. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() changing the '==' of two lines to 'is' is right, it likes 'if s_prev is None: s_prev = np.zeros_like(self.state.s)'

zhyma commented 6 years ago

one walk around is to replace that line by: if not isinstance(s_prev,list): s_prev = np.zeros_like(self.state.s) and the following line: if not isinstance(h_prev,list): h_prev = np.zeros_like(self.state.h)

This also happens to my Spyder 3.1.2 with Python 2.7.13 64bit.

tldrafael commented 6 years ago

this change gets to work to me, @zhyma , thanks.

but i'd like to know how not isinstance(s_prev, list) is equal to s_prev == None ?

because, i'm debugging the code here, and the expected class to s_prev is numpy.ndarray, so both values, this array and None will be evaluate as False in isinstance(s_prev, list)

zhyma commented 6 years ago

@tldrafael You are correct. I just got it wrong. With "if not isinstance(s_prev,list)", it runs into resetting s_prev as a zeros_like matrix everytime. Although it is running, the 'H' cell and the 'C' cell are not working properly. Thanks for pointing that out!

tldrafael commented 6 years ago

The problem seems that you can't compare an array to a single None. To solve that i compare the class of the object to Nonetype if s_prev.__class__ == 'NoneType', but after this the script raises another error ValueError: shapes (100,150) and (51,) not aligned: 150 (dim 1) != 51 (dim 0). I'm struggling here.

zhyma commented 6 years ago

I think using "==" to compare different types is an out-of-date method. I remember it was running correctly on my other machine with an old version of Python but not the up-to-date one. Haven't checked the Python manual yet.