Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sentiment analysis code of LSTM #3

Open
wangjiangchuan opened this issue Oct 22, 2017 · 0 comments
Open

sentiment analysis code of LSTM #3

wangjiangchuan opened this issue Oct 22, 2017 · 0 comments

Comments

@wangjiangchuan
Copy link

in your LSTM code for sentiment analysis, it goes like:
lstmCell = tf.contrib.rnn.BasicLSTMCell(lstmUnits) lstmCell = tf.contrib.rnn.DropoutWrapper(cell=lstmCell, output_keep_prob=0.25) value, _ = tf.nn.dynamic_rnn(lstmCell, data, dtype=tf.float32) weight = tf.Variable(tf.truncated_normal([lstmUnits, numClasses])) bias = tf.Variable(tf.constant(0.1, shape=[numClasses])) value = tf.transpose(value, [1, 0, 2]) last = tf.gather(value, int(value.get_shape()[0]) - 1) prediction = (tf.matmul(last, weight) + bias)
why not use the second value from function dynamic_rnn() ?
the second value is the final_state of LSTM cell, that is a tuple of (h, Ct).
The h is the output of the last cell, so you can just code like this
value, (c, h) = tf.nn.dynamic_rnn(lstmcell, data, dtype=tf.float32)
When get the h, we can directly apply the multiplication to it.
so, there is no need to use this:
value = tf.transpose(value, [1, 0, 2]) last = tf.gather(value, int(value.get_shape()[0]) - 1)
so, this is just some advices, in fact your tutorials are really good, Greet appreciates!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant