keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.91k stars 19.45k forks source link

ValueError: Shapes (64, 8) and (64, 16) are incompatible #18874

Closed Navien07 closed 9 months ago

Navien07 commented 10 months ago

` from sklearn.model_selection import GridSearchCV from scikeras.wrappers import KerasClassifier from keras.models import Sequential from keras.layers import Conv1D, MaxPooling1D, Flatten, Dense, Dropout, BatchNormalization import numpy as np

Define the 1D convolutional neural network model

def create_model(filters, kernel_size, conv_layers, dense_units, dropout_rate): model = Sequential() for i in range(conv_layers): model.add(Conv1D(filters=filters, kernel_size=kernel_size, activation='relu', input_shape=(x_train.shape[1], 1))) model.add(BatchNormalization()) model.add(Dropout(dropout_rate)) model.add(MaxPooling1D(pool_size=1)) model.add(Flatten()) model.add(Dense(units=dense_units, activation='relu')) model.add(Dropout(dropout_rate)) model.add(Dense(units=16, activation='softmax')) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) return model

Create a grid of parameters

param_grid = { 'filters': [256, 128, 64], 'kernel_size': [3, 5, 7], 'conv_layers': [3, 5, 7], 'dense_units': [32, 64, 128], 'dropout_rate': [0.2, 0.5, 0.8] }

Wrap the 1D convolutional neural network model using KerasClassifier

model = KerasClassifier(build_fn=create_model, epochs=100, batch_size=64, verbose=1, filters=256, kernel_size=3, conv_layers=3, dense_units=64, dropout_rate=0.2)

Perform grid search in parallel

grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=3, n_jobs=-1, verbose=1)

Fit the grid search to your data (assuming x_train and y_train are defined)

grid_search.fit(x_train, y_train)

Get the best model

best_model = grid_search.bestestimator.model

Print the best parameters and accuracy

print("Best Parameters: ", grid_search.bestparams) print("Best Accuracy: {:.2f}%".format(grid_search.bestscore * 100)) `

Fitting 3 folds for each of 243 candidates, totalling 729 fits

ValueError Traceback (most recent call last) c:\Users\navie\OneDrive - JLYON\Desktop\CSAI\Year 2\Semester 1\Software Engineering Group Project\Group Project\Speech Model\speech.ipynb Cell 33 line 3 35 grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=3, n_jobs=-1, verbose=1) 37 # Fit the grid search to your data (assuming x_train and y_train are defined) ---> 38 grid_search.fit(x_train, y_train) 40 # Get the best model 41 best_model = grid_search.bestestimator.model

File c:\ProgramData\anaconda3\Lib\site-packages\sklearn\base.py:1151, in _fit_context..decorator..wrapper(estimator, *args, *kwargs) 1144 estimator._validate_params() 1146 with config_context( 1147 skip_parameter_validation=( 1148 prefer_skip_nested_validation or global_skip_validation 1149 ) 1150 ): -> 1151 return fit_method(estimator, args, **kwargs)

File c:\ProgramData\anaconda3\Lib\site-packages\sklearn\model_selection_search.py:898, in BaseSearchCV.fit(self, X, y, groups, **fit_params) 892 results = self._format_results( 893 all_candidate_params, n_splits, all_out, all_more_results 894 ) 896 return results --> 898 self._run_search(evaluate_candidates) 900 # multimetric is determined here because in the case of a callable ... target.shape.assert_is_compatible_with(output.shape)

ValueError: Shapes (64, 8) and (64, 16) are incompatible

Please guide me on how I can resolve this issue immediately

SuryanarayanaY commented 10 months ago

Hi @Navien07 ,

From the error it seems you are getting target.shape as (64,8) and output.shape as (64,16) which are different. Could you please submit code snippet in the form of a gist ?

Thanks!

github-actions[bot] commented 10 months ago

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 9 months ago

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

google-ml-butler[bot] commented 9 months ago

Are you satisfied with the resolution of your issue? Yes No