python - Reshape Keras Input for LSTM -
i have 2 ndarrays, inputs , results, both consisting of multiple arrays looking this:
inputs = [ [[1,2],[2,2],[3,2]], [[2,1],[1,2],[2,3]], [[2,2],[1,1],[3,3]], ... ] results = [ [3,4,5], [3,3,5], [4,2,6], ... ]
i managed split them train , test arrays, train contains 66% of arrays , test other 33%. i'd reshape them further use in lstm script fails when inputting them np.reshape() function.
split = int(round(0.66 * results.shape[0])) train_results = results[:split, :] train_inputs = inputs[:split, :] test_results = results[split:, :] test_inputs = inputs[split:, :] x_train = np.reshape(train_inputs, (train_inputs.shape[0], train_inputs.shape[1], 1)) x_test = np.reshape(test_inputs, (test_inputs.shape[0], test_inputs.shape[1], 1))
please tell me how use np.reshape() correctly in case.
basically loosely following tutorial: https://github.com/vict0rsch/deep_learning/tree/master/keras/recurrent
you pass tuple np.reshape
.
for lstm layer, need shape (numberofexamples, timesteps, featuresperstep)
.
so, need know how many steps sequence has. looks of x array, i'll suppose have 3 steps , 2 features.
if that's case:
x_train = train_inputs.reshape((split,3,2)) x_test = x_test.reshape((test_inputs.shape[0], 3, 2))
if, otherwise, want 6 steps of 1 feature, shape (split,6,1)
. can anything, long multiplication of 3 elements in shape must remain same
for results. want results result in sequence, matching input steps? or single outputs (two independent outputs entire sequence)?
since you've got 3 results, , have assumed have 3 time steps, i'll assume these 3 results in sequence well, so, i'll reshape them as:
y_train = train_results.reshape((split,3,1)) #three steps, 1 result per step #for work, last lstm layer should use `return_sequences=true`.
but if 3 independent results:
y_train = train_results.reshape((split,3)) #for work, must have 3 cells in last layer, dense or lstm. lstm must have `return_sequences=false`.
Comments
Post a Comment