简体   繁体   English

ValueError:输入 0 与层 lstm_2 不兼容:预期 ndim=3,发现 ndim=4 - 多元时间序列数据

[英]ValueError: Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=4 - multivariate timeseries data

I have multivariate timeseries data with 100,000 rows and currently 32 features (the features will be reduced later).我有 100,000 行的多元时间序列数据,目前有 32 个特征(这些特征稍后会减少)。

I've already tried to use layer_flatten.我已经尝试过使用 layer_flatten。 as other suggested it on github.正如其他人在github上建议的那样。 Unfortunately didn't work for me.不幸的是对我不起作用。

The error is generated whe I try to build the keras model.当我尝试构建 keras 模型时会生成错误。

This is my code:这是我的代码:

lstm_v1 <- keras_model_sequential() %>% 
  layer_lstm(units = 32, input_shape = c(nrow(data), 1, ncol(data)), batch_size = nrow(data), return_sequences = T) %>%
  layer_dense(units = 1, activation = "sigmoid")

lstm_v1 %>% compile(
  loss = 'binary_crossentropy', 
  optimizer = 'rmsprop', 
   metrics = c('accuracy')
)

summary(lstm_v1)

hist_lstm_v1 <- lstm_v1 %>% fit(
  x = as.matrix(data), y = km_dt$cluster, batch_size = nrow(spg_tt_1_scaled), verbose = 2
)

Keras LSTM layer expects the input to be 3 dims as (batch_size, seq_length, input_dims) , but you have assigned it wrong. Keras LSTM 层期望输入为3 (batch_size, seq_length, input_dims)(batch_size, seq_length, input_dims) ,但您分配错误。 Try this尝试这个

layer_lstm(units = 32, input_shape = c(seq_length, 32), batch_size = batch_size, return_sequences = T)

You need to reshape your data to three dims, where new dims will represent the sequential data.您需要将数据重塑为三个维度,其中新的维度将代表顺序数据。

I used toy dataset to show an example, here data and labels are of shape ((150, 32), (150,)) initially, using the following script:我用玩具数据集来展示一个例子,这里的数据和标签最初是形状((150, 32), (150,)) ,使用以下脚本:

seq_length = 10 # choice
dataX = []
dataY = []
for i in range(0, 150 - seq_length, 1):
    dataX.append(data[i:i+seq_length])
    dataY.append(labels[i+seq_length-1])
import numpy as np
dataX = np.reshape(dataX, (-1, seq_length, 32))
dataY = np.reshape(dataY, (-1, 1))
# dataX.shape, dataY.shape

Output: ((140, 10, 32), (140, 1))输出: ((140, 10, 32), (140, 1))

Now you can safely feed it to model.现在您可以安全地将其提供给模型。

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

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