TensorFlow 预测用户的下一个数字

更新

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

这是我当前的错误和追溯信息:

    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].valueIndexError: list index out of rangeProcess 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) # 线性层

此外,度量标准应该适合你的问题(回归):

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注