首页 > 解决方案 > TensorFlow 预测用户的下一个号码

问题描述

更新

所以我的目标是创建一个机器学习程序,该程序接受用户给出的训练数字列表,并尝试预测他们接下来可能选择的数字。我对机器学习还很陌生,我想做这个快速的项目只是为了好玩。我遇到的一些问题包括:不知道如何更新我的训练标签以对应下一个数字的训练以及如何预测下一个数字。这是我当前的代码:

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)

这是我当前的错误和回溯:

    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

我会以错误的方式解决这个问题吗?欢迎任何帮助,谢谢!

标签: pythontensorflowmachine-learning

解决方案


如果要映射函数,则它们需要包含相同数量的样本。例如这里你要映射Y = X.

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

您的输出大小应包含(1,)您想要预测的单个连续数字。所以最后一层应该是:

keras.layers.Dense(1) # linear layer

指标也应该适合您的问题(回归):

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

您可以从此处找到可用的指标

编辑:将要预测的数字作为numpy数组传递:

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

同样在这种情况下,您可以尝试sgd优化器而不是adam.

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

使用带有 1 个神经元的 softmax 是一个很大的错误,您的模型1每次都会输出。上面,我没有指定任何激活,所以我做了那个输出神经元linear


推荐阅读