I’m trying to create a neural network to predict the is_canceled column of this dataset for my class assignment. The problem is when i ran model.fit() with some parameter it return the error:
Input 0 of layer “dense_49” is incompatible with the layer: expected axis -1 of input shape to have value 127, but received input with shape (None, 271)
I think there’s some problem with the input layer where i intend to create Embedding for some of my categorical columns and concatenate it with the input for numerical columns. This is my first time doing this and i dont know where is the wrong.
Can someone help me to fix the error.
Here’s my code
categorical_columns = ['hotel', 'meal', 'country', 'market_segment', 'distribution_channel', 'reserved_room_type',
'assigned_room_type', 'deposit_type', 'agent', 'company', 'customer_type']
numeric_columns = [col for col in df.columns if col not in categorical_columns]
numeric_columns.remove('is_canceled')
for col in categorical_columns:
df[col] = df[col].astype('category').cat.codes
X = df.drop('is_canceled', axis=1)
y = df['is_canceled']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.01, random_state=42)
# embedding input for categorical columns
inputs = []
embeddings = []
for col in categorical_columns:
input_cat = Input(shape=(1,), name=col)
emb = Embedding(input_dim=X[col].nunique(), output_dim=10)(input_cat)
emb = Flatten()(emb)
inputs.append(input_cat)
embeddings.append(emb)
# input for numeric columns
input_numeric = Input(shape=(len(numeric_columns),), name='numeric')
inputs.append(input_numeric)
# concatenate all input
x = Concatenate()(embeddings + [input_numeric])
x = Dense(256, activation='relu')(x)
x = Dense(128, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(32, activation='relu')(x)
output = Dense(1, activation='sigmoid')(x)
model = Model(inputs=inputs, outputs=output)
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy']
)
model.summary()
# Prepare input data for training
input_data_train = {}
for col in categorical_columns:
input_data_train[col] = X_train[col].values.reshape(-1, 1)
input_data_train['numeric'] = X_train[numeric_columns].values
y_train = np.array(y_train)
y_train = y_train.reshape(-1, 1)
y_test = np.array(y_test)
y_test = y_test.reshape(-1, 1)
input_data_test = {}
for col in categorical_columns:
input_data_test[col] = X_test[col].values.reshape(-1, 1)
input_data_test['numeric'] = X_test[numeric_columns].values
# Train the model
history = model.fit(input_data_train, y_train, validation_data=(input_data_test, y_test), epochs=10, batch_size=32)
Dũng Đình is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.