0

My input is simply a csv file with 45781 rows and two columns. I am trying to train my data on a neural network but when I try to fit the model, it throws me an error.

ValueError: Error when checking input: expected dense_26_input to have shape (45781,) but got array with shape (2,)

I tried implementing solution as given from this link :

Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29)

but i am still not able to run the code. Here's my code :

X = df.iloc[:, 0:2].values
y = df.iloc[:, 2].values

df_sklearn = df.copy()

lb_make = LabelEncoder()
df_sklearn['Type'] = lb_make.fit_transform(df['Type'])
df_sklearn.head() #Results in appending a new column to df

df_onehot = df.copy()
df_onehot = pd.get_dummies(df_onehot, columns=['Type'], prefix = ['Type'])

df_onehot_sklearn = df.copy()

lb = LabelBinarizer()
lb_results = lb.fit_transform(df_onehot_sklearn['Type'])
lb_results_df = pd.DataFrame(lb_results, columns=lb.classes_)

result_df = pd.concat([df_onehot_sklearn, lb_results_df], axis=1)


X_train, X_test, y_train, y_test = train_test_split(X, lb_results_df, test_size = 0.4)

classifier = Sequential()
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 45781))
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))
classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

classifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)
Kai Aeberli
  • 1,189
  • 8
  • 21

1 Answers1

1

The rows are usually your training examples, and the model expects you to omit this dimension. So if there are 45781 examples, each with 2 columns, input_dim should be 2. Also, you can omit the output_dim argument if you are stacking layers but you do have to specify units (number of neurons).

The last Dense Layer needs to have the same number of neurons as the dimension of y (20 in your case).

Below an example with dummy data:

X_train = np.random.random((300,2))
y_train = np.random.random((300,20))

classifier = Sequential()
classifier.add(Dense(units = 6, init = 'uniform', activation = 'relu', input_dim = 2))
classifier.add(Dense(units = 6, init = 'uniform', activation = 'relu'))
classifier.add(Dense(units = 20, init = 'uniform', activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
classifier.summary()

classifier.fit(X_train, y_train, batch_size=10, nb_epoch=100)

Kai Aeberli
  • 1,189
  • 8
  • 21