CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

How to extract the cell state and hidden state from an RNN model in CNTK?

Open noble6emc2 opened this issue 8 years ago • 5 comments

I know there is a way for tensorflow to do this, but I could not find any instructions neither online or in API document. I'm new to CNTK and wonder how to do this.

noble6emc2 avatar Feb 06 '18 13:02 noble6emc2

To be more specific, I only need the previous hidden state, because it is crucial in attention modeling. So is there way to do it? Best regards.

noble6emc2 avatar Feb 06 '18 14:02 noble6emc2

It sounds like a delay layer.

CynsierWang avatar Feb 06 '18 18:02 CynsierWang

def OneWordLookback(): x = C.placeholder() apply_x = C.splice(x, C.sequence. past_value(x)) return apply_x

def create_model(): with C.layers.default_options(initial_state=0.1): return C.layers.Sequential([ C.layers.Embedding(emb_dim), OneWordLookback(), C.layers.Recurrence(C.layers.LSTM(hidden_dim), go_backwards=False), C.layers.Dense(num_labels) ])` past_value() function returns the past value. The resulting tensor has the same shape as the input but is the previous logical sample.

CynsierWang avatar Feb 06 '18 18:02 CynsierWang

Can you try layers.RecurrenceFrom?

ke1337 avatar Feb 09 '18 21:02 ke1337

@CynsierWang I noticed that there is a placeholder in OneWordLookback(). But how can you bound it to an actual variable? Does something do it automatically?

whuala avatar Feb 11 '18 09:02 whuala