Search code examples
python-3.xtensorflow2.0

Why is accuracy not improving over 20% for Neural Network using Mean Square Error with Tensor Flow?


I've written a simple neural network to predict the output for a function f(x) = x^2. I can't get the accuracy above 20%, though. Does anyone know why?

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# load training data...

x_train = tf.range(10, dtype="float32")/9.0
y_train = (tf.range(10, dtype="float32")**2.0)/(9.0**2.0)

x_train = tf.reshape(x_train, (len(x_train), 1))
y_train = tf.reshape(y_train, (len(y_train), 1))

print(x_train)
print(y_train)

model = keras.Sequential([
    layers.Dense(10, input_dim=1, activation='relu', kernel_initializer='he_uniform'),
    layers.Dense(10, activation='relu', kernel_initializer='he_uniform'),
    layers.Dense(1)
])

model.compile(
    loss = "mse", # mean square error
    optimizer = "adam",
    metrics = ["accuracy"],
)

model.fit(x_train,y_train, epochs=250, batch_size=1, verbose=2)
yhat = model.predict(x_train)

print(tf.abs(yhat*(9.0**2.0)))

Solution

  • Because accuracy is for classification problems, and you have a regression problem.