Search code examples
kerasneural-networkloss-functionconv-neural-networkmulticlass-classification

"loss : nan" in training of "convolution 1D" neural Network for tabular data


I need to implement CNN for multi-class classification on Tabular Dataset. My data has X_train.shape = (1534185, 81, 1) and Y_train = (1534185, 11)

Here is a sample from my dataset

DataSetImage

I tried to normalize the data, but values are too big to be added and stored in float64.

The CNN Model I implemented is below

batchSize =  X_train.shape[0]
length =  X_train.shape[1]
channel = X_train.shape[2]
n_outputs = y_train.shape[1]


#Initialising the CNN
model = Sequential()

#1.Multiple convolution and max pooling

model.add(Convolution1D(filters=64, kernel_size=3, activation="relu", input_shape=(length, channel)))
model.add(MaxPooling1D(strides=4))
model.add(Dropout(0.1))
model.add(BatchNormalization())


model.add(Convolution1D(filters= 32, kernel_size=3, activation='relu'))
model.add(MaxPooling1D(strides=4))
model.add(Dropout(0.1))
model.add(BatchNormalization())


model.add(Convolution1D(filters= 16, kernel_size=3, activation='relu'))
model.add(MaxPooling1D(strides=4))
model.add(Dropout(0.1))
model.add(BatchNormalization())

#2.Flattening
model.add(Dropout(0.2))
model.add(Flatten())

#3.Full Connection

model.add(Dense(30, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

If I try to change the kernel size I get following Error

ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling1d_103/MaxPool' (op: 'MaxPool') with input shapes: [?,1,1,16].

When I try to train my Model using code below, I get no improvement in Accuracy with loss = Nan

history = model.fit(
    X_train,
    y_train,
    batch_size=1000,
    epochs=2,
    validation_data=(X_test, y_test),
)

Loss: nan

Error:
Train on 1534185 samples, validate on 657509 samples
Epoch 1/2
 956000/1534185 [=================>............] - ETA: 1:44 - loss: nan - accuracy: 0.0101

Need your Help


Solution

  • Try to check for inf values and replace them with nan and retry

    X_train.replace([np.inf, -np.inf], np.nan,inplace=True)
    X_train = X_train.fillna(0)