Keras for predicting the digit in a specific position in a number

I am using Keras to train and then predict the digit in the i-th place of a n-digit decimal number. For example, if the input is 123456 (i.e., n = 6), and i=2, then the program needs to find out a value of 4. I save the inputs in x_values and the training, testing and validation values corresponding to the elements of x_values are in y_values.

# Generate x_values by reading the first column from the csv
from io import StringIO
df = pd.read_csv(dst_path, skipinitialspace=True)

x_values = df['Input']
y_values = df['OutputA'] # OutputA indicates 0th element (i.e., i = 0)

I have two models. The first one is simple 'relu' which takes a scalar input and feeds it through 8 "neurons", followed by a final layer is a single neuron.

# Using Keras to create a simple model architecture
model_1 = tf.keras.Sequential()

# First layer takes a scalar input and feeds it through 8 "neurons". The
# neurons decide whether to activate based on the 'relu' activation function.
model_1.add(keras.layers.Dense(8, activation='relu', input_shape=(1,)))

# Final layer is a single neuron, since we want to output a single value
model_1.add(keras.layers.Dense(1))

# Compile the model using the standard 'adam' optimizer and the mean squared error or 'mse' loss function for regression.
model_1.compile(optimizer='adam', loss='mse', metrics=['mae'])

The second model is 16 neurons x 32 deep (considerably larger than the first one).

model = tf.keras.Sequential()

# First layer takes a scalar input and feeds it through 16 "neurons". The
# neurons decide whether to activate based on the 'relu' activation function.
NUM_NEURONS = 16
model.add(keras.layers.Dense(NUM_NEURONS, activation='relu', input_shape=(1,)))

# The new layers will help the network learn more complex representations
NUM_LAYERS = 30
for layer in range(NUM_LAYERS):
  model.add(keras.layers.Dense(NUM_NEURONS, activation='relu'))

# Final layer is a single neuron, since we want to output a single value
model.add(keras.layers.Dense(1))

# Compile the model using the standard 'adam' optimizer and the mean squared error or 'mse' loss function for regression.
model.compile(optimizer='adam', loss="mse", metrics=["mae"])

My overall training set has 15000 samples. I split the input set into training, testing then validation sets.

# Train the model on our training data while validating on our validation set
history_1 = model_1.fit(x_train, y_train, epochs=500, batch_size=64,
                        validation_data=(x_validate, y_validate))

After all these, when I calculate the Loss and the Mean Average Error, they are both very high (about 8.3 and 2.5 resp.).

What would be the best way to solve this problem? Playing with the width, depth of the network or the value of epoch did not have discernible effect. Any hint will be greatly appreciated.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum