Deep learning 多步骤、多功能LSTM,wh

Deep learning 多步骤、多功能LSTM,wh,deep-learning,lstm,linear-regression,multi-step,lstm-stateful,Deep Learning,Lstm,Linear Regression,Multi Step,Lstm Stateful,我有一个LSTM模型。数据将被分离、规范化和重塑。此外,输入数据的形状以样本、时间步长和特征为单位。但有一个问题我不明白。如果你能指导我解决这个问题,我将不胜感激 详情如下: 主数据框形状: x_data.shape: (10000, 4) y_data.shape: (10000, 2) 然后我拆分了数据: x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, train_size=TRAIN_SPLIT,

我有一个LSTM模型。数据将被分离、规范化和重塑。此外,输入数据的形状以样本、时间步长和特征为单位。但有一个问题我不明白。如果你能指导我解决这个问题,我将不胜感激

详情如下:

主数据框形状:

x_data.shape:  (10000, 4) y_data.shape:  (10000, 2)
然后我拆分了数据:

x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, train_size=TRAIN_SPLIT, shuffle=False)

x_train.shape:  (8000, 4) y_train.shape:  (8000, 2)
x_test.shape:  (2000, 4) y_test.shape:  (2000, 2)
然后我使用for循环按所需的时间步创建序列:

xtrain, ytrain, xtest, ytest = [], [], [], []

### train_rounds calculate the baches that can be placed completly into the shape
train_rounds = int(len(x_train)/TIME_STEPS) * TIME_STEPS


### i is equal to TIME_STEPS
for i  in range(TIME_STEPS, len(x_train)): 
    ### Select from row (i-TIME_STEPS (This means 0 for the first round)) to row(i)
    if i + 1 > train_rounds: 
        break
    xtrain.append(x_train[i-TIME_STEPS:i])
    ytrain.append(y_train[i-TIME_STEPS:i])

### test_rounds calculate the baches that can be placed completly into the shape
test_rounds = int(len(x_test)/TIME_STEPS) * TIME_STEPS           

for i  in range(TIME_STEPS, len(x_test)):
    if i + 1 > test_rounds: 
        break
    xtest.append(x_test[i-TIME_STEPS:i])
    ytest.append(y_test[i-TIME_STEPS:i])

x_train_final , y_train_final = np.array(xtrain), np.array(ytrain)
x_test_final , y_test_final = np.array(xtest), np.array(ytest)

x_train_final.shape:  (7956, 26, 4)
x_train_final.shape:  (7956, 26, 4)
创建模型

model = Sequential()

model.add(LSTM(units=Layer1, input_shape=(x_train_final.shape[1:]), return_sequences=True))
model.add(Dropout(DROPOUT))

model.add(LSTM(units=Layer2, return_sequences=True))
model.add(Dropout(DROPOUT))

model.add(LSTM(units=Layer3))
model.add(Dropout(DROPOUT))

model.add(Dense(units=2, activation=ACTIVATION))

model.compile(optimizer=OPTIMIZER, loss=LOSS)

model.summary()
回调

callback_checkpoint = ModelCheckpoint(filepath=f'{filepath}Checkpoints.h5', monitor='val_loss', save_best_only=True, save_weights_only=True, verbose=VERBOSE)
callback_tensorboard = TensorBoard(log_dir=f'{filepath}/{DATETIME}/', histogram_freq=0, update_freq="epoch", write_graph=True)
callback_eraly_stopping = EarlyStopping(monitor='val_loss',min_delta=0 , patience=PATIENCE)
callback_reduce_lr = ReduceLROnPlateau(monitor='val_loss', min_lr=1e-4, factor=0.1, patience=PATIENCE)
callbacks = [callback_checkpoint, callback_eraly_stopping, callback_tensorboard, callback_reduce_lr]


Model: "sequential_8"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_14 (LSTM)               (None, 26, 30)            4200      
_________________________________________________________________
dropout_9 (Dropout)          (None, 26, 30)            0         
_________________________________________________________________
lstm_15 (LSTM)               (None, 26, 16)            3008      
_________________________________________________________________
dropout_10 (Dropout)         (None, 26, 16)            0         
_________________________________________________________________
lstm_16 (LSTM)               (None, 8)                 800       
_________________________________________________________________
dropout_11 (Dropout)         (None, 8)                 0         
_________________________________________________________________
dense_3 (Dense)              (None, 2)                 18        
=================================================================
Total params: 8,026
Trainable params: 8,026
Non-trainable params: 0
_________________________________________________________________
符合模型

history = model.fit(x_train_final, y_train_final, epochs=EPOCHS, batch_size=BATCH_SIZE, callbacks=callbacks, validation_split=0.3, shuffle=False)
最后我得到了这个错误:

InvalidArgumentError:  Incompatible shapes: [50,2] vs. [50,26,2]
     [[node gradient_tape/mean_squared_error/BroadcastGradientArgs (defined at <ipython-input-33-5d89d163874f>:3) ]] [Op:__inference_train_function_9918]

Function call stack:
train_function
InvalidArgumentError:不兼容的形状:[50,2]与[50,26,2]
[node gradient_tape/均方误差/BroadcastGradientArgs(定义于:3)][Op:[UUUUU推断\U训练\U函数\U 9918]
函数调用堆栈:
列车功能
怎么了