Skip to main content
2 of 2
Trim chat and invitation for discussion
halfer
  • 20.2k
  • 20
  • 110
  • 207

From what you mentioned in the comments you are facing the below error.

ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (21, 224, 224, 3)

If you are using CNN, why is the First Layer a Dense Layer (I understand it from the name, dense_1_input), instead of a Convolutional Layer.

With the First Layer being the Convolutional Layer, you should pass (224,224,3) for the argument, input_shape

Complete code for Fine Tuning Batch_Size and Number of Epochs for Fashion_MNIST Dataset using CNN is shown below:

# To support both python 2 and python 3 from __future__ import division, print_function, unicode_literals from io import open # Common imports import numpy as np import os import tensorflow as tf from keras.layers import Input, Conv2D, MaxPool2D, Dense, Dropout, Flatten from keras.models import Sequential from keras.optimizers import Adam import matplotlib.pyplot as plt from keras.regularizers import l1_l2 from matplotlib.pyplot import axis as ax from keras.wrappers.scikit_learn import KerasClassifier from sklearn.model_selection import GridSearchCV X = tf.placeholder(tf.float32, shape=[None, 784], name="X") X_reshaped = tf.reshape(X, shape=[-1, 28, 28, 1]) y = tf.placeholder(tf.int32, shape=[None], name="y") def create_model(): # instantiate regularizer Regularizer = l1_l2(0.001) cnn_model = Sequential() cnn_model.add(Conv2D(filters = 64,kernel_size = 3, strides=(1, 1), input_shape = (28,28,1), activation='relu', data_format='channels_last', activity_regularizer=Regularizer)) cnn_model.add(MaxPool2D(pool_size = (2, 2))) cnn_model.add(Dropout(0.25)) cnn_model.add(Flatten()) cnn_model.add(Dense(units = 32, activation = 'relu', activity_regularizer=Regularizer)) cnn_model.add(Dense(units = 10, activation = 'sigmoid', activity_regularizer=Regularizer)) cnn_model.compile(loss ='sparse_categorical_crossentropy', optimizer=Adam(lr=0.001),metrics =['accuracy']) return cnn_model model = KerasClassifier(build_fn=create_model, verbose=0) (X_train, y_train), (X_test, y_test) = tf.keras.datasets.fashion_mnist.load_data() X_train = X_train.astype(np.float32).reshape(-1, 28*28) / 255.0 X_train_reshaped = np.reshape(X_train, newshape=[-1, 28, 28, 1]) X_test = X_test.astype(np.float32).reshape(-1, 28*28) / 255.0 X_test_reshaped = np.reshape(X_test, newshape=[-1, 28, 28, 1]) y_train = y_train.astype(int) y_test = y_test.astype(int) batch_size = [20, 40] epochs = [10, 50] param_grid = dict(batch_size=batch_size, epochs=epochs) grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1, cv=3) grid_result = grid.fit(X_train_reshaped, y_train) # summarize results print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_)) means = grid_result.cv_results_['mean_test_score'] stds = grid_result.cv_results_['std_test_score'] params = grid_result.cv_results_['params'] for mean, stdev, param in zip(means, stds, params): print("%f (%f) with: %r" % (mean, stdev, param)) 
user11530462