I have not yet figured out why, but every time I fit the model, the loss is shown to be NaN, though I have thoroughly (tried) to drop any NaN rows in the data that I am training against. This is my first time trying to learn ML, so patience with me would be appreciated.
I will walk through my code step by step here in an effort to better explain my choices:
- Import dependencies:
import pandas as pd from sklearn.model_selection import train_test_split from keras.models import Sequential from keras.layers import Dense from sklearn.metrics import accuracy_score from sklearn.linear_model import Ridge from sklearn.preprocessing import StandardScaler from sklearn.impute import SimpleImputer
- Read data, drop any rows containing NaN data, then check again for NaN rows
df = pd.read_csv('Wednesday-workingHours.pcap_ISCX.csv') df = df.dropna() df.drop([' Label'], axis=1).isna().sum()
- Split data & interpret target data
X = pd.get_dummies(df.drop([' Label'], axis=1)) Y = df[' Label'].apply(lambda x: 1 if x=='BENIGN' else 0) X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)
- Set the type of model & add layers
model = Sequential() model.add(Dense(units=32, activation='relu', input_dim=78)) model.add(Dense(units=64, activation='relu')) model.add(Dense(units=1, activation='sigmoid'))
- Compile the model
model.compile(loss='binary_crossentropy', optimizer="adam", metrics='accuracy')
- Fit the model
model.fit(X_train, Y_train, epochs=200, batch_size=100)
For reference, the dataset I am using for this is can be found here which has 692,704 rows and 79 columns.
Sample output of step 6:
Epoch 1/100 554/554 [==============================] - 1s 1ms/step - loss: nan - accuracy: 0.3652
As you could probably guess from the list of imports, I have tried using a scalar, imputer, and L2 Regularization to correct this problem, but it still persists and I have run out of ideas.
My attempts at these corrections were removed after each fail, but interestingly enough when I switch to L2 Reg. I get a value error:
ValueError: Input X contains infinity or a value too large for dtype('float64').
I also took a look at my model.summary() and didn’t find anything too concerning:
Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_3 (Dense) (None, 32) 2528 dense_4 (Dense) (None, 64) 2112 dense_5 (Dense) (None, 1) 65 ================================================================= Total params: 4705 (18.38 KB) Trainable params: 4705 (18.38 KB) Non-trainable params: 0 (0.00 Byte)
Again this is my first time with ML and I’m basing this code off of reading and tutorials.