Search code examples
pythonkerasdeep-learningepochbatchsize

How to modify model.fit settings?


Hi My code used to run fine untill I changed my data set. Now, I am getting an error at :

model.fit(train,train_df.iloc[:,-1],epochs=30, batch_size=20, verbose=1)

The error is:

ValueError: Error when checking input: expected dense_31_input to have shape (1125,) but got array with shape (103,)

The variables are :

enter image description here

scaler = StandardScaler()

train=scaler.fit_transform(train_df.iloc[:,:-1])
test=scaler.fit_transform(test_df.iloc[:,:-1])

# Creating Deep Model



model = Sequential()

# Add an input layer
model.add(Dense(562, activation='relu', input_shape=(1125,)))

# Add one hidden layer
model.add(Dense(562, activation='relu'))
model.add(BatchNormalization())

# Add an output layer
model.add(Dense(1, activation='sigmoid'))

#add improvements 

model.add(Dropout(0.3))
#Train the model

model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])

model.fit(train,train_df.iloc[:,-1],epochs=30, batch_size=20, verbose=1)

#TEst the model

y_pred = model.predict(test_df.iloc[:,:-1])

I assume to fix it. I need to change the batch_size and epochs? but what numbers should be used?


Solution

  • In general, models assume that the first dimension of the input data is the batch size. Models don't care what the batch size is though, so you never set it when creating the model. Instead, you should set input_shape to the shape of each sample of your input data. In your case, each sample appears to be a vector of length 103, so set input_shape to (103,).