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:UsersMason Choianaconda3envsmachine_learninglibsite-packagestensorflowpythonkerasenginetraining.py", line 130, in _method_wrapper
return method(self, *args, **kwargs)
File "C:UsersMason Choianaconda3envsmachine_learninglibsite-packagestensorflowpythonkerasenginetraining.py", line 1569, in predict
data_handler = data_adapter.DataHandler(
File "C:UsersMason Choianaconda3envsmachine_learninglibsite-packagestensorflowpythonkerasenginedata_adapter.py", line 1105, in __init__
self._adapter = adapter_cls(
File "C:UsersMason Choianaconda3envsmachine_learninglibsite-packagestensorflowpythonkerasenginedata_adapter.py", line 650, in __init__
self._internal_adapter = TensorLikeDataAdapter(
File "C:UsersMason Choianaconda3envsmachine_learninglibsite-packagestensorflowpythonkerasenginedata_adapter.py", line 275, in __init__
num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
File "C:UsersMason Choianaconda3envsmachine_learninglibsite-packagestensorflowpythonkerasenginedata_adapter.py", line 275, in <genexpr>
num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
File "C:UsersMason Choianaconda3envsmachine_learninglibsite-packagestensorflowpythonframeworktensor_shape.py", line 887, in __getitem__
return self._dims[key].value
IndexError: list index out of range
Process finished with exit code 1

我是不是搞错了?欢迎任何帮助,谢谢!

如果您想映射一个函数,那么它们需要包含相同数量的样本。例如,您希望在此处映射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')

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

相关内容

  • 没有找到相关文章

最新更新