Keras有状态LSTM的批量大小问题

时间:2020-10-14 16:46:40

标签: python tensorflow keras lstm

我试图在Python中使用tf.keras进行有状态的LSTM预测。特征的总大小为(10000,4),目标为(10000,1)。我做了80%和20%的培训,每批包含100个示例。因此,我将它们重塑为X_train.shape =(80,100,4)和y_train.shape =(80,100,1)和X_test.shape =(20,100,4)和y_test.shape =(20,100 ,1)。我想将批量大小设置为100。

输入形状为(80,100,4)。在这里,我们利用4个值来预测一个值。输出形状为(80,100,1)。但是,当我尝试使LSTM单元为有状态= True时。发生错误。我想知道如何重塑我的训练集和测试集。

InvalidArgumentError:    Specified a list with shape [100,4] from a tensor with shape [32,4]
     [[{{node TensorArrayUnstack/TensorListFromTensor}}]]
     [[functional_7/lstm/PartitionedCall]] [Op:__inference_train_function_10241]

Function call stack:
train_function -> train_function -> train_function

我不知道为什么会有张量为[32,4]的张量。有人可以帮我解决张量形状或矫正我的问题吗?相应的代码如下:

def buildModel(batchSize):
    X_input = Input(batch_size=batchSize, shape=(100,4), name='input', dtype=tf.float32)
    X = BatchNormalization()(X_input)
    
    X = LSTM(units = 2, return_sequences=True, stateful=True, name='lstm')(X_input)
    
    output = Dense(1, activation='relu', name='output')(X)
    
    model = Model(inputs=X_input, outputs=output)
    
    return model

0 个答案:

没有答案
相关问题