Search code examples
pythonpandasnumpykerassequential

Keras accuracy returning 0


So basically, I am working on this bullet optimization program. I wish to study how different ballistics parameters such as weight, length, and mass affect a ballistics coefficient. However, my training accuracy is 0, although there is loss and val_loss. I've read similar Stackoverflow posts regarding this, but none have helped me so far. Perhaps I just didn't do them right; I am referencing https://stackoverflow.com/a/63513872/12349188

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.utils import shuffle

df = pd.read_csv('Bullet Optimization\ShootForum Bullet DB_2.csv')

from sklearn.model_selection import train_test_split
from sklearn import preprocessing
dataset = df.values
X = dataset[:,0:12]
X = np.asarray(X).astype(np.float32)

y = dataset[:,13]
y = np.asarray(y).astype(np.float32)

X_train, X_val_and_test, y_train, y_val_and_test = train_test_split(X, y, test_size=0.3, shuffle=True)
X_val, X_test, y_val, y_test = train_test_split(X_val_and_test, y_val_and_test, test_size=0.5)

from keras.models import Sequential
from keras.layers import Dense, BatchNormalization

model = Sequential(
    [
        #2430 is the shape of X_train
        #BatchNormalization(axis=-1, momentum = 0.1),
        Dense(32, activation='relu'),
        Dense(32, activation='relu'),
        Dense(1,activation='softmax'),
    ]
)

model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])

history = model.fit(X_train, y_train, batch_size=64, epochs=100, validation_data=(X_val, y_val))

Did I do something wrong in my code? I know some python but I just kind of built upon the tutorials for my own purposes.


Solution

  • There are several problems in your code.

    First this line:

    Dense(1,activation='softmax')
    

    This line will cause the output 1 every time. So even if you are making classification, your accuracy would be 50% if you had 2 classes. Softmax outputs' sum will be equal to one. So using it with one neuron does not make sense.

    You need to change your loss and metric as this is a regression.

    loss='mse', metrics=['mse']
    

    Also your output neuron should be linear which means does not need any activation function. It should be like:

    Dense(1)