Python TensorFlow预测用户';下一个号码是什么
已更新 因此,我的目标是创建一个机器学习程序,该程序接受用户给出的培训编号列表,并尝试预测他们下一步可能选择的编号。我对机器学习相当陌生,我想做这个快速的项目只是为了好玩。我遇到的一些问题包括:不知道如何更新我的培训标签以适应下一个数字的培训,以及如何预测下一个数字。这是我目前的代码:Python TensorFlow预测用户';下一个号码是什么,python,tensorflow,machine-learning,Python,Tensorflow,Machine Learning,已更新 因此,我的目标是创建一个机器学习程序,该程序接受用户给出的培训编号列表,并尝试预测他们下一步可能选择的编号。我对机器学习相当陌生,我想做这个快速的项目只是为了好玩。我遇到的一些问题包括:不知道如何更新我的培训标签以适应下一个数字的培训,以及如何预测下一个数字。这是我目前的代码: import tensorflow as tf from tensorflow import keras import numpy as np import matplotlib.pyplot as plt
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
回溯(最近一次呼叫最后一次):
文件“C:/Users/Mason Choi/PycharmProjects/machine_learning/experiments.py”,第23行,在
预测=模型预测(测试编号)
文件“C:\Users\Mason Choi\anaconda3\envs\machine\u learning\lib\site packages\tensorflow\python\keras\engine\training.py”,第130行,在方法包装中
返回方法(self、*args、**kwargs)
文件“C:\Users\Mason Choi\anaconda3\envs\machine\u learning\lib\site packages\tensorflow\python\keras\engine\training.py”,第1569行,在predict中
data\u handler=data\u adapter.DataHandler(
文件“C:\Users\Mason Choi\anaconda3\envs\machine\u learning\lib\site packages\tensorflow\python\keras\engine\data\u adapter.py”,第1105行,在\uu init中__
self.\u adapter=适配器\u cls(
文件“C:\Users\Mason Choi\anaconda3\envs\machine\u learning\lib\site packages\tensorflow\python\keras\engine\data\u adapter.py”,第650行,在初始化中__
自。\内部\适配器=张力克达塔适配器(
文件“C:\Users\Mason Choi\anaconda3\envs\machine\u learning\lib\site packages\tensorflow\python\keras\engine\data\u adapter.py”,第275行,在初始化中__
num_samples=set(int(i.shape[0]),用于嵌套中的i.flatte(输入))
文件“C:\Users\Mason Choi\anaconda3\envs\machine\u learning\lib\site packages\tensorflow\python\keras\engine\data\u adapter.py”,第275行,在
num_samples=set(int(i.shape[0]),用于嵌套中的i.flatte(输入))
文件“C:\Users\Mason Choi\anaconda3\envs\machine\u learning\lib\site packages\tensorflow\python\framework\tensor\u shape.py”,第887行,在u getitem中__
返回self.\u dims[key].值
索引器:列表索引超出范围
进程已完成,退出代码为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
此外,指标应适合您的问题(回归):
你可以
编辑:将要预测的数字作为numpy
数组传递:
test_number = np.array([2])
predictions = model.predict(test_number)
同样在这种情况下,您可以尝试使用sgd
优化器,而不是adam
keras.layers.Dense(1, activation='softmax')
将softmax与1个神经元一起使用是一个很大的错误,您的模型每次都会输出
1
。如上所述,我没有指定任何激活,所以我让输出神经元linear
对于每个样本,您需要提供一个标签,这就是错误消息实际上所说的。感谢您的澄清,但我实际上如何实现它?应该吗我制作了一个数字为1-10的数据框?非常感谢,我知道培训是如何工作的,但是我仍然对如何使用培训模型预测新值感到困惑。我已经更新了我的问题,包括新的错误和解释。我更新了答案。因此,如果我理解正确,那么在输入列表时,所有这些更改都会发生数字0-9(例如:123456790)对数字2进行预测,在最好的情况下,预测应该返回类似于3的结果,对吗?在运行模型几次之后,它似乎不是很好,但总是在2到3之间,所以它是可以的。为了使它更好,我可以只输入相同的训练数据5次以获得更好的结果吗?(如:01 2 3 4 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0…)此外,我刚刚意识到我的model.fit将我输入的列车号与我的列车号标签相匹配。当我收集10个0-9号的用户序列时,我是否应该将列车号标签变量分配给它们?很抱歉,我向您提出了很多问题,但您确实做出了很大的改变,我非常感谢。为了让它变得更好,请增加e年代数(并非总是如此),不要复制数据,如012345678901234567890
。您可以向模型引入新数据,如0123…1718
fit()
方法将其分配给您的输入(特征)和相应的输出(标签)。
keras.layers.Dense(1, activation='softmax')