model = Sequential()
model.add(LSTM(512, input_dim = 4, return_sequences = True))
model.add(TimeDistributed(Dense(4)))
model.add(Activation('softmax'))
The input here is the one hot representation of a string and the dictionary size is set to be 4. In other word, there are four types of chars in this string. The output here is the probabilities that the next char ought to be.
If the length of input sequence is 1, the output dimension is 4 by 1. I just wonder could I feed the output back to the input and get an arbitrary length of output sequence (illustrated as follows). It may not be reasonable to plug back the probabilities but I just want to know the possibility to implement this one-to-many structure in keras. Thanks.
Example:
input1 -(LSTM)-> output1
output1 -(LSTM) -> output2
output2 - (LSTM) -> output3
We could get a 4 by 3 output in the end.
You have to do it in an external loop, as in
seq = [np.random.rand(4)]
for i in range(n_iter):
seq.append(model.predict(seq))
Thanks, kgrm. But how about the training process? If you add an external loop, I don't think model.fit( ) will work.
It will, but you'll need to add a Masking layer to train it on arbitrary-length (eg, zero-padded) sequences.
actually, I think he will have to write his own custom layer to do that. See this DreamyRNN for example: https://github.com/commaai/research/blob/master/models/layers.py#L334-L397
It takes a n frames and input and outputs n+m where the last m frames are generated by feeding outputs back as input.
That's not the case, you just have to reframe and rearrange your training data accordingly for the n+1-th step prediction task.
Thanks, EderSantana.
To kgrm: As I expect, only the first char is the input. In this scenario, I don't think the external loop will help construct the right output, which illustrated as follows,
1st char -> (LSTM) -> output1 (one single of LSTM parameters embedded)
output1 -> (LSTM) -> output2 (two sets of LSTM parameters convoluted with each other)
It's this convolution that makes things complicated. Thanks for your reply.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.
I have the same problem. Is there any way around this issue?
I switched to tensorflow and wrote everything from scratch.
@wgmao Can you share your code here please that you wrote in tensorflow? Thanks
I referred to https://github.com/LantaoYu/SeqGAN/blob/e2b52fb6309851b14765290e8a972ccac09f1bec/target_lstm.py to write customized recurrent layers.
Most helpful comment
actually, I think he will have to write his own custom layer to do that. See this
DreamyRNNfor example: https://github.com/commaai/research/blob/master/models/layers.py#L334-L397It takes a
nframes and input and outputsn+mwhere the lastmframes are generated by feeding outputs back as input.