如何完成这个AI?

import tensorflow as tffrom random import*from numpy import arrayinput_randoms = []for i in range(10000):    input_randoms.append([randint(0,100),randint(0,100)])output_randoms = []for pair in input_randoms:    output_randoms.append(sum(pair))model = tf.keras.models.Sequential([  tf.keras.layers.Flatten(input_shape=(2,)),  tf.keras.layers.Dense(128, activation='relu'),  tf.keras.layers.Dropout(0.2),  tf.keras.layers.Dense(202, activation='softmax')])model.compile(optimizer='adam',              loss='sparse_categorical_crossentropy',              metrics=['accuracy'])model.fit(array(input_randoms), output_randoms, epochs=5)model.evaluate([[50,32],[16,18]],  [82,34], verbose=2)

输出为:

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.Instructions for updating:If using Keras pass *_constraint arguments to layers.Train on 10000 samplesEpoch 1/510000/10000 [==============================] - 1s 107us/sample - loss: 7.1288 - acc: 0.0090Epoch 2/510000/10000 [==============================] - 1s 69us/sample - loss: 4.9724 - acc: 0.0120Epoch 3/510000/10000 [==============================] - 1s 67us/sample - loss: 4.8243 - acc: 0.0132Epoch 4/510000/10000 [==============================] - 1s 68us/sample - loss: 4.7343 - acc: 0.0113Epoch 5/510000/10000 [==============================] - 1s 69us/sample - loss: 4.6702 - acc: 0.0139---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-1-4b4fc1f78d7e> in <module>()     26 model.fit(array(input_randoms), output_randoms, epochs=5)     27 ---> 28 model.evaluate([[50,32],[16,18]],  [82,34], verbose=2)     29 3 frames/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)    570                              ': expected ' + names[i] + ' to have shape ' +    571                              str(shape) + ' but got array with shape ' +--> 572                              str(data_shape))    573   return data    574 ValueError: Error when checking input: expected flatten_input to have shape (2,) but got array with shape (1,)

我正在尝试完成这个AI。

据我所知,model.evaluate([[50,32],[16,18]], [82,34], verbose=2) 我应该输入两个样本输入及其正确的答案。我期望的输出如上所示,在错误之前的输出部分。


回答:

我认为你少了一对括号,一旦我加上括号,输入应该就是正确的了。我也是初学者,不太懂…

model.evaluate([[50,32],[16,18]],  [82,34], verbose=2)

改为

model.evaluate([[[50,32],[16,18]]],  [82,34], verbose=2)

我的测试截图

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中创建了一个多类分类项目。该项目可以对…

发表回复

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