Marie_Isa
Marie_Isa

Reputation: 47

For-Loop stops working after 2 iterations

I am setting up a temporal convolutional network in tensorflow 2.0 using Keras API for timeseries prediction and I am currently working on predicting the next timesteps. Now I have a problem with this for-loop:

def make_prediction(model, x_to_predict):
    next_step = model.predict(x_to_predict)
    return next_step

sequence = x_pred[0, :150, 0]

for next_timestep in range(20):
    range_to_predict = 150 + next_timestep
    sequence = sequence[next_timestep: range_to_predict].reshape([1, 150, 1])
    next_datapoint = make_prediction(model, sequence)
    sequence = np.append(sequence, next_datapoint)
    print("Timestep" + str(next_timestep))
    print("Predicted Value" + str(next_datapoint))

This loop works for the first two iterations, then it stops with following error:

Timestep0 Predicted Value[[0.49933335]] Timestep1 Predicted Value[[0.5245512]] Traceback (most recent call last): File "E:/...", line 105, in sequence = sequence[next_timestep:range_to_predict].reshape([1, 150, 1]) ValueError: cannot reshape array of size 149 into shape (1,150,1)

My model takes an input of shape (None, 150, 1) and I don't understand why it does work for the first 2 iterations.

I would appreciate any suggestions how to solve my problem

Upvotes: 0

Views: 190

Answers (1)

Elmir
Elmir

Reputation: 180

I mean, func sequence[next_timestep: range_to_predict] creates sequence with length range_to_predict - next_timestep. On first iteration next_timestep equals 0, and sequence has length 150. On second iteration next_timestep equals 1, and predict - next_timestep = 149, then sequence has shape 149, and can't be shaped to (1,150,1).

Upvotes: 1

Related Questions