[英]ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (2849, 1, 2)
So I want to predict the location of an agent within an environment encoded using cartesian coordinates.所以我想预测一个代理在使用笛卡尔坐标编码的环境中的位置。 For that I want to use an LSTM model but I am having some issues with setting a simple one up that I can then expand on.
为此,我想使用 LSTM model 但我在设置一个简单的然后可以扩展的问题时遇到了一些问题。 The data I use looks like this:
我使用的数据如下所示:
x0 y0 x1 y1 x2 y2 x5 y5
0 0 5 1 5 1 4 3 3
1 1 5 1 4 2 4 3 2
2 1 4 2 4 2 3 4 2
3 2 4 2 3 3 3 4 1
Where x0 through y2 are the features (or X) (with the number indicating the time step) and x5 and y5 is the to be predicted value (or y).其中 x0 到 y2 是特征(或 X)(数字表示时间步长),x5 和 y5 是要预测的值(或 y)。 So first I preprocessed the data to fit into an LSTM model like so:
所以首先我预处理数据以适应 LSTM model,如下所示:
path_df = pd.read_csv("data/preprocessed_data.csv", sep="\t", index_col=0)
X = path_df[["x0", "y0", "x1", "y1", "x2", "y2"]].to_numpy()
y = path_df[["x5", "y5"]].to_numpy()
X = X.reshape(len(X), 3, 2)
y = y.reshape(len(y), 1, 2)
This gives me arrays that look like this:这给了我看起来像这样的 arrays:
X[0] =
[[[ 3 1]
[ 3 2]
[ 2 2]]
Y[0] =
[[ 1 4]]
I think this is properly formatted to use in an LSTM model (if it is not please tell me).我认为这已正确格式化以在 LSTM model 中使用(如果不是,请告诉我)。 I then create a simple model usig keras like so:
然后我创建一个简单的 model 使用 keras 像这样:
model = Sequential()
model.add(LSTM(4, input_shape=(3, 2)))
model.add(Dense(1))
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(X, y, epochs=100, verbose=2)
If I'm correct I believe that this would give me a model that has an input layer of the shape (3,2) which is correct given the input data.如果我是正确的,我相信这会给我一个 model,它有一个形状为 (3,2) 的输入层,给定输入数据是正确的。 And an output layer that should give me 1 value, which would be the predicted location.
还有一个 output 层应该给我 1 个值,这将是预测的位置。 But when I run this I get:
但是当我运行它时,我得到:
ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (2849, 1, 2)
And I don't fully understand where this is coming from, the 2849 is the size of my data-set so that is where that number is coming from but I don't understand how to fix this.而且我不完全理解这是从哪里来的,2849 是我的数据集的大小,所以这就是这个数字的来源,但我不明白如何解决这个问题。 Any help would be appreciated!
任何帮助,将不胜感激!
your model output is actually 2D so you need to pass a 2D target.你的 model output 实际上是二维的,所以你需要传递一个二维目标。 you don't need to reshape the target in this way
y.reshape(len(y), 1, 2)
.您不需要以这种方式重塑目标
y.reshape(len(y), 1, 2)
。 simply let it in original 2D format简单地让它以原始的 2D 格式
X = np.random.uniform(0,1, (100,3,2))
y = np.random.uniform(0,1, (100,2))
model = Sequential()
model.add(LSTM(4, input_shape=(3, 2)))
model.add(Dense(2))
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(X, y, epochs=100, verbose=2)
your inputs look correct.您的输入看起来正确。 remember to set your Dense(2) in the output because you have 2 output features/coordinates
记得在 output 中设置你的 Dense(2) 因为你有 2 个 output 特征/坐标
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.