Python 如何应用ConvLSTM层

Python 如何应用ConvLSTM层,python,tensorflow,machine-learning,deep-learning,Python,Tensorflow,Machine Learning,Deep Learning,我在做一个输入为(700,50,34)的分类机器学习(批量、步骤、特征) 显然,更改输入的形状并简单地添加 model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3), padding="same", return_sequences=True)) model.add(BatchNormalization()) 不起作用 ValueError: Dimension 1 in both shapes must be equal, b

我在做一个输入为(700,50,34)的分类机器学习(批量、步骤、特征)

显然,更改输入的形状并简单地添加

model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3), padding="same", return_sequences=True))
model.add(BatchNormalization())
不起作用

ValueError: Dimension 1 in both shapes must be equal, but are 708 and 501264. Shapes are [?,708,50,40] and [?,501264,2500,40]. for 'conv_lst_m2d/while/Select' (op: 'Select') with input shapes: [?,501264,2500,40], [?,708,50,40], [?,708,50,40].

我应该如何接近?
关于过滤器的数量有什么建议吗?

我现在尝试使用您的代码。当您提供最少的工作示例时,您将在stackoverflow获得更好的答案。我构建了一些与您使用的形状相同的测试数据

当我们使用LSTMConv2D时,我可以重现您的问题。但是,由于您的数据不适合Conv2D层,因此将其替换为普通的LSTM层。 相反,如果您想使时间步长复杂化,我建议首先使用CONV1D层,然后使用LSTM

最低工作示例:

import tensorflow as tf
import numpy as np
from tensorflow.python.keras.layers import Masking, ConvLSTM2D, LSTM, Bidirectional, Dense
from tensorflow.python.keras.layers.normalization_v2 import BatchNormalization

batch_size=68
units=128
learning_rate=0.005
epochs=20
dropout=0.2
recurrent_dropout=0.2
X_train = np.random.rand(700, 50,34)
y_train = np.random.choice([0, 1], 700)
X_test = np.random.rand(100, 50, 34)
y_test = np.random.choice([0, 1], 100)
loss = tf.losses.binary_crossentropy

model = tf.keras.models.Sequential()
model.add(Masking(mask_value=0.0, input_shape=(X_train.shape[1], X_train.shape[2])))
# uncomment the line beneath for convolution
# model.add(Conv1D(filters=32, kernel_size=8, strides=1, activation="relu", padding="same"))
model.add(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout, return_sequences=True))
model.add(BatchNormalization())

model.add(Bidirectional(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout, return_sequences=True)))
model.add(Bidirectional(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout, return_sequences=True)))
model.add(Bidirectional(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout)))
model.add(Dense(30, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

adamopt = tf.keras.optimizers.Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-8)

model.compile(loss=loss,
              optimizer=adamopt,
              metrics=['accuracy'])

history = model.fit(X_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    validation_data=(X_test, y_test),
                    verbose=1)

score, acc = model.evaluate(X_test, y_test,
                            batch_size=batch_size)

yhat = model.predict(X_test)


试试这个,我相信CNN层应该在屏蔽层之前实现

    model = tf.keras.models.Sequential()
    
    model.add(Conv1D(filters=32, kernel_size=8, strides=1, activation="relu", padding="same",input_shape=(X_train.shape[1], X_train.shape[2])))
    model.add(MaxPooling1D(pool_size = 2))
    model.add(Conv1D(filters=16, kernel_size=8, strides=1, activation="relu", padding="same"))
    model.add(MaxPooling1D(pool_size = 2))
    
    
    model.add(Masking(mask_value=0.0))
    model.add(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout, return_sequences=True))
    model.add(Bidirectional(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout, return_sequences=True)))
    model.add(Bidirectional(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout, return_sequences=True)))
    model.add(Bidirectional(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout)))
    model.add(Dense(30, activation='relu'))
    model.add(Dense(10, activation='relu'))
    
    model.add(Dense(num_classes, activation='softmax'))

您在这里的输入数据是什么?我的理解正确吗?您有34个功能(与时间相关),希望一次使用50个(时间步长)?是的,因此如果没有ConvLSTM层,我的输入形状代码是
model.add(Masking(mask_value=0.0,input_shape=(X_train.shape[1],X_train.shape[2])
输入维度可能有问题。根据我看到的tensorflow站点,输入张量必须是5D。另一个问题可能是convLSTM层可能需要明确的掩码(只是一种可能性),确切地说是@sai,站点上提供的信息是有限的。我希望如果有人知道如何将它应用到我的案例中。你到底想要实现什么?您的数据没有LSTMConv2D的形状。缺少通道和行。通道可以是1,但行必须是实尺寸。否则,LSTMConv2D就不是您需要使用的层。你想盘旋什么?LSTMConv2D通常用于视频机器学习(随时间变化的3D阵列-->4D)。好的,我不确定Conv2D是否正是我想要的。我试图实现的是训练一个同时包含时域和频域特征的模型。因此,对于时间域,使用RNN(如LSTM)是合理的,但对于其他特性,我认为对此类数据集使用CNN可能会提高准确性。我不擅长CNN,所以我想知道是否有办法将CNN纳入模型。你介意在你的代码中写CNN层吗?嘿,利奥,我编辑了我的答案并为Conv1D层添加了一行。它使时间步长变得复杂。你也可以看看波编码器。一维卷积对您来说是正确的选择,而不是二维卷积,因为每个样本都有一个一维特征数组,而不是二维特征数组(如图片)。TypeError:Layer conv1d不支持掩蔽,但传递了一个输入掩码:张量(“掩蔽/挤压:0”,形状=(?,50),dtype=bool)
    model = tf.keras.models.Sequential()
    
    model.add(Conv1D(filters=32, kernel_size=8, strides=1, activation="relu", padding="same",input_shape=(X_train.shape[1], X_train.shape[2])))
    model.add(MaxPooling1D(pool_size = 2))
    model.add(Conv1D(filters=16, kernel_size=8, strides=1, activation="relu", padding="same"))
    model.add(MaxPooling1D(pool_size = 2))
    
    
    model.add(Masking(mask_value=0.0))
    model.add(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout, return_sequences=True))
    model.add(Bidirectional(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout, return_sequences=True)))
    model.add(Bidirectional(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout, return_sequences=True)))
    model.add(Bidirectional(LSTM(units, dropout=dropout, recurrent_dropout=recurrent_dropout)))
    model.add(Dense(30, activation='relu'))
    model.add(Dense(10, activation='relu'))
    
    model.add(Dense(num_classes, activation='softmax'))