-1

I am very new to machine learning and am trying to create a Keras model using data I have collected. It is perfectly uniform and loads in fine. Here is a sample:

n,d0,d1,d2,d3,d4,d5,d6,d7,d8,output
30,85.1,65.0,32.2,38.2,191.9,72.1,118.2,121.5,110.3,0.0
417,232.8,51.3,39.8,66.0,173.4,246.7,285.4,265.6,217.0,1.0
496,194.2,72.7,214.8,41.6,155.2,195.2,208.3,31.0,15.6,2.0
361,206.1,52.8,63.0,105.1,168.5,156.0,145.7,127.4,70.6,1.0
408,202.5,48.4,47.4,79.1,223.8,236.6,260.3,247.4,206.2,1.0

Here is my Keras code:

import numpy as np
import pandas
from tensorflow import keras
from sklearn.model_selection import train_test_split
data = pandas.read_csv("data.csv")
x = data[[f"d{i}" for i in range(9)]]
y = data[["output"]]

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1)

model = keras.models.Sequential()
model.add(keras.layers.Dense(12, input_dim=9, activation="relu"))
model.add(keras.layers.Dense(8, activation="relu"))
model.add(keras.layers.Dense(1, activation="softmax"))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=150, batch_size=10)
print(model.predict(np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8]])))
_, acc = model.evaluate(x, y)
print('Accuracy: %.2f' % (acc*100))

I don't see any issues, but I can't predict. Please could someone help?

1 Answers1

0

I think you need to fix your output and your compile step.

If you're doing regression (ie. predicting true, continuous values) then you need to change your loss to RMSE or some other continuous loss, and not use a softmax classification. I don't think the accuracy metric will work either.

If you're doing classification then you need to change the number of outputs to the number of classes that you have. You also can't use binary cross entropy because you have more than one class, so use CategoricalCrossentropy. Note that according to the docs you have to change your current label representation (0,1,2..) to a one-hot representation. You can easily do this by:

y_one_hot = keras.utils.to_categorical(y)
jhso
  • 3,103
  • 1
  • 5
  • 13