Skip to content

sentiment analysis code of LSTM #3

@wangjiangchuan

Description

@wangjiangchuan

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions