0

我正在尝试使用 LSTM、BiLSTM 和 GUE 构建不同的模型。训练和测试的 X 数据都是数组,而 y 是数据框

train_size=int(len(df2)*0.7)
train_dataset,test_dataset=df2.iloc[:train_size],df2.iloc[train_size:]
X_train=train_dataset.drop('Volume', axis=1)
Y_train=train_dataset.loc[:,['Volume']]
X_train=train_dataset.drop('Volume', axis=1)
Y_train=train_dataset.loc[:,['Volume']]
X_test=test_dataset.drop('Volume', axis=1)
Y_test=test_dataset.loc[:,['Volume']]
scaler_x=MinMaxScaler(feature_range= (0,1))
scaler_y=MinMaxScaler(feature_range= (0,1))
input_scaler=scaler_x.fit(X_train)
output_scaler=scaler_y.fit(Y_train)
train_y_norm=output_scaler.transform(Y_train)
train_x_norm=output_scaler.transform(X_train)
test_y_norm=output_scaler.transform(Y_test)
test_x_norm=output_scaler.transform(X_test)
def create_dataset (X, Y, time_steps =1):
    Xs,ys=[],[]
    for i in range (len(X)-time_steps):
        v= X[i:i+time_steps, :]
        Xs.append(v)
        ys.append(Y[i+time_steps])
        return np.array(Xs), np.array(ys)
TIME_STEPS=30
X_test, y_test=create_dataset(test_x_norm, test_y_norm, TIME_STEPS)
X_train, y_train=create_dataset(train_x_norm, train_y_norm, TIME_STEPS)
def create_model_bilstm(units): ## BiLSTM
    model=Sequential()
    model.add(Bidirectional(LSTM(units = units, return_sequences=True), input_shape=(X_train.shape[1], X_train.shape[2])))
    model.add(Bidirectional(LSTM(units=units)))
    model.add(Dense(1))
    #compile model
    model.compile(loss='mse', optimizer='adam')
    return model
    
## create LSTM 
def create_model (units , m):
    model=Sequential()
    model.add(m (units= units, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])))
    model.add(Dropout(0.2))
    model.add(m (units=units))
    model.add(Dropout(0.2))
    model.add(Dense(units=1))
    #compile model
    model.compile (loss='mse', optimizer='adam')
    return model
model_bilstm = create_model_bilstm(64)
model_gru=create_model(63,GRU)
model_lstm=create_model(63,LSTM)
    
    def fit_model(model):
        early_stop = keras.callbacks.EarlyStopping(monitor = 'val_loss',patience = 10)
        history = model.fit(X_train, Y_train, epochs = 100, validation_split = 0.2, batch_size = 32, shuffle = False, callbacks = [early_stop])
        return history
    history_bilstm = fit_model(model_bilstm)
    history_lstm = fit_model(model_lstm)
    history_gru = fit_model(model_gru)

它给了我一个值错误 ValueError: Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, <class 'pandas.core.frame.DataFrame'>

你能帮我解决这个问题吗?

4

0 回答 0