Here's my code : import matplotlib.pyplot as plt import tensorflow as tf from tensorflow…
- User
Here's my code :
import matplotlib.pyplot as plt import tensorflow as tf from tensorflow import keras from keras.models import Sequential from keras.layers import Dense from firebase import firebase import firebase_admin from firebase_admin import credentials from firebase_admin import db import numpy as np from sklearn.model_selection import train_test_split from keras.layers import Dropout, Dense, LSTM, SimpleRNN import pandas from sklearn.preprocessing import MinMaxScaler
def generate_future_timesteps(data1, data2, data3, data4, num_timesteps): # Use the last
num_timestepsdata points as inputmax_length = max(len(data1), len(data2), len(data3), len(data4)) future_input = np.zeros((4, max_length, 1)) future_input[0, :len(data1), :] = np.array(data1).reshape(-1, 1) future_input[1, :len(data2), :] = np.array(data2).reshape(-1, 1) future_input[2, :len(data3), :] = np.array(data3).reshape(-1, 1) future_input[3, :len(data4), :] = np.array(data4).reshape(-1, 1) # Reshape the input data to match the model's input shape future_input = future_input[:, :num_timesteps, :] future_input = np.swapaxes(future_input, 0, 1) return future_inputdef create_dataset(dataset, look_back): dataX, dataY = [], [] for i in range(len(dataset) - look_back - 1): a = dataset[i:(i + look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, :]) return np.array(dataX), np.array(dataY)
def create_rnn_model(trainX, trainY, testX, testY, rnn_units, look_back, input_shape = 0, name='RNN Model'): # Define the RNN model
batch = 16 if input_shape == 0: input_shape = (1, look_back) model = Sequential() model.add(SimpleRNN(rnn_units[0], activation='tanh', input_shape=input_shape, return_sequences=True)) model.add(SimpleRNN(rnn_units[1], activation='tanh')) model.add(Dense(rnn_units[2])) model.add(Dense(4)) # Compile the model model.compile(optimizer='adam', loss='mae') # Train the model on the training data history = model.fit(trainX, trainY, epochs=25, batch_size=batch, verbose=1, validation_split=0.2) # Evaluate the model on the testing data loss = model.evaluate(testX, testY, batch_size=batch) print(f'{name} loss: {loss}') return modeldef create_lstm_model(trainX, trainY, testX, testY, lstm_units, look_back, input_shape = 0, name='LSTM Model'): # Define the LSTM autoencoder model
batch = 16 if input_shape == 0: input_shape = (1, look_back) model = Sequential() model.add(LSTM(lstm_units[0], activation='relu', input_shape = input_shape, return_sequences = True)) model.add(LSTM(lstm_units[1], activation='relu', return_sequences = True)) model.add(LSTM(lstm_units[2], activation='relu')) model.add(Dense(lstm_units[3])) model.add(Dense(4)) # Compile the model model.compile(optimizer='adam', loss ='mae') # Train the model on the training data history = model.fit(trainX, trainY, epochs=25, batch_size=batch, verbose=1, validation_split=0.2) # Evaluate the model on the testing data loss = model.evaluate(testX, testY, batch_size=batch) print(f'{name} loss: {loss}') return modeldef ensemble_models(data1, data2, data3, data4, look_back): min_length = min(len(data1), len(data2), len(data3), len(data4))
# Slice each array to the minimum length data1 = data1[len(data1) - min_length:] data2 = data2[len(data2) - min_length:] data3 = data3[len(data3) - min_length:] data4 = data4[len(data4) - min_length:] # Pad the sequences with zeros so that they all have the same length max_length = max(len(data1), len(data2), len(data3), len(data4)) all_data = np.zeros((4, max_length, 1)) for i, data in enumerate([data1, data2, data3, data4]): all_data[i, :len(data), :] = np.array(data).reshape(-1, 1) d = {'hum': data1, 'light': data2, 'temp': data3, 'mois': data4} dataset = pandas.DataFrame(d) dataset = dataset.values dataset = dataset.astype("float32") scaler = MinMaxScaler() train_size = int(len(dataset) * 0.7) test_size = len(dataset) - train_size train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :] scaler.fit(train) train = scaler.transform(train) test = scaler.transform(test) trainX, trainY = create_dataset(train, look_back=look_back) testX, testY = create_dataset(test, look_back=look_back) trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) # Create the first LSTM model lstm1 = create_lstm_model(trainX, trainY, testX, testY, lstm_units=[128, 64, 32, 64], look_back=look_back) # Create the second LSTM model with a different architecture #lstm2 = create_lstm_model(trainX, trainY, testX, testY, lstm_units=[64, 32, 16, 32], name='LSTM Model 2') rnn = create_rnn_model(trainX, trainY, testX, testY, rnn_units=[128, 64, 32, 64], look_back=look_back) # Get predictions from both models predictions1 = lstm1.predict(testX) predictions2 = rnn.predict(testX) predictions1 = scaler.inverse_transform(predictions1) predictions2 = scaler.inverse_transform(predictions2) # Combine the predictions using a weighted average ensemble_weight_1 = 0.5 ensemble_weight_2 = 1.0 - ensemble_weight_1 ensemble_predictions = ensemble_weight_1 * predictions1 + ensemble_weight_2 * predictions2 # Evaluate the ensemble model’s performance testY = scaler.inverse_transform(testY) print_accuracy(ensemble_predictions, testY) # Now make predictions using the reinitialized model future_input = generate_future_timesteps(data1, data2, data3, data4, look_back) future_input = np.reshape(future_input, (-1, 1, look_back)) # Scale the input data future_input = future_input.reshape(-1, 4) future_input = scaler.transform(future_input) future_input = future_input.reshape(-1, 1, look_back) # Get predictions for future timesteps future_predictions1 = lstm1.predict(future_input) future_predictions2 = rnn.predict(future_input) # Revert the predictions back to original scaling future_predictions1 = scaler.inverse_transform(future_predictions1) future_predictions2 = scaler.inverse_transform(future_predictions2) # Combine the predictions using a weighted average future_ensemble_predictions = (ensemble_weight_1 * future_predictions1 + ensemble_weight_2 * future_predictions2) # Reshape array2 to match the first dimension of array1 array2_reshaped = np.repeat(future_ensemble_predictions, ensemble_predictions.shape[0] // future_ensemble_predictions.shape[0], axis=0) # Concatenate the reshaped arrays along axis 0 future_ensemble_predictions = np.concatenate((ensemble_predictions, array2_reshaped), axis=0) plot_data(ensemble_predictions, future_ensemble_predictions, testY) return lstm1, rnndef print_accuracy(ensemble_predictions, testY): mae = np.mean(np.abs(ensemble_predictions - testY), axis=0) print(f'Ensemble MAE: {mae}')
max_humidity = 100 # Set the maximum possible value for humidity max_light = 100 # Set the maximum possible value for light max_temperature = 50 # Set the maximum possible value for temperature max_moisture = 100 # Set the maximum possible value for moisture mae_humidity = np.mean(np.abs(ensemble_predictions[:, 0] - testY[:, 0])) mae_light = np.mean(np.abs(ensemble_predictions[:, 1] - testY[:, 1])) mae_temperature = np.mean(np.abs(ensemble_predictions[:, 2] - testY[:, 2])) mae_moisture = np.mean(np.abs(ensemble_predictions[:, 3] - testY[:, 3])) percentage_error_humidity = (mae_humidity / max_humidity) * 100 percentage_error_light = (mae_light / max_light) * 100 percentage_error_temperature = (mae_temperature / max_temperature) * 100 percentage_error_moisture = (mae_moisture / max_moisture) * 100 print(f'Humidity Percentage Error: {round(percentage_error_humidity, 2)}%') print(f'Light Percentage Error: {round(percentage_error_light, 2)}%') print(f'Temperature Percentage Error: {round(percentage_error_temperature, 2)}%') print(f'Moisture Percentage Error: {round(percentage_error_moisture, 2)}%') print(f'Mean error: {round(np.mean([percentage_error_light, percentage_error_humidity, percentage_error_moisture, percentage_error_temperature]), 2)}%') return 0def plot_data(ensemble_predictions, future_ensemble_predictions, testY): # (Optional) Visualization code
plt.figure(figsize=(16, 8)) predic_input = input("Comparer les prédictions avec les mesures ou prédire les mesures futures ? c/f") if predic_input == 'c': plt.plot(ensemble_predictions[:, 0], label='Humidity Predictions', linestyle='-') plt.plot(ensemble_predictions[:, 1], label='Light Predictions', linestyle='-') plt.plot(ensemble_predictions[:, 2], label='Temperature Predictions', linestyle='-') plt.plot(ensemble_predictions[:, 3], label='Moisture Predictions', linestyle='-') elif predic_input == 'f': plt.plot(future_ensemble_predictions[:, 0], label='Humidity Predictions', linestyle='-') plt.plot(future_ensemble_predictions[:, 1], label='Light Predictions', linestyle='-') plt.plot(future_ensemble_predictions[:, 2], label='Temperature Predictions', linestyle='-') plt.plot(future_ensemble_predictions[:, 3], label='Moisture Predictions', linestyle='-') else: print("Incorrect input received, plotting measured predictions...") plt.plot(ensemble_predictions[:, 0], label='Humidity Predictions', linestyle='-') plt.plot(ensemble_predictions[:, 1], label='Light Predictions', linestyle='-') plt.plot(ensemble_predictions[:, 2], label='Temperature Predictions', linestyle='-') plt.plot(ensemble_predictions[:, 3], label='Moisture Predictions', linestyle='-') plt.plot(testY[:, 0], label='Humidity Real', alpha=0.7, linestyle='dashed') plt.plot(testY[:, 1], label='Light Real', alpha=0.7, linestyle='dashed') plt.plot(testY[:, 2], label='Temperature Real', alpha=0.7, linestyle='dashed') plt.plot(testY[:, 3], label='Moisture Real', alpha=0.7, linestyle='dashed') plt.xlabel('Time Steps') plt.ylabel('Sensor Values') plt.legend() plt.show() data = input("print predicted data ? y/n") if data == "y": print(future_ensemble_predictions) return 0def start_firebase(): # Fetch the service account key JSON file contents cred = credentials.Certificate('gunadarma-monitoring-firebase-adminsdk-t4zec-95d7f006d3.json') # Initialize the app with a service account, granting admin privileges firebase_admin.initialize_app(cred, { 'databaseURL': "https://gunadarma-monitoring-default-rtdb.asia-southeast1.firebasedatabase.app" })
humidity = db.reference('/live/Humidity').get() light = db.reference('/live/Luminosity').get() temp = db.reference('/live/Temperature').get() moisture = db.reference('/live/moisture').get() print("humidity : ", humidity) print("luminosity : ", light) print("temperature : ", temp) print("moisture : ", moisture) record_hum = db.reference('/previous_Humidity').get() humrecord_list = [i for i in record_hum.values()] record_light = db.reference('/previous_Luminosity').get() lightrecord_list = [i for i in record_light.values()] record_temp = db.reference('/previous_Temperature').get() temprecord_list = [i for i in record_temp.values()] record_mois = db.reference('/previous_moisture').get() moisrecord_list = [i for i in record_mois.values()] return humrecord_list, lightrecord_list, temprecord_list, moisrecord_listdef remove_outliers(data1, data2, data3, data4, threshold=1):
humrecord_list = np.array(data1) lightrecord_list = np.array(data2) temprecord_list = np.array(data3) moisrecord_list = np.array(data4) z_humidity = np.abs((humrecord_list - np.mean(humrecord_list)) / np.std(humrecord_list)) z_light = np.abs((lightrecord_list - np.mean(lightrecord_list)) / np.std(lightrecord_list)) z_temperature = np.abs((temprecord_list - np.mean(temprecord_list)) / np.std(temprecord_list)) z_moisture = np.abs((moisrecord_list - np.mean(moisrecord_list)) / np.std(moisrecord_list)) # Remove any data point that has a z-score greater than the threshold humrecord_list = humrecord_list[z_humidity < threshold] lightrecord_list = lightrecord_list[z_light < threshold] temprecord_list = temprecord_list[z_temperature < threshold] moisrecord_list = moisrecord_list[z_moisture < threshold] return humrecord_list, lightrecord_list, temprecord_list, moisrecord_listif name == 'main':
humrecord_list, lightrecord_list, temprecord_list, moisrecord_list = start_firebase() humrecord_list, lightrecord_list, temprecord_list, moisrecord_list = remove_outliers(humrecord_list, lightrecord_list, temprecord_list, moisrecord_list, threshold=3) if input("plot les données ? ") == "o": plt.figure(figsize=(16, 8)) plt.plot(humrecord_list, color='r', label='hum') plt.plot(lightrecord_list, color='g', label='light') plt.plot(temprecord_list, color='b', label='temp') plt.plot(moisrecord_list, color='y', label='mois') plt.legend() plt.show() print("humidity history : (size ", len(humrecord_list), ") ", humrecord_list) print("luminosity history : (size ", len(lightrecord_list), ") ", lightrecord_list) print("temperature history : (size ", len(temprecord_list), ") ", temprecord_list) print("moisture history : (size ", len(moisrecord_list), ") ", moisrecord_list) ensemble_models(humrecord_list, lightrecord_list, temprecord_list, moisrecord_list, look_back=200)My problem is about future timesteps prediction. In fact predictions based on testY work fine and gives decent results BUT when I ask the model to predict future timesteps that have not been measured yet, it gives me really basic results like constant values through the time for each category.
Here's some of the result, the first part being a part of the prediction that is already measured (testY) and the last part is the last bunch of predictions of the future. [[78.36468 79.17461 24.37408 6.0894547] [78.567215 78.888794 24.395845 5.9449997] [78.69241 78.38314 24.393354 5.8619213] ... [79.2414 83.293686 24.013659 4.751072 ] [79.2414 83.293686 24.013659 4.751072 ] [79.2414 83.293686 24.013659 4.751072 ]]
- Assistant
Your model might be predicting constant values for future timesteps because:
-
Insufficient training or underfitting: The model might not have learned the relationships between the features and targets enough to make meaningful future predictions. You can try increasing the number of training epochs, using a more complex architecture, or increasing the size of the look-back window.
-
Lack of diversity in training data: If the training data does not contain a variety of examples, it is difficult for the model to learn different patterns and generalize well to future data. In this case, try collecting more diverse data, downsampling noisy data or even augmenting to create more synthetic data.
To address the issue, I would recommend the following steps:
-
Check the loss value during training for both LSTM and RNN, as well as their performances in the test set. Make sure the models are not underfitting or overfitting.
-
Perform a grid search on the look_back window and the model architectures (like the number of LSTM or RNN cells and layers) to find the best combination for your specific problem.
-
Investigate the training data for any possible presence of outliers, constant regions, or noise that could negatively affect the learning process. If needed, perform data cleaning or preprocessing that helps to improve the quality of the data.
-
Try other approaches like using bidirectional LSTM, attention mechanisms, or even using different types of recurrent layers like GRU.
Remember that predicting future timesteps for time-series data is a challenging task, and depending on the nature of the data, sometimes it is not possible to get accurate predictions for distant future time steps. However, by following these steps, you should be able to improve the predictions made by your models.
-
- User
Can you give me code changes for each advice you gave me ?
- Assistant
Here are some code changes corresponding to the advice I gave you:
- Select the best model based on validation loss:
Increase the training epochs to 50 and save the model with the best validation loss by setting the
callbacksargument usingModelCheckpoint:from keras.callbacks import ModelCheckpoint ... # In create_rnn_model and create_lstm_model functions history = model.fit(trainX, trainY, epochs=50, batch_size=batch, verbose=1, validation_split=0.2, callbacks=[ModelCheckpoint('best_model.h5', save_best_only=True, monitor='val_loss')]) ...- Perform a grid search on the look_back window and the model architectures:
Use a for loop with different parameters for the
look_backwindow (e.g., 50, 100, 200) and the model architectures (e.g., change the number of LSTM/RNN cells and layers):# Grid search example for look_back window look_back_list = [50, 100, 200] for look_back in look_back_list: model = ensemble_models(humrecord_list, lightrecord_list, temprecord_list, moisrecord_list, look_back=look_back) ...Similarly, create another loop for different model architectures. For example, try having 1, 2 or 3 LSTM/RNN layers, and different numbers of cells in those layers.
- Make sure no noisy or constant data exists in the dataset:
[You already have a
remove_outliersfunction in your code that filters out any data point with a z-score higher than the threshold. Therefore, no changes are needed here.]- Use other approaches like Bidirectional LSTM or GRU layers:
In the
create_lstm_modelfunction, replace theLSTM()layer with aBidirectional(LSTM())layer, and to try Gated Recurrent Unit (GRU) layers, replaceLSTM()withGRU():from keras.layers import Bidirectional, GRU ... # In the create_lstm_model function model.add(Bidirectional(LSTM(lstm_units[0], activation='relu', input_shape=input_shape, return_sequences=True))) model.add(Bidirectional(LSTM(lstm_units[1], activation='relu', return_sequences=True))) model.add(Bidirectional(LSTM(lstm_units[2], activation='relu'))) # Or using GRU layers model.add(GRU(lstm_units[0], activation='relu', input_shape=input_shape, return_sequences=True)) model.add(GRU(lstm_units[1], activation='relu', return_sequences=True)) model.add(GRU(lstm_units[2], activation='relu')) ...Keep in mind that you can combine these approaches (like a grid search of look_back window and Bidirectional LSTM/GRU layers) to find the optimal set of parameters for your specific problem. Also, note that the execution time can significantly increase due to the additional loops and more complex models.
- User
I did all of the above, and none of these solutions did improve my results, do you have any other idea ?
- Assistant
I'm sorry to hear that the previous suggestions didn't improve your results. Here are a few more ideas for improving the model's predictions:
- Delta features: Instead of directly predicting the future values, you can predict the differences (deltas) between the current values and future values. This can sometimes help the model capture better the changes in the time series. To do this, you'll need to modify the create_dataset() function to return the differences and add those differences to the last known values after making predictions.
def create_delta_dataset(dataset, look_back): dataX, dataY = [], [] for i in range(len(dataset) - look_back - 1): a = dataset[i:(i + look_back), :] dataX.append(a) deltas = dataset[i + look_back, :] - dataset[i + look_back - 1, :] dataY.append(deltas) return np.array(dataX), np.array(dataY) ... # In the ensemble_models() function trainX, trainY = create_delta_dataset(train, look_back=look_back) testX, testY = create_delta_dataset(test, look_back=look_back) ... # After making predictions ensemble_predictions = ensemble_weight_1 * predictions1 + ensemble_weight_2 * predictions2 # Compute the actual predictions by adding the deltas to the last known values ensemble_predictions = test[:-1, :] + ensemble_predictions- Model stacking: Instead of averaging the predictions from LSTM and RNN directly, you can use another machine learning model like Random Forest or XGBoost to learn how to best combine the strengths of the LSTM and RNN models.
from xgboost import XGBRegressor ... # After getting predictions from the two models predictions1 = lstm1.predict(trainX) predictions2 = rnn.predict(trainX) train_predictions = np.concatenate((predictions1, predictions2), axis=1) # Train an XGBoost model xgb_model = XGBRegressor() xgb_model.fit(train_predictions, trainY) # Use the XGBoost model to combine the LSTM and RNN predictions ensemble_predictions = xgb_model.predict(np.concatenate((predictions1, predictions2), axis=1))- Multi-step forecasting: Instead of only predicting one future timestep, you can change your model to predict multiple future timesteps (e.g., the next 10 timesteps). This way, if there's any dependency between timesteps, the model can learn that relationship better.
4.Temporal fusion transformers: Temporal fusion transformers (TFT) have recently shown great performance on time-series tasks. You can try implementing a TFT or use any existing implementation for your climate monitoring problem.
Please remember that the forecasting task's difficulty is heavily dependent on the nature of the data, and sometimes getting accurate predictions for the far future is not possible. The performance of the model may also be limited by the available data quality and quantity. Feel free to experiment with other approaches or techniques based on your understanding of the problem and the data.
- User
How can implement solutions 3 and 4 in my code?
- Assistant
I'll provide you with the changes required to implement solutions 3 (Multi-step forecasting) and 4 (Temporal fusion transformers) in your current code.
Solution 3: Multi-step forecasting
To predict multiple future timesteps, modify the
create_datasetfunction and the model outputs. Let's say you want to predict the nextn_futuretimesteps.def create_multi_step_dataset(dataset, look_back, n_future): dataX, dataY = [], [] for i in range(len(dataset) - look_back - n_future): a = dataset[i:(i + look_back), :] dataX.append(a) dataY.append(dataset[(i + look_back):(i + look_back + n_future), :]) return np.array(dataX), np.array(dataY) # In the ensemble_models() function n_future = 10 # The number of future timesteps you want to predict trainX, trainY = create_multi_step_dataset(train, look_back=look_back, n_future=n_future) testX, testY = create_multi_step_dataset(test, look_back=look_back, n_future=n_future) ... # In the create_lstm_model and create_rnn_model functions model.add(Dense(n_future * 4)) ... # After training the model and making predictions testY = testY.reshape(-1, 4) ensemble_predictions = ensemble_predictions.reshape(-1, 4, n_future) # Compute the average of the ensemble_predictions along axis 2 ensemble_predictions = np.mean(ensemble_predictions, axis=2)Solution 4: Temporal fusion transformers (TFT)
Implementing a TFT from scratch can be complex, and I recommend using an existing library for this purpose. One such library is
pytorch-forecasting, which has an implementation of TFT compatible with PyTorch.Here's a high-level guide on how to use this library for your problem:
-
Install the library by running
pip install pytorch-forecasting. -
Convert your data to a suitable format for the library - this usually means putting the data in a pandas DataFrame with columns such as "time_idx" (time index), "target" (values to predict), and any other relevant features.
-
Split your data into training and validation sets.
-
Define the TFT model using
pytorch-forecasting.TemporalFusionTransformerclass with appropriate arguments. -
Train the model using the
pytorch-forecasting.Trainerclass. -
Make predictions using the trained TFT model.
You can find examples of how to use the library for various time series problems in the library's documentation, as well as the examples provided in the GitHub repository.
Note that
pytorch-forecastingis built on PyTorch, and you might need to refactor some parts of your code (e.g., data handling and preprocessing) to make it compatible with this library. -
- User
Traceback (most recent call last): File "C:\Users\alois\Documents\Gunadarma\main.py", line 347, in ensemble_models(humrecord_list, lightrecord_list, temprecord_list, moisrecord_list, look_back=200) File "C:\Users\alois\Documents\Gunadarma\main.py", line 132, in ensemble_models trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) File "<array_function internals>", line 180, in reshape File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\numpy\core\fromnumeric.py", line 298, in reshape return _wrapfunc(a, 'reshape', newshape, order=order) File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\numpy\core\fromnumeric.py", line 57, in _wrapfunc return bound(*args, **kwds) ValueError: cannot reshape array of size 7147200 into shape (8934,1,200)
Process finished with e
- Assistant
It seems the error occurred when trying to reshape the
trainXarray. The error is due to a mismatch in dimensions after applying the changes for multi-step forecasting. This might be because thecreate_multi_step_datasetfunction is returning a different dimension than expected. To fix the error, let's update thecreate_multi_step_datasetfunction and ensure the correct dimensions are returned:def create_multi_step_dataset(dataset, look_back, n_future): dataX, dataY = [], [] for i in range(len(dataset) - look_back - n_future): a = dataset[i:(i + look_back), :] dataX.append(a) dataY.append(dataset[(i + look_back):(i + look_back + n_future), :]) return np.array(dataX), np.array(dataY) # In the ensemble_models() function n_future = 10 # The number of future timesteps you want to predict trainX, trainY = create_multi_step_dataset(train, look_back=look_back, n_future=n_future) testX, testY = create_multi_step_dataset(test, look_back=look_back, n_future=n_future) # Ensure that trainY and testY have the correct dimensions trainY = trainY.reshape(trainY.shape[0], -1) testY = testY.reshape(testY.shape[0], -1) ... # In the create_lstm_model and create_rnn_model functions model.add(Dense(n_future * 4)) ... # After training the model and making predictions testY = testY.reshape(-1, 4) ensemble_predictions = ensemble_predictions.reshape(-1, 4, n_future) # Compute the average of the ensemble_predictions along axis 2 ensemble_predictions = np.mean(ensemble_predictions, axis=2)The changes above should reshape the
trainYandtestYarrays with the correct dimensions, avoiding the "cannot reshape array" error. - User
Traceback (most recent call last): File "C:\Users\alois\Documents\Gunadarma\main.py", line 349, in ensemble_models(humrecord_list, lightrecord_list, temprecord_list, moisrecord_list, look_back=200) File "C:\Users\alois\Documents\Gunadarma\main.py", line 136, in ensemble_models lstm1 = create_lstm_model(trainX, trainY, testX, testY, lstm_units=[128, 64, 32, 64], look_back=look_back, n_future=n_future) File "C:\Users\alois\Documents\Gunadarma\main.py", line 88, in create_lstm_model history = model.fit(trainX, trainY, epochs=25, batch_size=batch, verbose=1, validation_split=0.2, File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "C:\Users\alois\AppData\Local\Temp_autograph_generated_file7mmxqe1d.py", line 15, in tf__train_function retval = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope) ValueError: in user code:
File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\keras\engine\training.py", line 1284, in train_function * return step_function(self, iterator) File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\keras\engine\training.py", line 1268, in step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\keras\engine\training.py", line 1249, in run_step ** outputs = model.train_step(data) File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\keras\engine\training.py", line 1051, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\keras\engine\training.py", line 1109, in compute_loss return self.compiled_loss( File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__ loss_value = loss_obj(y_t, y_p, sample_weight=sw) File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\keras\losses.py", line 142, in __call__ losses = call_fn(y_true, y_pred) File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\keras\losses.py", line 268, in call ** return ag_fn(y_true, y_pred, **self._fn_kwargs) File "C:\Users\alois\Documents\Gunadarma\venv\lib\site-packages\keras\losses.py", line 1611, in mean_absolute_error return backend.mean(tf.abs(y_pred - y_true), axis=-1) ValueError: Dimensions must be equal, but are 40 and 4 for '{{node mean_absolute_error/sub}} = Sub[T=DT_FLOAT](sequential/dense_1/BiasAdd, IteratorGetNext:1)' with input shapes: [?,40], [?,10,4]. - Assistant
My apologies for the confusion. I made a mistake in the previous message, specifically around reshaping the
ensemble_predictions. Please find the corrected version of theensemble_modelsfunction below:def ensemble_models(data1, data2, data3, data4, look_back, n_future): ... # In the ensemble_models() function trainX, trainY = create_multi_step_dataset(train, look_back=look_back, n_future=n_future) testX, testY = create_multi_step_dataset(test, look_back=look_back, n_future=n_future) ... lstm1 = create_lstm_model(trainX, trainY, testX, testY, lstm_units=[128, 64, 32, 64], look_back=look_back, n_future=n_future) ... ensemble_predictions = ensemble_weight_1 * predictions1 + ensemble_weight_2 * predictions2 # Reshape the ensemble_predictions so that it matches the dimensions of testY ensemble_predictions = ensemble_predictions.reshape(predictions1.shape[0], predictions1.shape[1], -1) # Compute the average of the ensemble_predictions along axis 2 ensemble_predictions = np.mean(ensemble_predictions, axis=2) ... plot_data(ensemble_predictions, future_ensemble_predictions, testY) ...Now, the errors related to dimension mismatches should be resolved. Let me know if you still encounter any issues or need further assistance.