繁体   English   中英

Tensorflow / Keras 预测 function Z78E6221F6393D1356681DB398F14CE6D 长度不匹配输入长度

[英]Tensorflow / Keras predict function output length does not match input length

我正在使用 Ubuntu 19.04 (Disco Dingo)、Python 3.7.3 和 TensorFlow 1.14。

我注意到 tensorflow.keras.Sequential.predict function 给出的输出数量与输入数量不同。 此外,输入和输出之间似乎没有关系。

例子:

import tensorflow as tf
import math
import numpy as np
import json

# We will train the model to recognize an XOR

x = [ [0,0], [0,1], [1,0], [1,1] ]

y = [ 0, 1, 1, 0 ]

xt = tf.cast(x, tf.float64)
yt = tf.cast(y, tf.float64)

# This model should be more than enough to learn an XOR

L0 = tf.keras.layers.Dense(2)
L1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
L2 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
L3 = tf.keras.layers.Dense(2, activation=tf.nn.softmax)

model = tf.keras.Sequential([L0,L1,L2,L3])

model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"]
)

model.fit(
    x=xt,
    y=yt,
    batch_size=32,
    epochs=1000, # Try to overfit data
    shuffle=False,
    steps_per_epoch=math.ceil(len(x)/32)
)

# While it is training, the loss drops to near zero
# and the accuracy goes to 100%.
# The large number of epochs and the small number of training examples
# should mean that the network is overtrained.

print("testing")

for i in range(len(y)):
    m = tf.cast([x[i]],tf.float64)
    # m should be the ith training example
    values = model.predict(m,steps=1)
    best = np.argmax(values[0])
    print(x[i],y[i],best)

我总是得到的 output 是:

(输入、正确答案、预测答案)

[0, 0] 0 0
[0, 1] 1 0
[1, 0] 1 0
[1, 1] 0 0

或者

[0, 0] 0 1
[0, 1] 1 1
[1, 0] 1 1
[1, 1] 0 1

所以,即使我认为网络会被过度训练,即使程序说准确率是 100% 并且损失几乎为零,output 看起来好像网络根本没有训练过。

更奇怪的是,当我用以下内容替换测试部分时:

print("testing")

m = tf.cast([], tf.float64)
values = model.predict(m, steps=1)
print(values)

我认为这将返回一个空数组或引发异常。 相反,它给出:

[[0.9979249  0.00207507]
 [0.10981816 0.89018184]
 [0.10981816 0.89018184]
 [0.9932179  0.0067821 ]]

这对应于[0,1,1,0]

因此,即使它没有任何可预测的内容,它仍然会给出某些东西的预测。 似乎预测与我们将整个训练集发送到预测方法中所期望的结果相匹配。

再次更换测试部分:

print("testing")

m = tf.cast([[0,0]], tf.float64)
# [0,0] is the first training example
# the output should be something close to [[1.0,0.0]]
values = model.predict(m, steps=1)
for j in range(len(values)):
    print(values[j])
exit()

我得到:

[0.9112452  0.08875483]
[0.00552484 0.9944752 ]
[0.00555605 0.99444395]
[0.9112452  0.08875483]

这对应于[0,1,1,0]

因此,要求它对零输入进行预测,会给出 4 个预测。 要求它预测一个输入会给出 4 个预测。 此外,如果我们将整个训练集放入预测 function 中,它给出的预测看起来就像我们所期望的那样。

关于发生了什么的任何想法? 如何让我的网络为给定的每个输入准确地给出一个预测?

为了社区的利益,在此处(答案部分)提供解决方案,即使它出现在评论部分。

1.14.0 >=2.0升级Tensorflow已解决该问题。

升级后测试部分按预期工作

m = tf.cast([[0,0]], tf.float64)
# [0,0] is the first training example
# the output should be something close to [[1.0,0.0]]
values = model.predict(m, steps=1)
for j in range(len(values)):
    print(values[j])
exit()

Output:

[0.9921625  0.00783745]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM