As I was fitting various artificial neural network, the code for TLNN was shown as incorrect especially the line containing the reshape function. Is there anything wrong with this? Does changing the data set imply that the reshape function wont work and shows this error
def forecast_TLNN(model, time_lagged_points, last_sequence, future_steps):
forecasted_values = []
max_lag = max(time_lagged_points)
for i in range(future_steps):
input_sequence = [last_sequence[max_lag - p] for p in time_lagged_points]
forecasted_value = model.predict(np.reshape(input_sequence, (1, len(input_sequence))))
forecasted_values.append(forecasted_value[0][0])
last_sequence = last_sequence[1:] + [forecasted_value[0][0]]
return forecasted_values
the error was shown in the line forecasted_value = model.predict(np.reshape(input_sequence, (1, len(input_sequence))))
I cant seem to find any corrections regarding this code all over the internet.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[83], line 13
10 # look_back, hidden_nodes, output_nodes, epochs, batch_size, future_steps
11 parameters_LSTM = [[1,2,3,4,5,6,7,8,9,10,11,12,13], [3,4,5,6], [1], [300], [20], [future_steps]]
---> 13 RMSE_info = compare_ANN_methods(rainfall_data, test_rainfall_data, scaler, parameters_FNN, parameters_TLNN, parameters_SANN, parameters_LSTM, future_steps)
Cell In[79], line 6, in compare_ANN_methods(rainfall_data, test_rainfall_data, scaler, parameters_FNN, parameters_TLNN, parameters_SANN, parameters_LSTM, future_steps)
3 information_FNN_df = get_accuracies_FNN(rainfall_data, test_rainfall_data, parameters_FNN, scaler)
4 optimized_params_FNN = analyze_results(information_FNN_df, test_rainfall_data, 'FNN')
----> 6 information_TLNN_df = get_accuracies_TLNN(rainfall_data, test_rainfall_data, parameters_TLNN, scaler)
7 optimized_params_TLNN = analyze_results(information_TLNN_df, test_rainfall_data, 'TLNN')
9 information_SANN_df = get_accuracies_SANN(rainfall_data, test_rainfall_data, parameters_SANN, scaler)
Cell In[55], line 21, in get_accuracies_TLNN(rainfall_data, test_rainfall_data, parameters, scaler)
18 batch_size = param[4]
19 future_steps = param[5]
---> 21 model_TLNN, forecasted_values_TLNN = TLNN(rainfall_data, time_lagged_points, hidden_nodes, output_nodes, epochs, batch_size, future_steps, scaler)
23 y_true = test_rainfall_data.iloc[:future_steps].Precipitation
24 mse, mae, mape, rmse = calculate_performance(y_true, forecasted_values_TLNN)
Cell In[53], line 9, in TLNN(data, time_lagged_points, hidden_nodes, output_nodes, epochs, batch_size, future_steps, scaler)
6 model_TLNN = train_model(model_TLNN, X_train, y_train, epochs, batch_size)
8 max_lag = max(time_lagged_points)
----> 9 forecasted_values_TLNN = forecast_TLNN(model_TLNN, time_lagged_points,
10 list(data[-max_lag:]), future_steps=future_steps)
11 forecasted_values_TLNN = list(scaler.inverse_transform([forecasted_values_TLNN])[0])
13 return model_TLNN, forecasted_values_TLNN
Cell In[51], line 6, in forecast_TLNN(model, time_lagged_points, last_sequence, future_steps)
4 for i in range(future_steps):
5 input_sequence = [last_sequence[max_lag - p] for p in time_lagged_points]
----> 6 forecasted_value = model.predict((np.reshape(input_sequence, (1, len(input_sequence)))))
7 forecasted_values.append(forecasted_value[0][0])
8 last_sequence = last_sequence[1:] + [forecasted_value[0][0]]
File ~anaconda3Libsite-packagesnumpycorefromnumeric.py:285, in reshape(a, newshape, order)
200 @array_function_dispatch(_reshape_dispatcher)
201 def reshape(a, newshape, order='C'):
202 """
203 Gives a new shape to an array without changing its data.
204
(...)
283 [5, 6]])
284 """
--> 285 return _wrapfunc(a, 'reshape', newshape, order=order)
File ~anaconda3Libsite-packagesnumpycorefromnumeric.py:56, in _wrapfunc(obj, method, *args, **kwds)
54 bound = getattr(obj, method, None)
55 if bound is None:
---> 56 return _wrapit(obj, method, *args, **kwds)
58 try:
59 return bound(*args, **kwds)
File ~anaconda3Libsite-packagesnumpycorefromnumeric.py:45, in _wrapit(obj, method, *args, **kwds)
43 except AttributeError:
44 wrap = None
---> 45 result = getattr(asarray(obj), method)(*args, **kwds)
46 if wrap:
47 if not isinstance(result, mu.ndarray):
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (5,) + inhomogeneous part.