【问题标题】:Keras doesn't predict multi output correctlyKeras 无法正确预测多输出
【发布时间】:2021-12-10 18:38:34
【问题描述】:

我有一个包含两个特征的数据集来预测这两个特征。这里和数据示例:

raw = {'one':  ['41.392953', '41.392889', '41.392825','41.392761', '41.392697'],
        'two': ['2.163917','2.163995','2.164072','2.164150','2.164229' ]}
   

当我使用 Keras 时(在我的代码下方):

# example of making predictions for a regression problem
from keras.models import Sequential
from keras.layers import Dense
X = raw[:-1]
y = raw[1:]
# define and fit the final model
model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mse', optimizer='adam')
model.fit(X[0:len(X)-1], y[0:len(y)-1], epochs=1000, verbose=0)
# make a prediction
Xnew=X[len(X)-1:len(X)]
ynew = model.predict(Xnew)
# show the inputs and predicted outputs
print("X=%s, Predicted=%s" % (Xnew, ynew))

但是,输出与输入不同,它应该包含两个参数并且大小相似。

X=        latitude  longitude
55740  41.392052   2.164564, Predicted=[[21.778254]]

【问题讨论】:

  • 数据集中 X 和 Y 的形状是什么?您是否希望将坐标作为输出?
  • X 和 Y 是坐标,X 样本和 Y 类(使用相同的样本)。输出也必须是坐标。
  • 好的,再说一遍,你的数据集中 X 和 Y 的形状是什么?
  • (341, 2) X 和 y

标签: python keras regression lstm prediction


【解决方案1】:

我认为问题在于您的输入格式。为什么不使用 4 作为输入维度?

我尝试使用不同的格式(numpy)。输出相当不错。

import numpy as np
raw = np.array([[41.392953, 41.392889, 41.392825,41.392761, 41.392697],
        [2.163917,2.163995,2.164072,2.164150,2.164229 ]])
# example of making predictions for a regression problem
from keras.models import Sequential
from keras.layers import Dense
X = raw[:,:-1]
y = raw[:,-1]
# define and fit the final model
model = Sequential()
model.add(Dense(4, input_dim=4, activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mse', optimizer='adam')
model.fit(X, y, epochs=1000, verbose=0)
# make a prediction
Xnew=X[len(X)-1:len(X)]
ynew = model.predict(Xnew)
# show the inputs and predicted outputs
print("X=%s, Predicted=%s" % (Xnew, ynew))

输出:

X=[[2.163917 2.163995 2.164072 2.16415 ]], Predicted=[[2.3935468]]

【讨论】:

【解决方案2】:

如果你想有两个输出,你必须在你的输出层明确地指定它们。例如:

from keras.models import Sequential
from keras.layers import Dense

X = tf.random.normal((341, 2))
Y = tf.random.normal((341, 2))
# define and fit the final model
model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Dense(2, activation='linear'))
model.compile(loss='mse', optimizer='adam')
model.fit(X, Y, epochs=1, verbose=0)
# make a prediction
Xnew=tf.random.normal((1, 2))
ynew = model.predict(Xnew)
# show the inputs and predicted outputs
print("X=%s, Predicted=%s" % (Xnew, ynew))
# X=tf.Tensor([[-0.8087067  0.5405918]], shape=(1, 2), dtype=float32), Predicted=[[-0.02120915 -0.0466493 ]]

【讨论】:

  • 我已将 input_dim 更改为 2,但是,我得到了唯一的一个值。我的 Xnew 变量是具有两个值的 pandas.core.frame.DataFrame。
  • input_dim 已经是 2。请检查原始问题中的代码。我改变的是:model.add(Dense(2, activation='linear')). Note the number 2.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 1970-01-01
  • 2017-04-17
  • 2016-10-31
  • 2017-08-27
  • 2018-06-19
  • 1970-01-01
相关资源
最近更新 更多