TimeDistributed(Dense) vs Dense in Keras – Same number of parameters

TimeDistributedDense applies a same dense to every time step during GRU/LSTM Cell unrolling. So the error function will be between predicted label sequence and the actual label sequence. (Which is normally the requirement for sequence to sequence labeling problems). However, with return_sequences=False, Dense layer is applied only once at the last cell. This is normally … Read more

ValueError: Input 0 is incompatible with layer lstm_13: expected ndim=3, found ndim=4

I solved the problem by making input size: (95000,360,1) and output size: (95000,22) and changed the input shape to (360,1) in the code where model is defined: model = Sequential() model.add(LSTM(22, input_shape=(360,1))) model.add(Dense(22, activation=’softmax’)) model.compile(loss=”categorical_crossentropy”, optimizer=”adam”, metrics=[‘accuracy’]) print(model.summary()) model.fit(ml2_train_input, ml2_train_output_enc, epochs=2, batch_size=500)

Pytorch – RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed

The problem is from my training loop: it doesn’t detach or repackage the hidden state in between batches? If so, then loss.backward() is trying to back-propagate all the way through to the start of time, which works for the first batch but not for the second because the graph for the first batch has been … Read more

Why do we “pack” the sequences in PyTorch?

I have stumbled upon this problem too and below is what I figured out. When training RNN (LSTM or GRU or vanilla-RNN), it is difficult to batch the variable length sequences. For example: if the length of sequences in a size 8 batch is [4,6,8,5,4,3,7,8], you will pad all the sequences and that will result … Read more

TensorFlow: Remember LSTM state for next batch (stateful LSTM)

I found out it was easiest to save the whole state for all layers in a placeholder. init_state = np.zeros((num_layers, 2, batch_size, state_size)) … state_placeholder = tf.placeholder(tf.float32, [num_layers, 2, batch_size, state_size]) Then unpack it and create a tuple of LSTMStateTuples before using the native tensorflow RNN Api. l = tf.unpack(state_placeholder, axis=0) rnn_tuple_state = tuple( [tf.nn.rnn_cell.LSTMStateTuple(l[idx][0], … Read more

Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29)

Setting timesteps = 1 (since, I want one timestep for each instance) and reshaping the X_train and X_test as: import numpy as np X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1])) X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1])) This worked!

Many to one and many to many LSTM examples in Keras

So: One-to-one: you could use a Dense layer as you are not processing sequences: model.add(Dense(output_size, input_shape=input_shape)) One-to-many: this option is not supported well as chaining models is not very easy in Keras, so the following version is the easiest one: model.add(RepeatVector(number_of_times, input_shape=input_shape)) model.add(LSTM(output_size, return_sequences=True)) Many-to-one: actually, your code snippet is (almost) an example of this … Read more

Keras : How should I prepare input data for RNN?

If you only want to predict the output using the most recent 5 inputs, there is no need to ever provide the full 600 time steps of any training sample. My suggestion would be to pass the training data in the following manner: t=0 t=1 t=2 t=3 t=4 t=5 … t=598 t=599 sample0 |———————| sample0 … Read more

tech