Mason Choi
Mason Choi

Reputation: 101

TensorFlow predict User's next number

UPDATED

So my goal is to create a machine learning program that takes a list of training numbers given by a user, and try to predict what number they might pick next. I am fairly new to machine learning, and wanted to make this quick project just for fun. Some issues that I am running into include: not knowing how to update my training labels to correspond to training for the next number and how to go about predicting that next number. Here is my current code:

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt    # I will add a visualization and other things later


train_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
train_labels = [1, 2, 3, 4, 5, 6, 7, 8, 9]
test_number = 2    # These values will be changed into inputs later to collect individual data

model = keras.Sequential([
    keras.layers.Input(shape=(1,)),    # Is this the correct way to input my data? I want 1 number to pass through here
    keras.layers.Dense(10, activation='relu'),
    keras.layers.Dense(1, activation='softmax')    # Later I want to input any number I want, but for now I will output a prediction number 1-10
])

model.compile(optimizer='adam',
              loss='mse',
              metrics=['mae'])

model.fit(train_numbers, train_labels, epochs=2)    # I am not sure if my fitting here works, my code does not make it here

predictions = model.predict(test_number)
print(predictions)

Here is my current error and traceback:

    Traceback (most recent call last):
  File "C:/Users/Mason Choi/PycharmProjects/machine_learning/experimentation.py", line 23, in <module>
    predictions = model.predict(test_number)
  File "C:\Users\Mason Choi\anaconda3\envs\machine_learning\lib\site-packages\tensorflow\python\keras\engine\training.py", line 130, in _method_wrapper
    return method(self, *args, **kwargs)
  File "C:\Users\Mason Choi\anaconda3\envs\machine_learning\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1569, in predict
    data_handler = data_adapter.DataHandler(
  File "C:\Users\Mason Choi\anaconda3\envs\machine_learning\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1105, in __init__
    self._adapter = adapter_cls(
  File "C:\Users\Mason Choi\anaconda3\envs\machine_learning\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 650, in __init__
    self._internal_adapter = TensorLikeDataAdapter(
  File "C:\Users\Mason Choi\anaconda3\envs\machine_learning\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 275, in __init__
    num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
  File "C:\Users\Mason Choi\anaconda3\envs\machine_learning\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 275, in <genexpr>
    num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
  File "C:\Users\Mason Choi\anaconda3\envs\machine_learning\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 887, in __getitem__
    return self._dims[key].value
IndexError: list index out of range

Process finished with exit code 1

Am I going about this the wrong way? Any help welcome, THANKS!

Upvotes: 1

Views: 4148

Answers (1)

Frightera
Frightera

Reputation: 5079

If you want to map a function, then they need to contain same number of samples. For example here you want to map Y = X.

train_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
train_labels = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Your output size should consist of (1,) as you want to predict a single continuous number. So last layer should be:

keras.layers.Dense(1) # linear layer

Also metrics should be appropriate for your problem(regression):

model.compile(optimizer='adam',
              loss='mse',
              metrics=['mae'])

You can find the available metrics from here.

Edit: Pass the numbers that you want to predict as a numpy array:

test_number = np.array([2])
predictions = model.predict(test_number)

Also in this case, you can try sgd optimizer instead of adam.

keras.layers.Dense(1, activation='softmax')

Having softmax with 1 neuron is a big mistake, your model will output 1 everytime. Above, I did not specify any activation, so I made that output neuron linear.

Upvotes: 4

Related Questions