0

I am new to programming. I am trying to classify two classes (Crash, Non-Crash) based on two features (Length, Traffic_Volume) using 1 dimensional CNN. When I am trying to train the following model,

# Training and Testing Data
X_train, y_train = train[['Traffic_Volume', 'length']].values, train['Crash'].values
X_test, y_test = SH[['Traffic_Volume', 'length']].values, SH['Crash'].values
print ('Training data shape : ', X_train.shape, y_train.shape)
print ('Testing data shape : ', X_test.shape, y_test.shape)
# Training data shape :  (316, 2) (316,)
# Testing data shape :  (343, 2) (343,)

# Fit and Evaluate a Model
def baseline_model(n_features=343, seed=100):
    numpy.random.seed(seed)
    # set_random_seed(seed)
    tensorflow.random.set_seed(seed)
    # create model
    model = Sequential()
    model.add(Conv1D(32, 3, padding = "same", input_shape=(343, 2)))
    model.add(Activation('relu'))
    model.add(MaxPooling1D(pool_size=2))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.2))#
    model.add(Dense(64, activation='relu'))#
    model.add(Dense(2))
    model.add(Activation('softmax'))
    # Compile model
    numpy.random.seed(seed)
    tensorflow.random.set_seed(seed)
    model.compile(loss='categorical_crossentropy', optimizer='adagrad', metrics=['accuracy'])
   
    print (model.summary())
    return model

# Classification

n_features=2   
n_classes=2
batch_size=10
from multi_adaboost_CNN import AdaBoostClassifier as Ada_CNN
n_estimators =10
epochs =1
bdt_real_test_CNN = Ada_CNN(
    base_estimator=baseline_model(n_features=n_features),
    n_estimators=n_estimators,
    learning_rate=1,
    epochs=epochs)

bdt_real_test_CNN.fit(X_train, y_train, batch_size)
y_pred_CNN = bdt_real_test_CNN.predict(X_train)
print('\n Training accuracy of bdt_real_test_CNN (AdaBoost+CNN): {}'.format(accuracy_score(bdt_real_test_CNN.predict(X_train),y_train)))

I found this ValueError:

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 2)

I want to know what I should change to get an efficient model (Data.shape, n_features, n_classes, etc.)?

Ahmad Yehia
  • 11
  • 1
  • 4
  • I think this answer will help https://stackoverflow.com/a/68172475/11813527. – Josh Ziegler Jul 06 '21 at 02:53
  • The article clarified a lot of things to me, but I still have the previous problem. Should I reshape `X_train` to 3d, since its shape is 2d and the model needs 3d input? Thanks, @JoshZiegle – Ahmad Yehia Jul 06 '21 at 05:35
  • Yes from the [TF docs](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv1D) it looks like the 2nd dimension is supposed to be the number of "time steps" in a given example and the 3rd is the number of features per time step. I'm not sure how that time steps number is determined... Maybe the tutorials in the docs can give some guidance. – Josh Ziegler Jul 06 '21 at 13:42
  • Actually, I can't describe how I am grateful for your help! I spent days solving this issue, and I was going to change the model due to the `input_shape` problem. Thanks for sharing the TF reference and thanks again for your time @JoshZiegler – Ahmad Yehia Jul 07 '21 at 08:31

0 Answers0