1 回答
TA贡献1810条经验 获得超4个赞
您在处理时间维度时遇到形状不匹配...当您尝试预测时间维度为 2 的内容时,时间输入 dim 为 1。因此您的网络中需要一些能够从 1 增加到 2 时间的东西方面。我使用了Upsampling1D图层,下面是一个完整的例子
# create fake data
X = np.random.uniform(0,1, (98,1,2))
Y = np.random.uniform(0,1, (98,2,1))
verbose, epochs, batch_size = 1, 20, 16
n_neurons = 100
n_inputs, n_features = X.shape[1], X.shape[2]
n_outputs = Y.shape[1]
model = Sequential()
model.add(LSTM(n_neurons, input_shape = (n_inputs, n_features), return_sequences=True))
model.add(UpSampling1D(n_outputs))
model.add(TimeDistributed(Dense(1)))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X, Y, epochs=epochs, batch_size=batch_size, verbose=verbose)
输入时间暗淡 > 输出时间暗淡,您可以使用 Lambda 或 Pooling 操作(如果维度匹配)。下面是一个使用 Lambda 的例子
X = np.random.uniform(0,1, (98,3,2))
Y = np.random.uniform(0,1, (98,2,1))
verbose, epochs, batch_size = 1, 20, 16
n_neurons = 100
n_inputs, n_features = X.shape[1], X.shape[2]
n_outputs = Y.shape[1]
model = Sequential()
model.add(LSTM(n_neurons, input_shape = (n_inputs, n_features), return_sequences=True))
model.add(Lambda(lambda x: x[:,-n_outputs:,:]))
model.add(TimeDistributed(Dense(1)))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X, Y, epochs=epochs, batch_size=batch_size, verbose=verbose)
添加回答
举报