【问题标题】:How to fix TypeError with respect to TensorFlow prediction?如何修复关于 TensorFlow 预测的 TypeError?
【发布时间】:2019-05-30 15:36:43
【问题描述】:

我一直在尝试使用 TensorFlow 执行神经网络预测,并且评估工作正常,但是当我将相同的数据放入预测中时,它会出现类型错误。

training_data 和 test_data 中的数据都是 2d numpy 整数数组,training_labels 和 test_labels 是 1d numpy 整数数组。

model = keras.Sequential([
  keras.layers.Dense(24, activation=tf.nn.selu),
  keras.layers.Dense(10, activation=tf.nn.tanh),
  keras.layers.Dense(5, activation=tf.nn.selu),
  keras.layers.Dense(5, activation=tf.nn.softmax)
])


model.compile(optimizer='SGD',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(training_data, training_labels, epochs=10)

test_loss,test_acc = model.evaluate(test_data, test_labels)

prediction = model.predict(test_data)

取出预测线时,代码按预期工作,但现在给出以下错误消息:

Traceback (most recent call last):
  File "learner.py", line 132, in <module>
    print("Prediction: " + model.predict(test_data))
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')

我已经确定所有数据都是整数,所以我不确定为什么会出现类型冲突。

【问题讨论】:

  • 您可以尝试打印test_data 并展示给我们看吗? dtype('S32') 表示我认为这是一个字符串。

标签: python tensorflow


【解决方案1】:

使用predict时,test_data应该和type(training_data[0])的数据类型相同,返回的数据类型是type(training_labels[0])

另外,

print("Prediction: " + model.predict(test_data))

必须

print("Prediction: " + str(model.predict(test_data)))

【讨论】:

    猜你喜欢
    • 2022-07-21
    • 2021-07-12
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2020-06-11
    • 1970-01-01
    相关资源
    最近更新 更多