Open sauravanand542 opened 4 years ago
The error you're encountering is related to the input shape mismatch between the expected input shape of the neural network model and the shape of the input data. Here's a step-by-step breakdown of the issue and suggestions for improvement:
Issue:
The error message indicates that the neural network model expects an input shape of (2026
,), but the actual input data has a shape of (20
,).
Analysis:
The input shape
is determined by the input_shape
parameter when creating the first layer of the neural network. In your case, the first layer is defined in the compile_model
function.
The get_bank
function returns (2026,) as the input_shape
, which seems to be the expected input shape for the neural network.
However, during training in the train_and_score
function, it appears that the actual input data (x_train
and x_test
) has a shape of (20,).
Possible Solutions: Check Data Dimensions:
Ensure that the input data loaded from the CSV file (clean.csv
) has the correct dimensions. It seems that the data might be loaded incorrectly, resulting in a shape mismatch.
Review Data Loading:
Review the data loading process in the get_bank function. Confirm that the features (x_train and x_test) have the correct shape. You might need to reshape or preprocess the data appropriately.
x_train = array[0:2026, :20]
Check Feature Dimension:
Confirm that the number of features matches the expected input shape. If the neural network expects (2026,) as input, each training example should have 2026 features. Debugging:
Print the shapes of x_train and x_test before training to understand their dimensions:
print("Shape of x_train:", x_train.shape) print("Shape of x_test:", x_test.shape)
Review Model Architecture:
Double-check the architecture of your neural network model in the compile_model function. Ensure that the first layer is correctly configured with the expected input shape.
model.add(Dense(nb_neurons, activation=activation, input_shape=input_shape))
Reshape Data if Necessary:
If the data has the correct information but is not shaped appropriately, you might need to reshape it. For example:
x_train = x_train.reshape((num_samples, 2026))
Check CSV Data:
Inspect the contents of your CSV file to ensure that the data is formatted correctly.
Additional Suggestions:
Validation Set: Consider using a separate validation set during training to monitor model performance and prevent overfitting.
Normalization: Ensure that data normalization is applied consistently during training and testing.
Callback Monitoring: Monitor the model during training using Keras callbacks to save the best model or perform early stopping. By addressing these points, you should be able to identify and resolve the input shape mismatch issue.
I used this code to apply genetic algorithm on other data. I have changed the code accordingly train.txt main.txt network.txt optimizer.txt I have made changes in the 'train' and 'main' files
instead of get_mnist()/get_cifar10(), I have created my own function get_bank() which goes like this: `""" Utility used by the Network class to actually train.
Based on: https://github.com/fchollet/keras/blob/master/examples/mnist_mlp.py
""" from keras.datasets import mnist, cifar10 from keras.models import Sequential from keras.layers import Dense, Dropout from keras.utils.np_utils import to_categorical from keras.callbacks import EarlyStopping import pandas as pd
Helper: Early stopping.
early_stopper = EarlyStopping(patience=5)
def get_bank(): """Retrieve the CIFAR dataset and process the data."""
set defaults
def compile_model(network, nb_classes, input_shape): """Compile a sequential model.
def train_and_score(network, dataset): """Train the model, return test loss.
`
`ValueError Traceback (most recent call last)